xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf32-hppa.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* BFD back-end for HP PA-RISC ELF files.
2*e992f068Schristos    Copyright (C) 1990-2022 Free Software Foundation, Inc.
375fd0b74Schristos 
475fd0b74Schristos    Original code by
575fd0b74Schristos 	Center for Software Science
675fd0b74Schristos 	Department of Computer Science
775fd0b74Schristos 	University of Utah
875fd0b74Schristos    Largely rewritten by Alan Modra <alan@linuxcare.com.au>
975fd0b74Schristos    Naming cleanup by Carlos O'Donell <carlos@systemhalted.org>
1075fd0b74Schristos    TLS support written by Randolph Chung <tausq@debian.org>
1175fd0b74Schristos 
1275fd0b74Schristos    This file is part of BFD, the Binary File Descriptor library.
1375fd0b74Schristos 
1475fd0b74Schristos    This program is free software; you can redistribute it and/or modify
1575fd0b74Schristos    it under the terms of the GNU General Public License as published by
1675fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
1775fd0b74Schristos    (at your option) any later version.
1875fd0b74Schristos 
1975fd0b74Schristos    This program is distributed in the hope that it will be useful,
2075fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
2175fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2275fd0b74Schristos    GNU General Public License for more details.
2375fd0b74Schristos 
2475fd0b74Schristos    You should have received a copy of the GNU General Public License
2575fd0b74Schristos    along with this program; if not, write to the Free Software
2675fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2775fd0b74Schristos    MA 02110-1301, USA.  */
2875fd0b74Schristos 
2975fd0b74Schristos #include "sysdep.h"
3075fd0b74Schristos #include "bfd.h"
3175fd0b74Schristos #include "libbfd.h"
3275fd0b74Schristos #include "elf-bfd.h"
3375fd0b74Schristos #include "elf/hppa.h"
3475fd0b74Schristos #include "libhppa.h"
3575fd0b74Schristos #include "elf32-hppa.h"
3675fd0b74Schristos #define ARCH_SIZE		32
3775fd0b74Schristos #include "elf32-hppa.h"
3875fd0b74Schristos #include "elf-hppa.h"
3975fd0b74Schristos 
4075fd0b74Schristos /* In order to gain some understanding of code in this file without
4175fd0b74Schristos    knowing all the intricate details of the linker, note the
4275fd0b74Schristos    following:
4375fd0b74Schristos 
4475fd0b74Schristos    Functions named elf32_hppa_* are called by external routines, other
4575fd0b74Schristos    functions are only called locally.  elf32_hppa_* functions appear
4675fd0b74Schristos    in this file more or less in the order in which they are called
4775fd0b74Schristos    from external routines.  eg. elf32_hppa_check_relocs is called
4875fd0b74Schristos    early in the link process, elf32_hppa_finish_dynamic_sections is
4975fd0b74Schristos    one of the last functions.  */
5075fd0b74Schristos 
5175fd0b74Schristos /* We use two hash tables to hold information for linking PA ELF objects.
5275fd0b74Schristos 
5375fd0b74Schristos    The first is the elf32_hppa_link_hash_table which is derived
5475fd0b74Schristos    from the standard ELF linker hash table.  We use this as a place to
5575fd0b74Schristos    attach other hash tables and static information.
5675fd0b74Schristos 
5775fd0b74Schristos    The second is the stub hash table which is derived from the
5875fd0b74Schristos    base BFD hash table.  The stub hash table holds the information
5975fd0b74Schristos    necessary to build the linker stubs during a link.
6075fd0b74Schristos 
6175fd0b74Schristos    There are a number of different stubs generated by the linker.
6275fd0b74Schristos 
6375fd0b74Schristos    Long branch stub:
6475fd0b74Schristos    :		ldil LR'X,%r1
6575fd0b74Schristos    :		be,n RR'X(%sr4,%r1)
6675fd0b74Schristos 
6775fd0b74Schristos    PIC long branch stub:
6875fd0b74Schristos    :		b,l .+8,%r1
6975fd0b74Schristos    :		addil LR'X - ($PIC_pcrel$0 - 4),%r1
7075fd0b74Schristos    :		be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
7175fd0b74Schristos 
7275fd0b74Schristos    Import stub to call shared library routine from normal object file
7375fd0b74Schristos    (single sub-space version)
74012573ebSchristos    :		addil LR'lt_ptr+ltoff,%dp	; get PLT address
75012573ebSchristos    :		ldo RR'lt_ptr+ltoff(%r1),%r22   ;
76012573ebSchristos    :		ldw 0(%r22),%r21		; get procedure entry point
7775fd0b74Schristos    :		bv %r0(%r21)
78012573ebSchristos    :		ldw 4(%r22),%r19		; get new dlt value.
7975fd0b74Schristos 
8075fd0b74Schristos    Import stub to call shared library routine from shared library
8175fd0b74Schristos    (single sub-space version)
82012573ebSchristos    :		addil LR'ltoff,%r19		; get PLT address
83012573ebSchristos    :		ldo RR'ltoff(%r1),%r22
84012573ebSchristos    :		ldw 0(%r22),%r21		; get procedure entry point
8575fd0b74Schristos    :		bv %r0(%r21)
86012573ebSchristos    :		ldw 4(%r22),%r19		; get new dlt value.
8775fd0b74Schristos 
8875fd0b74Schristos    Import stub to call shared library routine from normal object file
8975fd0b74Schristos    (multiple sub-space support)
90012573ebSchristos    :		addil LR'lt_ptr+ltoff,%dp	; get PLT address
91012573ebSchristos    :		ldo RR'lt_ptr+ltoff(%r1),%r22   ;
92012573ebSchristos    :		ldw 0(%r22),%r21		; get procedure entry point
93012573ebSchristos    :		ldsid (%r21),%r1		; get target sid
94012573ebSchristos    :		ldw 4(%r22),%r19		; get new dlt value.
9575fd0b74Schristos    :		mtsp %r1,%sr0
9675fd0b74Schristos    :		be 0(%sr0,%r21)			; branch to target
9775fd0b74Schristos    :		stw %rp,-24(%sp)		; save rp
9875fd0b74Schristos 
9975fd0b74Schristos    Import stub to call shared library routine from shared library
10075fd0b74Schristos    (multiple sub-space support)
101012573ebSchristos    :		addil LR'ltoff,%r19		; get PLT address
102012573ebSchristos    :		ldo RR'ltoff(%r1),%r22
103012573ebSchristos    :		ldw 0(%r22),%r21		; get procedure entry point
104012573ebSchristos    :		ldsid (%r21),%r1		; get target sid
105012573ebSchristos    :		ldw 4(%r22),%r19		; get new dlt value.
10675fd0b74Schristos    :		mtsp %r1,%sr0
10775fd0b74Schristos    :		be 0(%sr0,%r21)			; branch to target
10875fd0b74Schristos    :		stw %rp,-24(%sp)		; save rp
10975fd0b74Schristos 
11075fd0b74Schristos    Export stub to return from shared lib routine (multiple sub-space support)
11175fd0b74Schristos    One of these is created for each exported procedure in a shared
11275fd0b74Schristos    library (and stored in the shared lib).  Shared lib routines are
11375fd0b74Schristos    called via the first instruction in the export stub so that we can
11475fd0b74Schristos    do an inter-space return.  Not required for single sub-space.
11575fd0b74Schristos    :		bl,n X,%rp			; trap the return
11675fd0b74Schristos    :		nop
11775fd0b74Schristos    :		ldw -24(%sp),%rp		; restore the original rp
11875fd0b74Schristos    :		ldsid (%rp),%r1
11975fd0b74Schristos    :		mtsp %r1,%sr0
12075fd0b74Schristos    :		be,n 0(%sr0,%rp)		; inter-space return.  */
12175fd0b74Schristos 
12275fd0b74Schristos 
12375fd0b74Schristos /* Variable names follow a coding style.
12475fd0b74Schristos    Please follow this (Apps Hungarian) style:
12575fd0b74Schristos 
12675fd0b74Schristos    Structure/Variable			Prefix
12775fd0b74Schristos    elf_link_hash_table			"etab"
12875fd0b74Schristos    elf_link_hash_entry			"eh"
12975fd0b74Schristos 
13075fd0b74Schristos    elf32_hppa_link_hash_table		"htab"
13175fd0b74Schristos    elf32_hppa_link_hash_entry		"hh"
13275fd0b74Schristos 
13375fd0b74Schristos    bfd_hash_table			"btab"
13475fd0b74Schristos    bfd_hash_entry			"bh"
13575fd0b74Schristos 
13675fd0b74Schristos    bfd_hash_table containing stubs	"bstab"
13775fd0b74Schristos    elf32_hppa_stub_hash_entry		"hsh"
13875fd0b74Schristos 
13975fd0b74Schristos    Always remember to use GNU Coding Style. */
14075fd0b74Schristos 
14175fd0b74Schristos #define PLT_ENTRY_SIZE 8
14275fd0b74Schristos #define GOT_ENTRY_SIZE 4
143012573ebSchristos #define LONG_BRANCH_STUB_SIZE 8
144012573ebSchristos #define LONG_BRANCH_SHARED_STUB_SIZE 12
145012573ebSchristos #define IMPORT_STUB_SIZE 20
146012573ebSchristos #define IMPORT_SHARED_STUB_SIZE 32
147012573ebSchristos #define EXPORT_STUB_SIZE 24
14875fd0b74Schristos #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
14975fd0b74Schristos 
15075fd0b74Schristos static const bfd_byte plt_stub[] =
15175fd0b74Schristos {
152012573ebSchristos   0x0e, 0x80, 0x10, 0x95,  /* 1: ldw	0(%r20),%r21		*/
153012573ebSchristos   0xea, 0xa0, 0xc0, 0x00,  /*    bv	%r0(%r21)		*/
15475fd0b74Schristos   0x0e, 0x88, 0x10, 0x95,  /*    ldw	4(%r20),%r21		*/
15575fd0b74Schristos #define PLT_STUB_ENTRY (3*4)
15675fd0b74Schristos   0xea, 0x9f, 0x1f, 0xdd,  /*    b,l	1b,%r20			*/
15775fd0b74Schristos   0xd6, 0x80, 0x1c, 0x1e,  /*    depi	0,31,2,%r20		*/
15875fd0b74Schristos   0x00, 0xc0, 0xff, 0xee,  /* 9: .word	fixup_func		*/
15975fd0b74Schristos   0xde, 0xad, 0xbe, 0xef   /*    .word	fixup_ltp		*/
16075fd0b74Schristos };
16175fd0b74Schristos 
16275fd0b74Schristos /* Section name for stubs is the associated section name plus this
16375fd0b74Schristos    string.  */
16475fd0b74Schristos #define STUB_SUFFIX ".stub"
16575fd0b74Schristos 
16675fd0b74Schristos /* We don't need to copy certain PC- or GP-relative dynamic relocs
16775fd0b74Schristos    into a shared object's dynamic section.  All the relocs of the
16875fd0b74Schristos    limited class we are interested in, are absolute.  */
16975fd0b74Schristos #ifndef RELATIVE_DYNRELOCS
17075fd0b74Schristos #define RELATIVE_DYNRELOCS 0
17175fd0b74Schristos #define IS_ABSOLUTE_RELOC(r_type) 1
172ede78133Schristos #define pc_dynrelocs(hh) 0
17375fd0b74Schristos #endif
17475fd0b74Schristos 
17575fd0b74Schristos /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
17675fd0b74Schristos    copying dynamic variables from a shared lib into an app's dynbss
17775fd0b74Schristos    section, and instead use a dynamic relocation to point into the
17875fd0b74Schristos    shared lib.  */
17975fd0b74Schristos #define ELIMINATE_COPY_RELOCS 1
18075fd0b74Schristos 
18175fd0b74Schristos enum elf32_hppa_stub_type
18275fd0b74Schristos {
18375fd0b74Schristos   hppa_stub_long_branch,
18475fd0b74Schristos   hppa_stub_long_branch_shared,
18575fd0b74Schristos   hppa_stub_import,
18675fd0b74Schristos   hppa_stub_import_shared,
18775fd0b74Schristos   hppa_stub_export,
18875fd0b74Schristos   hppa_stub_none
18975fd0b74Schristos };
19075fd0b74Schristos 
19175fd0b74Schristos struct elf32_hppa_stub_hash_entry
19275fd0b74Schristos {
19375fd0b74Schristos   /* Base hash table entry structure.  */
19475fd0b74Schristos   struct bfd_hash_entry bh_root;
19575fd0b74Schristos 
19675fd0b74Schristos   /* The stub section.  */
19775fd0b74Schristos   asection *stub_sec;
19875fd0b74Schristos 
19975fd0b74Schristos   /* Offset within stub_sec of the beginning of this stub.  */
20075fd0b74Schristos   bfd_vma stub_offset;
20175fd0b74Schristos 
20275fd0b74Schristos   /* Given the symbol's value and its section we can determine its final
20375fd0b74Schristos      value when building the stubs (so the stub knows where to jump.  */
20475fd0b74Schristos   bfd_vma target_value;
20575fd0b74Schristos   asection *target_section;
20675fd0b74Schristos 
20775fd0b74Schristos   enum elf32_hppa_stub_type stub_type;
20875fd0b74Schristos 
20975fd0b74Schristos   /* The symbol table entry, if any, that this was derived from.  */
21075fd0b74Schristos   struct elf32_hppa_link_hash_entry *hh;
21175fd0b74Schristos 
21275fd0b74Schristos   /* Where this stub is being called from, or, in the case of combined
21375fd0b74Schristos      stub sections, the first input section in the group.  */
21475fd0b74Schristos   asection *id_sec;
21575fd0b74Schristos };
21675fd0b74Schristos 
217ede78133Schristos enum _tls_type
218ede78133Schristos   {
219ede78133Schristos     GOT_UNKNOWN = 0,
220ede78133Schristos     GOT_NORMAL = 1,
221ede78133Schristos     GOT_TLS_GD = 2,
222ede78133Schristos     GOT_TLS_LDM = 4,
223ede78133Schristos     GOT_TLS_IE = 8
224ede78133Schristos   };
225ede78133Schristos 
22675fd0b74Schristos struct elf32_hppa_link_hash_entry
22775fd0b74Schristos {
22875fd0b74Schristos   struct elf_link_hash_entry eh;
22975fd0b74Schristos 
23075fd0b74Schristos   /* A pointer to the most recently used stub hash entry against this
23175fd0b74Schristos      symbol.  */
23275fd0b74Schristos   struct elf32_hppa_stub_hash_entry *hsh_cache;
23375fd0b74Schristos 
234ede78133Schristos   ENUM_BITFIELD (_tls_type) tls_type : 8;
23575fd0b74Schristos 
23675fd0b74Schristos   /* Set if this symbol is used by a plabel reloc.  */
23775fd0b74Schristos   unsigned int plabel:1;
23875fd0b74Schristos };
23975fd0b74Schristos 
24075fd0b74Schristos struct elf32_hppa_link_hash_table
24175fd0b74Schristos {
24275fd0b74Schristos   /* The main hash table.  */
24375fd0b74Schristos   struct elf_link_hash_table etab;
24475fd0b74Schristos 
24575fd0b74Schristos   /* The stub hash table.  */
24675fd0b74Schristos   struct bfd_hash_table bstab;
24775fd0b74Schristos 
24875fd0b74Schristos   /* Linker stub bfd.  */
24975fd0b74Schristos   bfd *stub_bfd;
25075fd0b74Schristos 
25175fd0b74Schristos   /* Linker call-backs.  */
25275fd0b74Schristos   asection * (*add_stub_section) (const char *, asection *);
25375fd0b74Schristos   void (*layout_sections_again) (void);
25475fd0b74Schristos 
25575fd0b74Schristos   /* Array to keep track of which stub sections have been created, and
25675fd0b74Schristos      information on stub grouping.  */
25775fd0b74Schristos   struct map_stub
25875fd0b74Schristos   {
25975fd0b74Schristos     /* This is the section to which stubs in the group will be
26075fd0b74Schristos        attached.  */
26175fd0b74Schristos     asection *link_sec;
26275fd0b74Schristos     /* The stub section.  */
26375fd0b74Schristos     asection *stub_sec;
26475fd0b74Schristos   } *stub_group;
26575fd0b74Schristos 
26675fd0b74Schristos   /* Assorted information used by elf32_hppa_size_stubs.  */
26775fd0b74Schristos   unsigned int bfd_count;
26875fd0b74Schristos   unsigned int top_index;
26975fd0b74Schristos   asection **input_list;
27075fd0b74Schristos   Elf_Internal_Sym **all_local_syms;
27175fd0b74Schristos 
27275fd0b74Schristos   /* Used during a final link to store the base of the text and data
27375fd0b74Schristos      segments so that we can perform SEGREL relocations.  */
27475fd0b74Schristos   bfd_vma text_segment_base;
27575fd0b74Schristos   bfd_vma data_segment_base;
27675fd0b74Schristos 
27775fd0b74Schristos   /* Whether we support multiple sub-spaces for shared libs.  */
27875fd0b74Schristos   unsigned int multi_subspace:1;
27975fd0b74Schristos 
28075fd0b74Schristos   /* Flags set when various size branches are detected.  Used to
28175fd0b74Schristos      select suitable defaults for the stub group size.  */
28275fd0b74Schristos   unsigned int has_12bit_branch:1;
28375fd0b74Schristos   unsigned int has_17bit_branch:1;
28475fd0b74Schristos   unsigned int has_22bit_branch:1;
28575fd0b74Schristos 
28675fd0b74Schristos   /* Set if we need a .plt stub to support lazy dynamic linking.  */
28775fd0b74Schristos   unsigned int need_plt_stub:1;
28875fd0b74Schristos 
28975fd0b74Schristos   /* Data for LDM relocations.  */
29075fd0b74Schristos   union
29175fd0b74Schristos   {
29275fd0b74Schristos     bfd_signed_vma refcount;
29375fd0b74Schristos     bfd_vma offset;
29475fd0b74Schristos   } tls_ldm_got;
29575fd0b74Schristos };
29675fd0b74Schristos 
29775fd0b74Schristos /* Various hash macros and functions.  */
29875fd0b74Schristos #define hppa_link_hash_table(p) \
299*e992f068Schristos   ((is_elf_hash_table ((p)->hash)					\
300*e992f068Schristos     && elf_hash_table_id (elf_hash_table (p)) == HPPA32_ELF_DATA)	\
301*e992f068Schristos    ? (struct elf32_hppa_link_hash_table *) (p)->hash : NULL)
30275fd0b74Schristos 
30375fd0b74Schristos #define hppa_elf_hash_entry(ent) \
30475fd0b74Schristos   ((struct elf32_hppa_link_hash_entry *)(ent))
30575fd0b74Schristos 
30675fd0b74Schristos #define hppa_stub_hash_entry(ent) \
30775fd0b74Schristos   ((struct elf32_hppa_stub_hash_entry *)(ent))
30875fd0b74Schristos 
30975fd0b74Schristos #define hppa_stub_hash_lookup(table, string, create, copy) \
31075fd0b74Schristos   ((struct elf32_hppa_stub_hash_entry *) \
31175fd0b74Schristos    bfd_hash_lookup ((table), (string), (create), (copy)))
31275fd0b74Schristos 
31375fd0b74Schristos #define hppa_elf_local_got_tls_type(abfd) \
31475fd0b74Schristos   ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info * 2)))
31575fd0b74Schristos 
31675fd0b74Schristos #define hh_name(hh) \
31775fd0b74Schristos   (hh ? hh->eh.root.root.string : "<undef>")
31875fd0b74Schristos 
31975fd0b74Schristos #define eh_name(eh) \
32075fd0b74Schristos   (eh ? eh->root.root.string : "<undef>")
32175fd0b74Schristos 
32275fd0b74Schristos /* Assorted hash table functions.  */
32375fd0b74Schristos 
32475fd0b74Schristos /* Initialize an entry in the stub hash table.  */
32575fd0b74Schristos 
32675fd0b74Schristos static struct bfd_hash_entry *
stub_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)32775fd0b74Schristos stub_hash_newfunc (struct bfd_hash_entry *entry,
32875fd0b74Schristos 		   struct bfd_hash_table *table,
32975fd0b74Schristos 		   const char *string)
33075fd0b74Schristos {
33175fd0b74Schristos   /* Allocate the structure if it has not already been allocated by a
33275fd0b74Schristos      subclass.  */
33375fd0b74Schristos   if (entry == NULL)
33475fd0b74Schristos     {
33575fd0b74Schristos       entry = bfd_hash_allocate (table,
33675fd0b74Schristos 				 sizeof (struct elf32_hppa_stub_hash_entry));
33775fd0b74Schristos       if (entry == NULL)
33875fd0b74Schristos 	return entry;
33975fd0b74Schristos     }
34075fd0b74Schristos 
34175fd0b74Schristos   /* Call the allocation method of the superclass.  */
34275fd0b74Schristos   entry = bfd_hash_newfunc (entry, table, string);
34375fd0b74Schristos   if (entry != NULL)
34475fd0b74Schristos     {
34575fd0b74Schristos       struct elf32_hppa_stub_hash_entry *hsh;
34675fd0b74Schristos 
34775fd0b74Schristos       /* Initialize the local fields.  */
34875fd0b74Schristos       hsh = hppa_stub_hash_entry (entry);
34975fd0b74Schristos       hsh->stub_sec = NULL;
35075fd0b74Schristos       hsh->stub_offset = 0;
35175fd0b74Schristos       hsh->target_value = 0;
35275fd0b74Schristos       hsh->target_section = NULL;
35375fd0b74Schristos       hsh->stub_type = hppa_stub_long_branch;
35475fd0b74Schristos       hsh->hh = NULL;
35575fd0b74Schristos       hsh->id_sec = NULL;
35675fd0b74Schristos     }
35775fd0b74Schristos 
35875fd0b74Schristos   return entry;
35975fd0b74Schristos }
36075fd0b74Schristos 
36175fd0b74Schristos /* Initialize an entry in the link hash table.  */
36275fd0b74Schristos 
36375fd0b74Schristos static struct bfd_hash_entry *
hppa_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)36475fd0b74Schristos hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
36575fd0b74Schristos 			struct bfd_hash_table *table,
36675fd0b74Schristos 			const char *string)
36775fd0b74Schristos {
36875fd0b74Schristos   /* Allocate the structure if it has not already been allocated by a
36975fd0b74Schristos      subclass.  */
37075fd0b74Schristos   if (entry == NULL)
37175fd0b74Schristos     {
37275fd0b74Schristos       entry = bfd_hash_allocate (table,
37375fd0b74Schristos 				 sizeof (struct elf32_hppa_link_hash_entry));
37475fd0b74Schristos       if (entry == NULL)
37575fd0b74Schristos 	return entry;
37675fd0b74Schristos     }
37775fd0b74Schristos 
37875fd0b74Schristos   /* Call the allocation method of the superclass.  */
37975fd0b74Schristos   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
38075fd0b74Schristos   if (entry != NULL)
38175fd0b74Schristos     {
38275fd0b74Schristos       struct elf32_hppa_link_hash_entry *hh;
38375fd0b74Schristos 
38475fd0b74Schristos       /* Initialize the local fields.  */
38575fd0b74Schristos       hh = hppa_elf_hash_entry (entry);
38675fd0b74Schristos       hh->hsh_cache = NULL;
38775fd0b74Schristos       hh->plabel = 0;
38875fd0b74Schristos       hh->tls_type = GOT_UNKNOWN;
38975fd0b74Schristos     }
39075fd0b74Schristos 
39175fd0b74Schristos   return entry;
39275fd0b74Schristos }
39375fd0b74Schristos 
39475fd0b74Schristos /* Free the derived linker hash table.  */
39575fd0b74Schristos 
39675fd0b74Schristos static void
elf32_hppa_link_hash_table_free(bfd * obfd)39775fd0b74Schristos elf32_hppa_link_hash_table_free (bfd *obfd)
39875fd0b74Schristos {
39975fd0b74Schristos   struct elf32_hppa_link_hash_table *htab
40075fd0b74Schristos     = (struct elf32_hppa_link_hash_table *) obfd->link.hash;
40175fd0b74Schristos 
40275fd0b74Schristos   bfd_hash_table_free (&htab->bstab);
40375fd0b74Schristos   _bfd_elf_link_hash_table_free (obfd);
40475fd0b74Schristos }
40575fd0b74Schristos 
40675fd0b74Schristos /* Create the derived linker hash table.  The PA ELF port uses the derived
40775fd0b74Schristos    hash table to keep information specific to the PA ELF linker (without
40875fd0b74Schristos    using static variables).  */
40975fd0b74Schristos 
41075fd0b74Schristos static struct bfd_link_hash_table *
elf32_hppa_link_hash_table_create(bfd * abfd)41175fd0b74Schristos elf32_hppa_link_hash_table_create (bfd *abfd)
41275fd0b74Schristos {
41375fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
414*e992f068Schristos   size_t amt = sizeof (*htab);
41575fd0b74Schristos 
41675fd0b74Schristos   htab = bfd_zmalloc (amt);
41775fd0b74Schristos   if (htab == NULL)
41875fd0b74Schristos     return NULL;
41975fd0b74Schristos 
42075fd0b74Schristos   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, hppa_link_hash_newfunc,
42175fd0b74Schristos 				      sizeof (struct elf32_hppa_link_hash_entry),
42275fd0b74Schristos 				      HPPA32_ELF_DATA))
42375fd0b74Schristos     {
42475fd0b74Schristos       free (htab);
42575fd0b74Schristos       return NULL;
42675fd0b74Schristos     }
42775fd0b74Schristos 
42875fd0b74Schristos   /* Init the stub hash table too.  */
42975fd0b74Schristos   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
43075fd0b74Schristos 			    sizeof (struct elf32_hppa_stub_hash_entry)))
43175fd0b74Schristos     {
43275fd0b74Schristos       _bfd_elf_link_hash_table_free (abfd);
43375fd0b74Schristos       return NULL;
43475fd0b74Schristos     }
43575fd0b74Schristos   htab->etab.root.hash_table_free = elf32_hppa_link_hash_table_free;
436*e992f068Schristos   htab->etab.dt_pltgot_required = true;
43775fd0b74Schristos 
43875fd0b74Schristos   htab->text_segment_base = (bfd_vma) -1;
43975fd0b74Schristos   htab->data_segment_base = (bfd_vma) -1;
44075fd0b74Schristos   return &htab->etab.root;
44175fd0b74Schristos }
44275fd0b74Schristos 
44375fd0b74Schristos /* Initialize the linker stubs BFD so that we can use it for linker
44475fd0b74Schristos    created dynamic sections.  */
44575fd0b74Schristos 
44675fd0b74Schristos void
elf32_hppa_init_stub_bfd(bfd * abfd,struct bfd_link_info * info)44775fd0b74Schristos elf32_hppa_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
44875fd0b74Schristos {
44975fd0b74Schristos   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
45075fd0b74Schristos 
45175fd0b74Schristos   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS32;
45275fd0b74Schristos   htab->etab.dynobj = abfd;
45375fd0b74Schristos }
45475fd0b74Schristos 
45575fd0b74Schristos /* Build a name for an entry in the stub hash table.  */
45675fd0b74Schristos 
45775fd0b74Schristos static char *
hppa_stub_name(const asection * input_section,const asection * sym_sec,const struct elf32_hppa_link_hash_entry * hh,const Elf_Internal_Rela * rela)45875fd0b74Schristos hppa_stub_name (const asection *input_section,
45975fd0b74Schristos 		const asection *sym_sec,
46075fd0b74Schristos 		const struct elf32_hppa_link_hash_entry *hh,
46175fd0b74Schristos 		const Elf_Internal_Rela *rela)
46275fd0b74Schristos {
46375fd0b74Schristos   char *stub_name;
46475fd0b74Schristos   bfd_size_type len;
46575fd0b74Schristos 
46675fd0b74Schristos   if (hh)
46775fd0b74Schristos     {
46875fd0b74Schristos       len = 8 + 1 + strlen (hh_name (hh)) + 1 + 8 + 1;
46975fd0b74Schristos       stub_name = bfd_malloc (len);
47075fd0b74Schristos       if (stub_name != NULL)
47175fd0b74Schristos 	sprintf (stub_name, "%08x_%s+%x",
47275fd0b74Schristos 		 input_section->id & 0xffffffff,
47375fd0b74Schristos 		 hh_name (hh),
47475fd0b74Schristos 		 (int) rela->r_addend & 0xffffffff);
47575fd0b74Schristos     }
47675fd0b74Schristos   else
47775fd0b74Schristos     {
47875fd0b74Schristos       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
47975fd0b74Schristos       stub_name = bfd_malloc (len);
48075fd0b74Schristos       if (stub_name != NULL)
48175fd0b74Schristos 	sprintf (stub_name, "%08x_%x:%x+%x",
48275fd0b74Schristos 		 input_section->id & 0xffffffff,
48375fd0b74Schristos 		 sym_sec->id & 0xffffffff,
48475fd0b74Schristos 		 (int) ELF32_R_SYM (rela->r_info) & 0xffffffff,
48575fd0b74Schristos 		 (int) rela->r_addend & 0xffffffff);
48675fd0b74Schristos     }
48775fd0b74Schristos   return stub_name;
48875fd0b74Schristos }
48975fd0b74Schristos 
49075fd0b74Schristos /* Look up an entry in the stub hash.  Stub entries are cached because
49175fd0b74Schristos    creating the stub name takes a bit of time.  */
49275fd0b74Schristos 
49375fd0b74Schristos static struct elf32_hppa_stub_hash_entry *
hppa_get_stub_entry(const asection * input_section,const asection * sym_sec,struct elf32_hppa_link_hash_entry * hh,const Elf_Internal_Rela * rela,struct elf32_hppa_link_hash_table * htab)49475fd0b74Schristos hppa_get_stub_entry (const asection *input_section,
49575fd0b74Schristos 		     const asection *sym_sec,
49675fd0b74Schristos 		     struct elf32_hppa_link_hash_entry *hh,
49775fd0b74Schristos 		     const Elf_Internal_Rela *rela,
49875fd0b74Schristos 		     struct elf32_hppa_link_hash_table *htab)
49975fd0b74Schristos {
50075fd0b74Schristos   struct elf32_hppa_stub_hash_entry *hsh_entry;
50175fd0b74Schristos   const asection *id_sec;
50275fd0b74Schristos 
50375fd0b74Schristos   /* If this input section is part of a group of sections sharing one
50475fd0b74Schristos      stub section, then use the id of the first section in the group.
50575fd0b74Schristos      Stub names need to include a section id, as there may well be
50675fd0b74Schristos      more than one stub used to reach say, printf, and we need to
50775fd0b74Schristos      distinguish between them.  */
50875fd0b74Schristos   id_sec = htab->stub_group[input_section->id].link_sec;
509ede78133Schristos   if (id_sec == NULL)
510ede78133Schristos     return NULL;
51175fd0b74Schristos 
51275fd0b74Schristos   if (hh != NULL && hh->hsh_cache != NULL
51375fd0b74Schristos       && hh->hsh_cache->hh == hh
51475fd0b74Schristos       && hh->hsh_cache->id_sec == id_sec)
51575fd0b74Schristos     {
51675fd0b74Schristos       hsh_entry = hh->hsh_cache;
51775fd0b74Schristos     }
51875fd0b74Schristos   else
51975fd0b74Schristos     {
52075fd0b74Schristos       char *stub_name;
52175fd0b74Schristos 
52275fd0b74Schristos       stub_name = hppa_stub_name (id_sec, sym_sec, hh, rela);
52375fd0b74Schristos       if (stub_name == NULL)
52475fd0b74Schristos 	return NULL;
52575fd0b74Schristos 
52675fd0b74Schristos       hsh_entry = hppa_stub_hash_lookup (&htab->bstab,
527*e992f068Schristos 					  stub_name, false, false);
52875fd0b74Schristos       if (hh != NULL)
52975fd0b74Schristos 	hh->hsh_cache = hsh_entry;
53075fd0b74Schristos 
53175fd0b74Schristos       free (stub_name);
53275fd0b74Schristos     }
53375fd0b74Schristos 
53475fd0b74Schristos   return hsh_entry;
53575fd0b74Schristos }
53675fd0b74Schristos 
53775fd0b74Schristos /* Add a new stub entry to the stub hash.  Not all fields of the new
53875fd0b74Schristos    stub entry are initialised.  */
53975fd0b74Schristos 
54075fd0b74Schristos static struct elf32_hppa_stub_hash_entry *
hppa_add_stub(const char * stub_name,asection * section,struct elf32_hppa_link_hash_table * htab)54175fd0b74Schristos hppa_add_stub (const char *stub_name,
54275fd0b74Schristos 	       asection *section,
54375fd0b74Schristos 	       struct elf32_hppa_link_hash_table *htab)
54475fd0b74Schristos {
54575fd0b74Schristos   asection *link_sec;
54675fd0b74Schristos   asection *stub_sec;
54775fd0b74Schristos   struct elf32_hppa_stub_hash_entry *hsh;
54875fd0b74Schristos 
54975fd0b74Schristos   link_sec = htab->stub_group[section->id].link_sec;
55075fd0b74Schristos   stub_sec = htab->stub_group[section->id].stub_sec;
55175fd0b74Schristos   if (stub_sec == NULL)
55275fd0b74Schristos     {
55375fd0b74Schristos       stub_sec = htab->stub_group[link_sec->id].stub_sec;
55475fd0b74Schristos       if (stub_sec == NULL)
55575fd0b74Schristos 	{
55675fd0b74Schristos 	  size_t namelen;
55775fd0b74Schristos 	  bfd_size_type len;
55875fd0b74Schristos 	  char *s_name;
55975fd0b74Schristos 
56075fd0b74Schristos 	  namelen = strlen (link_sec->name);
56175fd0b74Schristos 	  len = namelen + sizeof (STUB_SUFFIX);
56275fd0b74Schristos 	  s_name = bfd_alloc (htab->stub_bfd, len);
56375fd0b74Schristos 	  if (s_name == NULL)
56475fd0b74Schristos 	    return NULL;
56575fd0b74Schristos 
56675fd0b74Schristos 	  memcpy (s_name, link_sec->name, namelen);
56775fd0b74Schristos 	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
56875fd0b74Schristos 	  stub_sec = (*htab->add_stub_section) (s_name, link_sec);
56975fd0b74Schristos 	  if (stub_sec == NULL)
57075fd0b74Schristos 	    return NULL;
57175fd0b74Schristos 	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
57275fd0b74Schristos 	}
57375fd0b74Schristos       htab->stub_group[section->id].stub_sec = stub_sec;
57475fd0b74Schristos     }
57575fd0b74Schristos 
57675fd0b74Schristos   /* Enter this entry into the linker stub hash table.  */
57775fd0b74Schristos   hsh = hppa_stub_hash_lookup (&htab->bstab, stub_name,
578*e992f068Schristos 				      true, false);
57975fd0b74Schristos   if (hsh == NULL)
58075fd0b74Schristos     {
581ede78133Schristos       /* xgettext:c-format */
582ede78133Schristos       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
583ede78133Schristos 			  section->owner, stub_name);
58475fd0b74Schristos       return NULL;
58575fd0b74Schristos     }
58675fd0b74Schristos 
58775fd0b74Schristos   hsh->stub_sec = stub_sec;
58875fd0b74Schristos   hsh->stub_offset = 0;
58975fd0b74Schristos   hsh->id_sec = link_sec;
59075fd0b74Schristos   return hsh;
59175fd0b74Schristos }
59275fd0b74Schristos 
59375fd0b74Schristos /* Determine the type of stub needed, if any, for a call.  */
59475fd0b74Schristos 
59575fd0b74Schristos static enum elf32_hppa_stub_type
hppa_type_of_stub(asection * input_sec,const Elf_Internal_Rela * rela,struct elf32_hppa_link_hash_entry * hh,bfd_vma destination,struct bfd_link_info * info)59675fd0b74Schristos hppa_type_of_stub (asection *input_sec,
59775fd0b74Schristos 		   const Elf_Internal_Rela *rela,
59875fd0b74Schristos 		   struct elf32_hppa_link_hash_entry *hh,
59975fd0b74Schristos 		   bfd_vma destination,
60075fd0b74Schristos 		   struct bfd_link_info *info)
60175fd0b74Schristos {
60275fd0b74Schristos   bfd_vma location;
60375fd0b74Schristos   bfd_vma branch_offset;
60475fd0b74Schristos   bfd_vma max_branch_offset;
60575fd0b74Schristos   unsigned int r_type;
60675fd0b74Schristos 
60775fd0b74Schristos   if (hh != NULL
60875fd0b74Schristos       && hh->eh.plt.offset != (bfd_vma) -1
60975fd0b74Schristos       && hh->eh.dynindx != -1
61075fd0b74Schristos       && !hh->plabel
61175fd0b74Schristos       && (bfd_link_pic (info)
61275fd0b74Schristos 	  || !hh->eh.def_regular
61375fd0b74Schristos 	  || hh->eh.root.type == bfd_link_hash_defweak))
61475fd0b74Schristos     {
61575fd0b74Schristos       /* We need an import stub.  Decide between hppa_stub_import
61675fd0b74Schristos 	 and hppa_stub_import_shared later.  */
61775fd0b74Schristos       return hppa_stub_import;
61875fd0b74Schristos     }
61975fd0b74Schristos 
620ede78133Schristos   if (destination == (bfd_vma) -1)
621ede78133Schristos     return hppa_stub_none;
622ede78133Schristos 
62375fd0b74Schristos   /* Determine where the call point is.  */
62475fd0b74Schristos   location = (input_sec->output_offset
62575fd0b74Schristos 	      + input_sec->output_section->vma
62675fd0b74Schristos 	      + rela->r_offset);
62775fd0b74Schristos 
62875fd0b74Schristos   branch_offset = destination - location - 8;
62975fd0b74Schristos   r_type = ELF32_R_TYPE (rela->r_info);
63075fd0b74Schristos 
63175fd0b74Schristos   /* Determine if a long branch stub is needed.  parisc branch offsets
63275fd0b74Schristos      are relative to the second instruction past the branch, ie. +8
63375fd0b74Schristos      bytes on from the branch instruction location.  The offset is
63475fd0b74Schristos      signed and counts in units of 4 bytes.  */
63575fd0b74Schristos   if (r_type == (unsigned int) R_PARISC_PCREL17F)
63675fd0b74Schristos     max_branch_offset = (1 << (17 - 1)) << 2;
63775fd0b74Schristos 
63875fd0b74Schristos   else if (r_type == (unsigned int) R_PARISC_PCREL12F)
63975fd0b74Schristos     max_branch_offset = (1 << (12 - 1)) << 2;
64075fd0b74Schristos 
64175fd0b74Schristos   else /* R_PARISC_PCREL22F.  */
64275fd0b74Schristos     max_branch_offset = (1 << (22 - 1)) << 2;
64375fd0b74Schristos 
64475fd0b74Schristos   if (branch_offset + max_branch_offset >= 2*max_branch_offset)
64575fd0b74Schristos     return hppa_stub_long_branch;
64675fd0b74Schristos 
64775fd0b74Schristos   return hppa_stub_none;
64875fd0b74Schristos }
64975fd0b74Schristos 
65075fd0b74Schristos /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
65175fd0b74Schristos    IN_ARG contains the link info pointer.  */
65275fd0b74Schristos 
65375fd0b74Schristos #define LDIL_R1		0x20200000	/* ldil  LR'XXX,%r1		*/
65475fd0b74Schristos #define BE_SR4_R1	0xe0202002	/* be,n  RR'XXX(%sr4,%r1)	*/
65575fd0b74Schristos 
65675fd0b74Schristos #define BL_R1		0xe8200000	/* b,l   .+8,%r1		*/
65775fd0b74Schristos #define ADDIL_R1	0x28200000	/* addil LR'XXX,%r1,%r1		*/
65875fd0b74Schristos #define DEPI_R1		0xd4201c1e	/* depi  0,31,2,%r1		*/
65975fd0b74Schristos 
66075fd0b74Schristos #define ADDIL_DP	0x2b600000	/* addil LR'XXX,%dp,%r1		*/
66175fd0b74Schristos #define LDW_R1_R21	0x48350000	/* ldw   RR'XXX(%sr0,%r1),%r21	*/
66275fd0b74Schristos #define BV_R0_R21	0xeaa0c000	/* bv    %r0(%r21)		*/
66375fd0b74Schristos #define LDW_R1_R19	0x48330000	/* ldw   RR'XXX(%sr0,%r1),%r19	*/
66475fd0b74Schristos 
66575fd0b74Schristos #define ADDIL_R19	0x2a600000	/* addil LR'XXX,%r19,%r1	*/
66675fd0b74Schristos #define LDW_R1_DP	0x483b0000	/* ldw   RR'XXX(%sr0,%r1),%dp	*/
66775fd0b74Schristos 
668012573ebSchristos #define LDO_R1_R22	0x34360000	/* ldo   RR'XXX(%r1),%r22	*/
669012573ebSchristos #define LDW_R22_R21	0x0ec01095	/* ldw   0(%r22),%r21		*/
670012573ebSchristos #define LDW_R22_R19	0x0ec81093	/* ldw   4(%r22),%r19		*/
671012573ebSchristos 
67275fd0b74Schristos #define LDSID_R21_R1	0x02a010a1	/* ldsid (%sr0,%r21),%r1	*/
67375fd0b74Schristos #define MTSP_R1		0x00011820	/* mtsp  %r1,%sr0		*/
67475fd0b74Schristos #define BE_SR0_R21	0xe2a00000	/* be    0(%sr0,%r21)		*/
67575fd0b74Schristos #define STW_RP		0x6bc23fd1	/* stw   %rp,-24(%sr0,%sp)	*/
67675fd0b74Schristos 
67775fd0b74Schristos #define BL22_RP		0xe800a002	/* b,l,n XXX,%rp		*/
67875fd0b74Schristos #define BL_RP		0xe8400002	/* b,l,n XXX,%rp		*/
67975fd0b74Schristos #define NOP		0x08000240	/* nop				*/
68075fd0b74Schristos #define LDW_RP		0x4bc23fd1	/* ldw   -24(%sr0,%sp),%rp	*/
68175fd0b74Schristos #define LDSID_RP_R1	0x004010a1	/* ldsid (%sr0,%rp),%r1		*/
68275fd0b74Schristos #define BE_SR0_RP	0xe0400002	/* be,n  0(%sr0,%rp)		*/
68375fd0b74Schristos 
68475fd0b74Schristos #ifndef R19_STUBS
68575fd0b74Schristos #define R19_STUBS 1
68675fd0b74Schristos #endif
68775fd0b74Schristos 
68875fd0b74Schristos #if R19_STUBS
68975fd0b74Schristos #define LDW_R1_DLT	LDW_R1_R19
69075fd0b74Schristos #else
69175fd0b74Schristos #define LDW_R1_DLT	LDW_R1_DP
69275fd0b74Schristos #endif
69375fd0b74Schristos 
694*e992f068Schristos static bool
hppa_build_one_stub(struct bfd_hash_entry * bh,void * in_arg)69575fd0b74Schristos hppa_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
69675fd0b74Schristos {
69775fd0b74Schristos   struct elf32_hppa_stub_hash_entry *hsh;
69875fd0b74Schristos   struct bfd_link_info *info;
69975fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
70075fd0b74Schristos   asection *stub_sec;
70175fd0b74Schristos   bfd *stub_bfd;
70275fd0b74Schristos   bfd_byte *loc;
70375fd0b74Schristos   bfd_vma sym_value;
70475fd0b74Schristos   bfd_vma insn;
70575fd0b74Schristos   bfd_vma off;
70675fd0b74Schristos   int val;
70775fd0b74Schristos   int size;
70875fd0b74Schristos 
70975fd0b74Schristos   /* Massage our args to the form they really have.  */
71075fd0b74Schristos   hsh = hppa_stub_hash_entry (bh);
71175fd0b74Schristos   info = (struct bfd_link_info *)in_arg;
71275fd0b74Schristos 
71375fd0b74Schristos   htab = hppa_link_hash_table (info);
71475fd0b74Schristos   if (htab == NULL)
715*e992f068Schristos     return false;
71675fd0b74Schristos 
71775fd0b74Schristos   stub_sec = hsh->stub_sec;
71875fd0b74Schristos 
71975fd0b74Schristos   /* Make a note of the offset within the stubs for this entry.  */
72075fd0b74Schristos   hsh->stub_offset = stub_sec->size;
72175fd0b74Schristos   loc = stub_sec->contents + hsh->stub_offset;
72275fd0b74Schristos 
72375fd0b74Schristos   stub_bfd = stub_sec->owner;
72475fd0b74Schristos 
72575fd0b74Schristos   switch (hsh->stub_type)
72675fd0b74Schristos     {
72775fd0b74Schristos     case hppa_stub_long_branch:
728*e992f068Schristos       /* Fail if the target section could not be assigned to an output
729*e992f068Schristos 	 section.  The user should fix his linker script.  */
730*e992f068Schristos       if (hsh->target_section->output_section == NULL
731*e992f068Schristos 	  && info->non_contiguous_regions)
732*e992f068Schristos 	info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output "
733*e992f068Schristos 				  "section. Retry without "
734*e992f068Schristos 				  "--enable-non-contiguous-regions.\n"),
735*e992f068Schristos 				hsh->target_section);
736*e992f068Schristos 
73775fd0b74Schristos       /* Create the long branch.  A long branch is formed with "ldil"
73875fd0b74Schristos 	 loading the upper bits of the target address into a register,
73975fd0b74Schristos 	 then branching with "be" which adds in the lower bits.
74075fd0b74Schristos 	 The "be" has its delay slot nullified.  */
74175fd0b74Schristos       sym_value = (hsh->target_value
74275fd0b74Schristos 		   + hsh->target_section->output_offset
74375fd0b74Schristos 		   + hsh->target_section->output_section->vma);
74475fd0b74Schristos 
74575fd0b74Schristos       val = hppa_field_adjust (sym_value, 0, e_lrsel);
74675fd0b74Schristos       insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
74775fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc);
74875fd0b74Schristos 
74975fd0b74Schristos       val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
75075fd0b74Schristos       insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
75175fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc + 4);
75275fd0b74Schristos 
753012573ebSchristos       size = LONG_BRANCH_STUB_SIZE;
75475fd0b74Schristos       break;
75575fd0b74Schristos 
75675fd0b74Schristos     case hppa_stub_long_branch_shared:
757*e992f068Schristos       /* Fail if the target section could not be assigned to an output
758*e992f068Schristos 	 section.  The user should fix his linker script.  */
759*e992f068Schristos       if (hsh->target_section->output_section == NULL
760*e992f068Schristos 	  && info->non_contiguous_regions)
761*e992f068Schristos 	info->callbacks->einfo (_("%F%P: Could not assign %pA to an output "
762*e992f068Schristos 				  "section. Retry without "
763*e992f068Schristos 				  "--enable-non-contiguous-regions.\n"),
764*e992f068Schristos 				hsh->target_section);
765*e992f068Schristos 
76675fd0b74Schristos       /* Branches are relative.  This is where we are going to.  */
76775fd0b74Schristos       sym_value = (hsh->target_value
76875fd0b74Schristos 		   + hsh->target_section->output_offset
76975fd0b74Schristos 		   + hsh->target_section->output_section->vma);
77075fd0b74Schristos 
77175fd0b74Schristos       /* And this is where we are coming from, more or less.  */
77275fd0b74Schristos       sym_value -= (hsh->stub_offset
77375fd0b74Schristos 		    + stub_sec->output_offset
77475fd0b74Schristos 		    + stub_sec->output_section->vma);
77575fd0b74Schristos 
77675fd0b74Schristos       bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
77775fd0b74Schristos       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
77875fd0b74Schristos       insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
77975fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc + 4);
78075fd0b74Schristos 
78175fd0b74Schristos       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
78275fd0b74Schristos       insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
78375fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc + 8);
784012573ebSchristos       size = LONG_BRANCH_SHARED_STUB_SIZE;
78575fd0b74Schristos       break;
78675fd0b74Schristos 
78775fd0b74Schristos     case hppa_stub_import:
78875fd0b74Schristos     case hppa_stub_import_shared:
78975fd0b74Schristos       off = hsh->hh->eh.plt.offset;
79075fd0b74Schristos       if (off >= (bfd_vma) -2)
79175fd0b74Schristos 	abort ();
79275fd0b74Schristos 
79375fd0b74Schristos       off &= ~ (bfd_vma) 1;
79475fd0b74Schristos       sym_value = (off
795ede78133Schristos 		   + htab->etab.splt->output_offset
796ede78133Schristos 		   + htab->etab.splt->output_section->vma
797ede78133Schristos 		   - elf_gp (htab->etab.splt->output_section->owner));
79875fd0b74Schristos 
79975fd0b74Schristos       insn = ADDIL_DP;
80075fd0b74Schristos #if R19_STUBS
80175fd0b74Schristos       if (hsh->stub_type == hppa_stub_import_shared)
80275fd0b74Schristos 	insn = ADDIL_R19;
80375fd0b74Schristos #endif
804012573ebSchristos 
805012573ebSchristos       /* Load function descriptor address into register %r22.  It is
806012573ebSchristos 	 sometimes needed for lazy binding.  */
80775fd0b74Schristos       val = hppa_field_adjust (sym_value, 0, e_lrsel),
80875fd0b74Schristos       insn = hppa_rebuild_insn ((int) insn, val, 21);
80975fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc);
81075fd0b74Schristos 
81175fd0b74Schristos       val = hppa_field_adjust (sym_value, 0, e_rrsel);
812012573ebSchristos       insn = hppa_rebuild_insn ((int) LDO_R1_R22, val, 14);
81375fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc + 4);
81475fd0b74Schristos 
815012573ebSchristos       bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R21, loc + 8);
816012573ebSchristos 
81775fd0b74Schristos       if (htab->multi_subspace)
81875fd0b74Schristos 	{
81975fd0b74Schristos 	  bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
820012573ebSchristos 	  bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R19,  loc + 16);
821012573ebSchristos 	  bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,      loc + 20);
822012573ebSchristos 	  bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21,   loc + 24);
823012573ebSchristos 	  bfd_put_32 (stub_bfd, (bfd_vma) STW_RP,       loc + 28);
82475fd0b74Schristos 
825012573ebSchristos 	  size = IMPORT_SHARED_STUB_SIZE;
82675fd0b74Schristos 	}
82775fd0b74Schristos       else
82875fd0b74Schristos 	{
829012573ebSchristos 	  bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 12);
830012573ebSchristos 	  bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R19, loc + 16);
83175fd0b74Schristos 
832012573ebSchristos 	  size = IMPORT_STUB_SIZE;
83375fd0b74Schristos 	}
83475fd0b74Schristos 
83575fd0b74Schristos       break;
83675fd0b74Schristos 
83775fd0b74Schristos     case hppa_stub_export:
838*e992f068Schristos       /* Fail if the target section could not be assigned to an output
839*e992f068Schristos 	 section.  The user should fix his linker script.  */
840*e992f068Schristos       if (hsh->target_section->output_section == NULL
841*e992f068Schristos 	  && info->non_contiguous_regions)
842*e992f068Schristos 	info->callbacks->einfo (_("%F%P: Could not assign %pA to an output "
843*e992f068Schristos 				  "section. Retry without "
844*e992f068Schristos 				  "--enable-non-contiguous-regions.\n"),
845*e992f068Schristos 				hsh->target_section);
846*e992f068Schristos 
84775fd0b74Schristos       /* Branches are relative.  This is where we are going to.  */
84875fd0b74Schristos       sym_value = (hsh->target_value
84975fd0b74Schristos 		   + hsh->target_section->output_offset
85075fd0b74Schristos 		   + hsh->target_section->output_section->vma);
85175fd0b74Schristos 
85275fd0b74Schristos       /* And this is where we are coming from.  */
85375fd0b74Schristos       sym_value -= (hsh->stub_offset
85475fd0b74Schristos 		    + stub_sec->output_offset
85575fd0b74Schristos 		    + stub_sec->output_section->vma);
85675fd0b74Schristos 
85775fd0b74Schristos       if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
85875fd0b74Schristos 	  && (!htab->has_22bit_branch
85975fd0b74Schristos 	      || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
86075fd0b74Schristos 	{
861ede78133Schristos 	  _bfd_error_handler
862ede78133Schristos 	    /* xgettext:c-format */
863ede78133Schristos 	    (_("%pB(%pA+%#" PRIx64 "): "
864ede78133Schristos 	       "cannot reach %s, recompile with -ffunction-sections"),
86575fd0b74Schristos 	     hsh->target_section->owner,
86675fd0b74Schristos 	     stub_sec,
867ede78133Schristos 	     (uint64_t) hsh->stub_offset,
86875fd0b74Schristos 	     hsh->bh_root.string);
86975fd0b74Schristos 	  bfd_set_error (bfd_error_bad_value);
870*e992f068Schristos 	  return false;
87175fd0b74Schristos 	}
87275fd0b74Schristos 
87375fd0b74Schristos       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
87475fd0b74Schristos       if (!htab->has_22bit_branch)
87575fd0b74Schristos 	insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
87675fd0b74Schristos       else
87775fd0b74Schristos 	insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
87875fd0b74Schristos       bfd_put_32 (stub_bfd, insn, loc);
87975fd0b74Schristos 
88075fd0b74Schristos       bfd_put_32 (stub_bfd, (bfd_vma) NOP,	   loc + 4);
88175fd0b74Schristos       bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP,      loc + 8);
88275fd0b74Schristos       bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
88375fd0b74Schristos       bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,     loc + 16);
88475fd0b74Schristos       bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP,   loc + 20);
88575fd0b74Schristos 
88675fd0b74Schristos       /* Point the function symbol at the stub.  */
88775fd0b74Schristos       hsh->hh->eh.root.u.def.section = stub_sec;
88875fd0b74Schristos       hsh->hh->eh.root.u.def.value = stub_sec->size;
88975fd0b74Schristos 
890012573ebSchristos       size = EXPORT_STUB_SIZE;
89175fd0b74Schristos       break;
89275fd0b74Schristos 
89375fd0b74Schristos     default:
89475fd0b74Schristos       BFD_FAIL ();
895*e992f068Schristos       return false;
89675fd0b74Schristos     }
89775fd0b74Schristos 
89875fd0b74Schristos   stub_sec->size += size;
899*e992f068Schristos   return true;
90075fd0b74Schristos }
90175fd0b74Schristos 
90275fd0b74Schristos #undef LDIL_R1
90375fd0b74Schristos #undef BE_SR4_R1
90475fd0b74Schristos #undef BL_R1
90575fd0b74Schristos #undef ADDIL_R1
90675fd0b74Schristos #undef DEPI_R1
90775fd0b74Schristos #undef LDW_R1_R21
90875fd0b74Schristos #undef LDW_R1_DLT
90975fd0b74Schristos #undef LDW_R1_R19
91075fd0b74Schristos #undef ADDIL_R19
91175fd0b74Schristos #undef LDW_R1_DP
91275fd0b74Schristos #undef LDSID_R21_R1
91375fd0b74Schristos #undef MTSP_R1
91475fd0b74Schristos #undef BE_SR0_R21
91575fd0b74Schristos #undef STW_RP
91675fd0b74Schristos #undef BV_R0_R21
91775fd0b74Schristos #undef BL_RP
91875fd0b74Schristos #undef NOP
91975fd0b74Schristos #undef LDW_RP
92075fd0b74Schristos #undef LDSID_RP_R1
92175fd0b74Schristos #undef BE_SR0_RP
92275fd0b74Schristos 
92375fd0b74Schristos /* As above, but don't actually build the stub.  Just bump offset so
92475fd0b74Schristos    we know stub section sizes.  */
92575fd0b74Schristos 
926*e992f068Schristos static bool
hppa_size_one_stub(struct bfd_hash_entry * bh,void * in_arg)92775fd0b74Schristos hppa_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
92875fd0b74Schristos {
92975fd0b74Schristos   struct elf32_hppa_stub_hash_entry *hsh;
93075fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
93175fd0b74Schristos   int size;
93275fd0b74Schristos 
93375fd0b74Schristos   /* Massage our args to the form they really have.  */
93475fd0b74Schristos   hsh = hppa_stub_hash_entry (bh);
93575fd0b74Schristos   htab = in_arg;
93675fd0b74Schristos 
93775fd0b74Schristos   if (hsh->stub_type == hppa_stub_long_branch)
938012573ebSchristos     size = LONG_BRANCH_STUB_SIZE;
93975fd0b74Schristos   else if (hsh->stub_type == hppa_stub_long_branch_shared)
940012573ebSchristos     size = LONG_BRANCH_SHARED_STUB_SIZE;
94175fd0b74Schristos   else if (hsh->stub_type == hppa_stub_export)
942012573ebSchristos     size = EXPORT_STUB_SIZE;
94375fd0b74Schristos   else /* hppa_stub_import or hppa_stub_import_shared.  */
94475fd0b74Schristos     {
94575fd0b74Schristos       if (htab->multi_subspace)
946012573ebSchristos 	size = IMPORT_SHARED_STUB_SIZE;
94775fd0b74Schristos       else
948012573ebSchristos 	size = IMPORT_STUB_SIZE;
94975fd0b74Schristos     }
95075fd0b74Schristos 
95175fd0b74Schristos   hsh->stub_sec->size += size;
952*e992f068Schristos   return true;
95375fd0b74Schristos }
95475fd0b74Schristos 
95575fd0b74Schristos /* Return nonzero if ABFD represents an HPPA ELF32 file.
95675fd0b74Schristos    Additionally we set the default architecture and machine.  */
95775fd0b74Schristos 
958*e992f068Schristos static bool
elf32_hppa_object_p(bfd * abfd)95975fd0b74Schristos elf32_hppa_object_p (bfd *abfd)
96075fd0b74Schristos {
96175fd0b74Schristos   Elf_Internal_Ehdr * i_ehdrp;
96275fd0b74Schristos   unsigned int flags;
96375fd0b74Schristos 
96475fd0b74Schristos   i_ehdrp = elf_elfheader (abfd);
96575fd0b74Schristos   if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
96675fd0b74Schristos     {
96775fd0b74Schristos       /* GCC on hppa-linux produces binaries with OSABI=GNU,
96875fd0b74Schristos 	 but the kernel produces corefiles with OSABI=SysV.  */
96975fd0b74Schristos       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU &&
97075fd0b74Schristos 	  i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
971*e992f068Schristos 	return false;
97275fd0b74Schristos     }
97375fd0b74Schristos   else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
97475fd0b74Schristos     {
97575fd0b74Schristos       /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
97675fd0b74Schristos 	 but the kernel produces corefiles with OSABI=SysV.  */
97775fd0b74Schristos       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
97875fd0b74Schristos 	  i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
979*e992f068Schristos 	return false;
98075fd0b74Schristos     }
98175fd0b74Schristos   else
98275fd0b74Schristos     {
98375fd0b74Schristos       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
984*e992f068Schristos 	return false;
98575fd0b74Schristos     }
98675fd0b74Schristos 
98775fd0b74Schristos   flags = i_ehdrp->e_flags;
98875fd0b74Schristos   switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
98975fd0b74Schristos     {
99075fd0b74Schristos     case EFA_PARISC_1_0:
99175fd0b74Schristos       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
99275fd0b74Schristos     case EFA_PARISC_1_1:
99375fd0b74Schristos       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
99475fd0b74Schristos     case EFA_PARISC_2_0:
99575fd0b74Schristos       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
99675fd0b74Schristos     case EFA_PARISC_2_0 | EF_PARISC_WIDE:
99775fd0b74Schristos       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
99875fd0b74Schristos     }
999*e992f068Schristos   return true;
100075fd0b74Schristos }
100175fd0b74Schristos 
100275fd0b74Schristos /* Create the .plt and .got sections, and set up our hash table
100375fd0b74Schristos    short-cuts to various dynamic sections.  */
100475fd0b74Schristos 
1005*e992f068Schristos static bool
elf32_hppa_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)100675fd0b74Schristos elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
100775fd0b74Schristos {
100875fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
100975fd0b74Schristos   struct elf_link_hash_entry *eh;
101075fd0b74Schristos 
101175fd0b74Schristos   /* Don't try to create the .plt and .got twice.  */
101275fd0b74Schristos   htab = hppa_link_hash_table (info);
101375fd0b74Schristos   if (htab == NULL)
1014*e992f068Schristos     return false;
1015ede78133Schristos   if (htab->etab.splt != NULL)
1016*e992f068Schristos     return true;
101775fd0b74Schristos 
101875fd0b74Schristos   /* Call the generic code to do most of the work.  */
101975fd0b74Schristos   if (! _bfd_elf_create_dynamic_sections (abfd, info))
1020*e992f068Schristos     return false;
102175fd0b74Schristos 
102275fd0b74Schristos   /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
102375fd0b74Schristos      application, because __canonicalize_funcptr_for_compare needs it.  */
102475fd0b74Schristos   eh = elf_hash_table (info)->hgot;
102575fd0b74Schristos   eh->forced_local = 0;
102675fd0b74Schristos   eh->other = STV_DEFAULT;
102775fd0b74Schristos   return bfd_elf_link_record_dynamic_symbol (info, eh);
102875fd0b74Schristos }
102975fd0b74Schristos 
103075fd0b74Schristos /* Copy the extra info we tack onto an elf_link_hash_entry.  */
103175fd0b74Schristos 
103275fd0b74Schristos static void
elf32_hppa_copy_indirect_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * eh_dir,struct elf_link_hash_entry * eh_ind)103375fd0b74Schristos elf32_hppa_copy_indirect_symbol (struct bfd_link_info *info,
103475fd0b74Schristos 				 struct elf_link_hash_entry *eh_dir,
103575fd0b74Schristos 				 struct elf_link_hash_entry *eh_ind)
103675fd0b74Schristos {
103775fd0b74Schristos   struct elf32_hppa_link_hash_entry *hh_dir, *hh_ind;
103875fd0b74Schristos 
103975fd0b74Schristos   hh_dir = hppa_elf_hash_entry (eh_dir);
104075fd0b74Schristos   hh_ind = hppa_elf_hash_entry (eh_ind);
104175fd0b74Schristos 
1042ede78133Schristos   if (eh_ind->root.type == bfd_link_hash_indirect)
104375fd0b74Schristos     {
1044ede78133Schristos       hh_dir->plabel |= hh_ind->plabel;
1045ede78133Schristos       hh_dir->tls_type |= hh_ind->tls_type;
104675fd0b74Schristos       hh_ind->tls_type = GOT_UNKNOWN;
104775fd0b74Schristos     }
104875fd0b74Schristos 
104975fd0b74Schristos   _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
105075fd0b74Schristos }
105175fd0b74Schristos 
105275fd0b74Schristos static int
elf32_hppa_optimized_tls_reloc(struct bfd_link_info * info ATTRIBUTE_UNUSED,int r_type,int is_local ATTRIBUTE_UNUSED)105375fd0b74Schristos elf32_hppa_optimized_tls_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
105475fd0b74Schristos 				int r_type, int is_local ATTRIBUTE_UNUSED)
105575fd0b74Schristos {
105675fd0b74Schristos   /* For now we don't support linker optimizations.  */
105775fd0b74Schristos   return r_type;
105875fd0b74Schristos }
105975fd0b74Schristos 
106075fd0b74Schristos /* Return a pointer to the local GOT, PLT and TLS reference counts
106175fd0b74Schristos    for ABFD.  Returns NULL if the storage allocation fails.  */
106275fd0b74Schristos 
106375fd0b74Schristos static bfd_signed_vma *
hppa32_elf_local_refcounts(bfd * abfd)106475fd0b74Schristos hppa32_elf_local_refcounts (bfd *abfd)
106575fd0b74Schristos {
106675fd0b74Schristos   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
106775fd0b74Schristos   bfd_signed_vma *local_refcounts;
106875fd0b74Schristos 
106975fd0b74Schristos   local_refcounts = elf_local_got_refcounts (abfd);
107075fd0b74Schristos   if (local_refcounts == NULL)
107175fd0b74Schristos     {
107275fd0b74Schristos       bfd_size_type size;
107375fd0b74Schristos 
107475fd0b74Schristos       /* Allocate space for local GOT and PLT reference
107575fd0b74Schristos 	 counts.  Done this way to save polluting elf_obj_tdata
107675fd0b74Schristos 	 with another target specific pointer.  */
107775fd0b74Schristos       size = symtab_hdr->sh_info;
107875fd0b74Schristos       size *= 2 * sizeof (bfd_signed_vma);
107975fd0b74Schristos       /* Add in space to store the local GOT TLS types.  */
108075fd0b74Schristos       size += symtab_hdr->sh_info;
108175fd0b74Schristos       local_refcounts = bfd_zalloc (abfd, size);
108275fd0b74Schristos       if (local_refcounts == NULL)
108375fd0b74Schristos 	return NULL;
108475fd0b74Schristos       elf_local_got_refcounts (abfd) = local_refcounts;
108575fd0b74Schristos       memset (hppa_elf_local_got_tls_type (abfd), GOT_UNKNOWN,
108675fd0b74Schristos 	      symtab_hdr->sh_info);
108775fd0b74Schristos     }
108875fd0b74Schristos   return local_refcounts;
108975fd0b74Schristos }
109075fd0b74Schristos 
109175fd0b74Schristos 
109275fd0b74Schristos /* Look through the relocs for a section during the first phase, and
109375fd0b74Schristos    calculate needed space in the global offset table, procedure linkage
109475fd0b74Schristos    table, and dynamic reloc sections.  At this point we haven't
109575fd0b74Schristos    necessarily read all the input files.  */
109675fd0b74Schristos 
1097*e992f068Schristos static bool
elf32_hppa_check_relocs(bfd * abfd,struct bfd_link_info * info,asection * sec,const Elf_Internal_Rela * relocs)109875fd0b74Schristos elf32_hppa_check_relocs (bfd *abfd,
109975fd0b74Schristos 			 struct bfd_link_info *info,
110075fd0b74Schristos 			 asection *sec,
110175fd0b74Schristos 			 const Elf_Internal_Rela *relocs)
110275fd0b74Schristos {
110375fd0b74Schristos   Elf_Internal_Shdr *symtab_hdr;
110475fd0b74Schristos   struct elf_link_hash_entry **eh_syms;
110575fd0b74Schristos   const Elf_Internal_Rela *rela;
110675fd0b74Schristos   const Elf_Internal_Rela *rela_end;
110775fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
110875fd0b74Schristos   asection *sreloc;
110975fd0b74Schristos 
111075fd0b74Schristos   if (bfd_link_relocatable (info))
1111*e992f068Schristos     return true;
111275fd0b74Schristos 
111375fd0b74Schristos   htab = hppa_link_hash_table (info);
111475fd0b74Schristos   if (htab == NULL)
1115*e992f068Schristos     return false;
111675fd0b74Schristos   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
111775fd0b74Schristos   eh_syms = elf_sym_hashes (abfd);
111875fd0b74Schristos   sreloc = NULL;
111975fd0b74Schristos 
112075fd0b74Schristos   rela_end = relocs + sec->reloc_count;
112175fd0b74Schristos   for (rela = relocs; rela < rela_end; rela++)
112275fd0b74Schristos     {
112375fd0b74Schristos       enum {
112475fd0b74Schristos 	NEED_GOT = 1,
112575fd0b74Schristos 	NEED_PLT = 2,
112675fd0b74Schristos 	NEED_DYNREL = 4,
112775fd0b74Schristos 	PLT_PLABEL = 8
112875fd0b74Schristos       };
112975fd0b74Schristos 
113075fd0b74Schristos       unsigned int r_symndx, r_type;
113175fd0b74Schristos       struct elf32_hppa_link_hash_entry *hh;
113275fd0b74Schristos       int need_entry = 0;
113375fd0b74Schristos 
113475fd0b74Schristos       r_symndx = ELF32_R_SYM (rela->r_info);
113575fd0b74Schristos 
113675fd0b74Schristos       if (r_symndx < symtab_hdr->sh_info)
113775fd0b74Schristos 	hh = NULL;
113875fd0b74Schristos       else
113975fd0b74Schristos 	{
114075fd0b74Schristos 	  hh =  hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
114175fd0b74Schristos 	  while (hh->eh.root.type == bfd_link_hash_indirect
114275fd0b74Schristos 		 || hh->eh.root.type == bfd_link_hash_warning)
114375fd0b74Schristos 	    hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
114475fd0b74Schristos 	}
114575fd0b74Schristos 
114675fd0b74Schristos       r_type = ELF32_R_TYPE (rela->r_info);
114775fd0b74Schristos       r_type = elf32_hppa_optimized_tls_reloc (info, r_type, hh == NULL);
114875fd0b74Schristos 
114975fd0b74Schristos       switch (r_type)
115075fd0b74Schristos 	{
115175fd0b74Schristos 	case R_PARISC_DLTIND14F:
115275fd0b74Schristos 	case R_PARISC_DLTIND14R:
115375fd0b74Schristos 	case R_PARISC_DLTIND21L:
115475fd0b74Schristos 	  /* This symbol requires a global offset table entry.  */
115575fd0b74Schristos 	  need_entry = NEED_GOT;
115675fd0b74Schristos 	  break;
115775fd0b74Schristos 
115875fd0b74Schristos 	case R_PARISC_PLABEL14R: /* "Official" procedure labels.  */
115975fd0b74Schristos 	case R_PARISC_PLABEL21L:
116075fd0b74Schristos 	case R_PARISC_PLABEL32:
116175fd0b74Schristos 	  /* If the addend is non-zero, we break badly.  */
116275fd0b74Schristos 	  if (rela->r_addend != 0)
116375fd0b74Schristos 	    abort ();
116475fd0b74Schristos 
116575fd0b74Schristos 	  /* If we are creating a shared library, then we need to
116675fd0b74Schristos 	     create a PLT entry for all PLABELs, because PLABELs with
116775fd0b74Schristos 	     local symbols may be passed via a pointer to another
116875fd0b74Schristos 	     object.  Additionally, output a dynamic relocation
116975fd0b74Schristos 	     pointing to the PLT entry.
117075fd0b74Schristos 
117175fd0b74Schristos 	     For executables, the original 32-bit ABI allowed two
117275fd0b74Schristos 	     different styles of PLABELs (function pointers):  For
117375fd0b74Schristos 	     global functions, the PLABEL word points into the .plt
117475fd0b74Schristos 	     two bytes past a (function address, gp) pair, and for
117575fd0b74Schristos 	     local functions the PLABEL points directly at the
117675fd0b74Schristos 	     function.  The magic +2 for the first type allows us to
117775fd0b74Schristos 	     differentiate between the two.  As you can imagine, this
117875fd0b74Schristos 	     is a real pain when it comes to generating code to call
117975fd0b74Schristos 	     functions indirectly or to compare function pointers.
118075fd0b74Schristos 	     We avoid the mess by always pointing a PLABEL into the
118175fd0b74Schristos 	     .plt, even for local functions.  */
1182ede78133Schristos 	  need_entry = PLT_PLABEL | NEED_PLT;
1183ede78133Schristos 	  if (bfd_link_pic (info))
1184ede78133Schristos 	    need_entry |= NEED_DYNREL;
118575fd0b74Schristos 	  break;
118675fd0b74Schristos 
118775fd0b74Schristos 	case R_PARISC_PCREL12F:
118875fd0b74Schristos 	  htab->has_12bit_branch = 1;
118975fd0b74Schristos 	  goto branch_common;
119075fd0b74Schristos 
119175fd0b74Schristos 	case R_PARISC_PCREL17C:
119275fd0b74Schristos 	case R_PARISC_PCREL17F:
119375fd0b74Schristos 	  htab->has_17bit_branch = 1;
119475fd0b74Schristos 	  goto branch_common;
119575fd0b74Schristos 
119675fd0b74Schristos 	case R_PARISC_PCREL22F:
119775fd0b74Schristos 	  htab->has_22bit_branch = 1;
119875fd0b74Schristos 	branch_common:
119975fd0b74Schristos 	  /* Function calls might need to go through the .plt, and
120075fd0b74Schristos 	     might require long branch stubs.  */
120175fd0b74Schristos 	  if (hh == NULL)
120275fd0b74Schristos 	    {
120375fd0b74Schristos 	      /* We know local syms won't need a .plt entry, and if
120475fd0b74Schristos 		 they need a long branch stub we can't guarantee that
120575fd0b74Schristos 		 we can reach the stub.  So just flag an error later
120675fd0b74Schristos 		 if we're doing a shared link and find we need a long
120775fd0b74Schristos 		 branch stub.  */
120875fd0b74Schristos 	      continue;
120975fd0b74Schristos 	    }
121075fd0b74Schristos 	  else
121175fd0b74Schristos 	    {
121275fd0b74Schristos 	      /* Global symbols will need a .plt entry if they remain
121375fd0b74Schristos 		 global, and in most cases won't need a long branch
121475fd0b74Schristos 		 stub.  Unfortunately, we have to cater for the case
121575fd0b74Schristos 		 where a symbol is forced local by versioning, or due
121675fd0b74Schristos 		 to symbolic linking, and we lose the .plt entry.  */
121775fd0b74Schristos 	      need_entry = NEED_PLT;
121875fd0b74Schristos 	      if (hh->eh.type == STT_PARISC_MILLI)
121975fd0b74Schristos 		need_entry = 0;
122075fd0b74Schristos 	    }
122175fd0b74Schristos 	  break;
122275fd0b74Schristos 
122375fd0b74Schristos 	case R_PARISC_SEGBASE:  /* Used to set segment base.  */
122475fd0b74Schristos 	case R_PARISC_SEGREL32: /* Relative reloc, used for unwind.  */
122575fd0b74Schristos 	case R_PARISC_PCREL14F: /* PC relative load/store.  */
122675fd0b74Schristos 	case R_PARISC_PCREL14R:
122775fd0b74Schristos 	case R_PARISC_PCREL17R: /* External branches.  */
122875fd0b74Schristos 	case R_PARISC_PCREL21L: /* As above, and for load/store too.  */
122975fd0b74Schristos 	case R_PARISC_PCREL32:
123075fd0b74Schristos 	  /* We don't need to propagate the relocation if linking a
123175fd0b74Schristos 	     shared object since these are section relative.  */
123275fd0b74Schristos 	  continue;
123375fd0b74Schristos 
123475fd0b74Schristos 	case R_PARISC_DPREL14F: /* Used for gp rel data load/store.  */
123575fd0b74Schristos 	case R_PARISC_DPREL14R:
123675fd0b74Schristos 	case R_PARISC_DPREL21L:
123775fd0b74Schristos 	  if (bfd_link_pic (info))
123875fd0b74Schristos 	    {
1239ede78133Schristos 	      _bfd_error_handler
1240ede78133Schristos 		/* xgettext:c-format */
1241ede78133Schristos 		(_("%pB: relocation %s can not be used when making a shared object; recompile with -fPIC"),
124275fd0b74Schristos 		 abfd,
124375fd0b74Schristos 		 elf_hppa_howto_table[r_type].name);
124475fd0b74Schristos 	      bfd_set_error (bfd_error_bad_value);
1245*e992f068Schristos 	      return false;
124675fd0b74Schristos 	    }
124775fd0b74Schristos 	  /* Fall through.  */
124875fd0b74Schristos 
124975fd0b74Schristos 	case R_PARISC_DIR17F: /* Used for external branches.  */
125075fd0b74Schristos 	case R_PARISC_DIR17R:
125175fd0b74Schristos 	case R_PARISC_DIR14F: /* Used for load/store from absolute locn.  */
125275fd0b74Schristos 	case R_PARISC_DIR14R:
125375fd0b74Schristos 	case R_PARISC_DIR21L: /* As above, and for ext branches too.  */
125475fd0b74Schristos 	case R_PARISC_DIR32: /* .word relocs.  */
125575fd0b74Schristos 	  /* We may want to output a dynamic relocation later.  */
125675fd0b74Schristos 	  need_entry = NEED_DYNREL;
125775fd0b74Schristos 	  break;
125875fd0b74Schristos 
125975fd0b74Schristos 	  /* This relocation describes the C++ object vtable hierarchy.
126075fd0b74Schristos 	     Reconstruct it for later use during GC.  */
126175fd0b74Schristos 	case R_PARISC_GNU_VTINHERIT:
126275fd0b74Schristos 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
1263*e992f068Schristos 	    return false;
126475fd0b74Schristos 	  continue;
126575fd0b74Schristos 
126675fd0b74Schristos 	  /* This relocation describes which C++ vtable entries are actually
126775fd0b74Schristos 	     used.  Record for later use during GC.  */
126875fd0b74Schristos 	case R_PARISC_GNU_VTENTRY:
1269012573ebSchristos 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
1270*e992f068Schristos 	    return false;
127175fd0b74Schristos 	  continue;
127275fd0b74Schristos 
127375fd0b74Schristos 	case R_PARISC_TLS_GD21L:
127475fd0b74Schristos 	case R_PARISC_TLS_GD14R:
127575fd0b74Schristos 	case R_PARISC_TLS_LDM21L:
127675fd0b74Schristos 	case R_PARISC_TLS_LDM14R:
127775fd0b74Schristos 	  need_entry = NEED_GOT;
127875fd0b74Schristos 	  break;
127975fd0b74Schristos 
128075fd0b74Schristos 	case R_PARISC_TLS_IE21L:
128175fd0b74Schristos 	case R_PARISC_TLS_IE14R:
1282ede78133Schristos 	  if (bfd_link_dll (info))
128375fd0b74Schristos 	    info->flags |= DF_STATIC_TLS;
128475fd0b74Schristos 	  need_entry = NEED_GOT;
128575fd0b74Schristos 	  break;
128675fd0b74Schristos 
128775fd0b74Schristos 	default:
128875fd0b74Schristos 	  continue;
128975fd0b74Schristos 	}
129075fd0b74Schristos 
129175fd0b74Schristos       /* Now carry out our orders.  */
129275fd0b74Schristos       if (need_entry & NEED_GOT)
129375fd0b74Schristos 	{
1294ede78133Schristos 	  int tls_type = GOT_NORMAL;
1295ede78133Schristos 
129675fd0b74Schristos 	  switch (r_type)
129775fd0b74Schristos 	    {
129875fd0b74Schristos 	    default:
129975fd0b74Schristos 	      break;
130075fd0b74Schristos 	    case R_PARISC_TLS_GD21L:
130175fd0b74Schristos 	    case R_PARISC_TLS_GD14R:
1302ede78133Schristos 	      tls_type = GOT_TLS_GD;
130375fd0b74Schristos 	      break;
130475fd0b74Schristos 	    case R_PARISC_TLS_LDM21L:
130575fd0b74Schristos 	    case R_PARISC_TLS_LDM14R:
1306ede78133Schristos 	      tls_type = GOT_TLS_LDM;
130775fd0b74Schristos 	      break;
130875fd0b74Schristos 	    case R_PARISC_TLS_IE21L:
130975fd0b74Schristos 	    case R_PARISC_TLS_IE14R:
1310ede78133Schristos 	      tls_type = GOT_TLS_IE;
131175fd0b74Schristos 	      break;
131275fd0b74Schristos 	    }
131375fd0b74Schristos 
131475fd0b74Schristos 	  /* Allocate space for a GOT entry, as well as a dynamic
131575fd0b74Schristos 	     relocation for this entry.  */
1316ede78133Schristos 	  if (htab->etab.sgot == NULL)
131775fd0b74Schristos 	    {
131875fd0b74Schristos 	      if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
1319*e992f068Schristos 		return false;
132075fd0b74Schristos 	    }
132175fd0b74Schristos 
132275fd0b74Schristos 	  if (hh != NULL)
132375fd0b74Schristos 	    {
1324ede78133Schristos 	      if (tls_type == GOT_TLS_LDM)
1325ede78133Schristos 		htab->tls_ldm_got.refcount += 1;
1326ede78133Schristos 	      else
132775fd0b74Schristos 		hh->eh.got.refcount += 1;
1328ede78133Schristos 	      hh->tls_type |= tls_type;
132975fd0b74Schristos 	    }
133075fd0b74Schristos 	  else
133175fd0b74Schristos 	    {
133275fd0b74Schristos 	      bfd_signed_vma *local_got_refcounts;
133375fd0b74Schristos 
133475fd0b74Schristos 	      /* This is a global offset table entry for a local symbol.  */
133575fd0b74Schristos 	      local_got_refcounts = hppa32_elf_local_refcounts (abfd);
133675fd0b74Schristos 	      if (local_got_refcounts == NULL)
1337*e992f068Schristos 		return false;
1338ede78133Schristos 	      if (tls_type == GOT_TLS_LDM)
1339ede78133Schristos 		htab->tls_ldm_got.refcount += 1;
1340ede78133Schristos 	      else
134175fd0b74Schristos 		local_got_refcounts[r_symndx] += 1;
134275fd0b74Schristos 
1343ede78133Schristos 	      hppa_elf_local_got_tls_type (abfd) [r_symndx] |= tls_type;
134475fd0b74Schristos 	    }
134575fd0b74Schristos 	}
134675fd0b74Schristos 
134775fd0b74Schristos       if (need_entry & NEED_PLT)
134875fd0b74Schristos 	{
134975fd0b74Schristos 	  /* If we are creating a shared library, and this is a reloc
135075fd0b74Schristos 	     against a weak symbol or a global symbol in a dynamic
135175fd0b74Schristos 	     object, then we will be creating an import stub and a
135275fd0b74Schristos 	     .plt entry for the symbol.  Similarly, on a normal link
135375fd0b74Schristos 	     to symbols defined in a dynamic object we'll need the
135475fd0b74Schristos 	     import stub and a .plt entry.  We don't know yet whether
135575fd0b74Schristos 	     the symbol is defined or not, so make an entry anyway and
135675fd0b74Schristos 	     clean up later in adjust_dynamic_symbol.  */
135775fd0b74Schristos 	  if ((sec->flags & SEC_ALLOC) != 0)
135875fd0b74Schristos 	    {
135975fd0b74Schristos 	      if (hh != NULL)
136075fd0b74Schristos 		{
136175fd0b74Schristos 		  hh->eh.needs_plt = 1;
136275fd0b74Schristos 		  hh->eh.plt.refcount += 1;
136375fd0b74Schristos 
136475fd0b74Schristos 		  /* If this .plt entry is for a plabel, mark it so
136575fd0b74Schristos 		     that adjust_dynamic_symbol will keep the entry
136675fd0b74Schristos 		     even if it appears to be local.  */
136775fd0b74Schristos 		  if (need_entry & PLT_PLABEL)
136875fd0b74Schristos 		    hh->plabel = 1;
136975fd0b74Schristos 		}
137075fd0b74Schristos 	      else if (need_entry & PLT_PLABEL)
137175fd0b74Schristos 		{
137275fd0b74Schristos 		  bfd_signed_vma *local_got_refcounts;
137375fd0b74Schristos 		  bfd_signed_vma *local_plt_refcounts;
137475fd0b74Schristos 
137575fd0b74Schristos 		  local_got_refcounts = hppa32_elf_local_refcounts (abfd);
137675fd0b74Schristos 		  if (local_got_refcounts == NULL)
1377*e992f068Schristos 		    return false;
137875fd0b74Schristos 		  local_plt_refcounts = (local_got_refcounts
137975fd0b74Schristos 					 + symtab_hdr->sh_info);
138075fd0b74Schristos 		  local_plt_refcounts[r_symndx] += 1;
138175fd0b74Schristos 		}
138275fd0b74Schristos 	    }
138375fd0b74Schristos 	}
138475fd0b74Schristos 
1385ede78133Schristos       if ((need_entry & NEED_DYNREL) != 0
1386ede78133Schristos 	  && (sec->flags & SEC_ALLOC) != 0)
138775fd0b74Schristos 	{
138875fd0b74Schristos 	  /* Flag this symbol as having a non-got, non-plt reference
138975fd0b74Schristos 	     so that we generate copy relocs if it turns out to be
139075fd0b74Schristos 	     dynamic.  */
1391ede78133Schristos 	  if (hh != NULL)
139275fd0b74Schristos 	    hh->eh.non_got_ref = 1;
139375fd0b74Schristos 
139475fd0b74Schristos 	  /* If we are creating a shared library then we need to copy
139575fd0b74Schristos 	     the reloc into the shared library.  However, if we are
139675fd0b74Schristos 	     linking with -Bsymbolic, we need only copy absolute
139775fd0b74Schristos 	     relocs or relocs against symbols that are not defined in
139875fd0b74Schristos 	     an object we are including in the link.  PC- or DP- or
139975fd0b74Schristos 	     DLT-relative relocs against any local sym or global sym
140075fd0b74Schristos 	     with DEF_REGULAR set, can be discarded.  At this point we
140175fd0b74Schristos 	     have not seen all the input files, so it is possible that
140275fd0b74Schristos 	     DEF_REGULAR is not set now but will be set later (it is
140375fd0b74Schristos 	     never cleared).  We account for that possibility below by
140475fd0b74Schristos 	     storing information in the dyn_relocs field of the
140575fd0b74Schristos 	     hash table entry.
140675fd0b74Schristos 
140775fd0b74Schristos 	     A similar situation to the -Bsymbolic case occurs when
140875fd0b74Schristos 	     creating shared libraries and symbol visibility changes
140975fd0b74Schristos 	     render the symbol local.
141075fd0b74Schristos 
141175fd0b74Schristos 	     As it turns out, all the relocs we will be creating here
141275fd0b74Schristos 	     are absolute, so we cannot remove them on -Bsymbolic
141375fd0b74Schristos 	     links or visibility changes anyway.  A STUB_REL reloc
141475fd0b74Schristos 	     is absolute too, as in that case it is the reloc in the
141575fd0b74Schristos 	     stub we will be creating, rather than copying the PCREL
141675fd0b74Schristos 	     reloc in the branch.
141775fd0b74Schristos 
141875fd0b74Schristos 	     If on the other hand, we are creating an executable, we
141975fd0b74Schristos 	     may need to keep relocations for symbols satisfied by a
142075fd0b74Schristos 	     dynamic library if we manage to avoid copy relocs for the
142175fd0b74Schristos 	     symbol.  */
142275fd0b74Schristos 	  if ((bfd_link_pic (info)
142375fd0b74Schristos 	       && (IS_ABSOLUTE_RELOC (r_type)
142475fd0b74Schristos 		   || (hh != NULL
142575fd0b74Schristos 		       && (!SYMBOLIC_BIND (info, &hh->eh)
142675fd0b74Schristos 			   || hh->eh.root.type == bfd_link_hash_defweak
142775fd0b74Schristos 			   || !hh->eh.def_regular))))
142875fd0b74Schristos 	      || (ELIMINATE_COPY_RELOCS
142975fd0b74Schristos 		  && !bfd_link_pic (info)
143075fd0b74Schristos 		  && hh != NULL
143175fd0b74Schristos 		  && (hh->eh.root.type == bfd_link_hash_defweak
143275fd0b74Schristos 		      || !hh->eh.def_regular)))
143375fd0b74Schristos 	    {
1434ede78133Schristos 	      struct elf_dyn_relocs *hdh_p;
1435ede78133Schristos 	      struct elf_dyn_relocs **hdh_head;
143675fd0b74Schristos 
143775fd0b74Schristos 	      /* Create a reloc section in dynobj and make room for
143875fd0b74Schristos 		 this reloc.  */
143975fd0b74Schristos 	      if (sreloc == NULL)
144075fd0b74Schristos 		{
144175fd0b74Schristos 		  sreloc = _bfd_elf_make_dynamic_reloc_section
1442*e992f068Schristos 		    (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ true);
144375fd0b74Schristos 
144475fd0b74Schristos 		  if (sreloc == NULL)
144575fd0b74Schristos 		    {
144675fd0b74Schristos 		      bfd_set_error (bfd_error_bad_value);
1447*e992f068Schristos 		      return false;
144875fd0b74Schristos 		    }
144975fd0b74Schristos 		}
145075fd0b74Schristos 
145175fd0b74Schristos 	      /* If this is a global symbol, we count the number of
145275fd0b74Schristos 		 relocations we need for this symbol.  */
145375fd0b74Schristos 	      if (hh != NULL)
145475fd0b74Schristos 		{
1455*e992f068Schristos 		  hdh_head = &hh->eh.dyn_relocs;
145675fd0b74Schristos 		}
145775fd0b74Schristos 	      else
145875fd0b74Schristos 		{
145975fd0b74Schristos 		  /* Track dynamic relocs needed for local syms too.
146075fd0b74Schristos 		     We really need local syms available to do this
146175fd0b74Schristos 		     easily.  Oh well.  */
146275fd0b74Schristos 		  asection *sr;
146375fd0b74Schristos 		  void *vpp;
146475fd0b74Schristos 		  Elf_Internal_Sym *isym;
146575fd0b74Schristos 
1466*e992f068Schristos 		  isym = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
146775fd0b74Schristos 						abfd, r_symndx);
146875fd0b74Schristos 		  if (isym == NULL)
1469*e992f068Schristos 		    return false;
147075fd0b74Schristos 
147175fd0b74Schristos 		  sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
147275fd0b74Schristos 		  if (sr == NULL)
147375fd0b74Schristos 		    sr = sec;
147475fd0b74Schristos 
147575fd0b74Schristos 		  vpp = &elf_section_data (sr)->local_dynrel;
1476ede78133Schristos 		  hdh_head = (struct elf_dyn_relocs **) vpp;
147775fd0b74Schristos 		}
147875fd0b74Schristos 
147975fd0b74Schristos 	      hdh_p = *hdh_head;
148075fd0b74Schristos 	      if (hdh_p == NULL || hdh_p->sec != sec)
148175fd0b74Schristos 		{
148275fd0b74Schristos 		  hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
148375fd0b74Schristos 		  if (hdh_p == NULL)
1484*e992f068Schristos 		    return false;
1485ede78133Schristos 		  hdh_p->next = *hdh_head;
148675fd0b74Schristos 		  *hdh_head = hdh_p;
148775fd0b74Schristos 		  hdh_p->sec = sec;
148875fd0b74Schristos 		  hdh_p->count = 0;
148975fd0b74Schristos #if RELATIVE_DYNRELOCS
1490ede78133Schristos 		  hdh_p->pc_count = 0;
149175fd0b74Schristos #endif
149275fd0b74Schristos 		}
149375fd0b74Schristos 
149475fd0b74Schristos 	      hdh_p->count += 1;
149575fd0b74Schristos #if RELATIVE_DYNRELOCS
149675fd0b74Schristos 	      if (!IS_ABSOLUTE_RELOC (rtype))
1497ede78133Schristos 		hdh_p->pc_count += 1;
149875fd0b74Schristos #endif
149975fd0b74Schristos 	    }
150075fd0b74Schristos 	}
150175fd0b74Schristos     }
150275fd0b74Schristos 
1503*e992f068Schristos   return true;
150475fd0b74Schristos }
150575fd0b74Schristos 
150675fd0b74Schristos /* Return the section that should be marked against garbage collection
150775fd0b74Schristos    for a given relocation.  */
150875fd0b74Schristos 
150975fd0b74Schristos static asection *
elf32_hppa_gc_mark_hook(asection * sec,struct bfd_link_info * info,Elf_Internal_Rela * rela,struct elf_link_hash_entry * hh,Elf_Internal_Sym * sym)151075fd0b74Schristos elf32_hppa_gc_mark_hook (asection *sec,
151175fd0b74Schristos 			 struct bfd_link_info *info,
151275fd0b74Schristos 			 Elf_Internal_Rela *rela,
151375fd0b74Schristos 			 struct elf_link_hash_entry *hh,
151475fd0b74Schristos 			 Elf_Internal_Sym *sym)
151575fd0b74Schristos {
151675fd0b74Schristos   if (hh != NULL)
151775fd0b74Schristos     switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
151875fd0b74Schristos       {
151975fd0b74Schristos       case R_PARISC_GNU_VTINHERIT:
152075fd0b74Schristos       case R_PARISC_GNU_VTENTRY:
152175fd0b74Schristos 	return NULL;
152275fd0b74Schristos       }
152375fd0b74Schristos 
152475fd0b74Schristos   return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
152575fd0b74Schristos }
152675fd0b74Schristos 
152775fd0b74Schristos /* Support for core dump NOTE sections.  */
152875fd0b74Schristos 
1529*e992f068Schristos static bool
elf32_hppa_grok_prstatus(bfd * abfd,Elf_Internal_Note * note)153075fd0b74Schristos elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
153175fd0b74Schristos {
153275fd0b74Schristos   int offset;
153375fd0b74Schristos   size_t size;
153475fd0b74Schristos 
153575fd0b74Schristos   switch (note->descsz)
153675fd0b74Schristos     {
153775fd0b74Schristos       default:
1538*e992f068Schristos 	return false;
153975fd0b74Schristos 
154075fd0b74Schristos       case 396:		/* Linux/hppa */
154175fd0b74Schristos 	/* pr_cursig */
154275fd0b74Schristos 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
154375fd0b74Schristos 
154475fd0b74Schristos 	/* pr_pid */
154575fd0b74Schristos 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
154675fd0b74Schristos 
154775fd0b74Schristos 	/* pr_reg */
154875fd0b74Schristos 	offset = 72;
154975fd0b74Schristos 	size = 320;
155075fd0b74Schristos 
155175fd0b74Schristos 	break;
155275fd0b74Schristos     }
155375fd0b74Schristos 
155475fd0b74Schristos   /* Make a ".reg/999" section.  */
155575fd0b74Schristos   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
155675fd0b74Schristos 					  size, note->descpos + offset);
155775fd0b74Schristos }
155875fd0b74Schristos 
1559*e992f068Schristos static bool
elf32_hppa_grok_psinfo(bfd * abfd,Elf_Internal_Note * note)156075fd0b74Schristos elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
156175fd0b74Schristos {
156275fd0b74Schristos   switch (note->descsz)
156375fd0b74Schristos     {
156475fd0b74Schristos       default:
1565*e992f068Schristos 	return false;
156675fd0b74Schristos 
156775fd0b74Schristos       case 124:		/* Linux/hppa elf_prpsinfo.  */
156875fd0b74Schristos 	elf_tdata (abfd)->core->program
156975fd0b74Schristos 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
157075fd0b74Schristos 	elf_tdata (abfd)->core->command
157175fd0b74Schristos 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
157275fd0b74Schristos     }
157375fd0b74Schristos 
157475fd0b74Schristos   /* Note that for some reason, a spurious space is tacked
157575fd0b74Schristos      onto the end of the args in some (at least one anyway)
157675fd0b74Schristos      implementations, so strip it off if it exists.  */
157775fd0b74Schristos   {
157875fd0b74Schristos     char *command = elf_tdata (abfd)->core->command;
157975fd0b74Schristos     int n = strlen (command);
158075fd0b74Schristos 
158175fd0b74Schristos     if (0 < n && command[n - 1] == ' ')
158275fd0b74Schristos       command[n - 1] = '\0';
158375fd0b74Schristos   }
158475fd0b74Schristos 
1585*e992f068Schristos   return true;
158675fd0b74Schristos }
158775fd0b74Schristos 
158875fd0b74Schristos /* Our own version of hide_symbol, so that we can keep plt entries for
158975fd0b74Schristos    plabels.  */
159075fd0b74Schristos 
159175fd0b74Schristos static void
elf32_hppa_hide_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * eh,bool force_local)159275fd0b74Schristos elf32_hppa_hide_symbol (struct bfd_link_info *info,
159375fd0b74Schristos 			struct elf_link_hash_entry *eh,
1594*e992f068Schristos 			bool force_local)
159575fd0b74Schristos {
159675fd0b74Schristos   if (force_local)
159775fd0b74Schristos     {
159875fd0b74Schristos       eh->forced_local = 1;
159975fd0b74Schristos       if (eh->dynindx != -1)
160075fd0b74Schristos 	{
160175fd0b74Schristos 	  eh->dynindx = -1;
160275fd0b74Schristos 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
160375fd0b74Schristos 				  eh->dynstr_index);
160475fd0b74Schristos 	}
160575fd0b74Schristos 
160675fd0b74Schristos       /* PR 16082: Remove version information from hidden symbol.  */
160775fd0b74Schristos       eh->verinfo.verdef = NULL;
160875fd0b74Schristos       eh->verinfo.vertree = NULL;
160975fd0b74Schristos     }
161075fd0b74Schristos 
161175fd0b74Schristos   /* STT_GNU_IFUNC symbol must go through PLT.  */
161275fd0b74Schristos   if (! hppa_elf_hash_entry (eh)->plabel
161375fd0b74Schristos       && eh->type != STT_GNU_IFUNC)
161475fd0b74Schristos     {
161575fd0b74Schristos       eh->needs_plt = 0;
161675fd0b74Schristos       eh->plt = elf_hash_table (info)->init_plt_offset;
161775fd0b74Schristos     }
161875fd0b74Schristos }
161975fd0b74Schristos 
1620ede78133Schristos /* Return true if we have dynamic relocs against H or any of its weak
1621ede78133Schristos    aliases, that apply to read-only sections.  Cannot be used after
1622ede78133Schristos    size_dynamic_sections.  */
1623ede78133Schristos 
1624*e992f068Schristos static bool
alias_readonly_dynrelocs(struct elf_link_hash_entry * eh)1625ede78133Schristos alias_readonly_dynrelocs (struct elf_link_hash_entry *eh)
1626ede78133Schristos {
1627ede78133Schristos   struct elf32_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1628ede78133Schristos   do
1629ede78133Schristos     {
1630*e992f068Schristos       if (_bfd_elf_readonly_dynrelocs (&hh->eh))
1631*e992f068Schristos 	return true;
1632ede78133Schristos       hh = hppa_elf_hash_entry (hh->eh.u.alias);
1633ede78133Schristos     } while (hh != NULL && &hh->eh != eh);
1634ede78133Schristos 
1635*e992f068Schristos   return false;
1636ede78133Schristos }
1637ede78133Schristos 
163875fd0b74Schristos /* Adjust a symbol defined by a dynamic object and referenced by a
163975fd0b74Schristos    regular object.  The current definition is in some section of the
164075fd0b74Schristos    dynamic object, but we're not including those sections.  We have to
164175fd0b74Schristos    change the definition to something the rest of the link can
164275fd0b74Schristos    understand.  */
164375fd0b74Schristos 
1644*e992f068Schristos static bool
elf32_hppa_adjust_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * eh)164575fd0b74Schristos elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
164675fd0b74Schristos 				  struct elf_link_hash_entry *eh)
164775fd0b74Schristos {
164875fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
1649ede78133Schristos   asection *sec, *srel;
165075fd0b74Schristos 
165175fd0b74Schristos   /* If this is a function, put it in the procedure linkage table.  We
165275fd0b74Schristos      will fill in the contents of the procedure linkage table later.  */
165375fd0b74Schristos   if (eh->type == STT_FUNC
165475fd0b74Schristos       || eh->needs_plt)
165575fd0b74Schristos     {
1656*e992f068Schristos       bool local = (SYMBOL_CALLS_LOCAL (info, eh)
1657ede78133Schristos 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh));
1658ede78133Schristos       /* Discard dyn_relocs when non-pic if we've decided that a
1659ede78133Schristos 	 function symbol is local.  */
1660ede78133Schristos       if (!bfd_link_pic (info) && local)
1661*e992f068Schristos 	eh->dyn_relocs = NULL;
1662ede78133Schristos 
166375fd0b74Schristos       /* If the symbol is used by a plabel, we must allocate a PLT slot.
166475fd0b74Schristos 	 The refcounts are not reliable when it has been hidden since
166575fd0b74Schristos 	 hide_symbol can be called before the plabel flag is set.  */
1666ede78133Schristos       if (hppa_elf_hash_entry (eh)->plabel)
166775fd0b74Schristos 	eh->plt.refcount = 1;
166875fd0b74Schristos 
1669ede78133Schristos       /* Note that unlike some other backends, the refcount is not
1670ede78133Schristos 	 incremented for a non-call (and non-plabel) function reference.  */
1671ede78133Schristos       else if (eh->plt.refcount <= 0
1672ede78133Schristos 	       || local)
167375fd0b74Schristos 	{
167475fd0b74Schristos 	  /* The .plt entry is not needed when:
167575fd0b74Schristos 	     a) Garbage collection has removed all references to the
167675fd0b74Schristos 	     symbol, or
167775fd0b74Schristos 	     b) We know for certain the symbol is defined in this
167875fd0b74Schristos 	     object, and it's not a weak definition, nor is the symbol
167975fd0b74Schristos 	     used by a plabel relocation.  Either this object is the
168075fd0b74Schristos 	     application or we are doing a shared symbolic link.  */
168175fd0b74Schristos 	  eh->plt.offset = (bfd_vma) -1;
168275fd0b74Schristos 	  eh->needs_plt = 0;
168375fd0b74Schristos 	}
168475fd0b74Schristos 
1685ede78133Schristos       /* Unlike other targets, elf32-hppa.c does not define a function
1686ede78133Schristos 	 symbol in a non-pic executable on PLT stub code, so we don't
1687ede78133Schristos 	 have a local definition in that case.  ie. dyn_relocs can't
1688ede78133Schristos 	 be discarded.  */
1689ede78133Schristos 
1690ede78133Schristos       /* Function symbols can't have copy relocs.  */
1691*e992f068Schristos       return true;
169275fd0b74Schristos     }
169375fd0b74Schristos   else
169475fd0b74Schristos     eh->plt.offset = (bfd_vma) -1;
169575fd0b74Schristos 
1696ede78133Schristos   htab = hppa_link_hash_table (info);
1697ede78133Schristos   if (htab == NULL)
1698*e992f068Schristos     return false;
1699ede78133Schristos 
170075fd0b74Schristos   /* If this is a weak symbol, and there is a real definition, the
170175fd0b74Schristos      processor independent code will have arranged for us to see the
170275fd0b74Schristos      real definition first, and we can just use the same value.  */
1703ede78133Schristos   if (eh->is_weakalias)
170475fd0b74Schristos     {
1705ede78133Schristos       struct elf_link_hash_entry *def = weakdef (eh);
1706ede78133Schristos       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1707ede78133Schristos       eh->root.u.def.section = def->root.u.def.section;
1708ede78133Schristos       eh->root.u.def.value = def->root.u.def.value;
1709ede78133Schristos       if (def->root.u.def.section == htab->etab.sdynbss
1710ede78133Schristos 	  || def->root.u.def.section == htab->etab.sdynrelro)
1711*e992f068Schristos 	eh->dyn_relocs = NULL;
1712*e992f068Schristos       return true;
171375fd0b74Schristos     }
171475fd0b74Schristos 
171575fd0b74Schristos   /* This is a reference to a symbol defined by a dynamic object which
171675fd0b74Schristos      is not a function.  */
171775fd0b74Schristos 
171875fd0b74Schristos   /* If we are creating a shared library, we must presume that the
171975fd0b74Schristos      only references to the symbol are via the global offset table.
172075fd0b74Schristos      For such cases we need not do anything here; the relocations will
172175fd0b74Schristos      be handled correctly by relocate_section.  */
172275fd0b74Schristos   if (bfd_link_pic (info))
1723*e992f068Schristos     return true;
172475fd0b74Schristos 
172575fd0b74Schristos   /* If there are no references to this symbol that do not use the
172675fd0b74Schristos      GOT, we don't need to generate a copy reloc.  */
172775fd0b74Schristos   if (!eh->non_got_ref)
1728*e992f068Schristos     return true;
172975fd0b74Schristos 
1730ede78133Schristos   /* If -z nocopyreloc was given, we won't generate them either.  */
1731ede78133Schristos   if (info->nocopyreloc)
1732*e992f068Schristos     return true;
1733ede78133Schristos 
1734ede78133Schristos   /* If we don't find any dynamic relocs in read-only sections, then
1735ede78133Schristos      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1736ede78133Schristos   if (ELIMINATE_COPY_RELOCS
1737ede78133Schristos       && !alias_readonly_dynrelocs (eh))
1738*e992f068Schristos     return true;
173975fd0b74Schristos 
174075fd0b74Schristos   /* We must allocate the symbol in our .dynbss section, which will
174175fd0b74Schristos      become part of the .bss section of the executable.  There will be
174275fd0b74Schristos      an entry for this symbol in the .dynsym section.  The dynamic
174375fd0b74Schristos      object will contain position independent code, so all references
174475fd0b74Schristos      from the dynamic object to this symbol will go through the global
174575fd0b74Schristos      offset table.  The dynamic linker will use the .dynsym entry to
174675fd0b74Schristos      determine the address it must put in the global offset table, so
174775fd0b74Schristos      both the dynamic object and the regular object will refer to the
174875fd0b74Schristos      same memory location for the variable.  */
1749ede78133Schristos   if ((eh->root.u.def.section->flags & SEC_READONLY) != 0)
1750ede78133Schristos     {
1751ede78133Schristos       sec = htab->etab.sdynrelro;
1752ede78133Schristos       srel = htab->etab.sreldynrelro;
1753ede78133Schristos     }
1754ede78133Schristos   else
1755ede78133Schristos     {
1756ede78133Schristos       sec = htab->etab.sdynbss;
1757ede78133Schristos       srel = htab->etab.srelbss;
1758ede78133Schristos     }
1759ede78133Schristos   if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
1760ede78133Schristos     {
176175fd0b74Schristos       /* We must generate a COPY reloc to tell the dynamic linker to
176275fd0b74Schristos 	 copy the initial value out of the dynamic object and into the
176375fd0b74Schristos 	 runtime process image.  */
1764ede78133Schristos       srel->size += sizeof (Elf32_External_Rela);
176575fd0b74Schristos       eh->needs_copy = 1;
176675fd0b74Schristos     }
176775fd0b74Schristos 
1768ede78133Schristos   /* We no longer want dyn_relocs.  */
1769*e992f068Schristos   eh->dyn_relocs = NULL;
177075fd0b74Schristos   return _bfd_elf_adjust_dynamic_copy (info, eh, sec);
177175fd0b74Schristos }
177275fd0b74Schristos 
1773ede78133Schristos /* If EH is undefined, make it dynamic if that makes sense.  */
1774ede78133Schristos 
1775*e992f068Schristos static bool
ensure_undef_dynamic(struct bfd_link_info * info,struct elf_link_hash_entry * eh)1776ede78133Schristos ensure_undef_dynamic (struct bfd_link_info *info,
1777ede78133Schristos 		      struct elf_link_hash_entry *eh)
1778ede78133Schristos {
1779ede78133Schristos   struct elf_link_hash_table *htab = elf_hash_table (info);
1780ede78133Schristos 
1781ede78133Schristos   if (htab->dynamic_sections_created
1782ede78133Schristos       && (eh->root.type == bfd_link_hash_undefweak
1783ede78133Schristos 	  || eh->root.type == bfd_link_hash_undefined)
1784ede78133Schristos       && eh->dynindx == -1
1785ede78133Schristos       && !eh->forced_local
1786ede78133Schristos       && eh->type != STT_PARISC_MILLI
1787ede78133Schristos       && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh)
1788ede78133Schristos       && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
1789ede78133Schristos     return bfd_elf_link_record_dynamic_symbol (info, eh);
1790*e992f068Schristos   return true;
1791ede78133Schristos }
1792ede78133Schristos 
179375fd0b74Schristos /* Allocate space in the .plt for entries that won't have relocations.
179475fd0b74Schristos    ie. plabel entries.  */
179575fd0b74Schristos 
1796*e992f068Schristos static bool
allocate_plt_static(struct elf_link_hash_entry * eh,void * inf)179775fd0b74Schristos allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
179875fd0b74Schristos {
179975fd0b74Schristos   struct bfd_link_info *info;
180075fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
180175fd0b74Schristos   struct elf32_hppa_link_hash_entry *hh;
180275fd0b74Schristos   asection *sec;
180375fd0b74Schristos 
180475fd0b74Schristos   if (eh->root.type == bfd_link_hash_indirect)
1805*e992f068Schristos     return true;
180675fd0b74Schristos 
180775fd0b74Schristos   info = (struct bfd_link_info *) inf;
180875fd0b74Schristos   hh = hppa_elf_hash_entry (eh);
180975fd0b74Schristos   htab = hppa_link_hash_table (info);
181075fd0b74Schristos   if (htab == NULL)
1811*e992f068Schristos     return false;
181275fd0b74Schristos 
181375fd0b74Schristos   if (htab->etab.dynamic_sections_created
181475fd0b74Schristos       && eh->plt.refcount > 0)
181575fd0b74Schristos     {
1816ede78133Schristos       if (!ensure_undef_dynamic (info, eh))
1817*e992f068Schristos 	return false;
181875fd0b74Schristos 
181975fd0b74Schristos       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
182075fd0b74Schristos 	{
182175fd0b74Schristos 	  /* Allocate these later.  From this point on, h->plabel
182275fd0b74Schristos 	     means that the plt entry is only used by a plabel.
182375fd0b74Schristos 	     We'll be using a normal plt entry for this symbol, so
182475fd0b74Schristos 	     clear the plabel indicator.  */
182575fd0b74Schristos 
182675fd0b74Schristos 	  hh->plabel = 0;
182775fd0b74Schristos 	}
182875fd0b74Schristos       else if (hh->plabel)
182975fd0b74Schristos 	{
183075fd0b74Schristos 	  /* Make an entry in the .plt section for plabel references
183175fd0b74Schristos 	     that won't have a .plt entry for other reasons.  */
1832ede78133Schristos 	  sec = htab->etab.splt;
183375fd0b74Schristos 	  eh->plt.offset = sec->size;
183475fd0b74Schristos 	  sec->size += PLT_ENTRY_SIZE;
1835ede78133Schristos 	  if (bfd_link_pic (info))
1836ede78133Schristos 	    htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
183775fd0b74Schristos 	}
183875fd0b74Schristos       else
183975fd0b74Schristos 	{
184075fd0b74Schristos 	  /* No .plt entry needed.  */
184175fd0b74Schristos 	  eh->plt.offset = (bfd_vma) -1;
184275fd0b74Schristos 	  eh->needs_plt = 0;
184375fd0b74Schristos 	}
184475fd0b74Schristos     }
184575fd0b74Schristos   else
184675fd0b74Schristos     {
184775fd0b74Schristos       eh->plt.offset = (bfd_vma) -1;
184875fd0b74Schristos       eh->needs_plt = 0;
184975fd0b74Schristos     }
185075fd0b74Schristos 
1851*e992f068Schristos   return true;
185275fd0b74Schristos }
185375fd0b74Schristos 
1854ede78133Schristos /* Calculate size of GOT entries for symbol given its TLS_TYPE.  */
1855ede78133Schristos 
1856ede78133Schristos static inline unsigned int
got_entries_needed(int tls_type)1857ede78133Schristos got_entries_needed (int tls_type)
1858ede78133Schristos {
1859ede78133Schristos   unsigned int need = 0;
1860ede78133Schristos 
1861ede78133Schristos   if ((tls_type & GOT_NORMAL) != 0)
1862ede78133Schristos     need += GOT_ENTRY_SIZE;
1863ede78133Schristos   if ((tls_type & GOT_TLS_GD) != 0)
1864ede78133Schristos     need += GOT_ENTRY_SIZE * 2;
1865ede78133Schristos   if ((tls_type & GOT_TLS_IE) != 0)
1866ede78133Schristos     need += GOT_ENTRY_SIZE;
1867ede78133Schristos   return need;
1868ede78133Schristos }
1869ede78133Schristos 
1870ede78133Schristos /* Calculate size of relocs needed for symbol given its TLS_TYPE and
1871ede78133Schristos    NEEDed GOT entries.  TPREL_KNOWN says a TPREL offset can be
1872ede78133Schristos    calculated at link time.  DTPREL_KNOWN says the same for a DTPREL
1873ede78133Schristos    offset.  */
1874ede78133Schristos 
1875ede78133Schristos static inline unsigned int
got_relocs_needed(int tls_type,unsigned int need,bool dtprel_known,bool tprel_known)1876ede78133Schristos got_relocs_needed (int tls_type, unsigned int need,
1877*e992f068Schristos 		   bool dtprel_known, bool tprel_known)
1878ede78133Schristos {
1879ede78133Schristos   /* All the entries we allocated need relocs.
1880ede78133Schristos      Except for GD and IE with local symbols.  */
1881ede78133Schristos   if ((tls_type & GOT_TLS_GD) != 0 && dtprel_known)
1882ede78133Schristos     need -= GOT_ENTRY_SIZE;
1883ede78133Schristos   if ((tls_type & GOT_TLS_IE) != 0 && tprel_known)
1884ede78133Schristos     need -= GOT_ENTRY_SIZE;
1885ede78133Schristos   return need * sizeof (Elf32_External_Rela) / GOT_ENTRY_SIZE;
1886ede78133Schristos }
1887ede78133Schristos 
188875fd0b74Schristos /* Allocate space in .plt, .got and associated reloc sections for
188975fd0b74Schristos    global syms.  */
189075fd0b74Schristos 
1891*e992f068Schristos static bool
allocate_dynrelocs(struct elf_link_hash_entry * eh,void * inf)189275fd0b74Schristos allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
189375fd0b74Schristos {
189475fd0b74Schristos   struct bfd_link_info *info;
189575fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
189675fd0b74Schristos   asection *sec;
189775fd0b74Schristos   struct elf32_hppa_link_hash_entry *hh;
1898ede78133Schristos   struct elf_dyn_relocs *hdh_p;
189975fd0b74Schristos 
190075fd0b74Schristos   if (eh->root.type == bfd_link_hash_indirect)
1901*e992f068Schristos     return true;
190275fd0b74Schristos 
190375fd0b74Schristos   info = inf;
190475fd0b74Schristos   htab = hppa_link_hash_table (info);
190575fd0b74Schristos   if (htab == NULL)
1906*e992f068Schristos     return false;
190775fd0b74Schristos 
190875fd0b74Schristos   hh = hppa_elf_hash_entry (eh);
190975fd0b74Schristos 
191075fd0b74Schristos   if (htab->etab.dynamic_sections_created
191175fd0b74Schristos       && eh->plt.offset != (bfd_vma) -1
191275fd0b74Schristos       && !hh->plabel
191375fd0b74Schristos       && eh->plt.refcount > 0)
191475fd0b74Schristos     {
191575fd0b74Schristos       /* Make an entry in the .plt section.  */
1916ede78133Schristos       sec = htab->etab.splt;
191775fd0b74Schristos       eh->plt.offset = sec->size;
191875fd0b74Schristos       sec->size += PLT_ENTRY_SIZE;
191975fd0b74Schristos 
192075fd0b74Schristos       /* We also need to make an entry in the .rela.plt section.  */
1921ede78133Schristos       htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
192275fd0b74Schristos       htab->need_plt_stub = 1;
192375fd0b74Schristos     }
192475fd0b74Schristos 
192575fd0b74Schristos   if (eh->got.refcount > 0)
192675fd0b74Schristos     {
1927ede78133Schristos       unsigned int need;
192875fd0b74Schristos 
1929ede78133Schristos       if (!ensure_undef_dynamic (info, eh))
1930*e992f068Schristos 	return false;
1931ede78133Schristos 
1932ede78133Schristos       sec = htab->etab.sgot;
193375fd0b74Schristos       eh->got.offset = sec->size;
1934ede78133Schristos       need = got_entries_needed (hh->tls_type);
1935ede78133Schristos       sec->size += need;
193675fd0b74Schristos       if (htab->etab.dynamic_sections_created
1937ede78133Schristos 	  && (bfd_link_dll (info)
1938ede78133Schristos 	      || (bfd_link_pic (info) && (hh->tls_type & GOT_NORMAL) != 0)
193975fd0b74Schristos 	      || (eh->dynindx != -1
1940ede78133Schristos 		  && !SYMBOL_REFERENCES_LOCAL (info, eh)))
1941ede78133Schristos 	  && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
194275fd0b74Schristos 	{
1943*e992f068Schristos 	  bool local = SYMBOL_REFERENCES_LOCAL (info, eh);
1944ede78133Schristos 	  htab->etab.srelgot->size
1945ede78133Schristos 	    += got_relocs_needed (hh->tls_type, need, local,
1946ede78133Schristos 				  local && bfd_link_executable (info));
194775fd0b74Schristos 	}
194875fd0b74Schristos     }
194975fd0b74Schristos   else
195075fd0b74Schristos     eh->got.offset = (bfd_vma) -1;
195175fd0b74Schristos 
1952ede78133Schristos   /* If no dynamic sections we can't have dynamic relocs.  */
1953ede78133Schristos   if (!htab->etab.dynamic_sections_created)
1954*e992f068Schristos     eh->dyn_relocs = NULL;
1955ede78133Schristos 
1956ede78133Schristos   /* Discard relocs on undefined syms with non-default visibility.  */
1957ede78133Schristos   else if ((eh->root.type == bfd_link_hash_undefined
1958ede78133Schristos 	    && ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
1959ede78133Schristos 	   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
1960*e992f068Schristos     eh->dyn_relocs = NULL;
1961ede78133Schristos 
1962*e992f068Schristos   if (eh->dyn_relocs == NULL)
1963*e992f068Schristos     return true;
196475fd0b74Schristos 
196575fd0b74Schristos   /* If this is a -Bsymbolic shared link, then we need to discard all
196675fd0b74Schristos      space allocated for dynamic pc-relative relocs against symbols
196775fd0b74Schristos      defined in a regular object.  For the normal shared case, discard
196875fd0b74Schristos      space for relocs that have become local due to symbol visibility
196975fd0b74Schristos      changes.  */
197075fd0b74Schristos   if (bfd_link_pic (info))
197175fd0b74Schristos     {
197275fd0b74Schristos #if RELATIVE_DYNRELOCS
197375fd0b74Schristos       if (SYMBOL_CALLS_LOCAL (info, eh))
197475fd0b74Schristos 	{
1975ede78133Schristos 	  struct elf_dyn_relocs **hdh_pp;
197675fd0b74Schristos 
1977*e992f068Schristos 	  for (hdh_pp = &eh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
197875fd0b74Schristos 	    {
1979ede78133Schristos 	      hdh_p->count -= hdh_p->pc_count;
1980ede78133Schristos 	      hdh_p->pc_count = 0;
198175fd0b74Schristos 	      if (hdh_p->count == 0)
1982ede78133Schristos 		*hdh_pp = hdh_p->next;
198375fd0b74Schristos 	      else
1984ede78133Schristos 		hdh_pp = &hdh_p->next;
198575fd0b74Schristos 	    }
198675fd0b74Schristos 	}
198775fd0b74Schristos #endif
198875fd0b74Schristos 
1989*e992f068Schristos       if (eh->dyn_relocs != NULL)
199075fd0b74Schristos 	{
1991ede78133Schristos 	  if (!ensure_undef_dynamic (info, eh))
1992*e992f068Schristos 	    return false;
199375fd0b74Schristos 	}
199475fd0b74Schristos     }
1995ede78133Schristos   else if (ELIMINATE_COPY_RELOCS)
199675fd0b74Schristos     {
199775fd0b74Schristos       /* For the non-shared case, discard space for relocs against
199875fd0b74Schristos 	 symbols which turn out to need copy relocs or are not
199975fd0b74Schristos 	 dynamic.  */
200075fd0b74Schristos 
2001ede78133Schristos       if (eh->dynamic_adjusted
2002ede78133Schristos 	  && !eh->def_regular
2003ede78133Schristos 	  && !ELF_COMMON_DEF_P (eh))
200475fd0b74Schristos 	{
2005ede78133Schristos 	  if (!ensure_undef_dynamic (info, eh))
2006*e992f068Schristos 	    return false;
200775fd0b74Schristos 
2008ede78133Schristos 	  if (eh->dynindx == -1)
2009*e992f068Schristos 	    eh->dyn_relocs = NULL;
2010ede78133Schristos 	}
2011ede78133Schristos       else
2012*e992f068Schristos 	eh->dyn_relocs = NULL;
201375fd0b74Schristos     }
201475fd0b74Schristos 
201575fd0b74Schristos   /* Finally, allocate space.  */
2016*e992f068Schristos   for (hdh_p = eh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->next)
201775fd0b74Schristos     {
201875fd0b74Schristos       asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
201975fd0b74Schristos       sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
202075fd0b74Schristos     }
202175fd0b74Schristos 
2022*e992f068Schristos   return true;
202375fd0b74Schristos }
202475fd0b74Schristos 
202575fd0b74Schristos /* This function is called via elf_link_hash_traverse to force
202675fd0b74Schristos    millicode symbols local so they do not end up as globals in the
202775fd0b74Schristos    dynamic symbol table.  We ought to be able to do this in
202875fd0b74Schristos    adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
202975fd0b74Schristos    for all dynamic symbols.  Arguably, this is a bug in
203075fd0b74Schristos    elf_adjust_dynamic_symbol.  */
203175fd0b74Schristos 
2032*e992f068Schristos static bool
clobber_millicode_symbols(struct elf_link_hash_entry * eh,void * info)203375fd0b74Schristos clobber_millicode_symbols (struct elf_link_hash_entry *eh,
2034*e992f068Schristos 			   void *info)
203575fd0b74Schristos {
203675fd0b74Schristos   if (eh->type == STT_PARISC_MILLI
203775fd0b74Schristos       && !eh->forced_local)
2038*e992f068Schristos     elf32_hppa_hide_symbol ((struct bfd_link_info *) info, eh, true);
2039*e992f068Schristos   return true;
204075fd0b74Schristos }
204175fd0b74Schristos 
204275fd0b74Schristos /* Set the sizes of the dynamic sections.  */
204375fd0b74Schristos 
2044*e992f068Schristos static bool
elf32_hppa_size_dynamic_sections(bfd * output_bfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)204575fd0b74Schristos elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
204675fd0b74Schristos 				  struct bfd_link_info *info)
204775fd0b74Schristos {
204875fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
204975fd0b74Schristos   bfd *dynobj;
205075fd0b74Schristos   bfd *ibfd;
205175fd0b74Schristos   asection *sec;
2052*e992f068Schristos   bool relocs;
205375fd0b74Schristos 
205475fd0b74Schristos   htab = hppa_link_hash_table (info);
205575fd0b74Schristos   if (htab == NULL)
2056*e992f068Schristos     return false;
205775fd0b74Schristos 
205875fd0b74Schristos   dynobj = htab->etab.dynobj;
205975fd0b74Schristos   if (dynobj == NULL)
206075fd0b74Schristos     abort ();
206175fd0b74Schristos 
206275fd0b74Schristos   if (htab->etab.dynamic_sections_created)
206375fd0b74Schristos     {
206475fd0b74Schristos       /* Set the contents of the .interp section to the interpreter.  */
206575fd0b74Schristos       if (bfd_link_executable (info) && !info->nointerp)
206675fd0b74Schristos 	{
206775fd0b74Schristos 	  sec = bfd_get_linker_section (dynobj, ".interp");
206875fd0b74Schristos 	  if (sec == NULL)
206975fd0b74Schristos 	    abort ();
207075fd0b74Schristos 	  sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
207175fd0b74Schristos 	  sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
207275fd0b74Schristos 	}
207375fd0b74Schristos 
207475fd0b74Schristos       /* Force millicode symbols local.  */
207575fd0b74Schristos       elf_link_hash_traverse (&htab->etab,
207675fd0b74Schristos 			      clobber_millicode_symbols,
207775fd0b74Schristos 			      info);
207875fd0b74Schristos     }
207975fd0b74Schristos 
208075fd0b74Schristos   /* Set up .got and .plt offsets for local syms, and space for local
208175fd0b74Schristos      dynamic relocs.  */
208275fd0b74Schristos   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
208375fd0b74Schristos     {
208475fd0b74Schristos       bfd_signed_vma *local_got;
208575fd0b74Schristos       bfd_signed_vma *end_local_got;
208675fd0b74Schristos       bfd_signed_vma *local_plt;
208775fd0b74Schristos       bfd_signed_vma *end_local_plt;
208875fd0b74Schristos       bfd_size_type locsymcount;
208975fd0b74Schristos       Elf_Internal_Shdr *symtab_hdr;
209075fd0b74Schristos       asection *srel;
209175fd0b74Schristos       char *local_tls_type;
209275fd0b74Schristos 
209375fd0b74Schristos       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
209475fd0b74Schristos 	continue;
209575fd0b74Schristos 
209675fd0b74Schristos       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
209775fd0b74Schristos 	{
2098ede78133Schristos 	  struct elf_dyn_relocs *hdh_p;
209975fd0b74Schristos 
2100ede78133Schristos 	  for (hdh_p = ((struct elf_dyn_relocs *)
210175fd0b74Schristos 		    elf_section_data (sec)->local_dynrel);
210275fd0b74Schristos 	       hdh_p != NULL;
2103ede78133Schristos 	       hdh_p = hdh_p->next)
210475fd0b74Schristos 	    {
210575fd0b74Schristos 	      if (!bfd_is_abs_section (hdh_p->sec)
210675fd0b74Schristos 		  && bfd_is_abs_section (hdh_p->sec->output_section))
210775fd0b74Schristos 		{
210875fd0b74Schristos 		  /* Input section has been discarded, either because
210975fd0b74Schristos 		     it is a copy of a linkonce section or due to
211075fd0b74Schristos 		     linker script /DISCARD/, so we'll be discarding
211175fd0b74Schristos 		     the relocs too.  */
211275fd0b74Schristos 		}
211375fd0b74Schristos 	      else if (hdh_p->count != 0)
211475fd0b74Schristos 		{
211575fd0b74Schristos 		  srel = elf_section_data (hdh_p->sec)->sreloc;
211675fd0b74Schristos 		  srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
211775fd0b74Schristos 		  if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
211875fd0b74Schristos 		    info->flags |= DF_TEXTREL;
211975fd0b74Schristos 		}
212075fd0b74Schristos 	    }
212175fd0b74Schristos 	}
212275fd0b74Schristos 
212375fd0b74Schristos       local_got = elf_local_got_refcounts (ibfd);
212475fd0b74Schristos       if (!local_got)
212575fd0b74Schristos 	continue;
212675fd0b74Schristos 
212775fd0b74Schristos       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
212875fd0b74Schristos       locsymcount = symtab_hdr->sh_info;
212975fd0b74Schristos       end_local_got = local_got + locsymcount;
213075fd0b74Schristos       local_tls_type = hppa_elf_local_got_tls_type (ibfd);
2131ede78133Schristos       sec = htab->etab.sgot;
2132ede78133Schristos       srel = htab->etab.srelgot;
213375fd0b74Schristos       for (; local_got < end_local_got; ++local_got)
213475fd0b74Schristos 	{
213575fd0b74Schristos 	  if (*local_got > 0)
213675fd0b74Schristos 	    {
2137ede78133Schristos 	      unsigned int need;
2138ede78133Schristos 
213975fd0b74Schristos 	      *local_got = sec->size;
2140ede78133Schristos 	      need = got_entries_needed (*local_tls_type);
2141ede78133Schristos 	      sec->size += need;
2142ede78133Schristos 	      if (bfd_link_dll (info)
2143ede78133Schristos 		  || (bfd_link_pic (info)
2144ede78133Schristos 		      && (*local_tls_type & GOT_NORMAL) != 0))
2145ede78133Schristos 		htab->etab.srelgot->size
2146*e992f068Schristos 		  += got_relocs_needed (*local_tls_type, need, true,
2147ede78133Schristos 					bfd_link_executable (info));
214875fd0b74Schristos 	    }
214975fd0b74Schristos 	  else
215075fd0b74Schristos 	    *local_got = (bfd_vma) -1;
215175fd0b74Schristos 
215275fd0b74Schristos 	  ++local_tls_type;
215375fd0b74Schristos 	}
215475fd0b74Schristos 
215575fd0b74Schristos       local_plt = end_local_got;
215675fd0b74Schristos       end_local_plt = local_plt + locsymcount;
215775fd0b74Schristos       if (! htab->etab.dynamic_sections_created)
215875fd0b74Schristos 	{
215975fd0b74Schristos 	  /* Won't be used, but be safe.  */
216075fd0b74Schristos 	  for (; local_plt < end_local_plt; ++local_plt)
216175fd0b74Schristos 	    *local_plt = (bfd_vma) -1;
216275fd0b74Schristos 	}
216375fd0b74Schristos       else
216475fd0b74Schristos 	{
2165ede78133Schristos 	  sec = htab->etab.splt;
2166ede78133Schristos 	  srel = htab->etab.srelplt;
216775fd0b74Schristos 	  for (; local_plt < end_local_plt; ++local_plt)
216875fd0b74Schristos 	    {
216975fd0b74Schristos 	      if (*local_plt > 0)
217075fd0b74Schristos 		{
217175fd0b74Schristos 		  *local_plt = sec->size;
217275fd0b74Schristos 		  sec->size += PLT_ENTRY_SIZE;
217375fd0b74Schristos 		  if (bfd_link_pic (info))
217475fd0b74Schristos 		    srel->size += sizeof (Elf32_External_Rela);
217575fd0b74Schristos 		}
217675fd0b74Schristos 	      else
217775fd0b74Schristos 		*local_plt = (bfd_vma) -1;
217875fd0b74Schristos 	    }
217975fd0b74Schristos 	}
218075fd0b74Schristos     }
218175fd0b74Schristos 
218275fd0b74Schristos   if (htab->tls_ldm_got.refcount > 0)
218375fd0b74Schristos     {
218475fd0b74Schristos       /* Allocate 2 got entries and 1 dynamic reloc for
218575fd0b74Schristos 	 R_PARISC_TLS_DTPMOD32 relocs.  */
2186ede78133Schristos       htab->tls_ldm_got.offset = htab->etab.sgot->size;
2187ede78133Schristos       htab->etab.sgot->size += (GOT_ENTRY_SIZE * 2);
2188ede78133Schristos       htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
218975fd0b74Schristos     }
219075fd0b74Schristos   else
219175fd0b74Schristos     htab->tls_ldm_got.offset = -1;
219275fd0b74Schristos 
219375fd0b74Schristos   /* Do all the .plt entries without relocs first.  The dynamic linker
219475fd0b74Schristos      uses the last .plt reloc to find the end of the .plt (and hence
219575fd0b74Schristos      the start of the .got) for lazy linking.  */
219675fd0b74Schristos   elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
219775fd0b74Schristos 
219875fd0b74Schristos   /* Allocate global sym .plt and .got entries, and space for global
219975fd0b74Schristos      sym dynamic relocs.  */
220075fd0b74Schristos   elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
220175fd0b74Schristos 
220275fd0b74Schristos   /* The check_relocs and adjust_dynamic_symbol entry points have
220375fd0b74Schristos      determined the sizes of the various dynamic sections.  Allocate
220475fd0b74Schristos      memory for them.  */
2205*e992f068Schristos   relocs = false;
220675fd0b74Schristos   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
220775fd0b74Schristos     {
220875fd0b74Schristos       if ((sec->flags & SEC_LINKER_CREATED) == 0)
220975fd0b74Schristos 	continue;
221075fd0b74Schristos 
2211ede78133Schristos       if (sec == htab->etab.splt)
221275fd0b74Schristos 	{
221375fd0b74Schristos 	  if (htab->need_plt_stub)
221475fd0b74Schristos 	    {
221575fd0b74Schristos 	      /* Make space for the plt stub at the end of the .plt
221675fd0b74Schristos 		 section.  We want this stub right at the end, up
221775fd0b74Schristos 		 against the .got section.  */
2218012573ebSchristos 	      int gotalign = bfd_section_alignment (htab->etab.sgot);
2219012573ebSchristos 	      int pltalign = bfd_section_alignment (sec);
2220012573ebSchristos 	      int align = gotalign > 3 ? gotalign : 3;
222175fd0b74Schristos 	      bfd_size_type mask;
222275fd0b74Schristos 
2223012573ebSchristos 	      if (align > pltalign)
2224012573ebSchristos 		bfd_set_section_alignment (sec, align);
222575fd0b74Schristos 	      mask = ((bfd_size_type) 1 << gotalign) - 1;
222675fd0b74Schristos 	      sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
222775fd0b74Schristos 	    }
222875fd0b74Schristos 	}
2229ede78133Schristos       else if (sec == htab->etab.sgot
2230ede78133Schristos 	       || sec == htab->etab.sdynbss
2231ede78133Schristos 	       || sec == htab->etab.sdynrelro)
223275fd0b74Schristos 	;
2233*e992f068Schristos       else if (startswith (bfd_section_name (sec), ".rela"))
223475fd0b74Schristos 	{
223575fd0b74Schristos 	  if (sec->size != 0)
223675fd0b74Schristos 	    {
223775fd0b74Schristos 	      /* Remember whether there are any reloc sections other
223875fd0b74Schristos 		 than .rela.plt.  */
2239ede78133Schristos 	      if (sec != htab->etab.srelplt)
2240*e992f068Schristos 		relocs = true;
224175fd0b74Schristos 
224275fd0b74Schristos 	      /* We use the reloc_count field as a counter if we need
224375fd0b74Schristos 		 to copy relocs into the output file.  */
224475fd0b74Schristos 	      sec->reloc_count = 0;
224575fd0b74Schristos 	    }
224675fd0b74Schristos 	}
224775fd0b74Schristos       else
224875fd0b74Schristos 	{
224975fd0b74Schristos 	  /* It's not one of our sections, so don't allocate space.  */
225075fd0b74Schristos 	  continue;
225175fd0b74Schristos 	}
225275fd0b74Schristos 
225375fd0b74Schristos       if (sec->size == 0)
225475fd0b74Schristos 	{
225575fd0b74Schristos 	  /* If we don't need this section, strip it from the
225675fd0b74Schristos 	     output file.  This is mostly to handle .rela.bss and
225775fd0b74Schristos 	     .rela.plt.  We must create both sections in
225875fd0b74Schristos 	     create_dynamic_sections, because they must be created
225975fd0b74Schristos 	     before the linker maps input sections to output
226075fd0b74Schristos 	     sections.  The linker does that before
226175fd0b74Schristos 	     adjust_dynamic_symbol is called, and it is that
226275fd0b74Schristos 	     function which decides whether anything needs to go
226375fd0b74Schristos 	     into these sections.  */
226475fd0b74Schristos 	  sec->flags |= SEC_EXCLUDE;
226575fd0b74Schristos 	  continue;
226675fd0b74Schristos 	}
226775fd0b74Schristos 
226875fd0b74Schristos       if ((sec->flags & SEC_HAS_CONTENTS) == 0)
226975fd0b74Schristos 	continue;
227075fd0b74Schristos 
227175fd0b74Schristos       /* Allocate memory for the section contents.  Zero it, because
227275fd0b74Schristos 	 we may not fill in all the reloc sections.  */
227375fd0b74Schristos       sec->contents = bfd_zalloc (dynobj, sec->size);
227475fd0b74Schristos       if (sec->contents == NULL)
2275*e992f068Schristos 	return false;
227675fd0b74Schristos     }
227775fd0b74Schristos 
2278*e992f068Schristos   return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
227975fd0b74Schristos }
228075fd0b74Schristos 
228175fd0b74Schristos /* External entry points for sizing and building linker stubs.  */
228275fd0b74Schristos 
228375fd0b74Schristos /* Set up various things so that we can make a list of input sections
228475fd0b74Schristos    for each output section included in the link.  Returns -1 on error,
228575fd0b74Schristos    0 when no stubs will be needed, and 1 on success.  */
228675fd0b74Schristos 
228775fd0b74Schristos int
elf32_hppa_setup_section_lists(bfd * output_bfd,struct bfd_link_info * info)228875fd0b74Schristos elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
228975fd0b74Schristos {
229075fd0b74Schristos   bfd *input_bfd;
229175fd0b74Schristos   unsigned int bfd_count;
229275fd0b74Schristos   unsigned int top_id, top_index;
229375fd0b74Schristos   asection *section;
229475fd0b74Schristos   asection **input_list, **list;
2295*e992f068Schristos   size_t amt;
229675fd0b74Schristos   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
229775fd0b74Schristos 
229875fd0b74Schristos   if (htab == NULL)
229975fd0b74Schristos     return -1;
230075fd0b74Schristos 
230175fd0b74Schristos   /* Count the number of input BFDs and find the top input section id.  */
230275fd0b74Schristos   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
230375fd0b74Schristos        input_bfd != NULL;
230475fd0b74Schristos        input_bfd = input_bfd->link.next)
230575fd0b74Schristos     {
230675fd0b74Schristos       bfd_count += 1;
230775fd0b74Schristos       for (section = input_bfd->sections;
230875fd0b74Schristos 	   section != NULL;
230975fd0b74Schristos 	   section = section->next)
231075fd0b74Schristos 	{
231175fd0b74Schristos 	  if (top_id < section->id)
231275fd0b74Schristos 	    top_id = section->id;
231375fd0b74Schristos 	}
231475fd0b74Schristos     }
231575fd0b74Schristos   htab->bfd_count = bfd_count;
231675fd0b74Schristos 
231775fd0b74Schristos   amt = sizeof (struct map_stub) * (top_id + 1);
231875fd0b74Schristos   htab->stub_group = bfd_zmalloc (amt);
231975fd0b74Schristos   if (htab->stub_group == NULL)
232075fd0b74Schristos     return -1;
232175fd0b74Schristos 
232275fd0b74Schristos   /* We can't use output_bfd->section_count here to find the top output
232375fd0b74Schristos      section index as some sections may have been removed, and
232475fd0b74Schristos      strip_excluded_output_sections doesn't renumber the indices.  */
232575fd0b74Schristos   for (section = output_bfd->sections, top_index = 0;
232675fd0b74Schristos        section != NULL;
232775fd0b74Schristos        section = section->next)
232875fd0b74Schristos     {
232975fd0b74Schristos       if (top_index < section->index)
233075fd0b74Schristos 	top_index = section->index;
233175fd0b74Schristos     }
233275fd0b74Schristos 
233375fd0b74Schristos   htab->top_index = top_index;
233475fd0b74Schristos   amt = sizeof (asection *) * (top_index + 1);
233575fd0b74Schristos   input_list = bfd_malloc (amt);
233675fd0b74Schristos   htab->input_list = input_list;
233775fd0b74Schristos   if (input_list == NULL)
233875fd0b74Schristos     return -1;
233975fd0b74Schristos 
234075fd0b74Schristos   /* For sections we aren't interested in, mark their entries with a
234175fd0b74Schristos      value we can check later.  */
234275fd0b74Schristos   list = input_list + top_index;
234375fd0b74Schristos   do
234475fd0b74Schristos     *list = bfd_abs_section_ptr;
234575fd0b74Schristos   while (list-- != input_list);
234675fd0b74Schristos 
234775fd0b74Schristos   for (section = output_bfd->sections;
234875fd0b74Schristos        section != NULL;
234975fd0b74Schristos        section = section->next)
235075fd0b74Schristos     {
235175fd0b74Schristos       if ((section->flags & SEC_CODE) != 0)
235275fd0b74Schristos 	input_list[section->index] = NULL;
235375fd0b74Schristos     }
235475fd0b74Schristos 
235575fd0b74Schristos   return 1;
235675fd0b74Schristos }
235775fd0b74Schristos 
235875fd0b74Schristos /* The linker repeatedly calls this function for each input section,
235975fd0b74Schristos    in the order that input sections are linked into output sections.
236075fd0b74Schristos    Build lists of input sections to determine groupings between which
236175fd0b74Schristos    we may insert linker stubs.  */
236275fd0b74Schristos 
236375fd0b74Schristos void
elf32_hppa_next_input_section(struct bfd_link_info * info,asection * isec)236475fd0b74Schristos elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
236575fd0b74Schristos {
236675fd0b74Schristos   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
236775fd0b74Schristos 
236875fd0b74Schristos   if (htab == NULL)
236975fd0b74Schristos     return;
237075fd0b74Schristos 
237175fd0b74Schristos   if (isec->output_section->index <= htab->top_index)
237275fd0b74Schristos     {
237375fd0b74Schristos       asection **list = htab->input_list + isec->output_section->index;
237475fd0b74Schristos       if (*list != bfd_abs_section_ptr)
237575fd0b74Schristos 	{
237675fd0b74Schristos 	  /* Steal the link_sec pointer for our list.  */
237775fd0b74Schristos #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
237875fd0b74Schristos 	  /* This happens to make the list in reverse order,
237975fd0b74Schristos 	     which is what we want.  */
238075fd0b74Schristos 	  PREV_SEC (isec) = *list;
238175fd0b74Schristos 	  *list = isec;
238275fd0b74Schristos 	}
238375fd0b74Schristos     }
238475fd0b74Schristos }
238575fd0b74Schristos 
238675fd0b74Schristos /* See whether we can group stub sections together.  Grouping stub
238775fd0b74Schristos    sections may result in fewer stubs.  More importantly, we need to
238875fd0b74Schristos    put all .init* and .fini* stubs at the beginning of the .init or
238975fd0b74Schristos    .fini output sections respectively, because glibc splits the
239075fd0b74Schristos    _init and _fini functions into multiple parts.  Putting a stub in
239175fd0b74Schristos    the middle of a function is not a good idea.  */
239275fd0b74Schristos 
239375fd0b74Schristos static void
group_sections(struct elf32_hppa_link_hash_table * htab,bfd_size_type stub_group_size,bool stubs_always_before_branch)239475fd0b74Schristos group_sections (struct elf32_hppa_link_hash_table *htab,
239575fd0b74Schristos 		bfd_size_type stub_group_size,
2396*e992f068Schristos 		bool stubs_always_before_branch)
239775fd0b74Schristos {
239875fd0b74Schristos   asection **list = htab->input_list + htab->top_index;
239975fd0b74Schristos   do
240075fd0b74Schristos     {
240175fd0b74Schristos       asection *tail = *list;
240275fd0b74Schristos       if (tail == bfd_abs_section_ptr)
240375fd0b74Schristos 	continue;
240475fd0b74Schristos       while (tail != NULL)
240575fd0b74Schristos 	{
240675fd0b74Schristos 	  asection *curr;
240775fd0b74Schristos 	  asection *prev;
240875fd0b74Schristos 	  bfd_size_type total;
2409*e992f068Schristos 	  bool big_sec;
241075fd0b74Schristos 
241175fd0b74Schristos 	  curr = tail;
241275fd0b74Schristos 	  total = tail->size;
241375fd0b74Schristos 	  big_sec = total >= stub_group_size;
241475fd0b74Schristos 
241575fd0b74Schristos 	  while ((prev = PREV_SEC (curr)) != NULL
241675fd0b74Schristos 		 && ((total += curr->output_offset - prev->output_offset)
241775fd0b74Schristos 		     < stub_group_size))
241875fd0b74Schristos 	    curr = prev;
241975fd0b74Schristos 
242075fd0b74Schristos 	  /* OK, the size from the start of CURR to the end is less
242175fd0b74Schristos 	     than 240000 bytes and thus can be handled by one stub
242275fd0b74Schristos 	     section.  (or the tail section is itself larger than
242375fd0b74Schristos 	     240000 bytes, in which case we may be toast.)
242475fd0b74Schristos 	     We should really be keeping track of the total size of
242575fd0b74Schristos 	     stubs added here, as stubs contribute to the final output
242675fd0b74Schristos 	     section size.  That's a little tricky, and this way will
242775fd0b74Schristos 	     only break if stubs added total more than 22144 bytes, or
242875fd0b74Schristos 	     2768 long branch stubs.  It seems unlikely for more than
242975fd0b74Schristos 	     2768 different functions to be called, especially from
243075fd0b74Schristos 	     code only 240000 bytes long.  This limit used to be
243175fd0b74Schristos 	     250000, but c++ code tends to generate lots of little
243275fd0b74Schristos 	     functions, and sometimes violated the assumption.  */
243375fd0b74Schristos 	  do
243475fd0b74Schristos 	    {
243575fd0b74Schristos 	      prev = PREV_SEC (tail);
243675fd0b74Schristos 	      /* Set up this stub group.  */
243775fd0b74Schristos 	      htab->stub_group[tail->id].link_sec = curr;
243875fd0b74Schristos 	    }
243975fd0b74Schristos 	  while (tail != curr && (tail = prev) != NULL);
244075fd0b74Schristos 
244175fd0b74Schristos 	  /* But wait, there's more!  Input sections up to 240000
244275fd0b74Schristos 	     bytes before the stub section can be handled by it too.
244375fd0b74Schristos 	     Don't do this if we have a really large section after the
244475fd0b74Schristos 	     stubs, as adding more stubs increases the chance that
244575fd0b74Schristos 	     branches may not reach into the stub section.  */
244675fd0b74Schristos 	  if (!stubs_always_before_branch && !big_sec)
244775fd0b74Schristos 	    {
244875fd0b74Schristos 	      total = 0;
244975fd0b74Schristos 	      while (prev != NULL
245075fd0b74Schristos 		     && ((total += tail->output_offset - prev->output_offset)
245175fd0b74Schristos 			 < stub_group_size))
245275fd0b74Schristos 		{
245375fd0b74Schristos 		  tail = prev;
245475fd0b74Schristos 		  prev = PREV_SEC (tail);
245575fd0b74Schristos 		  htab->stub_group[tail->id].link_sec = curr;
245675fd0b74Schristos 		}
245775fd0b74Schristos 	    }
245875fd0b74Schristos 	  tail = prev;
245975fd0b74Schristos 	}
246075fd0b74Schristos     }
246175fd0b74Schristos   while (list-- != htab->input_list);
246275fd0b74Schristos   free (htab->input_list);
246375fd0b74Schristos #undef PREV_SEC
246475fd0b74Schristos }
246575fd0b74Schristos 
246675fd0b74Schristos /* Read in all local syms for all input bfds, and create hash entries
246775fd0b74Schristos    for export stubs if we are building a multi-subspace shared lib.
246875fd0b74Schristos    Returns -1 on error, 1 if export stubs created, 0 otherwise.  */
246975fd0b74Schristos 
247075fd0b74Schristos static int
get_local_syms(bfd * output_bfd,bfd * input_bfd,struct bfd_link_info * info)247175fd0b74Schristos get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
247275fd0b74Schristos {
247375fd0b74Schristos   unsigned int bfd_indx;
247475fd0b74Schristos   Elf_Internal_Sym *local_syms, **all_local_syms;
247575fd0b74Schristos   int stub_changed = 0;
247675fd0b74Schristos   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
247775fd0b74Schristos 
247875fd0b74Schristos   if (htab == NULL)
247975fd0b74Schristos     return -1;
248075fd0b74Schristos 
248175fd0b74Schristos   /* We want to read in symbol extension records only once.  To do this
248275fd0b74Schristos      we need to read in the local symbols in parallel and save them for
248375fd0b74Schristos      later use; so hold pointers to the local symbols in an array.  */
2484*e992f068Schristos   size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
248575fd0b74Schristos   all_local_syms = bfd_zmalloc (amt);
248675fd0b74Schristos   htab->all_local_syms = all_local_syms;
248775fd0b74Schristos   if (all_local_syms == NULL)
248875fd0b74Schristos     return -1;
248975fd0b74Schristos 
249075fd0b74Schristos   /* Walk over all the input BFDs, swapping in local symbols.
249175fd0b74Schristos      If we are creating a shared library, create hash entries for the
249275fd0b74Schristos      export stubs.  */
249375fd0b74Schristos   for (bfd_indx = 0;
249475fd0b74Schristos        input_bfd != NULL;
249575fd0b74Schristos        input_bfd = input_bfd->link.next, bfd_indx++)
249675fd0b74Schristos     {
249775fd0b74Schristos       Elf_Internal_Shdr *symtab_hdr;
249875fd0b74Schristos 
249975fd0b74Schristos       /* We'll need the symbol table in a second.  */
250075fd0b74Schristos       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
250175fd0b74Schristos       if (symtab_hdr->sh_info == 0)
250275fd0b74Schristos 	continue;
250375fd0b74Schristos 
250475fd0b74Schristos       /* We need an array of the local symbols attached to the input bfd.  */
250575fd0b74Schristos       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
250675fd0b74Schristos       if (local_syms == NULL)
250775fd0b74Schristos 	{
250875fd0b74Schristos 	  local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
250975fd0b74Schristos 					     symtab_hdr->sh_info, 0,
251075fd0b74Schristos 					     NULL, NULL, NULL);
251175fd0b74Schristos 	  /* Cache them for elf_link_input_bfd.  */
251275fd0b74Schristos 	  symtab_hdr->contents = (unsigned char *) local_syms;
251375fd0b74Schristos 	}
251475fd0b74Schristos       if (local_syms == NULL)
251575fd0b74Schristos 	return -1;
251675fd0b74Schristos 
251775fd0b74Schristos       all_local_syms[bfd_indx] = local_syms;
251875fd0b74Schristos 
251975fd0b74Schristos       if (bfd_link_pic (info) && htab->multi_subspace)
252075fd0b74Schristos 	{
252175fd0b74Schristos 	  struct elf_link_hash_entry **eh_syms;
252275fd0b74Schristos 	  struct elf_link_hash_entry **eh_symend;
252375fd0b74Schristos 	  unsigned int symcount;
252475fd0b74Schristos 
252575fd0b74Schristos 	  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
252675fd0b74Schristos 		      - symtab_hdr->sh_info);
252775fd0b74Schristos 	  eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
252875fd0b74Schristos 	  eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
252975fd0b74Schristos 
253075fd0b74Schristos 	  /* Look through the global syms for functions;  We need to
253175fd0b74Schristos 	     build export stubs for all globally visible functions.  */
253275fd0b74Schristos 	  for (; eh_syms < eh_symend; eh_syms++)
253375fd0b74Schristos 	    {
253475fd0b74Schristos 	      struct elf32_hppa_link_hash_entry *hh;
253575fd0b74Schristos 
253675fd0b74Schristos 	      hh = hppa_elf_hash_entry (*eh_syms);
253775fd0b74Schristos 
253875fd0b74Schristos 	      while (hh->eh.root.type == bfd_link_hash_indirect
253975fd0b74Schristos 		     || hh->eh.root.type == bfd_link_hash_warning)
254075fd0b74Schristos 		   hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
254175fd0b74Schristos 
254275fd0b74Schristos 	      /* At this point in the link, undefined syms have been
254375fd0b74Schristos 		 resolved, so we need to check that the symbol was
254475fd0b74Schristos 		 defined in this BFD.  */
254575fd0b74Schristos 	      if ((hh->eh.root.type == bfd_link_hash_defined
254675fd0b74Schristos 		   || hh->eh.root.type == bfd_link_hash_defweak)
254775fd0b74Schristos 		  && hh->eh.type == STT_FUNC
254875fd0b74Schristos 		  && hh->eh.root.u.def.section->output_section != NULL
254975fd0b74Schristos 		  && (hh->eh.root.u.def.section->output_section->owner
255075fd0b74Schristos 		      == output_bfd)
255175fd0b74Schristos 		  && hh->eh.root.u.def.section->owner == input_bfd
255275fd0b74Schristos 		  && hh->eh.def_regular
255375fd0b74Schristos 		  && !hh->eh.forced_local
255475fd0b74Schristos 		  && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
255575fd0b74Schristos 		{
255675fd0b74Schristos 		  asection *sec;
255775fd0b74Schristos 		  const char *stub_name;
255875fd0b74Schristos 		  struct elf32_hppa_stub_hash_entry *hsh;
255975fd0b74Schristos 
256075fd0b74Schristos 		  sec = hh->eh.root.u.def.section;
256175fd0b74Schristos 		  stub_name = hh_name (hh);
256275fd0b74Schristos 		  hsh = hppa_stub_hash_lookup (&htab->bstab,
256375fd0b74Schristos 						      stub_name,
2564*e992f068Schristos 						      false, false);
256575fd0b74Schristos 		  if (hsh == NULL)
256675fd0b74Schristos 		    {
256775fd0b74Schristos 		      hsh = hppa_add_stub (stub_name, sec, htab);
256875fd0b74Schristos 		      if (!hsh)
256975fd0b74Schristos 			return -1;
257075fd0b74Schristos 
257175fd0b74Schristos 		      hsh->target_value = hh->eh.root.u.def.value;
257275fd0b74Schristos 		      hsh->target_section = hh->eh.root.u.def.section;
257375fd0b74Schristos 		      hsh->stub_type = hppa_stub_export;
257475fd0b74Schristos 		      hsh->hh = hh;
257575fd0b74Schristos 		      stub_changed = 1;
257675fd0b74Schristos 		    }
257775fd0b74Schristos 		  else
257875fd0b74Schristos 		    {
2579ede78133Schristos 		      /* xgettext:c-format */
2580ede78133Schristos 		      _bfd_error_handler (_("%pB: duplicate export stub %s"),
2581ede78133Schristos 					  input_bfd, stub_name);
258275fd0b74Schristos 		    }
258375fd0b74Schristos 		}
258475fd0b74Schristos 	    }
258575fd0b74Schristos 	}
258675fd0b74Schristos     }
258775fd0b74Schristos 
258875fd0b74Schristos   return stub_changed;
258975fd0b74Schristos }
259075fd0b74Schristos 
259175fd0b74Schristos /* Determine and set the size of the stub section for a final link.
259275fd0b74Schristos 
259375fd0b74Schristos    The basic idea here is to examine all the relocations looking for
259475fd0b74Schristos    PC-relative calls to a target that is unreachable with a "bl"
259575fd0b74Schristos    instruction.  */
259675fd0b74Schristos 
2597*e992f068Schristos bool
elf32_hppa_size_stubs(bfd * output_bfd,bfd * stub_bfd,struct bfd_link_info * info,bool multi_subspace,bfd_signed_vma group_size,asection * (* add_stub_section)(const char *,asection *),void (* layout_sections_again)(void))259875fd0b74Schristos elf32_hppa_size_stubs
259975fd0b74Schristos   (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2600*e992f068Schristos    bool multi_subspace, bfd_signed_vma group_size,
260175fd0b74Schristos    asection * (*add_stub_section) (const char *, asection *),
260275fd0b74Schristos    void (*layout_sections_again) (void))
260375fd0b74Schristos {
260475fd0b74Schristos   bfd_size_type stub_group_size;
2605*e992f068Schristos   bool stubs_always_before_branch;
2606*e992f068Schristos   bool stub_changed;
260775fd0b74Schristos   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
260875fd0b74Schristos 
260975fd0b74Schristos   if (htab == NULL)
2610*e992f068Schristos     return false;
261175fd0b74Schristos 
261275fd0b74Schristos   /* Stash our params away.  */
261375fd0b74Schristos   htab->stub_bfd = stub_bfd;
261475fd0b74Schristos   htab->multi_subspace = multi_subspace;
261575fd0b74Schristos   htab->add_stub_section = add_stub_section;
261675fd0b74Schristos   htab->layout_sections_again = layout_sections_again;
261775fd0b74Schristos   stubs_always_before_branch = group_size < 0;
261875fd0b74Schristos   if (group_size < 0)
261975fd0b74Schristos     stub_group_size = -group_size;
262075fd0b74Schristos   else
262175fd0b74Schristos     stub_group_size = group_size;
262275fd0b74Schristos   if (stub_group_size == 1)
262375fd0b74Schristos     {
262475fd0b74Schristos       /* Default values.  */
262575fd0b74Schristos       if (stubs_always_before_branch)
262675fd0b74Schristos 	{
262775fd0b74Schristos 	  stub_group_size = 7680000;
262875fd0b74Schristos 	  if (htab->has_17bit_branch || htab->multi_subspace)
262975fd0b74Schristos 	    stub_group_size = 240000;
263075fd0b74Schristos 	  if (htab->has_12bit_branch)
263175fd0b74Schristos 	    stub_group_size = 7500;
263275fd0b74Schristos 	}
263375fd0b74Schristos       else
263475fd0b74Schristos 	{
263575fd0b74Schristos 	  stub_group_size = 6971392;
263675fd0b74Schristos 	  if (htab->has_17bit_branch || htab->multi_subspace)
263775fd0b74Schristos 	    stub_group_size = 217856;
263875fd0b74Schristos 	  if (htab->has_12bit_branch)
263975fd0b74Schristos 	    stub_group_size = 6808;
264075fd0b74Schristos 	}
264175fd0b74Schristos     }
264275fd0b74Schristos 
264375fd0b74Schristos   group_sections (htab, stub_group_size, stubs_always_before_branch);
264475fd0b74Schristos 
264575fd0b74Schristos   switch (get_local_syms (output_bfd, info->input_bfds, info))
264675fd0b74Schristos     {
264775fd0b74Schristos     default:
264875fd0b74Schristos       if (htab->all_local_syms)
264975fd0b74Schristos 	goto error_ret_free_local;
2650*e992f068Schristos       return false;
265175fd0b74Schristos 
265275fd0b74Schristos     case 0:
2653*e992f068Schristos       stub_changed = false;
265475fd0b74Schristos       break;
265575fd0b74Schristos 
265675fd0b74Schristos     case 1:
2657*e992f068Schristos       stub_changed = true;
265875fd0b74Schristos       break;
265975fd0b74Schristos     }
266075fd0b74Schristos 
266175fd0b74Schristos   while (1)
266275fd0b74Schristos     {
266375fd0b74Schristos       bfd *input_bfd;
266475fd0b74Schristos       unsigned int bfd_indx;
266575fd0b74Schristos       asection *stub_sec;
266675fd0b74Schristos 
266775fd0b74Schristos       for (input_bfd = info->input_bfds, bfd_indx = 0;
266875fd0b74Schristos 	   input_bfd != NULL;
266975fd0b74Schristos 	   input_bfd = input_bfd->link.next, bfd_indx++)
267075fd0b74Schristos 	{
267175fd0b74Schristos 	  Elf_Internal_Shdr *symtab_hdr;
267275fd0b74Schristos 	  asection *section;
267375fd0b74Schristos 	  Elf_Internal_Sym *local_syms;
267475fd0b74Schristos 
267575fd0b74Schristos 	  /* We'll need the symbol table in a second.  */
267675fd0b74Schristos 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
267775fd0b74Schristos 	  if (symtab_hdr->sh_info == 0)
267875fd0b74Schristos 	    continue;
267975fd0b74Schristos 
268075fd0b74Schristos 	  local_syms = htab->all_local_syms[bfd_indx];
268175fd0b74Schristos 
268275fd0b74Schristos 	  /* Walk over each section attached to the input bfd.  */
268375fd0b74Schristos 	  for (section = input_bfd->sections;
268475fd0b74Schristos 	       section != NULL;
268575fd0b74Schristos 	       section = section->next)
268675fd0b74Schristos 	    {
268775fd0b74Schristos 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
268875fd0b74Schristos 
268975fd0b74Schristos 	      /* If there aren't any relocs, then there's nothing more
269075fd0b74Schristos 		 to do.  */
269175fd0b74Schristos 	      if ((section->flags & SEC_RELOC) == 0
2692ede78133Schristos 		  || (section->flags & SEC_ALLOC) == 0
2693ede78133Schristos 		  || (section->flags & SEC_LOAD) == 0
2694ede78133Schristos 		  || (section->flags & SEC_CODE) == 0
269575fd0b74Schristos 		  || section->reloc_count == 0)
269675fd0b74Schristos 		continue;
269775fd0b74Schristos 
269875fd0b74Schristos 	      /* If this section is a link-once section that will be
269975fd0b74Schristos 		 discarded, then don't create any stubs.  */
270075fd0b74Schristos 	      if (section->output_section == NULL
270175fd0b74Schristos 		  || section->output_section->owner != output_bfd)
270275fd0b74Schristos 		continue;
270375fd0b74Schristos 
270475fd0b74Schristos 	      /* Get the relocs.  */
270575fd0b74Schristos 	      internal_relocs
270675fd0b74Schristos 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
270775fd0b74Schristos 					     info->keep_memory);
270875fd0b74Schristos 	      if (internal_relocs == NULL)
270975fd0b74Schristos 		goto error_ret_free_local;
271075fd0b74Schristos 
271175fd0b74Schristos 	      /* Now examine each relocation.  */
271275fd0b74Schristos 	      irela = internal_relocs;
271375fd0b74Schristos 	      irelaend = irela + section->reloc_count;
271475fd0b74Schristos 	      for (; irela < irelaend; irela++)
271575fd0b74Schristos 		{
271675fd0b74Schristos 		  unsigned int r_type, r_indx;
271775fd0b74Schristos 		  enum elf32_hppa_stub_type stub_type;
271875fd0b74Schristos 		  struct elf32_hppa_stub_hash_entry *hsh;
271975fd0b74Schristos 		  asection *sym_sec;
272075fd0b74Schristos 		  bfd_vma sym_value;
272175fd0b74Schristos 		  bfd_vma destination;
272275fd0b74Schristos 		  struct elf32_hppa_link_hash_entry *hh;
272375fd0b74Schristos 		  char *stub_name;
272475fd0b74Schristos 		  const asection *id_sec;
272575fd0b74Schristos 
272675fd0b74Schristos 		  r_type = ELF32_R_TYPE (irela->r_info);
272775fd0b74Schristos 		  r_indx = ELF32_R_SYM (irela->r_info);
272875fd0b74Schristos 
272975fd0b74Schristos 		  if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
273075fd0b74Schristos 		    {
273175fd0b74Schristos 		      bfd_set_error (bfd_error_bad_value);
273275fd0b74Schristos 		    error_ret_free_internal:
273375fd0b74Schristos 		      if (elf_section_data (section)->relocs == NULL)
273475fd0b74Schristos 			free (internal_relocs);
273575fd0b74Schristos 		      goto error_ret_free_local;
273675fd0b74Schristos 		    }
273775fd0b74Schristos 
273875fd0b74Schristos 		  /* Only look for stubs on call instructions.  */
273975fd0b74Schristos 		  if (r_type != (unsigned int) R_PARISC_PCREL12F
274075fd0b74Schristos 		      && r_type != (unsigned int) R_PARISC_PCREL17F
274175fd0b74Schristos 		      && r_type != (unsigned int) R_PARISC_PCREL22F)
274275fd0b74Schristos 		    continue;
274375fd0b74Schristos 
274475fd0b74Schristos 		  /* Now determine the call target, its name, value,
274575fd0b74Schristos 		     section.  */
274675fd0b74Schristos 		  sym_sec = NULL;
274775fd0b74Schristos 		  sym_value = 0;
2748ede78133Schristos 		  destination = -1;
274975fd0b74Schristos 		  hh = NULL;
275075fd0b74Schristos 		  if (r_indx < symtab_hdr->sh_info)
275175fd0b74Schristos 		    {
275275fd0b74Schristos 		      /* It's a local symbol.  */
275375fd0b74Schristos 		      Elf_Internal_Sym *sym;
275475fd0b74Schristos 		      Elf_Internal_Shdr *hdr;
275575fd0b74Schristos 		      unsigned int shndx;
275675fd0b74Schristos 
275775fd0b74Schristos 		      sym = local_syms + r_indx;
275875fd0b74Schristos 		      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
275975fd0b74Schristos 			sym_value = sym->st_value;
276075fd0b74Schristos 		      shndx = sym->st_shndx;
276175fd0b74Schristos 		      if (shndx < elf_numsections (input_bfd))
276275fd0b74Schristos 			{
276375fd0b74Schristos 			  hdr = elf_elfsections (input_bfd)[shndx];
276475fd0b74Schristos 			  sym_sec = hdr->bfd_section;
276575fd0b74Schristos 			  destination = (sym_value + irela->r_addend
276675fd0b74Schristos 					 + sym_sec->output_offset
276775fd0b74Schristos 					 + sym_sec->output_section->vma);
276875fd0b74Schristos 			}
276975fd0b74Schristos 		    }
277075fd0b74Schristos 		  else
277175fd0b74Schristos 		    {
277275fd0b74Schristos 		      /* It's an external symbol.  */
277375fd0b74Schristos 		      int e_indx;
277475fd0b74Schristos 
277575fd0b74Schristos 		      e_indx = r_indx - symtab_hdr->sh_info;
277675fd0b74Schristos 		      hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
277775fd0b74Schristos 
277875fd0b74Schristos 		      while (hh->eh.root.type == bfd_link_hash_indirect
277975fd0b74Schristos 			     || hh->eh.root.type == bfd_link_hash_warning)
278075fd0b74Schristos 			hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
278175fd0b74Schristos 
278275fd0b74Schristos 		      if (hh->eh.root.type == bfd_link_hash_defined
278375fd0b74Schristos 			  || hh->eh.root.type == bfd_link_hash_defweak)
278475fd0b74Schristos 			{
278575fd0b74Schristos 			  sym_sec = hh->eh.root.u.def.section;
278675fd0b74Schristos 			  sym_value = hh->eh.root.u.def.value;
278775fd0b74Schristos 			  if (sym_sec->output_section != NULL)
278875fd0b74Schristos 			    destination = (sym_value + irela->r_addend
278975fd0b74Schristos 					   + sym_sec->output_offset
279075fd0b74Schristos 					   + sym_sec->output_section->vma);
279175fd0b74Schristos 			}
279275fd0b74Schristos 		      else if (hh->eh.root.type == bfd_link_hash_undefweak)
279375fd0b74Schristos 			{
279475fd0b74Schristos 			  if (! bfd_link_pic (info))
279575fd0b74Schristos 			    continue;
279675fd0b74Schristos 			}
279775fd0b74Schristos 		      else if (hh->eh.root.type == bfd_link_hash_undefined)
279875fd0b74Schristos 			{
279975fd0b74Schristos 			  if (! (info->unresolved_syms_in_objects == RM_IGNORE
280075fd0b74Schristos 				 && (ELF_ST_VISIBILITY (hh->eh.other)
280175fd0b74Schristos 				     == STV_DEFAULT)
280275fd0b74Schristos 				 && hh->eh.type != STT_PARISC_MILLI))
280375fd0b74Schristos 			    continue;
280475fd0b74Schristos 			}
280575fd0b74Schristos 		      else
280675fd0b74Schristos 			{
280775fd0b74Schristos 			  bfd_set_error (bfd_error_bad_value);
280875fd0b74Schristos 			  goto error_ret_free_internal;
280975fd0b74Schristos 			}
281075fd0b74Schristos 		    }
281175fd0b74Schristos 
281275fd0b74Schristos 		  /* Determine what (if any) linker stub is needed.  */
281375fd0b74Schristos 		  stub_type = hppa_type_of_stub (section, irela, hh,
281475fd0b74Schristos 						 destination, info);
281575fd0b74Schristos 		  if (stub_type == hppa_stub_none)
281675fd0b74Schristos 		    continue;
281775fd0b74Schristos 
281875fd0b74Schristos 		  /* Support for grouping stub sections.  */
281975fd0b74Schristos 		  id_sec = htab->stub_group[section->id].link_sec;
282075fd0b74Schristos 
282175fd0b74Schristos 		  /* Get the name of this stub.  */
282275fd0b74Schristos 		  stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
282375fd0b74Schristos 		  if (!stub_name)
282475fd0b74Schristos 		    goto error_ret_free_internal;
282575fd0b74Schristos 
282675fd0b74Schristos 		  hsh = hppa_stub_hash_lookup (&htab->bstab,
282775fd0b74Schristos 						      stub_name,
2828*e992f068Schristos 						      false, false);
282975fd0b74Schristos 		  if (hsh != NULL)
283075fd0b74Schristos 		    {
283175fd0b74Schristos 		      /* The proper stub has already been created.  */
283275fd0b74Schristos 		      free (stub_name);
283375fd0b74Schristos 		      continue;
283475fd0b74Schristos 		    }
283575fd0b74Schristos 
283675fd0b74Schristos 		  hsh = hppa_add_stub (stub_name, section, htab);
283775fd0b74Schristos 		  if (hsh == NULL)
283875fd0b74Schristos 		    {
283975fd0b74Schristos 		      free (stub_name);
284075fd0b74Schristos 		      goto error_ret_free_internal;
284175fd0b74Schristos 		    }
284275fd0b74Schristos 
284375fd0b74Schristos 		  hsh->target_value = sym_value;
284475fd0b74Schristos 		  hsh->target_section = sym_sec;
284575fd0b74Schristos 		  hsh->stub_type = stub_type;
284675fd0b74Schristos 		  if (bfd_link_pic (info))
284775fd0b74Schristos 		    {
284875fd0b74Schristos 		      if (stub_type == hppa_stub_import)
284975fd0b74Schristos 			hsh->stub_type = hppa_stub_import_shared;
285075fd0b74Schristos 		      else if (stub_type == hppa_stub_long_branch)
285175fd0b74Schristos 			hsh->stub_type = hppa_stub_long_branch_shared;
285275fd0b74Schristos 		    }
285375fd0b74Schristos 		  hsh->hh = hh;
2854*e992f068Schristos 		  stub_changed = true;
285575fd0b74Schristos 		}
285675fd0b74Schristos 
285775fd0b74Schristos 	      /* We're done with the internal relocs, free them.  */
285875fd0b74Schristos 	      if (elf_section_data (section)->relocs == NULL)
285975fd0b74Schristos 		free (internal_relocs);
286075fd0b74Schristos 	    }
286175fd0b74Schristos 	}
286275fd0b74Schristos 
286375fd0b74Schristos       if (!stub_changed)
286475fd0b74Schristos 	break;
286575fd0b74Schristos 
286675fd0b74Schristos       /* OK, we've added some stubs.  Find out the new size of the
286775fd0b74Schristos 	 stub sections.  */
286875fd0b74Schristos       for (stub_sec = htab->stub_bfd->sections;
286975fd0b74Schristos 	   stub_sec != NULL;
287075fd0b74Schristos 	   stub_sec = stub_sec->next)
287175fd0b74Schristos 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
287275fd0b74Schristos 	  stub_sec->size = 0;
287375fd0b74Schristos 
287475fd0b74Schristos       bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
287575fd0b74Schristos 
287675fd0b74Schristos       /* Ask the linker to do its stuff.  */
287775fd0b74Schristos       (*htab->layout_sections_again) ();
2878*e992f068Schristos       stub_changed = false;
287975fd0b74Schristos     }
288075fd0b74Schristos 
288175fd0b74Schristos   free (htab->all_local_syms);
2882*e992f068Schristos   return true;
288375fd0b74Schristos 
288475fd0b74Schristos  error_ret_free_local:
288575fd0b74Schristos   free (htab->all_local_syms);
2886*e992f068Schristos   return false;
288775fd0b74Schristos }
288875fd0b74Schristos 
288975fd0b74Schristos /* For a final link, this function is called after we have sized the
289075fd0b74Schristos    stubs to provide a value for __gp.  */
289175fd0b74Schristos 
2892*e992f068Schristos bool
elf32_hppa_set_gp(bfd * abfd,struct bfd_link_info * info)289375fd0b74Schristos elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
289475fd0b74Schristos {
289575fd0b74Schristos   struct bfd_link_hash_entry *h;
289675fd0b74Schristos   asection *sec = NULL;
289775fd0b74Schristos   bfd_vma gp_val = 0;
289875fd0b74Schristos 
2899*e992f068Schristos   h = bfd_link_hash_lookup (info->hash, "$global$", false, false, false);
290075fd0b74Schristos 
290175fd0b74Schristos   if (h != NULL
290275fd0b74Schristos       && (h->type == bfd_link_hash_defined
290375fd0b74Schristos 	  || h->type == bfd_link_hash_defweak))
290475fd0b74Schristos     {
290575fd0b74Schristos       gp_val = h->u.def.value;
290675fd0b74Schristos       sec = h->u.def.section;
290775fd0b74Schristos     }
290875fd0b74Schristos   else
290975fd0b74Schristos     {
291075fd0b74Schristos       asection *splt = bfd_get_section_by_name (abfd, ".plt");
291175fd0b74Schristos       asection *sgot = bfd_get_section_by_name (abfd, ".got");
291275fd0b74Schristos 
291375fd0b74Schristos       /* Choose to point our LTP at, in this order, one of .plt, .got,
291475fd0b74Schristos 	 or .data, if these sections exist.  In the case of choosing
291575fd0b74Schristos 	 .plt try to make the LTP ideal for addressing anywhere in the
291675fd0b74Schristos 	 .plt or .got with a 14 bit signed offset.  Typically, the end
291775fd0b74Schristos 	 of the .plt is the start of the .got, so choose .plt + 0x2000
291875fd0b74Schristos 	 if either the .plt or .got is larger than 0x2000.  If both
291975fd0b74Schristos 	 the .plt and .got are smaller than 0x2000, choose the end of
292075fd0b74Schristos 	 the .plt section.  */
292175fd0b74Schristos       sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
292275fd0b74Schristos 	  ? NULL : splt;
292375fd0b74Schristos       if (sec != NULL)
292475fd0b74Schristos 	{
292575fd0b74Schristos 	  gp_val = sec->size;
292675fd0b74Schristos 	  if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
292775fd0b74Schristos 	    {
292875fd0b74Schristos 	      gp_val = 0x2000;
292975fd0b74Schristos 	    }
293075fd0b74Schristos 	}
293175fd0b74Schristos       else
293275fd0b74Schristos 	{
293375fd0b74Schristos 	  sec = sgot;
293475fd0b74Schristos 	  if (sec != NULL)
293575fd0b74Schristos 	    {
293675fd0b74Schristos 	      if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
293775fd0b74Schristos 		{
293875fd0b74Schristos 		  /* We know we don't have a .plt.  If .got is large,
293975fd0b74Schristos 		     offset our LTP.  */
294075fd0b74Schristos 		  if (sec->size > 0x2000)
294175fd0b74Schristos 		    gp_val = 0x2000;
294275fd0b74Schristos 		}
294375fd0b74Schristos 	    }
294475fd0b74Schristos 	  else
294575fd0b74Schristos 	    {
294675fd0b74Schristos 	      /* No .plt or .got.  Who cares what the LTP is?  */
294775fd0b74Schristos 	      sec = bfd_get_section_by_name (abfd, ".data");
294875fd0b74Schristos 	    }
294975fd0b74Schristos 	}
295075fd0b74Schristos 
295175fd0b74Schristos       if (h != NULL)
295275fd0b74Schristos 	{
295375fd0b74Schristos 	  h->type = bfd_link_hash_defined;
295475fd0b74Schristos 	  h->u.def.value = gp_val;
295575fd0b74Schristos 	  if (sec != NULL)
295675fd0b74Schristos 	    h->u.def.section = sec;
295775fd0b74Schristos 	  else
295875fd0b74Schristos 	    h->u.def.section = bfd_abs_section_ptr;
295975fd0b74Schristos 	}
296075fd0b74Schristos     }
296175fd0b74Schristos 
2962ede78133Schristos   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2963ede78133Schristos     {
296475fd0b74Schristos       if (sec != NULL && sec->output_section != NULL)
296575fd0b74Schristos 	gp_val += sec->output_section->vma + sec->output_offset;
296675fd0b74Schristos 
296775fd0b74Schristos       elf_gp (abfd) = gp_val;
2968ede78133Schristos     }
2969*e992f068Schristos   return true;
297075fd0b74Schristos }
297175fd0b74Schristos 
297275fd0b74Schristos /* Build all the stubs associated with the current output file.  The
297375fd0b74Schristos    stubs are kept in a hash table attached to the main linker hash
297475fd0b74Schristos    table.  We also set up the .plt entries for statically linked PIC
297575fd0b74Schristos    functions here.  This function is called via hppaelf_finish in the
297675fd0b74Schristos    linker.  */
297775fd0b74Schristos 
2978*e992f068Schristos bool
elf32_hppa_build_stubs(struct bfd_link_info * info)297975fd0b74Schristos elf32_hppa_build_stubs (struct bfd_link_info *info)
298075fd0b74Schristos {
298175fd0b74Schristos   asection *stub_sec;
298275fd0b74Schristos   struct bfd_hash_table *table;
298375fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
298475fd0b74Schristos 
298575fd0b74Schristos   htab = hppa_link_hash_table (info);
298675fd0b74Schristos   if (htab == NULL)
2987*e992f068Schristos     return false;
298875fd0b74Schristos 
298975fd0b74Schristos   for (stub_sec = htab->stub_bfd->sections;
299075fd0b74Schristos        stub_sec != NULL;
299175fd0b74Schristos        stub_sec = stub_sec->next)
299275fd0b74Schristos     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
299375fd0b74Schristos 	&& stub_sec->size != 0)
299475fd0b74Schristos       {
299575fd0b74Schristos 	/* Allocate memory to hold the linker stubs.  */
299675fd0b74Schristos 	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
299775fd0b74Schristos 	if (stub_sec->contents == NULL)
2998*e992f068Schristos 	  return false;
299975fd0b74Schristos 	stub_sec->size = 0;
300075fd0b74Schristos       }
300175fd0b74Schristos 
300275fd0b74Schristos   /* Build the stubs as directed by the stub hash table.  */
300375fd0b74Schristos   table = &htab->bstab;
300475fd0b74Schristos   bfd_hash_traverse (table, hppa_build_one_stub, info);
300575fd0b74Schristos 
3006*e992f068Schristos   return true;
300775fd0b74Schristos }
300875fd0b74Schristos 
300975fd0b74Schristos /* Return the base vma address which should be subtracted from the real
301075fd0b74Schristos    address when resolving a dtpoff relocation.
301175fd0b74Schristos    This is PT_TLS segment p_vaddr.  */
301275fd0b74Schristos 
301375fd0b74Schristos static bfd_vma
dtpoff_base(struct bfd_link_info * info)301475fd0b74Schristos dtpoff_base (struct bfd_link_info *info)
301575fd0b74Schristos {
301675fd0b74Schristos   /* If tls_sec is NULL, we should have signalled an error already.  */
301775fd0b74Schristos   if (elf_hash_table (info)->tls_sec == NULL)
301875fd0b74Schristos     return 0;
301975fd0b74Schristos   return elf_hash_table (info)->tls_sec->vma;
302075fd0b74Schristos }
302175fd0b74Schristos 
302275fd0b74Schristos /* Return the relocation value for R_PARISC_TLS_TPOFF*..  */
302375fd0b74Schristos 
302475fd0b74Schristos static bfd_vma
tpoff(struct bfd_link_info * info,bfd_vma address)302575fd0b74Schristos tpoff (struct bfd_link_info *info, bfd_vma address)
302675fd0b74Schristos {
302775fd0b74Schristos   struct elf_link_hash_table *htab = elf_hash_table (info);
302875fd0b74Schristos 
302975fd0b74Schristos   /* If tls_sec is NULL, we should have signalled an error already.  */
303075fd0b74Schristos   if (htab->tls_sec == NULL)
303175fd0b74Schristos     return 0;
303275fd0b74Schristos   /* hppa TLS ABI is variant I and static TLS block start just after
303375fd0b74Schristos      tcbhead structure which has 2 pointer fields.  */
303475fd0b74Schristos   return (address - htab->tls_sec->vma
303575fd0b74Schristos 	  + align_power ((bfd_vma) 8, htab->tls_sec->alignment_power));
303675fd0b74Schristos }
303775fd0b74Schristos 
303875fd0b74Schristos /* Perform a final link.  */
303975fd0b74Schristos 
3040*e992f068Schristos static bool
elf32_hppa_final_link(bfd * abfd,struct bfd_link_info * info)304175fd0b74Schristos elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
304275fd0b74Schristos {
304375fd0b74Schristos   struct stat buf;
304475fd0b74Schristos 
304575fd0b74Schristos   /* Invoke the regular ELF linker to do all the work.  */
304675fd0b74Schristos   if (!bfd_elf_final_link (abfd, info))
3047*e992f068Schristos     return false;
304875fd0b74Schristos 
304975fd0b74Schristos   /* If we're producing a final executable, sort the contents of the
305075fd0b74Schristos      unwind section.  */
305175fd0b74Schristos   if (bfd_link_relocatable (info))
3052*e992f068Schristos     return true;
305375fd0b74Schristos 
305475fd0b74Schristos   /* Do not attempt to sort non-regular files.  This is here
305575fd0b74Schristos      especially for configure scripts and kernel builds which run
305675fd0b74Schristos      tests with "ld [...] -o /dev/null".  */
3057*e992f068Schristos   if (stat (bfd_get_filename (abfd), &buf) != 0
305875fd0b74Schristos       || !S_ISREG(buf.st_mode))
3059*e992f068Schristos     return true;
306075fd0b74Schristos 
306175fd0b74Schristos   return elf_hppa_sort_unwind (abfd);
306275fd0b74Schristos }
306375fd0b74Schristos 
306475fd0b74Schristos /* Record the lowest address for the data and text segments.  */
306575fd0b74Schristos 
306675fd0b74Schristos static void
hppa_record_segment_addr(bfd * abfd,asection * section,void * data)306775fd0b74Schristos hppa_record_segment_addr (bfd *abfd, asection *section, void *data)
306875fd0b74Schristos {
306975fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
307075fd0b74Schristos 
307175fd0b74Schristos   htab = (struct elf32_hppa_link_hash_table*) data;
307275fd0b74Schristos   if (htab == NULL)
307375fd0b74Schristos     return;
307475fd0b74Schristos 
307575fd0b74Schristos   if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
307675fd0b74Schristos     {
307775fd0b74Schristos       bfd_vma value;
307875fd0b74Schristos       Elf_Internal_Phdr *p;
307975fd0b74Schristos 
308075fd0b74Schristos       p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
308175fd0b74Schristos       BFD_ASSERT (p != NULL);
308275fd0b74Schristos       value = p->p_vaddr;
308375fd0b74Schristos 
308475fd0b74Schristos       if ((section->flags & SEC_READONLY) != 0)
308575fd0b74Schristos 	{
308675fd0b74Schristos 	  if (value < htab->text_segment_base)
308775fd0b74Schristos 	    htab->text_segment_base = value;
308875fd0b74Schristos 	}
308975fd0b74Schristos       else
309075fd0b74Schristos 	{
309175fd0b74Schristos 	  if (value < htab->data_segment_base)
309275fd0b74Schristos 	    htab->data_segment_base = value;
309375fd0b74Schristos 	}
309475fd0b74Schristos     }
309575fd0b74Schristos }
309675fd0b74Schristos 
309775fd0b74Schristos /* Perform a relocation as part of a final link.  */
309875fd0b74Schristos 
309975fd0b74Schristos static bfd_reloc_status_type
final_link_relocate(asection * input_section,bfd_byte * contents,const Elf_Internal_Rela * rela,bfd_vma value,struct elf32_hppa_link_hash_table * htab,asection * sym_sec,struct elf32_hppa_link_hash_entry * hh,struct bfd_link_info * info)310075fd0b74Schristos final_link_relocate (asection *input_section,
310175fd0b74Schristos 		     bfd_byte *contents,
310275fd0b74Schristos 		     const Elf_Internal_Rela *rela,
310375fd0b74Schristos 		     bfd_vma value,
310475fd0b74Schristos 		     struct elf32_hppa_link_hash_table *htab,
310575fd0b74Schristos 		     asection *sym_sec,
310675fd0b74Schristos 		     struct elf32_hppa_link_hash_entry *hh,
310775fd0b74Schristos 		     struct bfd_link_info *info)
310875fd0b74Schristos {
3109012573ebSchristos   unsigned int insn;
311075fd0b74Schristos   unsigned int r_type = ELF32_R_TYPE (rela->r_info);
311175fd0b74Schristos   unsigned int orig_r_type = r_type;
311275fd0b74Schristos   reloc_howto_type *howto = elf_hppa_howto_table + r_type;
311375fd0b74Schristos   int r_format = howto->bitsize;
311475fd0b74Schristos   enum hppa_reloc_field_selector_type_alt r_field;
311575fd0b74Schristos   bfd *input_bfd = input_section->owner;
311675fd0b74Schristos   bfd_vma offset = rela->r_offset;
311775fd0b74Schristos   bfd_vma max_branch_offset = 0;
311875fd0b74Schristos   bfd_byte *hit_data = contents + offset;
311975fd0b74Schristos   bfd_signed_vma addend = rela->r_addend;
312075fd0b74Schristos   bfd_vma location;
312175fd0b74Schristos   struct elf32_hppa_stub_hash_entry *hsh = NULL;
312275fd0b74Schristos   int val;
312375fd0b74Schristos 
312475fd0b74Schristos   if (r_type == R_PARISC_NONE)
312575fd0b74Schristos     return bfd_reloc_ok;
312675fd0b74Schristos 
312775fd0b74Schristos   insn = bfd_get_32 (input_bfd, hit_data);
312875fd0b74Schristos 
312975fd0b74Schristos   /* Find out where we are and where we're going.  */
313075fd0b74Schristos   location = (offset +
313175fd0b74Schristos 	      input_section->output_offset +
313275fd0b74Schristos 	      input_section->output_section->vma);
313375fd0b74Schristos 
313475fd0b74Schristos   /* If we are not building a shared library, convert DLTIND relocs to
313575fd0b74Schristos      DPREL relocs.  */
313675fd0b74Schristos   if (!bfd_link_pic (info))
313775fd0b74Schristos     {
313875fd0b74Schristos       switch (r_type)
313975fd0b74Schristos 	{
314075fd0b74Schristos 	  case R_PARISC_DLTIND21L:
314175fd0b74Schristos 	  case R_PARISC_TLS_GD21L:
314275fd0b74Schristos 	  case R_PARISC_TLS_LDM21L:
314375fd0b74Schristos 	  case R_PARISC_TLS_IE21L:
314475fd0b74Schristos 	    r_type = R_PARISC_DPREL21L;
314575fd0b74Schristos 	    break;
314675fd0b74Schristos 
314775fd0b74Schristos 	  case R_PARISC_DLTIND14R:
314875fd0b74Schristos 	  case R_PARISC_TLS_GD14R:
314975fd0b74Schristos 	  case R_PARISC_TLS_LDM14R:
315075fd0b74Schristos 	  case R_PARISC_TLS_IE14R:
315175fd0b74Schristos 	    r_type = R_PARISC_DPREL14R;
315275fd0b74Schristos 	    break;
315375fd0b74Schristos 
315475fd0b74Schristos 	  case R_PARISC_DLTIND14F:
315575fd0b74Schristos 	    r_type = R_PARISC_DPREL14F;
315675fd0b74Schristos 	    break;
315775fd0b74Schristos 	}
315875fd0b74Schristos     }
315975fd0b74Schristos 
316075fd0b74Schristos   switch (r_type)
316175fd0b74Schristos     {
316275fd0b74Schristos     case R_PARISC_PCREL12F:
316375fd0b74Schristos     case R_PARISC_PCREL17F:
316475fd0b74Schristos     case R_PARISC_PCREL22F:
316575fd0b74Schristos       /* If this call should go via the plt, find the import stub in
316675fd0b74Schristos 	 the stub hash.  */
316775fd0b74Schristos       if (sym_sec == NULL
316875fd0b74Schristos 	  || sym_sec->output_section == NULL
316975fd0b74Schristos 	  || (hh != NULL
317075fd0b74Schristos 	      && hh->eh.plt.offset != (bfd_vma) -1
317175fd0b74Schristos 	      && hh->eh.dynindx != -1
317275fd0b74Schristos 	      && !hh->plabel
317375fd0b74Schristos 	      && (bfd_link_pic (info)
317475fd0b74Schristos 		  || !hh->eh.def_regular
317575fd0b74Schristos 		  || hh->eh.root.type == bfd_link_hash_defweak)))
317675fd0b74Schristos 	{
317775fd0b74Schristos 	  hsh = hppa_get_stub_entry (input_section, sym_sec,
317875fd0b74Schristos 				     hh, rela, htab);
317975fd0b74Schristos 	  if (hsh != NULL)
318075fd0b74Schristos 	    {
318175fd0b74Schristos 	      value = (hsh->stub_offset
318275fd0b74Schristos 		       + hsh->stub_sec->output_offset
318375fd0b74Schristos 		       + hsh->stub_sec->output_section->vma);
318475fd0b74Schristos 	      addend = 0;
318575fd0b74Schristos 	    }
318675fd0b74Schristos 	  else if (sym_sec == NULL && hh != NULL
318775fd0b74Schristos 		   && hh->eh.root.type == bfd_link_hash_undefweak)
318875fd0b74Schristos 	    {
318975fd0b74Schristos 	      /* It's OK if undefined weak.  Calls to undefined weak
319075fd0b74Schristos 		 symbols behave as if the "called" function
319175fd0b74Schristos 		 immediately returns.  We can thus call to a weak
319275fd0b74Schristos 		 function without first checking whether the function
319375fd0b74Schristos 		 is defined.  */
319475fd0b74Schristos 	      value = location;
319575fd0b74Schristos 	      addend = 8;
319675fd0b74Schristos 	    }
319775fd0b74Schristos 	  else
319875fd0b74Schristos 	    return bfd_reloc_undefined;
319975fd0b74Schristos 	}
320075fd0b74Schristos       /* Fall thru.  */
320175fd0b74Schristos 
320275fd0b74Schristos     case R_PARISC_PCREL21L:
320375fd0b74Schristos     case R_PARISC_PCREL17C:
320475fd0b74Schristos     case R_PARISC_PCREL17R:
320575fd0b74Schristos     case R_PARISC_PCREL14R:
320675fd0b74Schristos     case R_PARISC_PCREL14F:
320775fd0b74Schristos     case R_PARISC_PCREL32:
320875fd0b74Schristos       /* Make it a pc relative offset.  */
320975fd0b74Schristos       value -= location;
321075fd0b74Schristos       addend -= 8;
321175fd0b74Schristos       break;
321275fd0b74Schristos 
321375fd0b74Schristos     case R_PARISC_DPREL21L:
321475fd0b74Schristos     case R_PARISC_DPREL14R:
321575fd0b74Schristos     case R_PARISC_DPREL14F:
321675fd0b74Schristos       /* Convert instructions that use the linkage table pointer (r19) to
321775fd0b74Schristos 	 instructions that use the global data pointer (dp).  This is the
321875fd0b74Schristos 	 most efficient way of using PIC code in an incomplete executable,
321975fd0b74Schristos 	 but the user must follow the standard runtime conventions for
322075fd0b74Schristos 	 accessing data for this to work.  */
322175fd0b74Schristos       if (orig_r_type != r_type)
322275fd0b74Schristos 	{
322375fd0b74Schristos 	  if (r_type == R_PARISC_DPREL21L)
322475fd0b74Schristos 	    {
322575fd0b74Schristos 	      /* GCC sometimes uses a register other than r19 for the
322675fd0b74Schristos 		 operation, so we must convert any addil instruction
322775fd0b74Schristos 		 that uses this relocation.  */
3228012573ebSchristos 	      if ((insn & 0xfc000000) == OP_ADDIL << 26)
322975fd0b74Schristos 		insn = ADDIL_DP;
323075fd0b74Schristos 	      else
323175fd0b74Schristos 		/* We must have a ldil instruction.  It's too hard to find
323275fd0b74Schristos 		   and convert the associated add instruction, so issue an
323375fd0b74Schristos 		   error.  */
3234ede78133Schristos 		_bfd_error_handler
3235ede78133Schristos 		  /* xgettext:c-format */
3236ede78133Schristos 		  (_("%pB(%pA+%#" PRIx64 "): %s fixup for insn %#x "
3237ede78133Schristos 		     "is not supported in a non-shared link"),
323875fd0b74Schristos 		   input_bfd,
323975fd0b74Schristos 		   input_section,
3240ede78133Schristos 		   (uint64_t) offset,
324175fd0b74Schristos 		   howto->name,
324275fd0b74Schristos 		   insn);
324375fd0b74Schristos 	    }
324475fd0b74Schristos 	  else if (r_type == R_PARISC_DPREL14F)
324575fd0b74Schristos 	    {
324675fd0b74Schristos 	      /* This must be a format 1 load/store.  Change the base
324775fd0b74Schristos 		 register to dp.  */
324875fd0b74Schristos 	      insn = (insn & 0xfc1ffff) | (27 << 21);
324975fd0b74Schristos 	    }
325075fd0b74Schristos 	}
325175fd0b74Schristos 
325275fd0b74Schristos       /* For all the DP relative relocations, we need to examine the symbol's
325375fd0b74Schristos 	 section.  If it has no section or if it's a code section, then
325475fd0b74Schristos 	 "data pointer relative" makes no sense.  In that case we don't
325575fd0b74Schristos 	 adjust the "value", and for 21 bit addil instructions, we change the
325675fd0b74Schristos 	 source addend register from %dp to %r0.  This situation commonly
325775fd0b74Schristos 	 arises for undefined weak symbols and when a variable's "constness"
325875fd0b74Schristos 	 is declared differently from the way the variable is defined.  For
325975fd0b74Schristos 	 instance: "extern int foo" with foo defined as "const int foo".  */
326075fd0b74Schristos       if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
326175fd0b74Schristos 	{
3262012573ebSchristos 	  if ((insn & ((0x3fu << 26) | (0x1f << 21)))
3263012573ebSchristos 	      == ((OP_ADDIL << 26) | (27 << 21)))
326475fd0b74Schristos 	    {
326575fd0b74Schristos 	      insn &= ~ (0x1f << 21);
326675fd0b74Schristos 	    }
326775fd0b74Schristos 	  /* Now try to make things easy for the dynamic linker.  */
326875fd0b74Schristos 
326975fd0b74Schristos 	  break;
327075fd0b74Schristos 	}
327175fd0b74Schristos       /* Fall thru.  */
327275fd0b74Schristos 
327375fd0b74Schristos     case R_PARISC_DLTIND21L:
327475fd0b74Schristos     case R_PARISC_DLTIND14R:
327575fd0b74Schristos     case R_PARISC_DLTIND14F:
327675fd0b74Schristos     case R_PARISC_TLS_GD21L:
327775fd0b74Schristos     case R_PARISC_TLS_LDM21L:
327875fd0b74Schristos     case R_PARISC_TLS_IE21L:
327975fd0b74Schristos     case R_PARISC_TLS_GD14R:
328075fd0b74Schristos     case R_PARISC_TLS_LDM14R:
328175fd0b74Schristos     case R_PARISC_TLS_IE14R:
328275fd0b74Schristos       value -= elf_gp (input_section->output_section->owner);
328375fd0b74Schristos       break;
328475fd0b74Schristos 
328575fd0b74Schristos     case R_PARISC_SEGREL32:
328675fd0b74Schristos       if ((sym_sec->flags & SEC_CODE) != 0)
328775fd0b74Schristos 	value -= htab->text_segment_base;
328875fd0b74Schristos       else
328975fd0b74Schristos 	value -= htab->data_segment_base;
329075fd0b74Schristos       break;
329175fd0b74Schristos 
329275fd0b74Schristos     default:
329375fd0b74Schristos       break;
329475fd0b74Schristos     }
329575fd0b74Schristos 
329675fd0b74Schristos   switch (r_type)
329775fd0b74Schristos     {
329875fd0b74Schristos     case R_PARISC_DIR32:
329975fd0b74Schristos     case R_PARISC_DIR14F:
330075fd0b74Schristos     case R_PARISC_DIR17F:
330175fd0b74Schristos     case R_PARISC_PCREL17C:
330275fd0b74Schristos     case R_PARISC_PCREL14F:
330375fd0b74Schristos     case R_PARISC_PCREL32:
330475fd0b74Schristos     case R_PARISC_DPREL14F:
330575fd0b74Schristos     case R_PARISC_PLABEL32:
330675fd0b74Schristos     case R_PARISC_DLTIND14F:
330775fd0b74Schristos     case R_PARISC_SEGBASE:
330875fd0b74Schristos     case R_PARISC_SEGREL32:
330975fd0b74Schristos     case R_PARISC_TLS_DTPMOD32:
331075fd0b74Schristos     case R_PARISC_TLS_DTPOFF32:
331175fd0b74Schristos     case R_PARISC_TLS_TPREL32:
331275fd0b74Schristos       r_field = e_fsel;
331375fd0b74Schristos       break;
331475fd0b74Schristos 
331575fd0b74Schristos     case R_PARISC_DLTIND21L:
331675fd0b74Schristos     case R_PARISC_PCREL21L:
331775fd0b74Schristos     case R_PARISC_PLABEL21L:
331875fd0b74Schristos       r_field = e_lsel;
331975fd0b74Schristos       break;
332075fd0b74Schristos 
332175fd0b74Schristos     case R_PARISC_DIR21L:
332275fd0b74Schristos     case R_PARISC_DPREL21L:
332375fd0b74Schristos     case R_PARISC_TLS_GD21L:
332475fd0b74Schristos     case R_PARISC_TLS_LDM21L:
332575fd0b74Schristos     case R_PARISC_TLS_LDO21L:
332675fd0b74Schristos     case R_PARISC_TLS_IE21L:
332775fd0b74Schristos     case R_PARISC_TLS_LE21L:
332875fd0b74Schristos       r_field = e_lrsel;
332975fd0b74Schristos       break;
333075fd0b74Schristos 
333175fd0b74Schristos     case R_PARISC_PCREL17R:
333275fd0b74Schristos     case R_PARISC_PCREL14R:
333375fd0b74Schristos     case R_PARISC_PLABEL14R:
333475fd0b74Schristos     case R_PARISC_DLTIND14R:
333575fd0b74Schristos       r_field = e_rsel;
333675fd0b74Schristos       break;
333775fd0b74Schristos 
333875fd0b74Schristos     case R_PARISC_DIR17R:
333975fd0b74Schristos     case R_PARISC_DIR14R:
334075fd0b74Schristos     case R_PARISC_DPREL14R:
334175fd0b74Schristos     case R_PARISC_TLS_GD14R:
334275fd0b74Schristos     case R_PARISC_TLS_LDM14R:
334375fd0b74Schristos     case R_PARISC_TLS_LDO14R:
334475fd0b74Schristos     case R_PARISC_TLS_IE14R:
334575fd0b74Schristos     case R_PARISC_TLS_LE14R:
334675fd0b74Schristos       r_field = e_rrsel;
334775fd0b74Schristos       break;
334875fd0b74Schristos 
334975fd0b74Schristos     case R_PARISC_PCREL12F:
335075fd0b74Schristos     case R_PARISC_PCREL17F:
335175fd0b74Schristos     case R_PARISC_PCREL22F:
335275fd0b74Schristos       r_field = e_fsel;
335375fd0b74Schristos 
335475fd0b74Schristos       if (r_type == (unsigned int) R_PARISC_PCREL17F)
335575fd0b74Schristos 	{
335675fd0b74Schristos 	  max_branch_offset = (1 << (17-1)) << 2;
335775fd0b74Schristos 	}
335875fd0b74Schristos       else if (r_type == (unsigned int) R_PARISC_PCREL12F)
335975fd0b74Schristos 	{
336075fd0b74Schristos 	  max_branch_offset = (1 << (12-1)) << 2;
336175fd0b74Schristos 	}
336275fd0b74Schristos       else
336375fd0b74Schristos 	{
336475fd0b74Schristos 	  max_branch_offset = (1 << (22-1)) << 2;
336575fd0b74Schristos 	}
336675fd0b74Schristos 
336775fd0b74Schristos       /* sym_sec is NULL on undefined weak syms or when shared on
336875fd0b74Schristos 	 undefined syms.  We've already checked for a stub for the
336975fd0b74Schristos 	 shared undefined case.  */
337075fd0b74Schristos       if (sym_sec == NULL)
337175fd0b74Schristos 	break;
337275fd0b74Schristos 
337375fd0b74Schristos       /* If the branch is out of reach, then redirect the
337475fd0b74Schristos 	 call to the local stub for this function.  */
337575fd0b74Schristos       if (value + addend + max_branch_offset >= 2*max_branch_offset)
337675fd0b74Schristos 	{
337775fd0b74Schristos 	  hsh = hppa_get_stub_entry (input_section, sym_sec,
337875fd0b74Schristos 				     hh, rela, htab);
337975fd0b74Schristos 	  if (hsh == NULL)
338075fd0b74Schristos 	    return bfd_reloc_undefined;
338175fd0b74Schristos 
338275fd0b74Schristos 	  /* Munge up the value and addend so that we call the stub
338375fd0b74Schristos 	     rather than the procedure directly.  */
338475fd0b74Schristos 	  value = (hsh->stub_offset
338575fd0b74Schristos 		   + hsh->stub_sec->output_offset
338675fd0b74Schristos 		   + hsh->stub_sec->output_section->vma
338775fd0b74Schristos 		   - location);
338875fd0b74Schristos 	  addend = -8;
338975fd0b74Schristos 	}
339075fd0b74Schristos       break;
339175fd0b74Schristos 
339275fd0b74Schristos     /* Something we don't know how to handle.  */
339375fd0b74Schristos     default:
339475fd0b74Schristos       return bfd_reloc_notsupported;
339575fd0b74Schristos     }
339675fd0b74Schristos 
339775fd0b74Schristos   /* Make sure we can reach the stub.  */
339875fd0b74Schristos   if (max_branch_offset != 0
339975fd0b74Schristos       && value + addend + max_branch_offset >= 2*max_branch_offset)
340075fd0b74Schristos     {
3401ede78133Schristos       _bfd_error_handler
3402ede78133Schristos 	/* xgettext:c-format */
3403ede78133Schristos 	(_("%pB(%pA+%#" PRIx64 "): cannot reach %s, "
3404ede78133Schristos 	   "recompile with -ffunction-sections"),
340575fd0b74Schristos 	 input_bfd,
340675fd0b74Schristos 	 input_section,
3407ede78133Schristos 	 (uint64_t) offset,
340875fd0b74Schristos 	 hsh->bh_root.string);
340975fd0b74Schristos       bfd_set_error (bfd_error_bad_value);
341075fd0b74Schristos       return bfd_reloc_notsupported;
341175fd0b74Schristos     }
341275fd0b74Schristos 
341375fd0b74Schristos   val = hppa_field_adjust (value, addend, r_field);
341475fd0b74Schristos 
341575fd0b74Schristos   switch (r_type)
341675fd0b74Schristos     {
341775fd0b74Schristos     case R_PARISC_PCREL12F:
341875fd0b74Schristos     case R_PARISC_PCREL17C:
341975fd0b74Schristos     case R_PARISC_PCREL17F:
342075fd0b74Schristos     case R_PARISC_PCREL17R:
342175fd0b74Schristos     case R_PARISC_PCREL22F:
342275fd0b74Schristos     case R_PARISC_DIR17F:
342375fd0b74Schristos     case R_PARISC_DIR17R:
342475fd0b74Schristos       /* This is a branch.  Divide the offset by four.
342575fd0b74Schristos 	 Note that we need to decide whether it's a branch or
342675fd0b74Schristos 	 otherwise by inspecting the reloc.  Inspecting insn won't
342775fd0b74Schristos 	 work as insn might be from a .word directive.  */
342875fd0b74Schristos       val >>= 2;
342975fd0b74Schristos       break;
343075fd0b74Schristos 
343175fd0b74Schristos     default:
343275fd0b74Schristos       break;
343375fd0b74Schristos     }
343475fd0b74Schristos 
343575fd0b74Schristos   insn = hppa_rebuild_insn (insn, val, r_format);
343675fd0b74Schristos 
343775fd0b74Schristos   /* Update the instruction word.  */
343875fd0b74Schristos   bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
343975fd0b74Schristos   return bfd_reloc_ok;
344075fd0b74Schristos }
344175fd0b74Schristos 
344275fd0b74Schristos /* Relocate an HPPA ELF section.  */
344375fd0b74Schristos 
3444*e992f068Schristos static int
elf32_hppa_relocate_section(bfd * output_bfd,struct bfd_link_info * info,bfd * input_bfd,asection * input_section,bfd_byte * contents,Elf_Internal_Rela * relocs,Elf_Internal_Sym * local_syms,asection ** local_sections)344575fd0b74Schristos elf32_hppa_relocate_section (bfd *output_bfd,
344675fd0b74Schristos 			     struct bfd_link_info *info,
344775fd0b74Schristos 			     bfd *input_bfd,
344875fd0b74Schristos 			     asection *input_section,
344975fd0b74Schristos 			     bfd_byte *contents,
345075fd0b74Schristos 			     Elf_Internal_Rela *relocs,
345175fd0b74Schristos 			     Elf_Internal_Sym *local_syms,
345275fd0b74Schristos 			     asection **local_sections)
345375fd0b74Schristos {
345475fd0b74Schristos   bfd_vma *local_got_offsets;
345575fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
345675fd0b74Schristos   Elf_Internal_Shdr *symtab_hdr;
345775fd0b74Schristos   Elf_Internal_Rela *rela;
345875fd0b74Schristos   Elf_Internal_Rela *relend;
345975fd0b74Schristos 
346075fd0b74Schristos   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
346175fd0b74Schristos 
346275fd0b74Schristos   htab = hppa_link_hash_table (info);
346375fd0b74Schristos   if (htab == NULL)
3464*e992f068Schristos     return false;
346575fd0b74Schristos 
346675fd0b74Schristos   local_got_offsets = elf_local_got_offsets (input_bfd);
346775fd0b74Schristos 
346875fd0b74Schristos   rela = relocs;
346975fd0b74Schristos   relend = relocs + input_section->reloc_count;
347075fd0b74Schristos   for (; rela < relend; rela++)
347175fd0b74Schristos     {
347275fd0b74Schristos       unsigned int r_type;
347375fd0b74Schristos       reloc_howto_type *howto;
347475fd0b74Schristos       unsigned int r_symndx;
347575fd0b74Schristos       struct elf32_hppa_link_hash_entry *hh;
347675fd0b74Schristos       Elf_Internal_Sym *sym;
347775fd0b74Schristos       asection *sym_sec;
347875fd0b74Schristos       bfd_vma relocation;
347975fd0b74Schristos       bfd_reloc_status_type rstatus;
348075fd0b74Schristos       const char *sym_name;
3481*e992f068Schristos       bool plabel;
3482*e992f068Schristos       bool warned_undef;
348375fd0b74Schristos 
348475fd0b74Schristos       r_type = ELF32_R_TYPE (rela->r_info);
348575fd0b74Schristos       if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
348675fd0b74Schristos 	{
348775fd0b74Schristos 	  bfd_set_error (bfd_error_bad_value);
3488*e992f068Schristos 	  return false;
348975fd0b74Schristos 	}
349075fd0b74Schristos       if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
349175fd0b74Schristos 	  || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
349275fd0b74Schristos 	continue;
349375fd0b74Schristos 
349475fd0b74Schristos       r_symndx = ELF32_R_SYM (rela->r_info);
349575fd0b74Schristos       hh = NULL;
349675fd0b74Schristos       sym = NULL;
349775fd0b74Schristos       sym_sec = NULL;
3498*e992f068Schristos       warned_undef = false;
349975fd0b74Schristos       if (r_symndx < symtab_hdr->sh_info)
350075fd0b74Schristos 	{
350175fd0b74Schristos 	  /* This is a local symbol, h defaults to NULL.  */
350275fd0b74Schristos 	  sym = local_syms + r_symndx;
350375fd0b74Schristos 	  sym_sec = local_sections[r_symndx];
350475fd0b74Schristos 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
350575fd0b74Schristos 	}
350675fd0b74Schristos       else
350775fd0b74Schristos 	{
350875fd0b74Schristos 	  struct elf_link_hash_entry *eh;
3509*e992f068Schristos 	  bool unresolved_reloc, ignored;
351075fd0b74Schristos 	  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
351175fd0b74Schristos 
351275fd0b74Schristos 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
351375fd0b74Schristos 				   r_symndx, symtab_hdr, sym_hashes,
351475fd0b74Schristos 				   eh, sym_sec, relocation,
351575fd0b74Schristos 				   unresolved_reloc, warned_undef,
351675fd0b74Schristos 				   ignored);
351775fd0b74Schristos 
351875fd0b74Schristos 	  if (!bfd_link_relocatable (info)
351975fd0b74Schristos 	      && relocation == 0
352075fd0b74Schristos 	      && eh->root.type != bfd_link_hash_defined
352175fd0b74Schristos 	      && eh->root.type != bfd_link_hash_defweak
352275fd0b74Schristos 	      && eh->root.type != bfd_link_hash_undefweak)
352375fd0b74Schristos 	    {
352475fd0b74Schristos 	      if (info->unresolved_syms_in_objects == RM_IGNORE
352575fd0b74Schristos 		  && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
352675fd0b74Schristos 		  && eh->type == STT_PARISC_MILLI)
352775fd0b74Schristos 		{
352875fd0b74Schristos 		  (*info->callbacks->undefined_symbol)
352975fd0b74Schristos 		    (info, eh_name (eh), input_bfd,
3530*e992f068Schristos 		     input_section, rela->r_offset, false);
3531*e992f068Schristos 		  warned_undef = true;
353275fd0b74Schristos 		}
353375fd0b74Schristos 	    }
353475fd0b74Schristos 	  hh = hppa_elf_hash_entry (eh);
353575fd0b74Schristos 	}
353675fd0b74Schristos 
353775fd0b74Schristos       if (sym_sec != NULL && discarded_section (sym_sec))
353875fd0b74Schristos 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
353975fd0b74Schristos 					 rela, 1, relend,
354075fd0b74Schristos 					 elf_hppa_howto_table + r_type, 0,
354175fd0b74Schristos 					 contents);
354275fd0b74Schristos 
354375fd0b74Schristos       if (bfd_link_relocatable (info))
354475fd0b74Schristos 	continue;
354575fd0b74Schristos 
354675fd0b74Schristos       /* Do any required modifications to the relocation value, and
354775fd0b74Schristos 	 determine what types of dynamic info we need to output, if
354875fd0b74Schristos 	 any.  */
354975fd0b74Schristos       plabel = 0;
355075fd0b74Schristos       switch (r_type)
355175fd0b74Schristos 	{
355275fd0b74Schristos 	case R_PARISC_DLTIND14F:
355375fd0b74Schristos 	case R_PARISC_DLTIND14R:
355475fd0b74Schristos 	case R_PARISC_DLTIND21L:
355575fd0b74Schristos 	  {
355675fd0b74Schristos 	    bfd_vma off;
3557*e992f068Schristos 	    bool do_got = false;
3558*e992f068Schristos 	    bool reloc = bfd_link_pic (info);
355975fd0b74Schristos 
356075fd0b74Schristos 	    /* Relocation is to the entry for this symbol in the
356175fd0b74Schristos 	       global offset table.  */
356275fd0b74Schristos 	    if (hh != NULL)
356375fd0b74Schristos 	      {
3564*e992f068Schristos 		bool dyn;
356575fd0b74Schristos 
356675fd0b74Schristos 		off = hh->eh.got.offset;
356775fd0b74Schristos 		dyn = htab->etab.dynamic_sections_created;
3568ede78133Schristos 		reloc = (!UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh)
3569ede78133Schristos 			 && (reloc
3570ede78133Schristos 			     || (hh->eh.dynindx != -1
3571ede78133Schristos 				 && !SYMBOL_REFERENCES_LOCAL (info, &hh->eh))));
3572ede78133Schristos 		if (!reloc
3573ede78133Schristos 		    || !WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
357475fd0b74Schristos 							 bfd_link_pic (info),
357575fd0b74Schristos 							 &hh->eh))
357675fd0b74Schristos 		  {
357775fd0b74Schristos 		    /* If we aren't going to call finish_dynamic_symbol,
357875fd0b74Schristos 		       then we need to handle initialisation of the .got
357975fd0b74Schristos 		       entry and create needed relocs here.  Since the
358075fd0b74Schristos 		       offset must always be a multiple of 4, we use the
358175fd0b74Schristos 		       least significant bit to record whether we have
358275fd0b74Schristos 		       initialised it already.  */
358375fd0b74Schristos 		    if ((off & 1) != 0)
358475fd0b74Schristos 		      off &= ~1;
358575fd0b74Schristos 		    else
358675fd0b74Schristos 		      {
358775fd0b74Schristos 			hh->eh.got.offset |= 1;
3588*e992f068Schristos 			do_got = true;
358975fd0b74Schristos 		      }
359075fd0b74Schristos 		  }
359175fd0b74Schristos 	      }
359275fd0b74Schristos 	    else
359375fd0b74Schristos 	      {
359475fd0b74Schristos 		/* Local symbol case.  */
359575fd0b74Schristos 		if (local_got_offsets == NULL)
359675fd0b74Schristos 		  abort ();
359775fd0b74Schristos 
359875fd0b74Schristos 		off = local_got_offsets[r_symndx];
359975fd0b74Schristos 
360075fd0b74Schristos 		/* The offset must always be a multiple of 4.  We use
360175fd0b74Schristos 		   the least significant bit to record whether we have
360275fd0b74Schristos 		   already generated the necessary reloc.  */
360375fd0b74Schristos 		if ((off & 1) != 0)
360475fd0b74Schristos 		  off &= ~1;
360575fd0b74Schristos 		else
360675fd0b74Schristos 		  {
360775fd0b74Schristos 		    local_got_offsets[r_symndx] |= 1;
3608*e992f068Schristos 		    do_got = true;
360975fd0b74Schristos 		  }
361075fd0b74Schristos 	      }
361175fd0b74Schristos 
361275fd0b74Schristos 	    if (do_got)
361375fd0b74Schristos 	      {
3614ede78133Schristos 		if (reloc)
361575fd0b74Schristos 		  {
361675fd0b74Schristos 		    /* Output a dynamic relocation for this GOT entry.
361775fd0b74Schristos 		       In this case it is relative to the base of the
361875fd0b74Schristos 		       object because the symbol index is zero.  */
361975fd0b74Schristos 		    Elf_Internal_Rela outrel;
362075fd0b74Schristos 		    bfd_byte *loc;
3621ede78133Schristos 		    asection *sec = htab->etab.srelgot;
362275fd0b74Schristos 
362375fd0b74Schristos 		    outrel.r_offset = (off
3624ede78133Schristos 				       + htab->etab.sgot->output_offset
3625ede78133Schristos 				       + htab->etab.sgot->output_section->vma);
362675fd0b74Schristos 		    outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
362775fd0b74Schristos 		    outrel.r_addend = relocation;
362875fd0b74Schristos 		    loc = sec->contents;
362975fd0b74Schristos 		    loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
363075fd0b74Schristos 		    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
363175fd0b74Schristos 		  }
363275fd0b74Schristos 		else
363375fd0b74Schristos 		  bfd_put_32 (output_bfd, relocation,
3634ede78133Schristos 			      htab->etab.sgot->contents + off);
363575fd0b74Schristos 	      }
363675fd0b74Schristos 
363775fd0b74Schristos 	    if (off >= (bfd_vma) -2)
363875fd0b74Schristos 	      abort ();
363975fd0b74Schristos 
364075fd0b74Schristos 	    /* Add the base of the GOT to the relocation value.  */
364175fd0b74Schristos 	    relocation = (off
3642ede78133Schristos 			  + htab->etab.sgot->output_offset
3643ede78133Schristos 			  + htab->etab.sgot->output_section->vma);
364475fd0b74Schristos 	  }
364575fd0b74Schristos 	  break;
364675fd0b74Schristos 
364775fd0b74Schristos 	case R_PARISC_SEGREL32:
364875fd0b74Schristos 	  /* If this is the first SEGREL relocation, then initialize
364975fd0b74Schristos 	     the segment base values.  */
365075fd0b74Schristos 	  if (htab->text_segment_base == (bfd_vma) -1)
365175fd0b74Schristos 	    bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
365275fd0b74Schristos 	  break;
365375fd0b74Schristos 
365475fd0b74Schristos 	case R_PARISC_PLABEL14R:
365575fd0b74Schristos 	case R_PARISC_PLABEL21L:
365675fd0b74Schristos 	case R_PARISC_PLABEL32:
365775fd0b74Schristos 	  if (htab->etab.dynamic_sections_created)
365875fd0b74Schristos 	    {
365975fd0b74Schristos 	      bfd_vma off;
3660*e992f068Schristos 	      bool do_plt = 0;
366175fd0b74Schristos 	      /* If we have a global symbol with a PLT slot, then
366275fd0b74Schristos 		 redirect this relocation to it.  */
366375fd0b74Schristos 	      if (hh != NULL)
366475fd0b74Schristos 		{
366575fd0b74Schristos 		  off = hh->eh.plt.offset;
366675fd0b74Schristos 		  if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1,
366775fd0b74Schristos 							 bfd_link_pic (info),
366875fd0b74Schristos 							 &hh->eh))
366975fd0b74Schristos 		    {
3670ede78133Schristos 		      /* In a non-shared link, adjust_dynamic_symbol
367175fd0b74Schristos 			 isn't called for symbols forced local.  We
367275fd0b74Schristos 			 need to write out the plt entry here.  */
367375fd0b74Schristos 		      if ((off & 1) != 0)
367475fd0b74Schristos 			off &= ~1;
367575fd0b74Schristos 		      else
367675fd0b74Schristos 			{
367775fd0b74Schristos 			  hh->eh.plt.offset |= 1;
367875fd0b74Schristos 			  do_plt = 1;
367975fd0b74Schristos 			}
368075fd0b74Schristos 		    }
368175fd0b74Schristos 		}
368275fd0b74Schristos 	      else
368375fd0b74Schristos 		{
368475fd0b74Schristos 		  bfd_vma *local_plt_offsets;
368575fd0b74Schristos 
368675fd0b74Schristos 		  if (local_got_offsets == NULL)
368775fd0b74Schristos 		    abort ();
368875fd0b74Schristos 
368975fd0b74Schristos 		  local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
369075fd0b74Schristos 		  off = local_plt_offsets[r_symndx];
369175fd0b74Schristos 
369275fd0b74Schristos 		  /* As for the local .got entry case, we use the last
369375fd0b74Schristos 		     bit to record whether we've already initialised
369475fd0b74Schristos 		     this local .plt entry.  */
369575fd0b74Schristos 		  if ((off & 1) != 0)
369675fd0b74Schristos 		    off &= ~1;
369775fd0b74Schristos 		  else
369875fd0b74Schristos 		    {
369975fd0b74Schristos 		      local_plt_offsets[r_symndx] |= 1;
370075fd0b74Schristos 		      do_plt = 1;
370175fd0b74Schristos 		    }
370275fd0b74Schristos 		}
370375fd0b74Schristos 
370475fd0b74Schristos 	      if (do_plt)
370575fd0b74Schristos 		{
370675fd0b74Schristos 		  if (bfd_link_pic (info))
370775fd0b74Schristos 		    {
370875fd0b74Schristos 		      /* Output a dynamic IPLT relocation for this
370975fd0b74Schristos 			 PLT entry.  */
371075fd0b74Schristos 		      Elf_Internal_Rela outrel;
371175fd0b74Schristos 		      bfd_byte *loc;
3712ede78133Schristos 		      asection *s = htab->etab.srelplt;
371375fd0b74Schristos 
371475fd0b74Schristos 		      outrel.r_offset = (off
3715ede78133Schristos 					 + htab->etab.splt->output_offset
3716ede78133Schristos 					 + htab->etab.splt->output_section->vma);
371775fd0b74Schristos 		      outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
371875fd0b74Schristos 		      outrel.r_addend = relocation;
371975fd0b74Schristos 		      loc = s->contents;
372075fd0b74Schristos 		      loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
372175fd0b74Schristos 		      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
372275fd0b74Schristos 		    }
372375fd0b74Schristos 		  else
372475fd0b74Schristos 		    {
372575fd0b74Schristos 		      bfd_put_32 (output_bfd,
372675fd0b74Schristos 				  relocation,
3727ede78133Schristos 				  htab->etab.splt->contents + off);
372875fd0b74Schristos 		      bfd_put_32 (output_bfd,
3729ede78133Schristos 				  elf_gp (htab->etab.splt->output_section->owner),
3730ede78133Schristos 				  htab->etab.splt->contents + off + 4);
373175fd0b74Schristos 		    }
373275fd0b74Schristos 		}
373375fd0b74Schristos 
373475fd0b74Schristos 	      if (off >= (bfd_vma) -2)
373575fd0b74Schristos 		abort ();
373675fd0b74Schristos 
373775fd0b74Schristos 	      /* PLABELs contain function pointers.  Relocation is to
373875fd0b74Schristos 		 the entry for the function in the .plt.  The magic +2
373975fd0b74Schristos 		 offset signals to $$dyncall that the function pointer
374075fd0b74Schristos 		 is in the .plt and thus has a gp pointer too.
374175fd0b74Schristos 		 Exception:  Undefined PLABELs should have a value of
374275fd0b74Schristos 		 zero.  */
374375fd0b74Schristos 	      if (hh == NULL
374475fd0b74Schristos 		  || (hh->eh.root.type != bfd_link_hash_undefweak
374575fd0b74Schristos 		      && hh->eh.root.type != bfd_link_hash_undefined))
374675fd0b74Schristos 		{
374775fd0b74Schristos 		  relocation = (off
3748ede78133Schristos 				+ htab->etab.splt->output_offset
3749ede78133Schristos 				+ htab->etab.splt->output_section->vma
375075fd0b74Schristos 				+ 2);
375175fd0b74Schristos 		}
375275fd0b74Schristos 	      plabel = 1;
375375fd0b74Schristos 	    }
3754ede78133Schristos 	  /* Fall through.  */
375575fd0b74Schristos 
375675fd0b74Schristos 	case R_PARISC_DIR17F:
375775fd0b74Schristos 	case R_PARISC_DIR17R:
375875fd0b74Schristos 	case R_PARISC_DIR14F:
375975fd0b74Schristos 	case R_PARISC_DIR14R:
376075fd0b74Schristos 	case R_PARISC_DIR21L:
376175fd0b74Schristos 	case R_PARISC_DPREL14F:
376275fd0b74Schristos 	case R_PARISC_DPREL14R:
376375fd0b74Schristos 	case R_PARISC_DPREL21L:
376475fd0b74Schristos 	case R_PARISC_DIR32:
376575fd0b74Schristos 	  if ((input_section->flags & SEC_ALLOC) == 0)
376675fd0b74Schristos 	    break;
376775fd0b74Schristos 
3768ede78133Schristos 	  if (bfd_link_pic (info)
3769ede78133Schristos 	      ? ((hh == NULL
3770*e992f068Schristos 		  || hh->eh.dyn_relocs != NULL)
3771ede78133Schristos 		 && ((hh != NULL && pc_dynrelocs (hh))
3772ede78133Schristos 		     || IS_ABSOLUTE_RELOC (r_type)))
3773ede78133Schristos 	      : (hh != NULL
3774*e992f068Schristos 		 && hh->eh.dyn_relocs != NULL))
377575fd0b74Schristos 	    {
377675fd0b74Schristos 	      Elf_Internal_Rela outrel;
3777*e992f068Schristos 	      bool skip;
377875fd0b74Schristos 	      asection *sreloc;
377975fd0b74Schristos 	      bfd_byte *loc;
378075fd0b74Schristos 
378175fd0b74Schristos 	      /* When generating a shared object, these relocations
378275fd0b74Schristos 		 are copied into the output file to be resolved at run
378375fd0b74Schristos 		 time.  */
378475fd0b74Schristos 
378575fd0b74Schristos 	      outrel.r_addend = rela->r_addend;
378675fd0b74Schristos 	      outrel.r_offset =
378775fd0b74Schristos 		_bfd_elf_section_offset (output_bfd, info, input_section,
378875fd0b74Schristos 					 rela->r_offset);
378975fd0b74Schristos 	      skip = (outrel.r_offset == (bfd_vma) -1
379075fd0b74Schristos 		      || outrel.r_offset == (bfd_vma) -2);
379175fd0b74Schristos 	      outrel.r_offset += (input_section->output_offset
379275fd0b74Schristos 				  + input_section->output_section->vma);
379375fd0b74Schristos 
379475fd0b74Schristos 	      if (skip)
379575fd0b74Schristos 		{
379675fd0b74Schristos 		  memset (&outrel, 0, sizeof (outrel));
379775fd0b74Schristos 		}
379875fd0b74Schristos 	      else if (hh != NULL
379975fd0b74Schristos 		       && hh->eh.dynindx != -1
380075fd0b74Schristos 		       && (plabel
380175fd0b74Schristos 			   || !IS_ABSOLUTE_RELOC (r_type)
380275fd0b74Schristos 			   || !bfd_link_pic (info)
380375fd0b74Schristos 			   || !SYMBOLIC_BIND (info, &hh->eh)
380475fd0b74Schristos 			   || !hh->eh.def_regular))
380575fd0b74Schristos 		{
380675fd0b74Schristos 		  outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
380775fd0b74Schristos 		}
380875fd0b74Schristos 	      else /* It's a local symbol, or one marked to become local.  */
380975fd0b74Schristos 		{
381075fd0b74Schristos 		  int indx = 0;
381175fd0b74Schristos 
381275fd0b74Schristos 		  /* Add the absolute offset of the symbol.  */
381375fd0b74Schristos 		  outrel.r_addend += relocation;
381475fd0b74Schristos 
381575fd0b74Schristos 		  /* Global plabels need to be processed by the
381675fd0b74Schristos 		     dynamic linker so that functions have at most one
381775fd0b74Schristos 		     fptr.  For this reason, we need to differentiate
381875fd0b74Schristos 		     between global and local plabels, which we do by
381975fd0b74Schristos 		     providing the function symbol for a global plabel
382075fd0b74Schristos 		     reloc, and no symbol for local plabels.  */
382175fd0b74Schristos 		  if (! plabel
382275fd0b74Schristos 		      && sym_sec != NULL
382375fd0b74Schristos 		      && sym_sec->output_section != NULL
382475fd0b74Schristos 		      && ! bfd_is_abs_section (sym_sec))
382575fd0b74Schristos 		    {
382675fd0b74Schristos 		      asection *osec;
382775fd0b74Schristos 
382875fd0b74Schristos 		      osec = sym_sec->output_section;
382975fd0b74Schristos 		      indx = elf_section_data (osec)->dynindx;
383075fd0b74Schristos 		      if (indx == 0)
383175fd0b74Schristos 			{
383275fd0b74Schristos 			  osec = htab->etab.text_index_section;
383375fd0b74Schristos 			  indx = elf_section_data (osec)->dynindx;
383475fd0b74Schristos 			}
383575fd0b74Schristos 		      BFD_ASSERT (indx != 0);
383675fd0b74Schristos 
383775fd0b74Schristos 		      /* We are turning this relocation into one
383875fd0b74Schristos 			 against a section symbol, so subtract out the
383975fd0b74Schristos 			 output section's address but not the offset
384075fd0b74Schristos 			 of the input section in the output section.  */
384175fd0b74Schristos 		      outrel.r_addend -= osec->vma;
384275fd0b74Schristos 		    }
384375fd0b74Schristos 
384475fd0b74Schristos 		  outrel.r_info = ELF32_R_INFO (indx, r_type);
384575fd0b74Schristos 		}
384675fd0b74Schristos 	      sreloc = elf_section_data (input_section)->sreloc;
384775fd0b74Schristos 	      if (sreloc == NULL)
384875fd0b74Schristos 		abort ();
384975fd0b74Schristos 
385075fd0b74Schristos 	      loc = sreloc->contents;
385175fd0b74Schristos 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
385275fd0b74Schristos 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
385375fd0b74Schristos 	    }
385475fd0b74Schristos 	  break;
385575fd0b74Schristos 
385675fd0b74Schristos 	case R_PARISC_TLS_LDM21L:
385775fd0b74Schristos 	case R_PARISC_TLS_LDM14R:
385875fd0b74Schristos 	  {
385975fd0b74Schristos 	    bfd_vma off;
386075fd0b74Schristos 
386175fd0b74Schristos 	    off = htab->tls_ldm_got.offset;
386275fd0b74Schristos 	    if (off & 1)
386375fd0b74Schristos 	      off &= ~1;
386475fd0b74Schristos 	    else
386575fd0b74Schristos 	      {
386675fd0b74Schristos 		Elf_Internal_Rela outrel;
386775fd0b74Schristos 		bfd_byte *loc;
386875fd0b74Schristos 
386975fd0b74Schristos 		outrel.r_offset = (off
3870ede78133Schristos 				   + htab->etab.sgot->output_section->vma
3871ede78133Schristos 				   + htab->etab.sgot->output_offset);
387275fd0b74Schristos 		outrel.r_addend = 0;
387375fd0b74Schristos 		outrel.r_info = ELF32_R_INFO (0, R_PARISC_TLS_DTPMOD32);
3874ede78133Schristos 		loc = htab->etab.srelgot->contents;
3875ede78133Schristos 		loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
387675fd0b74Schristos 
387775fd0b74Schristos 		bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
387875fd0b74Schristos 		htab->tls_ldm_got.offset |= 1;
387975fd0b74Schristos 	      }
388075fd0b74Schristos 
388175fd0b74Schristos 	    /* Add the base of the GOT to the relocation value.  */
388275fd0b74Schristos 	    relocation = (off
3883ede78133Schristos 			  + htab->etab.sgot->output_offset
3884ede78133Schristos 			  + htab->etab.sgot->output_section->vma);
388575fd0b74Schristos 
388675fd0b74Schristos 	    break;
388775fd0b74Schristos 	  }
388875fd0b74Schristos 
388975fd0b74Schristos 	case R_PARISC_TLS_LDO21L:
389075fd0b74Schristos 	case R_PARISC_TLS_LDO14R:
389175fd0b74Schristos 	  relocation -= dtpoff_base (info);
389275fd0b74Schristos 	  break;
389375fd0b74Schristos 
389475fd0b74Schristos 	case R_PARISC_TLS_GD21L:
389575fd0b74Schristos 	case R_PARISC_TLS_GD14R:
389675fd0b74Schristos 	case R_PARISC_TLS_IE21L:
389775fd0b74Schristos 	case R_PARISC_TLS_IE14R:
389875fd0b74Schristos 	  {
389975fd0b74Schristos 	    bfd_vma off;
390075fd0b74Schristos 	    int indx;
390175fd0b74Schristos 	    char tls_type;
390275fd0b74Schristos 
390375fd0b74Schristos 	    indx = 0;
390475fd0b74Schristos 	    if (hh != NULL)
390575fd0b74Schristos 	      {
3906ede78133Schristos 		if (!htab->etab.dynamic_sections_created
3907ede78133Schristos 		    || hh->eh.dynindx == -1
3908ede78133Schristos 		    || SYMBOL_REFERENCES_LOCAL (info, &hh->eh)
3909ede78133Schristos 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh))
3910ede78133Schristos 		  /* This is actually a static link, or it is a
3911ede78133Schristos 		     -Bsymbolic link and the symbol is defined
3912ede78133Schristos 		     locally, or the symbol was forced to be local
3913ede78133Schristos 		     because of a version file.  */
3914ede78133Schristos 		  ;
3915ede78133Schristos 		else
391675fd0b74Schristos 		  indx = hh->eh.dynindx;
391775fd0b74Schristos 		off = hh->eh.got.offset;
391875fd0b74Schristos 		tls_type = hh->tls_type;
391975fd0b74Schristos 	      }
392075fd0b74Schristos 	    else
392175fd0b74Schristos 	      {
392275fd0b74Schristos 		off = local_got_offsets[r_symndx];
392375fd0b74Schristos 		tls_type = hppa_elf_local_got_tls_type (input_bfd)[r_symndx];
392475fd0b74Schristos 	      }
392575fd0b74Schristos 
392675fd0b74Schristos 	    if (tls_type == GOT_UNKNOWN)
392775fd0b74Schristos 	      abort ();
392875fd0b74Schristos 
392975fd0b74Schristos 	    if ((off & 1) != 0)
393075fd0b74Schristos 	      off &= ~1;
393175fd0b74Schristos 	    else
393275fd0b74Schristos 	      {
3933*e992f068Schristos 		bool need_relocs = false;
393475fd0b74Schristos 		Elf_Internal_Rela outrel;
393575fd0b74Schristos 		bfd_byte *loc = NULL;
393675fd0b74Schristos 		int cur_off = off;
393775fd0b74Schristos 
393875fd0b74Schristos 		/* The GOT entries have not been initialized yet.  Do it
393975fd0b74Schristos 		   now, and emit any relocations.  If both an IE GOT and a
394075fd0b74Schristos 		   GD GOT are necessary, we emit the GD first.  */
394175fd0b74Schristos 
3942ede78133Schristos 		if (indx != 0
3943ede78133Schristos 		    || (bfd_link_dll (info)
394475fd0b74Schristos 			&& (hh == NULL
3945ede78133Schristos 			    || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh))))
394675fd0b74Schristos 		  {
3947*e992f068Schristos 		    need_relocs = true;
3948ede78133Schristos 		    loc = htab->etab.srelgot->contents;
3949ede78133Schristos 		    loc += (htab->etab.srelgot->reloc_count
3950ede78133Schristos 			    * sizeof (Elf32_External_Rela));
395175fd0b74Schristos 		  }
395275fd0b74Schristos 
395375fd0b74Schristos 		if (tls_type & GOT_TLS_GD)
395475fd0b74Schristos 		  {
395575fd0b74Schristos 		    if (need_relocs)
395675fd0b74Schristos 		      {
3957ede78133Schristos 			outrel.r_offset
3958ede78133Schristos 			  = (cur_off
3959ede78133Schristos 			     + htab->etab.sgot->output_section->vma
3960ede78133Schristos 			     + htab->etab.sgot->output_offset);
3961ede78133Schristos 			outrel.r_info
3962ede78133Schristos 			  = ELF32_R_INFO (indx, R_PARISC_TLS_DTPMOD32);
396375fd0b74Schristos 			outrel.r_addend = 0;
396475fd0b74Schristos 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3965ede78133Schristos 			htab->etab.srelgot->reloc_count++;
396675fd0b74Schristos 			loc += sizeof (Elf32_External_Rela);
396775fd0b74Schristos 			bfd_put_32 (output_bfd, 0,
3968ede78133Schristos 				    htab->etab.sgot->contents + cur_off);
396975fd0b74Schristos 		      }
397075fd0b74Schristos 		    else
397175fd0b74Schristos 		      /* If we are not emitting relocations for a
397275fd0b74Schristos 			 general dynamic reference, then we must be in a
397375fd0b74Schristos 			 static link or an executable link with the
397475fd0b74Schristos 			 symbol binding locally.  Mark it as belonging
397575fd0b74Schristos 			 to module 1, the executable.  */
397675fd0b74Schristos 		      bfd_put_32 (output_bfd, 1,
3977ede78133Schristos 				  htab->etab.sgot->contents + cur_off);
3978ede78133Schristos 
3979ede78133Schristos 		    if (indx != 0)
3980ede78133Schristos 		      {
3981ede78133Schristos 			outrel.r_info
3982ede78133Schristos 			  = ELF32_R_INFO (indx, R_PARISC_TLS_DTPOFF32);
3983ede78133Schristos 			outrel.r_offset += 4;
3984ede78133Schristos 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3985ede78133Schristos 			htab->etab.srelgot->reloc_count++;
3986ede78133Schristos 			loc += sizeof (Elf32_External_Rela);
3987ede78133Schristos 			bfd_put_32 (output_bfd, 0,
3988ede78133Schristos 				    htab->etab.sgot->contents + cur_off + 4);
398975fd0b74Schristos 		      }
3990ede78133Schristos 		    else
3991ede78133Schristos 		      bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3992ede78133Schristos 				  htab->etab.sgot->contents + cur_off + 4);
399375fd0b74Schristos 		    cur_off += 8;
399475fd0b74Schristos 		  }
399575fd0b74Schristos 
399675fd0b74Schristos 		if (tls_type & GOT_TLS_IE)
399775fd0b74Schristos 		  {
3998ede78133Schristos 		    if (need_relocs
3999ede78133Schristos 			&& !(bfd_link_executable (info)
4000ede78133Schristos 			     && SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
400175fd0b74Schristos 		      {
4002ede78133Schristos 			outrel.r_offset
4003ede78133Schristos 			  = (cur_off
4004ede78133Schristos 			     + htab->etab.sgot->output_section->vma
4005ede78133Schristos 			     + htab->etab.sgot->output_offset);
4006ede78133Schristos 			outrel.r_info = ELF32_R_INFO (indx,
4007ede78133Schristos 						      R_PARISC_TLS_TPREL32);
400875fd0b74Schristos 			if (indx == 0)
400975fd0b74Schristos 			  outrel.r_addend = relocation - dtpoff_base (info);
401075fd0b74Schristos 			else
401175fd0b74Schristos 			  outrel.r_addend = 0;
401275fd0b74Schristos 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4013ede78133Schristos 			htab->etab.srelgot->reloc_count++;
401475fd0b74Schristos 			loc += sizeof (Elf32_External_Rela);
401575fd0b74Schristos 		      }
401675fd0b74Schristos 		    else
401775fd0b74Schristos 		      bfd_put_32 (output_bfd, tpoff (info, relocation),
4018ede78133Schristos 				  htab->etab.sgot->contents + cur_off);
401975fd0b74Schristos 		    cur_off += 4;
402075fd0b74Schristos 		  }
402175fd0b74Schristos 
402275fd0b74Schristos 		if (hh != NULL)
402375fd0b74Schristos 		  hh->eh.got.offset |= 1;
402475fd0b74Schristos 		else
402575fd0b74Schristos 		  local_got_offsets[r_symndx] |= 1;
402675fd0b74Schristos 	      }
402775fd0b74Schristos 
4028ede78133Schristos 	    if ((tls_type & GOT_NORMAL) != 0
4029ede78133Schristos 		&& (tls_type & (GOT_TLS_GD | GOT_TLS_LDM | GOT_TLS_IE)) != 0)
4030ede78133Schristos 	      {
4031ede78133Schristos 		if (hh != NULL)
4032ede78133Schristos 		  _bfd_error_handler (_("%s has both normal and TLS relocs"),
4033ede78133Schristos 				      hh_name (hh));
4034ede78133Schristos 		else
4035ede78133Schristos 		  {
4036ede78133Schristos 		    Elf_Internal_Sym *isym
4037*e992f068Schristos 		      = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
4038ede78133Schristos 					       input_bfd, r_symndx);
4039ede78133Schristos 		    if (isym == NULL)
4040*e992f068Schristos 		      return false;
4041ede78133Schristos 		    sym_name
4042ede78133Schristos 		      = bfd_elf_string_from_elf_section (input_bfd,
4043ede78133Schristos 							 symtab_hdr->sh_link,
4044ede78133Schristos 							 isym->st_name);
4045ede78133Schristos 		    if (sym_name == NULL)
4046*e992f068Schristos 		      return false;
4047ede78133Schristos 		    if (*sym_name == '\0')
4048012573ebSchristos 		      sym_name = bfd_section_name (sym_sec);
4049ede78133Schristos 		    _bfd_error_handler
4050ede78133Schristos 		      (_("%pB:%s has both normal and TLS relocs"),
4051ede78133Schristos 		       input_bfd, sym_name);
4052ede78133Schristos 		  }
4053ede78133Schristos 		bfd_set_error (bfd_error_bad_value);
4054*e992f068Schristos 		return false;
4055ede78133Schristos 	      }
4056ede78133Schristos 
405775fd0b74Schristos 	    if ((tls_type & GOT_TLS_GD)
405875fd0b74Schristos 		&& r_type != R_PARISC_TLS_GD21L
405975fd0b74Schristos 		&& r_type != R_PARISC_TLS_GD14R)
406075fd0b74Schristos 	      off += 2 * GOT_ENTRY_SIZE;
406175fd0b74Schristos 
406275fd0b74Schristos 	    /* Add the base of the GOT to the relocation value.  */
406375fd0b74Schristos 	    relocation = (off
4064ede78133Schristos 			  + htab->etab.sgot->output_offset
4065ede78133Schristos 			  + htab->etab.sgot->output_section->vma);
406675fd0b74Schristos 
406775fd0b74Schristos 	    break;
406875fd0b74Schristos 	  }
406975fd0b74Schristos 
407075fd0b74Schristos 	case R_PARISC_TLS_LE21L:
407175fd0b74Schristos 	case R_PARISC_TLS_LE14R:
407275fd0b74Schristos 	  {
407375fd0b74Schristos 	    relocation = tpoff (info, relocation);
407475fd0b74Schristos 	    break;
407575fd0b74Schristos 	  }
407675fd0b74Schristos 	  break;
407775fd0b74Schristos 
407875fd0b74Schristos 	default:
407975fd0b74Schristos 	  break;
408075fd0b74Schristos 	}
408175fd0b74Schristos 
408275fd0b74Schristos       rstatus = final_link_relocate (input_section, contents, rela, relocation,
408375fd0b74Schristos 			       htab, sym_sec, hh, info);
408475fd0b74Schristos 
408575fd0b74Schristos       if (rstatus == bfd_reloc_ok)
408675fd0b74Schristos 	continue;
408775fd0b74Schristos 
408875fd0b74Schristos       if (hh != NULL)
408975fd0b74Schristos 	sym_name = hh_name (hh);
409075fd0b74Schristos       else
409175fd0b74Schristos 	{
409275fd0b74Schristos 	  sym_name = bfd_elf_string_from_elf_section (input_bfd,
409375fd0b74Schristos 						      symtab_hdr->sh_link,
409475fd0b74Schristos 						      sym->st_name);
409575fd0b74Schristos 	  if (sym_name == NULL)
4096*e992f068Schristos 	    return false;
409775fd0b74Schristos 	  if (*sym_name == '\0')
4098012573ebSchristos 	    sym_name = bfd_section_name (sym_sec);
409975fd0b74Schristos 	}
410075fd0b74Schristos 
410175fd0b74Schristos       howto = elf_hppa_howto_table + r_type;
410275fd0b74Schristos 
410375fd0b74Schristos       if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
410475fd0b74Schristos 	{
410575fd0b74Schristos 	  if (rstatus == bfd_reloc_notsupported || !warned_undef)
410675fd0b74Schristos 	    {
4107ede78133Schristos 	      _bfd_error_handler
4108ede78133Schristos 		/* xgettext:c-format */
4109ede78133Schristos 		(_("%pB(%pA+%#" PRIx64 "): cannot handle %s for %s"),
411075fd0b74Schristos 		 input_bfd,
411175fd0b74Schristos 		 input_section,
4112ede78133Schristos 		 (uint64_t) rela->r_offset,
411375fd0b74Schristos 		 howto->name,
411475fd0b74Schristos 		 sym_name);
411575fd0b74Schristos 	      bfd_set_error (bfd_error_bad_value);
4116*e992f068Schristos 	      return false;
411775fd0b74Schristos 	    }
411875fd0b74Schristos 	}
411975fd0b74Schristos       else
412075fd0b74Schristos 	(*info->callbacks->reloc_overflow)
412175fd0b74Schristos 	  (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
412275fd0b74Schristos 	   (bfd_vma) 0, input_bfd, input_section, rela->r_offset);
412375fd0b74Schristos     }
412475fd0b74Schristos 
4125*e992f068Schristos   return true;
412675fd0b74Schristos }
412775fd0b74Schristos 
412875fd0b74Schristos /* Finish up dynamic symbol handling.  We set the contents of various
412975fd0b74Schristos    dynamic sections here.  */
413075fd0b74Schristos 
4131*e992f068Schristos static bool
elf32_hppa_finish_dynamic_symbol(bfd * output_bfd,struct bfd_link_info * info,struct elf_link_hash_entry * eh,Elf_Internal_Sym * sym)413275fd0b74Schristos elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
413375fd0b74Schristos 				  struct bfd_link_info *info,
413475fd0b74Schristos 				  struct elf_link_hash_entry *eh,
413575fd0b74Schristos 				  Elf_Internal_Sym *sym)
413675fd0b74Schristos {
413775fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
413875fd0b74Schristos   Elf_Internal_Rela rela;
413975fd0b74Schristos   bfd_byte *loc;
414075fd0b74Schristos 
414175fd0b74Schristos   htab = hppa_link_hash_table (info);
414275fd0b74Schristos   if (htab == NULL)
4143*e992f068Schristos     return false;
414475fd0b74Schristos 
414575fd0b74Schristos   if (eh->plt.offset != (bfd_vma) -1)
414675fd0b74Schristos     {
414775fd0b74Schristos       bfd_vma value;
414875fd0b74Schristos 
414975fd0b74Schristos       if (eh->plt.offset & 1)
415075fd0b74Schristos 	abort ();
415175fd0b74Schristos 
415275fd0b74Schristos       /* This symbol has an entry in the procedure linkage table.  Set
415375fd0b74Schristos 	 it up.
415475fd0b74Schristos 
415575fd0b74Schristos 	 The format of a plt entry is
415675fd0b74Schristos 	 <funcaddr>
415775fd0b74Schristos 	 <__gp>
415875fd0b74Schristos       */
415975fd0b74Schristos       value = 0;
416075fd0b74Schristos       if (eh->root.type == bfd_link_hash_defined
416175fd0b74Schristos 	  || eh->root.type == bfd_link_hash_defweak)
416275fd0b74Schristos 	{
416375fd0b74Schristos 	  value = eh->root.u.def.value;
416475fd0b74Schristos 	  if (eh->root.u.def.section->output_section != NULL)
416575fd0b74Schristos 	    value += (eh->root.u.def.section->output_offset
416675fd0b74Schristos 		      + eh->root.u.def.section->output_section->vma);
416775fd0b74Schristos 	}
416875fd0b74Schristos 
416975fd0b74Schristos       /* Create a dynamic IPLT relocation for this entry.  */
417075fd0b74Schristos       rela.r_offset = (eh->plt.offset
4171ede78133Schristos 		      + htab->etab.splt->output_offset
4172ede78133Schristos 		      + htab->etab.splt->output_section->vma);
417375fd0b74Schristos       if (eh->dynindx != -1)
417475fd0b74Schristos 	{
417575fd0b74Schristos 	  rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
417675fd0b74Schristos 	  rela.r_addend = 0;
417775fd0b74Schristos 	}
417875fd0b74Schristos       else
417975fd0b74Schristos 	{
418075fd0b74Schristos 	  /* This symbol has been marked to become local, and is
418175fd0b74Schristos 	     used by a plabel so must be kept in the .plt.  */
418275fd0b74Schristos 	  rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
418375fd0b74Schristos 	  rela.r_addend = value;
418475fd0b74Schristos 	}
418575fd0b74Schristos 
4186ede78133Schristos       loc = htab->etab.srelplt->contents;
4187ede78133Schristos       loc += htab->etab.srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
4188ede78133Schristos       bfd_elf32_swap_reloca_out (htab->etab.splt->output_section->owner, &rela, loc);
418975fd0b74Schristos 
419075fd0b74Schristos       if (!eh->def_regular)
419175fd0b74Schristos 	{
419275fd0b74Schristos 	  /* Mark the symbol as undefined, rather than as defined in
419375fd0b74Schristos 	     the .plt section.  Leave the value alone.  */
419475fd0b74Schristos 	  sym->st_shndx = SHN_UNDEF;
419575fd0b74Schristos 	}
419675fd0b74Schristos     }
419775fd0b74Schristos 
419875fd0b74Schristos   if (eh->got.offset != (bfd_vma) -1
4199ede78133Schristos       && (hppa_elf_hash_entry (eh)->tls_type & GOT_NORMAL) != 0
4200ede78133Schristos       && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
420175fd0b74Schristos     {
4202*e992f068Schristos       bool is_dyn = (eh->dynindx != -1
4203ede78133Schristos 		     && !SYMBOL_REFERENCES_LOCAL (info, eh));
4204ede78133Schristos 
4205ede78133Schristos       if (is_dyn || bfd_link_pic (info))
4206ede78133Schristos 	{
4207ede78133Schristos 	  /* This symbol has an entry in the global offset table.  Set
4208ede78133Schristos 	     it up.  */
420975fd0b74Schristos 
421075fd0b74Schristos 	  rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
4211ede78133Schristos 			   + htab->etab.sgot->output_offset
4212ede78133Schristos 			   + htab->etab.sgot->output_section->vma);
421375fd0b74Schristos 
421475fd0b74Schristos 	  /* If this is a -Bsymbolic link and the symbol is defined
4215ede78133Schristos 	     locally or was forced to be local because of a version
4216ede78133Schristos 	     file, we just want to emit a RELATIVE reloc.  The entry
4217ede78133Schristos 	     in the global offset table will already have been
4218ede78133Schristos 	     initialized in the relocate_section function.  */
4219ede78133Schristos 	  if (!is_dyn)
422075fd0b74Schristos 	    {
422175fd0b74Schristos 	      rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
422275fd0b74Schristos 	      rela.r_addend = (eh->root.u.def.value
422375fd0b74Schristos 			       + eh->root.u.def.section->output_offset
422475fd0b74Schristos 			       + eh->root.u.def.section->output_section->vma);
422575fd0b74Schristos 	    }
422675fd0b74Schristos 	  else
422775fd0b74Schristos 	    {
422875fd0b74Schristos 	      if ((eh->got.offset & 1) != 0)
422975fd0b74Schristos 		abort ();
423075fd0b74Schristos 
4231ede78133Schristos 	      bfd_put_32 (output_bfd, 0,
4232ede78133Schristos 			  htab->etab.sgot->contents + (eh->got.offset & ~1));
423375fd0b74Schristos 	      rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
423475fd0b74Schristos 	      rela.r_addend = 0;
423575fd0b74Schristos 	    }
423675fd0b74Schristos 
4237ede78133Schristos 	  loc = htab->etab.srelgot->contents;
4238ede78133Schristos 	  loc += (htab->etab.srelgot->reloc_count++
4239ede78133Schristos 		  * sizeof (Elf32_External_Rela));
424075fd0b74Schristos 	  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
424175fd0b74Schristos 	}
4242ede78133Schristos     }
424375fd0b74Schristos 
424475fd0b74Schristos   if (eh->needs_copy)
424575fd0b74Schristos     {
424675fd0b74Schristos       asection *sec;
424775fd0b74Schristos 
424875fd0b74Schristos       /* This symbol needs a copy reloc.  Set it up.  */
424975fd0b74Schristos 
425075fd0b74Schristos       if (! (eh->dynindx != -1
425175fd0b74Schristos 	     && (eh->root.type == bfd_link_hash_defined
425275fd0b74Schristos 		 || eh->root.type == bfd_link_hash_defweak)))
425375fd0b74Schristos 	abort ();
425475fd0b74Schristos 
425575fd0b74Schristos       rela.r_offset = (eh->root.u.def.value
425675fd0b74Schristos 		      + eh->root.u.def.section->output_offset
425775fd0b74Schristos 		      + eh->root.u.def.section->output_section->vma);
425875fd0b74Schristos       rela.r_addend = 0;
425975fd0b74Schristos       rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
4260ede78133Schristos       if (eh->root.u.def.section == htab->etab.sdynrelro)
4261ede78133Schristos 	sec = htab->etab.sreldynrelro;
4262ede78133Schristos       else
4263ede78133Schristos 	sec = htab->etab.srelbss;
426475fd0b74Schristos       loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
426575fd0b74Schristos       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
426675fd0b74Schristos     }
426775fd0b74Schristos 
426875fd0b74Schristos   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
426975fd0b74Schristos   if (eh == htab->etab.hdynamic || eh == htab->etab.hgot)
427075fd0b74Schristos     {
427175fd0b74Schristos       sym->st_shndx = SHN_ABS;
427275fd0b74Schristos     }
427375fd0b74Schristos 
4274*e992f068Schristos   return true;
427575fd0b74Schristos }
427675fd0b74Schristos 
427775fd0b74Schristos /* Used to decide how to sort relocs in an optimal manner for the
427875fd0b74Schristos    dynamic linker, before writing them out.  */
427975fd0b74Schristos 
428075fd0b74Schristos static enum elf_reloc_type_class
elf32_hppa_reloc_type_class(const struct bfd_link_info * info ATTRIBUTE_UNUSED,const asection * rel_sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * rela)428175fd0b74Schristos elf32_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
428275fd0b74Schristos 			     const asection *rel_sec ATTRIBUTE_UNUSED,
428375fd0b74Schristos 			     const Elf_Internal_Rela *rela)
428475fd0b74Schristos {
428575fd0b74Schristos   /* Handle TLS relocs first; we don't want them to be marked
428675fd0b74Schristos      relative by the "if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)"
428775fd0b74Schristos      check below.  */
428875fd0b74Schristos   switch ((int) ELF32_R_TYPE (rela->r_info))
428975fd0b74Schristos     {
429075fd0b74Schristos       case R_PARISC_TLS_DTPMOD32:
429175fd0b74Schristos       case R_PARISC_TLS_DTPOFF32:
429275fd0b74Schristos       case R_PARISC_TLS_TPREL32:
429375fd0b74Schristos 	return reloc_class_normal;
429475fd0b74Schristos     }
429575fd0b74Schristos 
429675fd0b74Schristos   if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)
429775fd0b74Schristos     return reloc_class_relative;
429875fd0b74Schristos 
429975fd0b74Schristos   switch ((int) ELF32_R_TYPE (rela->r_info))
430075fd0b74Schristos     {
430175fd0b74Schristos     case R_PARISC_IPLT:
430275fd0b74Schristos       return reloc_class_plt;
430375fd0b74Schristos     case R_PARISC_COPY:
430475fd0b74Schristos       return reloc_class_copy;
430575fd0b74Schristos     default:
430675fd0b74Schristos       return reloc_class_normal;
430775fd0b74Schristos     }
430875fd0b74Schristos }
430975fd0b74Schristos 
431075fd0b74Schristos /* Finish up the dynamic sections.  */
431175fd0b74Schristos 
4312*e992f068Schristos static bool
elf32_hppa_finish_dynamic_sections(bfd * output_bfd,struct bfd_link_info * info)431375fd0b74Schristos elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
431475fd0b74Schristos 				    struct bfd_link_info *info)
431575fd0b74Schristos {
431675fd0b74Schristos   bfd *dynobj;
431775fd0b74Schristos   struct elf32_hppa_link_hash_table *htab;
431875fd0b74Schristos   asection *sdyn;
431975fd0b74Schristos   asection * sgot;
432075fd0b74Schristos 
432175fd0b74Schristos   htab = hppa_link_hash_table (info);
432275fd0b74Schristos   if (htab == NULL)
4323*e992f068Schristos     return false;
432475fd0b74Schristos 
432575fd0b74Schristos   dynobj = htab->etab.dynobj;
432675fd0b74Schristos 
4327ede78133Schristos   sgot = htab->etab.sgot;
432875fd0b74Schristos   /* A broken linker script might have discarded the dynamic sections.
432975fd0b74Schristos      Catch this here so that we do not seg-fault later on.  */
433075fd0b74Schristos   if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
4331*e992f068Schristos     return false;
433275fd0b74Schristos 
433375fd0b74Schristos   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
433475fd0b74Schristos 
433575fd0b74Schristos   if (htab->etab.dynamic_sections_created)
433675fd0b74Schristos     {
433775fd0b74Schristos       Elf32_External_Dyn *dyncon, *dynconend;
433875fd0b74Schristos 
433975fd0b74Schristos       if (sdyn == NULL)
434075fd0b74Schristos 	abort ();
434175fd0b74Schristos 
434275fd0b74Schristos       dyncon = (Elf32_External_Dyn *) sdyn->contents;
434375fd0b74Schristos       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
434475fd0b74Schristos       for (; dyncon < dynconend; dyncon++)
434575fd0b74Schristos 	{
434675fd0b74Schristos 	  Elf_Internal_Dyn dyn;
434775fd0b74Schristos 	  asection *s;
434875fd0b74Schristos 
434975fd0b74Schristos 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
435075fd0b74Schristos 
435175fd0b74Schristos 	  switch (dyn.d_tag)
435275fd0b74Schristos 	    {
435375fd0b74Schristos 	    default:
435475fd0b74Schristos 	      continue;
435575fd0b74Schristos 
435675fd0b74Schristos 	    case DT_PLTGOT:
435775fd0b74Schristos 	      /* Use PLTGOT to set the GOT register.  */
435875fd0b74Schristos 	      dyn.d_un.d_ptr = elf_gp (output_bfd);
435975fd0b74Schristos 	      break;
436075fd0b74Schristos 
436175fd0b74Schristos 	    case DT_JMPREL:
4362ede78133Schristos 	      s = htab->etab.srelplt;
436375fd0b74Schristos 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
436475fd0b74Schristos 	      break;
436575fd0b74Schristos 
436675fd0b74Schristos 	    case DT_PLTRELSZ:
4367ede78133Schristos 	      s = htab->etab.srelplt;
436875fd0b74Schristos 	      dyn.d_un.d_val = s->size;
436975fd0b74Schristos 	      break;
437075fd0b74Schristos 	    }
437175fd0b74Schristos 
437275fd0b74Schristos 	  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
437375fd0b74Schristos 	}
437475fd0b74Schristos     }
437575fd0b74Schristos 
437675fd0b74Schristos   if (sgot != NULL && sgot->size != 0)
437775fd0b74Schristos     {
437875fd0b74Schristos       /* Fill in the first entry in the global offset table.
437975fd0b74Schristos 	 We use it to point to our dynamic section, if we have one.  */
438075fd0b74Schristos       bfd_put_32 (output_bfd,
438175fd0b74Schristos 		  sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
438275fd0b74Schristos 		  sgot->contents);
438375fd0b74Schristos 
438475fd0b74Schristos       /* The second entry is reserved for use by the dynamic linker.  */
438575fd0b74Schristos       memset (sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
438675fd0b74Schristos 
438775fd0b74Schristos       /* Set .got entry size.  */
438875fd0b74Schristos       elf_section_data (sgot->output_section)
438975fd0b74Schristos 	->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
439075fd0b74Schristos     }
439175fd0b74Schristos 
4392ede78133Schristos   if (htab->etab.splt != NULL && htab->etab.splt->size != 0)
439375fd0b74Schristos     {
439475fd0b74Schristos       /* Set plt entry size to 0 instead of PLT_ENTRY_SIZE, since we add the
439575fd0b74Schristos 	 plt stubs and as such the section does not hold a table of fixed-size
439675fd0b74Schristos 	 entries.  */
4397ede78133Schristos       elf_section_data (htab->etab.splt->output_section)->this_hdr.sh_entsize = 0;
439875fd0b74Schristos 
439975fd0b74Schristos       if (htab->need_plt_stub)
440075fd0b74Schristos 	{
440175fd0b74Schristos 	  /* Set up the .plt stub.  */
4402ede78133Schristos 	  memcpy (htab->etab.splt->contents
4403ede78133Schristos 		  + htab->etab.splt->size - sizeof (plt_stub),
440475fd0b74Schristos 		  plt_stub, sizeof (plt_stub));
440575fd0b74Schristos 
4406ede78133Schristos 	  if ((htab->etab.splt->output_offset
4407ede78133Schristos 	       + htab->etab.splt->output_section->vma
4408ede78133Schristos 	       + htab->etab.splt->size)
440975fd0b74Schristos 	      != (sgot->output_offset
441075fd0b74Schristos 		  + sgot->output_section->vma))
441175fd0b74Schristos 	    {
4412ede78133Schristos 	      _bfd_error_handler
441375fd0b74Schristos 		(_(".got section not immediately after .plt section"));
4414*e992f068Schristos 	      return false;
441575fd0b74Schristos 	    }
441675fd0b74Schristos 	}
441775fd0b74Schristos     }
441875fd0b74Schristos 
4419*e992f068Schristos   return true;
442075fd0b74Schristos }
442175fd0b74Schristos 
442275fd0b74Schristos /* Called when writing out an object file to decide the type of a
442375fd0b74Schristos    symbol.  */
442475fd0b74Schristos static int
elf32_hppa_elf_get_symbol_type(Elf_Internal_Sym * elf_sym,int type)442575fd0b74Schristos elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
442675fd0b74Schristos {
442775fd0b74Schristos   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
442875fd0b74Schristos     return STT_PARISC_MILLI;
442975fd0b74Schristos   else
443075fd0b74Schristos     return type;
443175fd0b74Schristos }
443275fd0b74Schristos 
443375fd0b74Schristos /* Misc BFD support code.  */
443475fd0b74Schristos #define bfd_elf32_bfd_is_local_label_name    elf_hppa_is_local_label_name
443575fd0b74Schristos #define bfd_elf32_bfd_reloc_type_lookup	     elf_hppa_reloc_type_lookup
443675fd0b74Schristos #define bfd_elf32_bfd_reloc_name_lookup      elf_hppa_reloc_name_lookup
443775fd0b74Schristos #define elf_info_to_howto		     elf_hppa_info_to_howto
443875fd0b74Schristos #define elf_info_to_howto_rel		     elf_hppa_info_to_howto_rel
443975fd0b74Schristos 
444075fd0b74Schristos /* Stuff for the BFD linker.  */
444175fd0b74Schristos #define bfd_elf32_bfd_final_link	     elf32_hppa_final_link
444275fd0b74Schristos #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
444375fd0b74Schristos #define elf_backend_adjust_dynamic_symbol    elf32_hppa_adjust_dynamic_symbol
444475fd0b74Schristos #define elf_backend_copy_indirect_symbol     elf32_hppa_copy_indirect_symbol
444575fd0b74Schristos #define elf_backend_check_relocs	     elf32_hppa_check_relocs
4446ede78133Schristos #define elf_backend_relocs_compatible	     _bfd_elf_relocs_compatible
444775fd0b74Schristos #define elf_backend_create_dynamic_sections  elf32_hppa_create_dynamic_sections
444875fd0b74Schristos #define elf_backend_fake_sections	     elf_hppa_fake_sections
444975fd0b74Schristos #define elf_backend_relocate_section	     elf32_hppa_relocate_section
445075fd0b74Schristos #define elf_backend_hide_symbol		     elf32_hppa_hide_symbol
445175fd0b74Schristos #define elf_backend_finish_dynamic_symbol    elf32_hppa_finish_dynamic_symbol
445275fd0b74Schristos #define elf_backend_finish_dynamic_sections  elf32_hppa_finish_dynamic_sections
445375fd0b74Schristos #define elf_backend_size_dynamic_sections    elf32_hppa_size_dynamic_sections
445475fd0b74Schristos #define elf_backend_init_index_section	     _bfd_elf_init_1_index_section
445575fd0b74Schristos #define elf_backend_gc_mark_hook	     elf32_hppa_gc_mark_hook
445675fd0b74Schristos #define elf_backend_grok_prstatus	     elf32_hppa_grok_prstatus
445775fd0b74Schristos #define elf_backend_grok_psinfo		     elf32_hppa_grok_psinfo
445875fd0b74Schristos #define elf_backend_object_p		     elf32_hppa_object_p
445975fd0b74Schristos #define elf_backend_final_write_processing   elf_hppa_final_write_processing
446075fd0b74Schristos #define elf_backend_get_symbol_type	     elf32_hppa_elf_get_symbol_type
446175fd0b74Schristos #define elf_backend_reloc_type_class	     elf32_hppa_reloc_type_class
446275fd0b74Schristos #define elf_backend_action_discarded	     elf_hppa_action_discarded
446375fd0b74Schristos 
446475fd0b74Schristos #define elf_backend_can_gc_sections	     1
446575fd0b74Schristos #define elf_backend_can_refcount	     1
446675fd0b74Schristos #define elf_backend_plt_alignment	     2
446775fd0b74Schristos #define elf_backend_want_got_plt	     0
446875fd0b74Schristos #define elf_backend_plt_readonly	     0
446975fd0b74Schristos #define elf_backend_want_plt_sym	     0
447075fd0b74Schristos #define elf_backend_got_header_size	     8
4471ede78133Schristos #define elf_backend_want_dynrelro	     1
447275fd0b74Schristos #define elf_backend_rela_normal		     1
4473ede78133Schristos #define elf_backend_dtrel_excludes_plt	     1
4474ede78133Schristos #define elf_backend_no_page_alias	     1
447575fd0b74Schristos 
447675fd0b74Schristos #define TARGET_BIG_SYM		hppa_elf32_vec
447775fd0b74Schristos #define TARGET_BIG_NAME		"elf32-hppa"
447875fd0b74Schristos #define ELF_ARCH		bfd_arch_hppa
447975fd0b74Schristos #define ELF_TARGET_ID		HPPA32_ELF_DATA
448075fd0b74Schristos #define ELF_MACHINE_CODE	EM_PARISC
448175fd0b74Schristos #define ELF_MAXPAGESIZE		0x1000
448275fd0b74Schristos #define ELF_OSABI		ELFOSABI_HPUX
448375fd0b74Schristos #define elf32_bed		elf32_hppa_hpux_bed
448475fd0b74Schristos 
448575fd0b74Schristos #include "elf32-target.h"
448675fd0b74Schristos 
448775fd0b74Schristos #undef TARGET_BIG_SYM
448875fd0b74Schristos #define TARGET_BIG_SYM		hppa_elf32_linux_vec
448975fd0b74Schristos #undef TARGET_BIG_NAME
449075fd0b74Schristos #define TARGET_BIG_NAME		"elf32-hppa-linux"
449175fd0b74Schristos #undef ELF_OSABI
449275fd0b74Schristos #define ELF_OSABI		ELFOSABI_GNU
449375fd0b74Schristos #undef elf32_bed
449475fd0b74Schristos #define elf32_bed		elf32_hppa_linux_bed
449575fd0b74Schristos 
449675fd0b74Schristos #include "elf32-target.h"
449775fd0b74Schristos 
449875fd0b74Schristos #undef TARGET_BIG_SYM
449975fd0b74Schristos #define TARGET_BIG_SYM		hppa_elf32_nbsd_vec
450075fd0b74Schristos #undef TARGET_BIG_NAME
450175fd0b74Schristos #define TARGET_BIG_NAME		"elf32-hppa-netbsd"
450275fd0b74Schristos #undef ELF_OSABI
450375fd0b74Schristos #define ELF_OSABI		ELFOSABI_NETBSD
450475fd0b74Schristos #undef elf32_bed
450575fd0b74Schristos #define elf32_bed		elf32_hppa_netbsd_bed
450675fd0b74Schristos 
450775fd0b74Schristos #include "elf32-target.h"
4508