2010-05-14 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / bfd / elfxx-ia64.c
CommitLineData
800eeca4 1/* IA-64 support for 64-bit ELF
01e1a5bc 2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4dfe6ac6 3 2008, 2009, 2010 Free Software Foundation, Inc.
800eeca4
JW
4 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5
5e8d7549 6 This file is part of BFD, the Binary File Descriptor library.
800eeca4 7
5e8d7549
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
5e8d7549 11 (at your option) any later version.
800eeca4 12
5e8d7549
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
800eeca4 17
5e8d7549
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
800eeca4 22
800eeca4 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
800eeca4
JW
25#include "libbfd.h"
26#include "elf-bfd.h"
27#include "opcode/ia64.h"
28#include "elf/ia64.h"
0aa92b58
JJ
29#include "objalloc.h"
30#include "hashtab.h"
800eeca4 31
5a260b66
L
32#define ARCH_SIZE NN
33
34#if ARCH_SIZE == 64
35#define LOG_SECTION_ALIGN 3
36#endif
37
38#if ARCH_SIZE == 32
39#define LOG_SECTION_ALIGN 2
40#endif
41
5e8d7549 42/* THE RULES for all the stuff the linker creates --
b34976b6 43
5e8d7549
NC
44 GOT Entries created in response to LTOFF or LTOFF_FPTR
45 relocations. Dynamic relocs created for dynamic
46 symbols in an application; REL relocs for locals
47 in a shared library.
b34976b6 48
5e8d7549
NC
49 FPTR The canonical function descriptor. Created for local
50 symbols in applications. Descriptors for dynamic symbols
51 and local symbols in shared libraries are created by
52 ld.so. Thus there are no dynamic relocs against these
53 objects. The FPTR relocs for such _are_ passed through
54 to the dynamic relocation tables.
b34976b6 55
5e8d7549
NC
56 FULL_PLT Created for a PCREL21B relocation against a dynamic symbol.
57 Requires the creation of a PLTOFF entry. This does not
58 require any dynamic relocations.
b34976b6 59
5e8d7549
NC
60 PLTOFF Created by PLTOFF relocations. For local symbols, this
61 is an alternate function descriptor, and in shared libraries
62 requires two REL relocations. Note that this cannot be
63 transformed into an FPTR relocation, since it must be in
64 range of the GP. For dynamic symbols, this is a function
65 descriptor for a MIN_PLT entry, and requires one IPLT reloc.
b34976b6 66
5e8d7549 67 MIN_PLT Created by PLTOFF entries against dynamic symbols. This
4cc11e76 68 does not require dynamic relocations. */
800eeca4 69
7256a114
TG
70/* Only add code for vms when the vms target is enabled. This is required
71 because it depends on vms-lib.c for its archive format and we don't want
72 to compile that code if it is not used. */
73#if ARCH_SIZE == 64 && \
74 (defined (HAVE_bfd_elf64_ia64_vms_vec) || defined (HAVE_all_vecs))
75#define INCLUDE_IA64_VMS
76#endif
77
78
800eeca4
JW
79#define NELEMS(a) ((int) (sizeof (a) / sizeof ((a)[0])))
80
81typedef struct bfd_hash_entry *(*new_hash_entry_func)
4dfe6ac6 82 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
800eeca4
JW
83
84/* In dynamically (linker-) created sections, we generally need to keep track
85 of the place a symbol or expression got allocated to. This is done via hash
86 tables that store entries of the following type. */
87
bbe66d08 88struct elfNN_ia64_dyn_sym_info
800eeca4
JW
89{
90 /* The addend for which this entry is relevant. */
91 bfd_vma addend;
92
800eeca4
JW
93 bfd_vma got_offset;
94 bfd_vma fptr_offset;
95 bfd_vma pltoff_offset;
96 bfd_vma plt_offset;
97 bfd_vma plt2_offset;
13ae64f3
JJ
98 bfd_vma tprel_offset;
99 bfd_vma dtpmod_offset;
100 bfd_vma dtprel_offset;
800eeca4 101
4cc11e76 102 /* The symbol table entry, if any, that this was derived from. */
800eeca4 103 struct elf_link_hash_entry *h;
3e932841 104
800eeca4
JW
105 /* Used to count non-got, non-plt relocations for delayed sizing
106 of relocation sections. */
bbe66d08 107 struct elfNN_ia64_dyn_reloc_entry
800eeca4 108 {
bbe66d08 109 struct elfNN_ia64_dyn_reloc_entry *next;
800eeca4
JW
110 asection *srel;
111 int type;
112 int count;
ac33696c
L
113
114 /* Is this reloc against readonly section? */
115 bfd_boolean reltext;
800eeca4
JW
116 } *reloc_entries;
117
b34976b6 118 /* TRUE when the section contents have been updated. */
800eeca4
JW
119 unsigned got_done : 1;
120 unsigned fptr_done : 1;
121 unsigned pltoff_done : 1;
13ae64f3
JJ
122 unsigned tprel_done : 1;
123 unsigned dtpmod_done : 1;
124 unsigned dtprel_done : 1;
800eeca4 125
b34976b6 126 /* TRUE for the different kinds of linker data we want created. */
800eeca4 127 unsigned want_got : 1;
2c4c2bc0 128 unsigned want_gotx : 1;
800eeca4
JW
129 unsigned want_fptr : 1;
130 unsigned want_ltoff_fptr : 1;
131 unsigned want_plt : 1;
132 unsigned want_plt2 : 1;
133 unsigned want_pltoff : 1;
13ae64f3
JJ
134 unsigned want_tprel : 1;
135 unsigned want_dtpmod : 1;
136 unsigned want_dtprel : 1;
800eeca4
JW
137};
138
bbe66d08 139struct elfNN_ia64_local_hash_entry
800eeca4 140{
0aa92b58
JJ
141 int id;
142 unsigned int r_sym;
396a682d
L
143 /* The number of elements in elfNN_ia64_dyn_sym_info array. */
144 unsigned int count;
145 /* The number of sorted elements in elfNN_ia64_dyn_sym_info array. */
146 unsigned int sorted_count;
147 /* The size of elfNN_ia64_dyn_sym_info array. */
148 unsigned int size;
149 /* The array of elfNN_ia64_dyn_sym_info. */
bbe66d08 150 struct elfNN_ia64_dyn_sym_info *info;
f7460f5f 151
b34976b6 152 /* TRUE if this hash entry's addends was translated for
f7460f5f
JJ
153 SHF_MERGE optimization. */
154 unsigned sec_merge_done : 1;
800eeca4
JW
155};
156
bbe66d08 157struct elfNN_ia64_link_hash_entry
800eeca4
JW
158{
159 struct elf_link_hash_entry root;
396a682d
L
160 /* The number of elements in elfNN_ia64_dyn_sym_info array. */
161 unsigned int count;
162 /* The number of sorted elements in elfNN_ia64_dyn_sym_info array. */
163 unsigned int sorted_count;
164 /* The size of elfNN_ia64_dyn_sym_info array. */
165 unsigned int size;
166 /* The array of elfNN_ia64_dyn_sym_info. */
bbe66d08 167 struct elfNN_ia64_dyn_sym_info *info;
800eeca4
JW
168};
169
bbe66d08 170struct elfNN_ia64_link_hash_table
800eeca4 171{
5e8d7549 172 /* The main hash table. */
800eeca4
JW
173 struct elf_link_hash_table root;
174
4dfe6ac6
NC
175 asection *fptr_sec; /* Function descriptor table (or NULL). */
176 asection *rel_fptr_sec; /* Dynamic relocation section for same. */
177 asection *pltoff_sec; /* Private descriptors for plt (or NULL). */
178 asection *rel_pltoff_sec; /* Dynamic relocation section for same. */
800eeca4 179
4dfe6ac6
NC
180 bfd_size_type minplt_entries; /* Number of minplt entries. */
181 unsigned reltext : 1; /* Are there relocs against readonly sections? */
182 unsigned self_dtpmod_done : 1;/* Has self DTPMOD entry been finished? */
183 bfd_vma self_dtpmod_offset; /* .got offset to self DTPMOD entry. */
a231ddc0
L
184 /* There are maybe R_IA64_GPREL22 relocations, including those
185 optimized from R_IA64_LTOFF22X, against non-SHF_IA_64_SHORT
186 sections. We need to record those sections so that we can choose
187 a proper GP to cover all R_IA64_GPREL22 relocations. */
4dfe6ac6
NC
188 asection *max_short_sec; /* Maximum short output section. */
189 bfd_vma max_short_offset; /* Maximum short offset. */
190 asection *min_short_sec; /* Minimum short output section. */
191 bfd_vma min_short_offset; /* Minimum short offset. */
800eeca4 192
0aa92b58
JJ
193 htab_t loc_hash_table;
194 void *loc_hash_memory;
800eeca4
JW
195};
196
2c4c2bc0
RH
197struct elfNN_ia64_allocate_data
198{
199 struct bfd_link_info *info;
200 bfd_size_type ofs;
4a78a1f4 201 bfd_boolean only_got;
2c4c2bc0
RH
202};
203
bbe66d08 204#define elfNN_ia64_hash_table(p) \
4dfe6ac6
NC
205 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
206 == IA64_ELF_DATA ? ((struct elfNN_ia64_link_hash_table *) ((p)->hash)) : NULL)
800eeca4 207
eae50df2
L
208static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
209 (struct elfNN_ia64_link_hash_table *ia64_info,
210 struct elf_link_hash_entry *h,
211 bfd *abfd, const Elf_Internal_Rela *rel, bfd_boolean create);
b34976b6 212static bfd_boolean elfNN_ia64_dynamic_symbol_p
eae50df2
L
213 (struct elf_link_hash_entry *h, struct bfd_link_info *info, int);
214static bfd_reloc_status_type elfNN_ia64_install_value
215 (bfd_byte *hit_addr, bfd_vma val, unsigned int r_type);
216static bfd_boolean elfNN_ia64_choose_gp
217 (bfd *abfd, struct bfd_link_info *info);
218static void elfNN_ia64_relax_ldxmov
219 (bfd_byte *contents, bfd_vma off);
bbe66d08 220static void elfNN_ia64_dyn_sym_traverse
eae50df2
L
221 (struct elfNN_ia64_link_hash_table *ia64_info,
222 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
223 PTR info);
b34976b6 224static bfd_boolean allocate_global_data_got
eae50df2 225 (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
b34976b6 226static bfd_boolean allocate_global_fptr_got
eae50df2 227 (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
b34976b6 228static bfd_boolean allocate_local_got
eae50df2 229 (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
b34976b6 230static bfd_boolean elfNN_ia64_hpux_vec
eae50df2
L
231 (const bfd_target *vec);
232static bfd_boolean allocate_dynrel_entries
233 (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data);
234static asection *get_pltoff
235 (bfd *abfd, struct bfd_link_info *info,
236 struct elfNN_ia64_link_hash_table *ia64_info);
800eeca4 237\f
5e8d7549 238/* ia64-specific relocation. */
800eeca4
JW
239
240/* Perform a relocation. Not much to do here as all the hard work is
bbe66d08 241 done in elfNN_ia64_final_link_relocate. */
800eeca4 242static bfd_reloc_status_type
eae50df2
L
243elfNN_ia64_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
244 asymbol *sym ATTRIBUTE_UNUSED,
245 PTR data ATTRIBUTE_UNUSED, asection *input_section,
246 bfd *output_bfd, char **error_message)
800eeca4
JW
247{
248 if (output_bfd)
249 {
250 reloc->address += input_section->output_offset;
251 return bfd_reloc_ok;
252 }
6e84a906
DJ
253
254 if (input_section->flags & SEC_DEBUGGING)
255 return bfd_reloc_continue;
256
bbe66d08 257 *error_message = "Unsupported call to elfNN_ia64_reloc";
800eeca4
JW
258 return bfd_reloc_notsupported;
259}
260
261#define IA64_HOWTO(TYPE, NAME, SIZE, PCREL, IN) \
262 HOWTO (TYPE, 0, SIZE, 0, PCREL, 0, complain_overflow_signed, \
eff26f78 263 elfNN_ia64_reloc, NAME, FALSE, 0, -1, IN)
800eeca4
JW
264
265/* This table has to be sorted according to increasing number of the
266 TYPE field. */
267static reloc_howto_type ia64_howto_table[] =
268 {
b34976b6
AM
269 IA64_HOWTO (R_IA64_NONE, "NONE", 0, FALSE, TRUE),
270
271 IA64_HOWTO (R_IA64_IMM14, "IMM14", 0, FALSE, TRUE),
272 IA64_HOWTO (R_IA64_IMM22, "IMM22", 0, FALSE, TRUE),
273 IA64_HOWTO (R_IA64_IMM64, "IMM64", 0, FALSE, TRUE),
274 IA64_HOWTO (R_IA64_DIR32MSB, "DIR32MSB", 2, FALSE, TRUE),
275 IA64_HOWTO (R_IA64_DIR32LSB, "DIR32LSB", 2, FALSE, TRUE),
276 IA64_HOWTO (R_IA64_DIR64MSB, "DIR64MSB", 4, FALSE, TRUE),
277 IA64_HOWTO (R_IA64_DIR64LSB, "DIR64LSB", 4, FALSE, TRUE),
278
279 IA64_HOWTO (R_IA64_GPREL22, "GPREL22", 0, FALSE, TRUE),
280 IA64_HOWTO (R_IA64_GPREL64I, "GPREL64I", 0, FALSE, TRUE),
281 IA64_HOWTO (R_IA64_GPREL32MSB, "GPREL32MSB", 2, FALSE, TRUE),
282 IA64_HOWTO (R_IA64_GPREL32LSB, "GPREL32LSB", 2, FALSE, TRUE),
283 IA64_HOWTO (R_IA64_GPREL64MSB, "GPREL64MSB", 4, FALSE, TRUE),
284 IA64_HOWTO (R_IA64_GPREL64LSB, "GPREL64LSB", 4, FALSE, TRUE),
285
286 IA64_HOWTO (R_IA64_LTOFF22, "LTOFF22", 0, FALSE, TRUE),
287 IA64_HOWTO (R_IA64_LTOFF64I, "LTOFF64I", 0, FALSE, TRUE),
288
289 IA64_HOWTO (R_IA64_PLTOFF22, "PLTOFF22", 0, FALSE, TRUE),
290 IA64_HOWTO (R_IA64_PLTOFF64I, "PLTOFF64I", 0, FALSE, TRUE),
291 IA64_HOWTO (R_IA64_PLTOFF64MSB, "PLTOFF64MSB", 4, FALSE, TRUE),
292 IA64_HOWTO (R_IA64_PLTOFF64LSB, "PLTOFF64LSB", 4, FALSE, TRUE),
293
294 IA64_HOWTO (R_IA64_FPTR64I, "FPTR64I", 0, FALSE, TRUE),
295 IA64_HOWTO (R_IA64_FPTR32MSB, "FPTR32MSB", 2, FALSE, TRUE),
296 IA64_HOWTO (R_IA64_FPTR32LSB, "FPTR32LSB", 2, FALSE, TRUE),
297 IA64_HOWTO (R_IA64_FPTR64MSB, "FPTR64MSB", 4, FALSE, TRUE),
298 IA64_HOWTO (R_IA64_FPTR64LSB, "FPTR64LSB", 4, FALSE, TRUE),
299
300 IA64_HOWTO (R_IA64_PCREL60B, "PCREL60B", 0, TRUE, TRUE),
301 IA64_HOWTO (R_IA64_PCREL21B, "PCREL21B", 0, TRUE, TRUE),
302 IA64_HOWTO (R_IA64_PCREL21M, "PCREL21M", 0, TRUE, TRUE),
303 IA64_HOWTO (R_IA64_PCREL21F, "PCREL21F", 0, TRUE, TRUE),
304 IA64_HOWTO (R_IA64_PCREL32MSB, "PCREL32MSB", 2, TRUE, TRUE),
305 IA64_HOWTO (R_IA64_PCREL32LSB, "PCREL32LSB", 2, TRUE, TRUE),
306 IA64_HOWTO (R_IA64_PCREL64MSB, "PCREL64MSB", 4, TRUE, TRUE),
307 IA64_HOWTO (R_IA64_PCREL64LSB, "PCREL64LSB", 4, TRUE, TRUE),
308
309 IA64_HOWTO (R_IA64_LTOFF_FPTR22, "LTOFF_FPTR22", 0, FALSE, TRUE),
310 IA64_HOWTO (R_IA64_LTOFF_FPTR64I, "LTOFF_FPTR64I", 0, FALSE, TRUE),
311 IA64_HOWTO (R_IA64_LTOFF_FPTR32MSB, "LTOFF_FPTR32MSB", 2, FALSE, TRUE),
312 IA64_HOWTO (R_IA64_LTOFF_FPTR32LSB, "LTOFF_FPTR32LSB", 2, FALSE, TRUE),
313 IA64_HOWTO (R_IA64_LTOFF_FPTR64MSB, "LTOFF_FPTR64MSB", 4, FALSE, TRUE),
314 IA64_HOWTO (R_IA64_LTOFF_FPTR64LSB, "LTOFF_FPTR64LSB", 4, FALSE, TRUE),
315
316 IA64_HOWTO (R_IA64_SEGREL32MSB, "SEGREL32MSB", 2, FALSE, TRUE),
317 IA64_HOWTO (R_IA64_SEGREL32LSB, "SEGREL32LSB", 2, FALSE, TRUE),
318 IA64_HOWTO (R_IA64_SEGREL64MSB, "SEGREL64MSB", 4, FALSE, TRUE),
319 IA64_HOWTO (R_IA64_SEGREL64LSB, "SEGREL64LSB", 4, FALSE, TRUE),
320
321 IA64_HOWTO (R_IA64_SECREL32MSB, "SECREL32MSB", 2, FALSE, TRUE),
322 IA64_HOWTO (R_IA64_SECREL32LSB, "SECREL32LSB", 2, FALSE, TRUE),
323 IA64_HOWTO (R_IA64_SECREL64MSB, "SECREL64MSB", 4, FALSE, TRUE),
324 IA64_HOWTO (R_IA64_SECREL64LSB, "SECREL64LSB", 4, FALSE, TRUE),
325
326 IA64_HOWTO (R_IA64_REL32MSB, "REL32MSB", 2, FALSE, TRUE),
327 IA64_HOWTO (R_IA64_REL32LSB, "REL32LSB", 2, FALSE, TRUE),
328 IA64_HOWTO (R_IA64_REL64MSB, "REL64MSB", 4, FALSE, TRUE),
329 IA64_HOWTO (R_IA64_REL64LSB, "REL64LSB", 4, FALSE, TRUE),
330
331 IA64_HOWTO (R_IA64_LTV32MSB, "LTV32MSB", 2, FALSE, TRUE),
332 IA64_HOWTO (R_IA64_LTV32LSB, "LTV32LSB", 2, FALSE, TRUE),
333 IA64_HOWTO (R_IA64_LTV64MSB, "LTV64MSB", 4, FALSE, TRUE),
334 IA64_HOWTO (R_IA64_LTV64LSB, "LTV64LSB", 4, FALSE, TRUE),
335
336 IA64_HOWTO (R_IA64_PCREL21BI, "PCREL21BI", 0, TRUE, TRUE),
337 IA64_HOWTO (R_IA64_PCREL22, "PCREL22", 0, TRUE, TRUE),
338 IA64_HOWTO (R_IA64_PCREL64I, "PCREL64I", 0, TRUE, TRUE),
339
340 IA64_HOWTO (R_IA64_IPLTMSB, "IPLTMSB", 4, FALSE, TRUE),
341 IA64_HOWTO (R_IA64_IPLTLSB, "IPLTLSB", 4, FALSE, TRUE),
342 IA64_HOWTO (R_IA64_COPY, "COPY", 4, FALSE, TRUE),
343 IA64_HOWTO (R_IA64_LTOFF22X, "LTOFF22X", 0, FALSE, TRUE),
344 IA64_HOWTO (R_IA64_LDXMOV, "LDXMOV", 0, FALSE, TRUE),
345
346 IA64_HOWTO (R_IA64_TPREL14, "TPREL14", 0, FALSE, FALSE),
347 IA64_HOWTO (R_IA64_TPREL22, "TPREL22", 0, FALSE, FALSE),
348 IA64_HOWTO (R_IA64_TPREL64I, "TPREL64I", 0, FALSE, FALSE),
1fc0d173
JJ
349 IA64_HOWTO (R_IA64_TPREL64MSB, "TPREL64MSB", 4, FALSE, FALSE),
350 IA64_HOWTO (R_IA64_TPREL64LSB, "TPREL64LSB", 4, FALSE, FALSE),
b34976b6
AM
351 IA64_HOWTO (R_IA64_LTOFF_TPREL22, "LTOFF_TPREL22", 0, FALSE, FALSE),
352
0ca3e455
JB
353 IA64_HOWTO (R_IA64_DTPMOD64MSB, "DTPMOD64MSB", 4, FALSE, FALSE),
354 IA64_HOWTO (R_IA64_DTPMOD64LSB, "DTPMOD64LSB", 4, FALSE, FALSE),
b34976b6
AM
355 IA64_HOWTO (R_IA64_LTOFF_DTPMOD22, "LTOFF_DTPMOD22", 0, FALSE, FALSE),
356
357 IA64_HOWTO (R_IA64_DTPREL14, "DTPREL14", 0, FALSE, FALSE),
358 IA64_HOWTO (R_IA64_DTPREL22, "DTPREL22", 0, FALSE, FALSE),
359 IA64_HOWTO (R_IA64_DTPREL64I, "DTPREL64I", 0, FALSE, FALSE),
1fc0d173
JJ
360 IA64_HOWTO (R_IA64_DTPREL32MSB, "DTPREL32MSB", 2, FALSE, FALSE),
361 IA64_HOWTO (R_IA64_DTPREL32LSB, "DTPREL32LSB", 2, FALSE, FALSE),
362 IA64_HOWTO (R_IA64_DTPREL64MSB, "DTPREL64MSB", 4, FALSE, FALSE),
363 IA64_HOWTO (R_IA64_DTPREL64LSB, "DTPREL64LSB", 4, FALSE, FALSE),
b34976b6 364 IA64_HOWTO (R_IA64_LTOFF_DTPREL22, "LTOFF_DTPREL22", 0, FALSE, FALSE),
800eeca4
JW
365 };
366
367static unsigned char elf_code_to_howto_index[R_IA64_MAX_RELOC_CODE + 1];
368
369/* Given a BFD reloc type, return the matching HOWTO structure. */
370
5e8d7549 371static reloc_howto_type *
eae50df2 372lookup_howto (unsigned int rtype)
800eeca4
JW
373{
374 static int inited = 0;
375 int i;
376
377 if (!inited)
378 {
379 inited = 1;
380
381 memset (elf_code_to_howto_index, 0xff, sizeof (elf_code_to_howto_index));
382 for (i = 0; i < NELEMS (ia64_howto_table); ++i)
383 elf_code_to_howto_index[ia64_howto_table[i].type] = i;
384 }
385
d0fb9a8d
JJ
386 if (rtype > R_IA64_MAX_RELOC_CODE)
387 return 0;
800eeca4
JW
388 i = elf_code_to_howto_index[rtype];
389 if (i >= NELEMS (ia64_howto_table))
390 return 0;
391 return ia64_howto_table + i;
392}
393
394static reloc_howto_type*
eae50df2
L
395elfNN_ia64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
396 bfd_reloc_code_real_type bfd_code)
800eeca4
JW
397{
398 unsigned int rtype;
399
400 switch (bfd_code)
401 {
402 case BFD_RELOC_NONE: rtype = R_IA64_NONE; break;
403
404 case BFD_RELOC_IA64_IMM14: rtype = R_IA64_IMM14; break;
405 case BFD_RELOC_IA64_IMM22: rtype = R_IA64_IMM22; break;
406 case BFD_RELOC_IA64_IMM64: rtype = R_IA64_IMM64; break;
407
408 case BFD_RELOC_IA64_DIR32MSB: rtype = R_IA64_DIR32MSB; break;
409 case BFD_RELOC_IA64_DIR32LSB: rtype = R_IA64_DIR32LSB; break;
410 case BFD_RELOC_IA64_DIR64MSB: rtype = R_IA64_DIR64MSB; break;
411 case BFD_RELOC_IA64_DIR64LSB: rtype = R_IA64_DIR64LSB; break;
412
413 case BFD_RELOC_IA64_GPREL22: rtype = R_IA64_GPREL22; break;
414 case BFD_RELOC_IA64_GPREL64I: rtype = R_IA64_GPREL64I; break;
415 case BFD_RELOC_IA64_GPREL32MSB: rtype = R_IA64_GPREL32MSB; break;
416 case BFD_RELOC_IA64_GPREL32LSB: rtype = R_IA64_GPREL32LSB; break;
417 case BFD_RELOC_IA64_GPREL64MSB: rtype = R_IA64_GPREL64MSB; break;
418 case BFD_RELOC_IA64_GPREL64LSB: rtype = R_IA64_GPREL64LSB; break;
419
420 case BFD_RELOC_IA64_LTOFF22: rtype = R_IA64_LTOFF22; break;
421 case BFD_RELOC_IA64_LTOFF64I: rtype = R_IA64_LTOFF64I; break;
422
423 case BFD_RELOC_IA64_PLTOFF22: rtype = R_IA64_PLTOFF22; break;
424 case BFD_RELOC_IA64_PLTOFF64I: rtype = R_IA64_PLTOFF64I; break;
425 case BFD_RELOC_IA64_PLTOFF64MSB: rtype = R_IA64_PLTOFF64MSB; break;
426 case BFD_RELOC_IA64_PLTOFF64LSB: rtype = R_IA64_PLTOFF64LSB; break;
427 case BFD_RELOC_IA64_FPTR64I: rtype = R_IA64_FPTR64I; break;
428 case BFD_RELOC_IA64_FPTR32MSB: rtype = R_IA64_FPTR32MSB; break;
429 case BFD_RELOC_IA64_FPTR32LSB: rtype = R_IA64_FPTR32LSB; break;
430 case BFD_RELOC_IA64_FPTR64MSB: rtype = R_IA64_FPTR64MSB; break;
431 case BFD_RELOC_IA64_FPTR64LSB: rtype = R_IA64_FPTR64LSB; break;
432
433 case BFD_RELOC_IA64_PCREL21B: rtype = R_IA64_PCREL21B; break;
748abff6 434 case BFD_RELOC_IA64_PCREL21BI: rtype = R_IA64_PCREL21BI; break;
800eeca4
JW
435 case BFD_RELOC_IA64_PCREL21M: rtype = R_IA64_PCREL21M; break;
436 case BFD_RELOC_IA64_PCREL21F: rtype = R_IA64_PCREL21F; break;
748abff6
RH
437 case BFD_RELOC_IA64_PCREL22: rtype = R_IA64_PCREL22; break;
438 case BFD_RELOC_IA64_PCREL60B: rtype = R_IA64_PCREL60B; break;
439 case BFD_RELOC_IA64_PCREL64I: rtype = R_IA64_PCREL64I; break;
800eeca4
JW
440 case BFD_RELOC_IA64_PCREL32MSB: rtype = R_IA64_PCREL32MSB; break;
441 case BFD_RELOC_IA64_PCREL32LSB: rtype = R_IA64_PCREL32LSB; break;
442 case BFD_RELOC_IA64_PCREL64MSB: rtype = R_IA64_PCREL64MSB; break;
443 case BFD_RELOC_IA64_PCREL64LSB: rtype = R_IA64_PCREL64LSB; break;
444
445 case BFD_RELOC_IA64_LTOFF_FPTR22: rtype = R_IA64_LTOFF_FPTR22; break;
446 case BFD_RELOC_IA64_LTOFF_FPTR64I: rtype = R_IA64_LTOFF_FPTR64I; break;
a4bd8390
JW
447 case BFD_RELOC_IA64_LTOFF_FPTR32MSB: rtype = R_IA64_LTOFF_FPTR32MSB; break;
448 case BFD_RELOC_IA64_LTOFF_FPTR32LSB: rtype = R_IA64_LTOFF_FPTR32LSB; break;
800eeca4
JW
449 case BFD_RELOC_IA64_LTOFF_FPTR64MSB: rtype = R_IA64_LTOFF_FPTR64MSB; break;
450 case BFD_RELOC_IA64_LTOFF_FPTR64LSB: rtype = R_IA64_LTOFF_FPTR64LSB; break;
451
800eeca4
JW
452 case BFD_RELOC_IA64_SEGREL32MSB: rtype = R_IA64_SEGREL32MSB; break;
453 case BFD_RELOC_IA64_SEGREL32LSB: rtype = R_IA64_SEGREL32LSB; break;
454 case BFD_RELOC_IA64_SEGREL64MSB: rtype = R_IA64_SEGREL64MSB; break;
455 case BFD_RELOC_IA64_SEGREL64LSB: rtype = R_IA64_SEGREL64LSB; break;
456
457 case BFD_RELOC_IA64_SECREL32MSB: rtype = R_IA64_SECREL32MSB; break;
458 case BFD_RELOC_IA64_SECREL32LSB: rtype = R_IA64_SECREL32LSB; break;
459 case BFD_RELOC_IA64_SECREL64MSB: rtype = R_IA64_SECREL64MSB; break;
460 case BFD_RELOC_IA64_SECREL64LSB: rtype = R_IA64_SECREL64LSB; break;
461
462 case BFD_RELOC_IA64_REL32MSB: rtype = R_IA64_REL32MSB; break;
463 case BFD_RELOC_IA64_REL32LSB: rtype = R_IA64_REL32LSB; break;
464 case BFD_RELOC_IA64_REL64MSB: rtype = R_IA64_REL64MSB; break;
465 case BFD_RELOC_IA64_REL64LSB: rtype = R_IA64_REL64LSB; break;
466
467 case BFD_RELOC_IA64_LTV32MSB: rtype = R_IA64_LTV32MSB; break;
468 case BFD_RELOC_IA64_LTV32LSB: rtype = R_IA64_LTV32LSB; break;
469 case BFD_RELOC_IA64_LTV64MSB: rtype = R_IA64_LTV64MSB; break;
470 case BFD_RELOC_IA64_LTV64LSB: rtype = R_IA64_LTV64LSB; break;
471
472 case BFD_RELOC_IA64_IPLTMSB: rtype = R_IA64_IPLTMSB; break;
473 case BFD_RELOC_IA64_IPLTLSB: rtype = R_IA64_IPLTLSB; break;
800eeca4
JW
474 case BFD_RELOC_IA64_COPY: rtype = R_IA64_COPY; break;
475 case BFD_RELOC_IA64_LTOFF22X: rtype = R_IA64_LTOFF22X; break;
476 case BFD_RELOC_IA64_LDXMOV: rtype = R_IA64_LDXMOV; break;
477
13ae64f3 478 case BFD_RELOC_IA64_TPREL14: rtype = R_IA64_TPREL14; break;
800eeca4 479 case BFD_RELOC_IA64_TPREL22: rtype = R_IA64_TPREL22; break;
13ae64f3 480 case BFD_RELOC_IA64_TPREL64I: rtype = R_IA64_TPREL64I; break;
800eeca4
JW
481 case BFD_RELOC_IA64_TPREL64MSB: rtype = R_IA64_TPREL64MSB; break;
482 case BFD_RELOC_IA64_TPREL64LSB: rtype = R_IA64_TPREL64LSB; break;
13ae64f3
JJ
483 case BFD_RELOC_IA64_LTOFF_TPREL22: rtype = R_IA64_LTOFF_TPREL22; break;
484
485 case BFD_RELOC_IA64_DTPMOD64MSB: rtype = R_IA64_DTPMOD64MSB; break;
486 case BFD_RELOC_IA64_DTPMOD64LSB: rtype = R_IA64_DTPMOD64LSB; break;
487 case BFD_RELOC_IA64_LTOFF_DTPMOD22: rtype = R_IA64_LTOFF_DTPMOD22; break;
488
489 case BFD_RELOC_IA64_DTPREL14: rtype = R_IA64_DTPREL14; break;
490 case BFD_RELOC_IA64_DTPREL22: rtype = R_IA64_DTPREL22; break;
491 case BFD_RELOC_IA64_DTPREL64I: rtype = R_IA64_DTPREL64I; break;
492 case BFD_RELOC_IA64_DTPREL32MSB: rtype = R_IA64_DTPREL32MSB; break;
493 case BFD_RELOC_IA64_DTPREL32LSB: rtype = R_IA64_DTPREL32LSB; break;
494 case BFD_RELOC_IA64_DTPREL64MSB: rtype = R_IA64_DTPREL64MSB; break;
495 case BFD_RELOC_IA64_DTPREL64LSB: rtype = R_IA64_DTPREL64LSB; break;
496 case BFD_RELOC_IA64_LTOFF_DTPREL22: rtype = R_IA64_LTOFF_DTPREL22; break;
800eeca4
JW
497
498 default: return 0;
499 }
500 return lookup_howto (rtype);
501}
502
157090f7
AM
503static reloc_howto_type *
504elfNN_ia64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
505 const char *r_name)
506{
507 unsigned int i;
508
509 for (i = 0;
510 i < sizeof (ia64_howto_table) / sizeof (ia64_howto_table[0]);
511 i++)
512 if (ia64_howto_table[i].name != NULL
513 && strcasecmp (ia64_howto_table[i].name, r_name) == 0)
514 return &ia64_howto_table[i];
515
516 return NULL;
517}
518
800eeca4
JW
519/* Given a ELF reloc, return the matching HOWTO structure. */
520
521static void
eae50df2
L
522elfNN_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
523 arelent *bfd_reloc,
524 Elf_Internal_Rela *elf_reloc)
800eeca4 525{
dc810e39
AM
526 bfd_reloc->howto
527 = lookup_howto ((unsigned int) ELFNN_R_TYPE (elf_reloc->r_info));
800eeca4
JW
528}
529\f
530#define PLT_HEADER_SIZE (3 * 16)
531#define PLT_MIN_ENTRY_SIZE (1 * 16)
532#define PLT_FULL_ENTRY_SIZE (2 * 16)
533#define PLT_RESERVED_WORDS 3
534
535static const bfd_byte plt_header[PLT_HEADER_SIZE] =
536{
537 0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21, /* [MMI] mov r2=r14;; */
538 0xe0, 0x00, 0x08, 0x00, 0x48, 0x00, /* addl r14=0,r2 */
539 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
540 0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14, /* [MMI] ld8 r16=[r14],8;; */
541 0x10, 0x41, 0x38, 0x30, 0x28, 0x00, /* ld8 r17=[r14],8 */
542 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
543 0x11, 0x08, 0x00, 0x1c, 0x18, 0x10, /* [MIB] ld8 r1=[r14] */
544 0x60, 0x88, 0x04, 0x80, 0x03, 0x00, /* mov b6=r17 */
545 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
546};
547
548static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
549{
550 0x11, 0x78, 0x00, 0x00, 0x00, 0x24, /* [MIB] mov r15=0 */
551 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* nop.i 0x0 */
552 0x00, 0x00, 0x00, 0x40 /* br.few 0 <PLT0>;; */
553};
554
555static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
556{
557 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
8b6f2683 558 0x00, 0x41, 0x3c, 0x70, 0x29, 0xc0, /* ld8.acq r16=[r15],8*/
800eeca4
JW
559 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
560 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
561 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
562 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
563};
564
565#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
748abff6 566
748abff6
RH
567static const bfd_byte oor_brl[16] =
568{
569 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
570 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;; */
571 0x00, 0x00, 0x00, 0xc0
572};
3f7deb8a
L
573
574static const bfd_byte oor_ip[48] =
575{
576 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
577 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, /* movl r15=0 */
578 0x01, 0x00, 0x00, 0x60,
579 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MII] nop.m 0 */
580 0x00, 0x01, 0x00, 0x60, 0x00, 0x00, /* mov r16=ip;; */
581 0xf2, 0x80, 0x00, 0x80, /* add r16=r15,r16;; */
582 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
583 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
584 0x60, 0x00, 0x80, 0x00 /* br b6;; */
585};
586
587static size_t oor_branch_size = sizeof (oor_brl);
588
589void
590bfd_elfNN_ia64_after_parse (int itanium)
591{
592 oor_branch_size = itanium ? sizeof (oor_ip) : sizeof (oor_brl);
593}
594
83b6bd86
L
595#define BTYPE_SHIFT 6
596#define Y_SHIFT 26
597#define X6_SHIFT 27
598#define X4_SHIFT 27
599#define X3_SHIFT 33
600#define X2_SHIFT 31
601#define X_SHIFT 33
602#define OPCODE_SHIFT 37
603
604#define OPCODE_BITS (0xfLL << OPCODE_SHIFT)
605#define X6_BITS (0x3fLL << X6_SHIFT)
606#define X4_BITS (0xfLL << X4_SHIFT)
607#define X3_BITS (0x7LL << X3_SHIFT)
608#define X2_BITS (0x3LL << X2_SHIFT)
609#define X_BITS (0x1LL << X_SHIFT)
610#define Y_BITS (0x1LL << Y_SHIFT)
611#define BTYPE_BITS (0x7LL << BTYPE_SHIFT)
612#define PREDICATE_BITS (0x3fLL)
613
614#define IS_NOP_B(i) \
615 (((i) & (OPCODE_BITS | X6_BITS)) == (2LL << OPCODE_SHIFT))
616#define IS_NOP_F(i) \
617 (((i) & (OPCODE_BITS | X_BITS | X6_BITS | Y_BITS)) \
618 == (0x1LL << X6_SHIFT))
619#define IS_NOP_I(i) \
620 (((i) & (OPCODE_BITS | X3_BITS | X6_BITS | Y_BITS)) \
621 == (0x1LL << X6_SHIFT))
622#define IS_NOP_M(i) \
623 (((i) & (OPCODE_BITS | X3_BITS | X2_BITS | X4_BITS | Y_BITS)) \
624 == (0x1LL << X4_SHIFT))
625#define IS_BR_COND(i) \
626 (((i) & (OPCODE_BITS | BTYPE_BITS)) == (0x4LL << OPCODE_SHIFT))
627#define IS_BR_CALL(i) \
628 (((i) & OPCODE_BITS) == (0x5LL << OPCODE_SHIFT))
629
630static bfd_boolean
631elfNN_ia64_relax_br (bfd_byte *contents, bfd_vma off)
632{
d3ce72d0 633 unsigned int template_val, mlx;
83b6bd86
L
634 bfd_vma t0, t1, s0, s1, s2, br_code;
635 long br_slot;
636 bfd_byte *hit_addr;
637
638 hit_addr = (bfd_byte *) (contents + off);
639 br_slot = (long) hit_addr & 0x3;
640 hit_addr -= br_slot;
641 t0 = bfd_getl64 (hit_addr + 0);
642 t1 = bfd_getl64 (hit_addr + 8);
643
644 /* Check if we can turn br into brl. A label is always at the start
645 of the bundle. Even if there are predicates on NOPs, we still
646 perform this optimization. */
d3ce72d0 647 template_val = t0 & 0x1e;
83b6bd86
L
648 s0 = (t0 >> 5) & 0x1ffffffffffLL;
649 s1 = ((t0 >> 46) | (t1 << 18)) & 0x1ffffffffffLL;
650 s2 = (t1 >> 23) & 0x1ffffffffffLL;
651 switch (br_slot)
652 {
653 case 0:
654 /* Check if slot 1 and slot 2 are NOPs. Possible template is
655 BBB. We only need to check nop.b. */
656 if (!(IS_NOP_B (s1) && IS_NOP_B (s2)))
657 return FALSE;
658 br_code = s0;
659 break;
660 case 1:
661 /* Check if slot 2 is NOP. Possible templates are MBB and BBB.
662 For BBB, slot 0 also has to be nop.b. */
d3ce72d0 663 if (!((template_val == 0x12 /* MBB */
83b6bd86 664 && IS_NOP_B (s2))
d3ce72d0 665 || (template_val == 0x16 /* BBB */
83b6bd86
L
666 && IS_NOP_B (s0)
667 && IS_NOP_B (s2))))
668 return FALSE;
669 br_code = s1;
670 break;
671 case 2:
672 /* Check if slot 1 is NOP. Possible templates are MIB, MBB, BBB,
673 MMB and MFB. For BBB, slot 0 also has to be nop.b. */
d3ce72d0 674 if (!((template_val == 0x10 /* MIB */
83b6bd86 675 && IS_NOP_I (s1))
d3ce72d0 676 || (template_val == 0x12 /* MBB */
83b6bd86 677 && IS_NOP_B (s1))
d3ce72d0 678 || (template_val == 0x16 /* BBB */
83b6bd86
L
679 && IS_NOP_B (s0)
680 && IS_NOP_B (s1))
d3ce72d0 681 || (template_val == 0x18 /* MMB */
83b6bd86 682 && IS_NOP_M (s1))
d3ce72d0 683 || (template_val == 0x1c /* MFB */
83b6bd86
L
684 && IS_NOP_F (s1))))
685 return FALSE;
686 br_code = s2;
687 break;
688 default:
689 /* It should never happen. */
690 abort ();
691 }
9a2e389a 692
83b6bd86
L
693 /* We can turn br.cond/br.call into brl.cond/brl.call. */
694 if (!(IS_BR_COND (br_code) || IS_BR_CALL (br_code)))
695 return FALSE;
696
697 /* Turn br into brl by setting bit 40. */
698 br_code |= 0x1LL << 40;
699
700 /* Turn the old bundle into a MLX bundle with the same stop-bit
701 variety. */
702 if (t0 & 0x1)
703 mlx = 0x5;
704 else
705 mlx = 0x4;
706
d3ce72d0 707 if (template_val == 0x16)
83b6bd86 708 {
5e27d427
L
709 /* For BBB, we need to put nop.m in slot 0. We keep the original
710 predicate only if slot 0 isn't br. */
711 if (br_slot == 0)
712 t0 = 0LL;
713 else
714 t0 &= PREDICATE_BITS << 5;
83b6bd86
L
715 t0 |= 0x1LL << (X4_SHIFT + 5);
716 }
717 else
718 {
719 /* Keep the original instruction in slot 0. */
720 t0 &= 0x1ffffffffffLL << 5;
721 }
722
723 t0 |= mlx;
724
725 /* Put brl in slot 1. */
726 t1 = br_code << 23;
727
728 bfd_putl64 (t0, hit_addr);
729 bfd_putl64 (t1, hit_addr + 8);
730 return TRUE;
731}
732
03609792 733static void
bbb268c3 734elfNN_ia64_relax_brl (bfd_byte *contents, bfd_vma off)
03609792 735{
d3ce72d0 736 int template_val;
03609792 737 bfd_byte *hit_addr;
fc3ab699 738 bfd_vma t0, t1, i0, i1, i2;
03609792
L
739
740 hit_addr = (bfd_byte *) (contents + off);
741 hit_addr -= (long) hit_addr & 0x3;
fc3ab699
L
742 t0 = bfd_getl64 (hit_addr);
743 t1 = bfd_getl64 (hit_addr + 8);
03609792 744
7e3102a7 745 /* Keep the instruction in slot 0. */
fc3ab699
L
746 i0 = (t0 >> 5) & 0x1ffffffffffLL;
747 /* Use nop.b for slot 1. */
748 i1 = 0x4000000000LL;
7e3102a7 749 /* For slot 2, turn brl into br by masking out bit 40. */
fc3ab699 750 i2 = (t1 >> 23) & 0x0ffffffffffLL;
7e3102a7 751
fc3ab699
L
752 /* Turn a MLX bundle into a MBB bundle with the same stop-bit
753 variety. */
754 if (t0 & 0x1)
d3ce72d0 755 template_val = 0x13;
fc3ab699 756 else
d3ce72d0
NC
757 template_val = 0x12;
758 t0 = (i1 << 46) | (i0 << 5) | template_val;
fc3ab699 759 t1 = (i2 << 23) | (i1 >> 18);
7e3102a7 760
fc3ab699
L
761 bfd_putl64 (t0, hit_addr);
762 bfd_putl64 (t1, hit_addr + 8);
03609792 763}
fbbc3759
L
764
765/* Rename some of the generic section flags to better document how they
766 are used here. */
b0dddeec
AM
767#define skip_relax_pass_0 sec_flg0
768#define skip_relax_pass_1 sec_flg1
fbbc3759 769
748abff6 770\f
2c4c2bc0 771/* These functions do relaxation for IA-64 ELF. */
748abff6 772
a231ddc0
L
773static void
774elfNN_ia64_update_short_info (asection *sec, bfd_vma offset,
775 struct elfNN_ia64_link_hash_table *ia64_info)
776{
f4dcf782
L
777 /* Skip ABS and SHF_IA_64_SHORT sections. */
778 if (sec == bfd_abs_section_ptr
779 || (sec->flags & SEC_SMALL_DATA) != 0)
a231ddc0
L
780 return;
781
782 if (!ia64_info->min_short_sec)
783 {
784 ia64_info->max_short_sec = sec;
785 ia64_info->max_short_offset = offset;
786 ia64_info->min_short_sec = sec;
787 ia64_info->min_short_offset = offset;
788 }
789 else if (sec == ia64_info->max_short_sec
790 && offset > ia64_info->max_short_offset)
791 ia64_info->max_short_offset = offset;
792 else if (sec == ia64_info->min_short_sec
793 && offset < ia64_info->min_short_offset)
794 ia64_info->min_short_offset = offset;
795 else if (sec->output_section->vma
affb4bf3 796 > ia64_info->max_short_sec->vma)
a231ddc0
L
797 {
798 ia64_info->max_short_sec = sec;
799 ia64_info->max_short_offset = offset;
800 }
801 else if (sec->output_section->vma
affb4bf3 802 < ia64_info->min_short_sec->vma)
a231ddc0
L
803 {
804 ia64_info->min_short_sec = sec;
805 ia64_info->min_short_offset = offset;
806 }
807}
808
b34976b6 809static bfd_boolean
eae50df2
L
810elfNN_ia64_relax_section (bfd *abfd, asection *sec,
811 struct bfd_link_info *link_info,
812 bfd_boolean *again)
748abff6
RH
813{
814 struct one_fixup
815 {
816 struct one_fixup *next;
817 asection *tsec;
818 bfd_vma toff;
819 bfd_vma trampoff;
820 };
821
822 Elf_Internal_Shdr *symtab_hdr;
823 Elf_Internal_Rela *internal_relocs;
748abff6
RH
824 Elf_Internal_Rela *irel, *irelend;
825 bfd_byte *contents;
6cdc0ccc 826 Elf_Internal_Sym *isymbuf = NULL;
bbe66d08 827 struct elfNN_ia64_link_hash_table *ia64_info;
748abff6 828 struct one_fixup *fixups = NULL;
b34976b6
AM
829 bfd_boolean changed_contents = FALSE;
830 bfd_boolean changed_relocs = FALSE;
2c4c2bc0 831 bfd_boolean changed_got = FALSE;
fbbc3759
L
832 bfd_boolean skip_relax_pass_0 = TRUE;
833 bfd_boolean skip_relax_pass_1 = TRUE;
2c4c2bc0 834 bfd_vma gp = 0;
748abff6 835
46f5aac8
KH
836 /* Assume we're not going to change any sizes, and we'll only need
837 one pass. */
b34976b6 838 *again = FALSE;
748abff6 839
c8a1f254
NS
840 if (link_info->relocatable)
841 (*link_info->callbacks->einfo)
842 (_("%P%F: --relax and -r may not be used together\n"));
843
04b3329b 844 /* Don't even try to relax for non-ELF outputs. */
0eddce27 845 if (!is_elf_hash_table (link_info->hash))
04b3329b
L
846 return FALSE;
847
c7996ad6 848 /* Nothing to do if there are no relocations or there is no need for
fbbc3759 849 the current pass. */
748abff6 850 if ((sec->flags & SEC_RELOC) == 0
c7996ad6 851 || sec->reloc_count == 0
fbbc3759
L
852 || (link_info->relax_pass == 0 && sec->skip_relax_pass_0)
853 || (link_info->relax_pass == 1 && sec->skip_relax_pass_1))
b34976b6 854 return TRUE;
748abff6 855
4dfe6ac6
NC
856 ia64_info = elfNN_ia64_hash_table (link_info);
857 if (ia64_info == NULL)
858 return FALSE;
859
748abff6
RH
860 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
861
862 /* Load the relocations for this section. */
45d6a902 863 internal_relocs = (_bfd_elf_link_read_relocs
4dfe6ac6 864 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
748abff6
RH
865 link_info->keep_memory));
866 if (internal_relocs == NULL)
b34976b6 867 return FALSE;
748abff6 868
748abff6
RH
869 irelend = internal_relocs + sec->reloc_count;
870
748abff6 871 /* Get the section contents. */
748abff6
RH
872 if (elf_section_data (sec)->this_hdr.contents != NULL)
873 contents = elf_section_data (sec)->this_hdr.contents;
874 else
875 {
eea6121a 876 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
748abff6
RH
877 goto error_return;
878 }
879
2c4c2bc0 880 for (irel = internal_relocs; irel < irelend; irel++)
748abff6 881 {
2f9bd3f6 882 unsigned long r_type = ELFNN_R_TYPE (irel->r_info);
748abff6 883 bfd_vma symaddr, reladdr, trampoff, toff, roff;
748abff6
RH
884 asection *tsec;
885 struct one_fixup *f;
dc810e39 886 bfd_size_type amt;
2c4c2bc0
RH
887 bfd_boolean is_branch;
888 struct elfNN_ia64_dyn_sym_info *dyn_i;
bf8b15af 889 char symtype;
748abff6 890
2c4c2bc0
RH
891 switch (r_type)
892 {
893 case R_IA64_PCREL21B:
894 case R_IA64_PCREL21BI:
895 case R_IA64_PCREL21M:
896 case R_IA64_PCREL21F:
fbbc3759
L
897 /* In pass 1, all br relaxations are done. We can skip it. */
898 if (link_info->relax_pass == 1)
c7996ad6 899 continue;
fbbc3759 900 skip_relax_pass_0 = FALSE;
2c4c2bc0
RH
901 is_branch = TRUE;
902 break;
903
03609792 904 case R_IA64_PCREL60B:
fbbc3759
L
905 /* We can't optimize brl to br in pass 0 since br relaxations
906 will increase the code size. Defer it to pass 1. */
907 if (link_info->relax_pass == 0)
03609792 908 {
fbbc3759 909 skip_relax_pass_1 = FALSE;
03609792
L
910 continue;
911 }
912 is_branch = TRUE;
913 break;
914
a231ddc0
L
915 case R_IA64_GPREL22:
916 /* Update max_short_sec/min_short_sec. */
917
2c4c2bc0
RH
918 case R_IA64_LTOFF22X:
919 case R_IA64_LDXMOV:
fbbc3759
L
920 /* We can't relax ldx/mov in pass 0 since br relaxations will
921 increase the code size. Defer it to pass 1. */
922 if (link_info->relax_pass == 0)
c7996ad6 923 {
fbbc3759 924 skip_relax_pass_1 = FALSE;
c7996ad6
L
925 continue;
926 }
2c4c2bc0
RH
927 is_branch = FALSE;
928 break;
929
930 default:
931 continue;
932 }
748abff6
RH
933
934 /* Get the value of the symbol referred to by the reloc. */
bbe66d08 935 if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
748abff6
RH
936 {
937 /* A local symbol. */
6cdc0ccc
AM
938 Elf_Internal_Sym *isym;
939
940 /* Read this BFD's local symbols. */
941 if (isymbuf == NULL)
942 {
943 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
944 if (isymbuf == NULL)
945 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
946 symtab_hdr->sh_info, 0,
947 NULL, NULL, NULL);
948 if (isymbuf == 0)
949 goto error_return;
950 }
951
60d8b524 952 isym = isymbuf + ELFNN_R_SYM (irel->r_info);
6cdc0ccc 953 if (isym->st_shndx == SHN_UNDEF)
4cc11e76 954 continue; /* We can't do anything with undefined symbols. */
6cdc0ccc 955 else if (isym->st_shndx == SHN_ABS)
748abff6 956 tsec = bfd_abs_section_ptr;
6cdc0ccc 957 else if (isym->st_shndx == SHN_COMMON)
748abff6 958 tsec = bfd_com_section_ptr;
6cdc0ccc 959 else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
d9cf1b54 960 tsec = bfd_com_section_ptr;
3e932841 961 else
6cdc0ccc 962 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
748abff6 963
6cdc0ccc 964 toff = isym->st_value;
2c4c2bc0 965 dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, FALSE);
bf8b15af 966 symtype = ELF_ST_TYPE (isym->st_info);
748abff6
RH
967 }
968 else
969 {
970 unsigned long indx;
971 struct elf_link_hash_entry *h;
748abff6 972
bbe66d08 973 indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
748abff6
RH
974 h = elf_sym_hashes (abfd)[indx];
975 BFD_ASSERT (h != NULL);
976
977 while (h->root.type == bfd_link_hash_indirect
978 || h->root.type == bfd_link_hash_warning)
979 h = (struct elf_link_hash_entry *) h->root.u.i.link;
980
b34976b6 981 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, FALSE);
748abff6
RH
982
983 /* For branches to dynamic symbols, we're interested instead
984 in a branch to the PLT entry. */
2c4c2bc0 985 if (is_branch && dyn_i && dyn_i->want_plt2)
748abff6 986 {
2f9bd3f6
RH
987 /* Internal branches shouldn't be sent to the PLT.
988 Leave this for now and we'll give an error later. */
989 if (r_type != R_IA64_PCREL21B)
990 continue;
991
cc3bd654 992 tsec = ia64_info->root.splt;
748abff6 993 toff = dyn_i->plt2_offset;
3fa1d917 994 BFD_ASSERT (irel->r_addend == 0);
748abff6 995 }
2c4c2bc0
RH
996
997 /* Can't do anything else with dynamic symbols. */
986a241f 998 else if (elfNN_ia64_dynamic_symbol_p (h, link_info, r_type))
2c4c2bc0
RH
999 continue;
1000
748abff6
RH
1001 else
1002 {
4cc11e76 1003 /* We can't do anything with undefined symbols. */
748abff6
RH
1004 if (h->root.type == bfd_link_hash_undefined
1005 || h->root.type == bfd_link_hash_undefweak)
1006 continue;
1007
1008 tsec = h->root.u.def.section;
1009 toff = h->root.u.def.value;
1010 }
bf8b15af
L
1011
1012 symtype = h->type;
5dd23ec1 1013 }
3fa1d917 1014
9f2e92c5 1015 if (tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
bf8b15af
L
1016 {
1017 /* At this stage in linking, no SEC_MERGE symbol has been
1018 adjusted, so all references to such symbols need to be
1019 passed through _bfd_merged_section_offset. (Later, in
1020 relocate_section, all SEC_MERGE symbols *except* for
1021 section symbols have been adjusted.)
1022
1023 gas may reduce relocations against symbols in SEC_MERGE
1024 sections to a relocation against the section symbol when
1025 the original addend was zero. When the reloc is against
1026 a section symbol we should include the addend in the
1027 offset passed to _bfd_merged_section_offset, since the
1028 location of interest is the original symbol. On the
1029 other hand, an access to "sym+addend" where "sym" is not
1030 a section symbol should not include the addend; Such an
1031 access is presumed to be an offset from "sym"; The
1032 location of interest is just "sym". */
1033 if (symtype == STT_SECTION)
1034 toff += irel->r_addend;
f12123c0 1035
bf8b15af
L
1036 toff = _bfd_merged_section_offset (abfd, &tsec,
1037 elf_section_data (tsec)->sec_info,
1038 toff);
1039
1040 if (symtype != STT_SECTION)
1041 toff += irel->r_addend;
1042 }
9f2e92c5
L
1043 else
1044 toff += irel->r_addend;
1045
3fa1d917 1046 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
748abff6
RH
1047
1048 roff = irel->r_offset;
748abff6 1049
2c4c2bc0
RH
1050 if (is_branch)
1051 {
de0d9f33
L
1052 bfd_signed_vma offset;
1053
2c4c2bc0
RH
1054 reladdr = (sec->output_section->vma
1055 + sec->output_offset
1056 + roff) & (bfd_vma) -4;
748abff6 1057
feddcd0d
L
1058 /* The .plt section is aligned at 32byte and the .text section
1059 is aligned at 64byte. The .text section is right after the
1060 .plt section. After the first relaxation pass, linker may
1061 increase the gap between the .plt and .text sections up
1062 to 32byte. We assume linker will always insert 32byte
1063 between the .plt and .text sections after the the first
1064 relaxation pass. */
cc3bd654 1065 if (tsec == ia64_info->root.splt)
feddcd0d
L
1066 offset = -0x1000000 + 32;
1067 else
1068 offset = -0x1000000;
1069
2c4c2bc0 1070 /* If the branch is in range, no need to do anything. */
feddcd0d 1071 if ((bfd_signed_vma) (symaddr - reladdr) >= offset
2c4c2bc0 1072 && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
03609792
L
1073 {
1074 /* If the 60-bit branch is in 21-bit range, optimize it. */
1075 if (r_type == R_IA64_PCREL60B)
1076 {
bbb268c3 1077 elfNN_ia64_relax_brl (contents, roff);
03609792
L
1078
1079 irel->r_info
7e3102a7 1080 = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
03609792
L
1081 R_IA64_PCREL21B);
1082
1083 /* If the original relocation offset points to slot
1084 1, change it to slot 2. */
1085 if ((irel->r_offset & 3) == 1)
1086 irel->r_offset += 1;
1087 }
1088
1089 continue;
1090 }
1091 else if (r_type == R_IA64_PCREL60B)
2c4c2bc0 1092 continue;
83b6bd86
L
1093 else if (elfNN_ia64_relax_br (contents, roff))
1094 {
1095 irel->r_info
1096 = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1097 R_IA64_PCREL60B);
1098
1099 /* Make the relocation offset point to slot 1. */
1100 irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
1101 continue;
1102 }
748abff6 1103
e525914f
L
1104 /* We can't put a trampoline in a .init/.fini section. Issue
1105 an error. */
1106 if (strcmp (sec->output_section->name, ".init") == 0
1107 || strcmp (sec->output_section->name, ".fini") == 0)
1108 {
1109 (*_bfd_error_handler)
d003868e
AM
1110 (_("%B: Can't relax br at 0x%lx in section `%A'. Please use brl or indirect branch."),
1111 sec->owner, sec, (unsigned long) roff);
e525914f
L
1112 bfd_set_error (bfd_error_bad_value);
1113 goto error_return;
1114 }
1115
2c4c2bc0 1116 /* If the branch and target are in the same section, you've
c5509b92
L
1117 got one honking big section and we can't help you unless
1118 you are branching backwards. You'll get an error message
1119 later. */
1120 if (tsec == sec && toff > roff)
2c4c2bc0 1121 continue;
748abff6 1122
2c4c2bc0
RH
1123 /* Look for an existing fixup to this address. */
1124 for (f = fixups; f ; f = f->next)
1125 if (f->tsec == tsec && f->toff == toff)
1126 break;
748abff6 1127
2c4c2bc0 1128 if (f == NULL)
748abff6 1129 {
2c4c2bc0
RH
1130 /* Two alternatives: If it's a branch to a PLT entry, we can
1131 make a copy of the FULL_PLT entry. Otherwise, we'll have
1132 to use a `brl' insn to get where we're going. */
1133
1134 size_t size;
1135
cc3bd654 1136 if (tsec == ia64_info->root.splt)
2c4c2bc0
RH
1137 size = sizeof (plt_full_entry);
1138 else
3f7deb8a 1139 size = oor_branch_size;
748abff6 1140
2c4c2bc0 1141 /* Resize the current section to make room for the new branch. */
eea6121a 1142 trampoff = (sec->size + 15) & (bfd_vma) -16;
de0d9f33
L
1143
1144 /* If trampoline is out of range, there is nothing we
1145 can do. */
1146 offset = trampoff - (roff & (bfd_vma) -4);
1147 if (offset < -0x1000000 || offset > 0x0FFFFF0)
1148 continue;
1149
2c4c2bc0
RH
1150 amt = trampoff + size;
1151 contents = (bfd_byte *) bfd_realloc (contents, amt);
1152 if (contents == NULL)
1153 goto error_return;
eea6121a 1154 sec->size = amt;
748abff6 1155
cc3bd654 1156 if (tsec == ia64_info->root.splt)
2c4c2bc0
RH
1157 {
1158 memcpy (contents + trampoff, plt_full_entry, size);
748abff6 1159
2c4c2bc0
RH
1160 /* Hijack the old relocation for use as the PLTOFF reloc. */
1161 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1162 R_IA64_PLTOFF22);
1163 irel->r_offset = trampoff;
1164 }
1165 else
1166 {
3f7deb8a
L
1167 if (size == sizeof (oor_ip))
1168 {
1169 memcpy (contents + trampoff, oor_ip, size);
1170 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1171 R_IA64_PCREL64I);
1172 irel->r_addend -= 16;
1173 irel->r_offset = trampoff + 2;
1174 }
1175 else
1176 {
1177 memcpy (contents + trampoff, oor_brl, size);
1178 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1179 R_IA64_PCREL60B);
1180 irel->r_offset = trampoff + 2;
1181 }
1182
2c4c2bc0
RH
1183 }
1184
1185 /* Record the fixup so we don't do it again this section. */
1186 f = (struct one_fixup *)
1187 bfd_malloc ((bfd_size_type) sizeof (*f));
1188 f->next = fixups;
1189 f->tsec = tsec;
1190 f->toff = toff;
1191 f->trampoff = trampoff;
1192 fixups = f;
748abff6 1193 }
2c4c2bc0
RH
1194 else
1195 {
de0d9f33
L
1196 /* If trampoline is out of range, there is nothing we
1197 can do. */
1198 offset = f->trampoff - (roff & (bfd_vma) -4);
1199 if (offset < -0x1000000 || offset > 0x0FFFFF0)
1200 continue;
1201
2c4c2bc0
RH
1202 /* Nop out the reloc, since we're finalizing things here. */
1203 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
1204 }
1205
de0d9f33 1206 /* Fix up the existing branch to hit the trampoline. */
bbb268c3
JW
1207 if (elfNN_ia64_install_value (contents + roff, offset, r_type)
1208 != bfd_reloc_ok)
2c4c2bc0 1209 goto error_return;
748abff6 1210
2c4c2bc0
RH
1211 changed_contents = TRUE;
1212 changed_relocs = TRUE;
748abff6
RH
1213 }
1214 else
1215 {
2c4c2bc0
RH
1216 /* Fetch the gp. */
1217 if (gp == 0)
1218 {
1219 bfd *obfd = sec->output_section->owner;
1220 gp = _bfd_get_gp_value (obfd);
1221 if (gp == 0)
1222 {
1223 if (!elfNN_ia64_choose_gp (obfd, link_info))
1224 goto error_return;
1225 gp = _bfd_get_gp_value (obfd);
1226 }
1227 }
748abff6 1228
2c4c2bc0 1229 /* If the data is out of range, do nothing. */
484a4f9c
RH
1230 if ((bfd_signed_vma) (symaddr - gp) >= 0x200000
1231 ||(bfd_signed_vma) (symaddr - gp) < -0x200000)
2c4c2bc0 1232 continue;
748abff6 1233
a231ddc0 1234 if (r_type == R_IA64_GPREL22)
affb4bf3 1235 elfNN_ia64_update_short_info (tsec->output_section,
a231ddc0
L
1236 tsec->output_offset + toff,
1237 ia64_info);
1238 else if (r_type == R_IA64_LTOFF22X)
2c4c2bc0
RH
1239 {
1240 irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
1241 R_IA64_GPREL22);
1242 changed_relocs = TRUE;
1243 if (dyn_i->want_gotx)
1244 {
1245 dyn_i->want_gotx = 0;
1246 changed_got |= !dyn_i->want_got;
1247 }
a231ddc0 1248
affb4bf3 1249 elfNN_ia64_update_short_info (tsec->output_section,
a231ddc0
L
1250 tsec->output_offset + toff,
1251 ia64_info);
2c4c2bc0
RH
1252 }
1253 else
1254 {
bbb268c3 1255 elfNN_ia64_relax_ldxmov (contents, roff);
2c4c2bc0
RH
1256 irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
1257 changed_contents = TRUE;
1258 changed_relocs = TRUE;
1259 }
1260 }
748abff6
RH
1261 }
1262
2c4c2bc0
RH
1263 /* ??? If we created fixups, this may push the code segment large
1264 enough that the data segment moves, which will change the GP.
1265 Reset the GP so that we re-calculate next round. We need to
1266 do this at the _beginning_ of the next round; now will not do. */
f12123c0 1267
748abff6
RH
1268 /* Clean up and go home. */
1269 while (fixups)
1270 {
1271 struct one_fixup *f = fixups;
1272 fixups = fixups->next;
1273 free (f);
1274 }
1275
6cdc0ccc
AM
1276 if (isymbuf != NULL
1277 && symtab_hdr->contents != (unsigned char *) isymbuf)
748abff6
RH
1278 {
1279 if (! link_info->keep_memory)
6cdc0ccc 1280 free (isymbuf);
748abff6
RH
1281 else
1282 {
6cdc0ccc
AM
1283 /* Cache the symbols for elf_link_input_bfd. */
1284 symtab_hdr->contents = (unsigned char *) isymbuf;
748abff6
RH
1285 }
1286 }
1287
6cdc0ccc
AM
1288 if (contents != NULL
1289 && elf_section_data (sec)->this_hdr.contents != contents)
748abff6 1290 {
6cdc0ccc
AM
1291 if (!changed_contents && !link_info->keep_memory)
1292 free (contents);
748abff6
RH
1293 else
1294 {
6cdc0ccc
AM
1295 /* Cache the section contents for elf_link_input_bfd. */
1296 elf_section_data (sec)->this_hdr.contents = contents;
748abff6
RH
1297 }
1298 }
1299
6cdc0ccc
AM
1300 if (elf_section_data (sec)->relocs != internal_relocs)
1301 {
1302 if (!changed_relocs)
1303 free (internal_relocs);
1304 else
1305 elf_section_data (sec)->relocs = internal_relocs;
1306 }
1307
2c4c2bc0
RH
1308 if (changed_got)
1309 {
1310 struct elfNN_ia64_allocate_data data;
1311 data.info = link_info;
1312 data.ofs = 0;
9d73f260 1313 ia64_info->self_dtpmod_offset = (bfd_vma) -1;
2c4c2bc0
RH
1314
1315 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
1316 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
1317 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
cc3bd654 1318 ia64_info->root.sgot->size = data.ofs;
2c4c2bc0 1319
90b263f3 1320 if (ia64_info->root.dynamic_sections_created
cc3bd654 1321 && ia64_info->root.srelgot != NULL)
4a78a1f4
AS
1322 {
1323 /* Resize .rela.got. */
cc3bd654 1324 ia64_info->root.srelgot->size = 0;
4a78a1f4
AS
1325 if (link_info->shared
1326 && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
cc3bd654 1327 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
4a78a1f4
AS
1328 data.only_got = TRUE;
1329 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries,
1330 &data);
1331 }
2c4c2bc0
RH
1332 }
1333
fbbc3759
L
1334 if (link_info->relax_pass == 0)
1335 {
1336 /* Pass 0 is only needed to relax br. */
1337 sec->skip_relax_pass_0 = skip_relax_pass_0;
1338 sec->skip_relax_pass_1 = skip_relax_pass_1;
1339 }
c7996ad6 1340
748abff6 1341 *again = changed_contents || changed_relocs;
b34976b6 1342 return TRUE;
748abff6
RH
1343
1344 error_return:
6cdc0ccc
AM
1345 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
1346 free (isymbuf);
1347 if (contents != NULL
1348 && elf_section_data (sec)->this_hdr.contents != contents)
1349 free (contents);
1350 if (internal_relocs != NULL
1351 && elf_section_data (sec)->relocs != internal_relocs)
1352 free (internal_relocs);
b34976b6 1353 return FALSE;
748abff6 1354}
fbbc3759
L
1355#undef skip_relax_pass_0
1356#undef skip_relax_pass_1
2c4c2bc0
RH
1357
1358static void
eae50df2 1359elfNN_ia64_relax_ldxmov (bfd_byte *contents, bfd_vma off)
2c4c2bc0
RH
1360{
1361 int shift, r1, r3;
1362 bfd_vma dword, insn;
1363
1364 switch ((int)off & 0x3)
1365 {
1366 case 0: shift = 5; break;
1367 case 1: shift = 14; off += 3; break;
1368 case 2: shift = 23; off += 6; break;
60d8b524 1369 default:
2c4c2bc0
RH
1370 abort ();
1371 }
1372
bbb268c3 1373 dword = bfd_getl64 (contents + off);
2c4c2bc0
RH
1374 insn = (dword >> shift) & 0x1ffffffffffLL;
1375
1376 r1 = (insn >> 6) & 127;
1377 r3 = (insn >> 20) & 127;
1378 if (r1 == r3)
1379 insn = 0x8000000; /* nop */
1380 else
1381 insn = (insn & 0x7f01fff) | 0x10800000000LL; /* (qp) mov r1 = r3 */
1382
1383 dword &= ~(0x1ffffffffffLL << shift);
1384 dword |= (insn << shift);
bbb268c3 1385 bfd_putl64 (dword, contents + off);
2c4c2bc0 1386}
800eeca4 1387\f
b34976b6 1388/* Return TRUE if NAME is an unwind table section name. */
81545d45 1389
b34976b6 1390static inline bfd_boolean
0112cd26 1391is_unwind_section_name (bfd *abfd, const char *name)
81545d45 1392{
d9cf1b54
AM
1393 if (elfNN_ia64_hpux_vec (abfd->xvec)
1394 && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
b34976b6 1395 return FALSE;
d9cf1b54 1396
0112cd26
NC
1397 return ((CONST_STRNEQ (name, ELF_STRING_ia64_unwind)
1398 && ! CONST_STRNEQ (name, ELF_STRING_ia64_unwind_info))
1399 || CONST_STRNEQ (name, ELF_STRING_ia64_unwind_once));
81545d45
RH
1400}
1401
800eeca4 1402/* Handle an IA-64 specific section when reading an object file. This
6dc132d9
L
1403 is called when bfd_section_from_shdr finds a section with an unknown
1404 type. */
800eeca4 1405
b34976b6 1406static bfd_boolean
6dc132d9
L
1407elfNN_ia64_section_from_shdr (bfd *abfd,
1408 Elf_Internal_Shdr *hdr,
1409 const char *name,
1410 int shindex)
800eeca4
JW
1411{
1412 asection *newsect;
1413
1414 /* There ought to be a place to keep ELF backend specific flags, but
1415 at the moment there isn't one. We just keep track of the
1416 sections by their name, instead. Fortunately, the ABI gives
1417 suggested names for all the MIPS specific sections, so we will
1418 probably get away with this. */
1419 switch (hdr->sh_type)
1420 {
1421 case SHT_IA_64_UNWIND:
d9cf1b54 1422 case SHT_IA_64_HP_OPT_ANOT:
800eeca4
JW
1423 break;
1424
1425 case SHT_IA_64_EXT:
1426 if (strcmp (name, ELF_STRING_ia64_archext) != 0)
b34976b6 1427 return FALSE;
800eeca4
JW
1428 break;
1429
1430 default:
b34976b6 1431 return FALSE;
800eeca4
JW
1432 }
1433
6dc132d9 1434 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 1435 return FALSE;
800eeca4
JW
1436 newsect = hdr->bfd_section;
1437
b34976b6 1438 return TRUE;
fa152c49
JW
1439}
1440
1441/* Convert IA-64 specific section flags to bfd internal section flags. */
1442
1443/* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
1444 flag. */
1445
b34976b6 1446static bfd_boolean
eae50df2
L
1447elfNN_ia64_section_flags (flagword *flags,
1448 const Elf_Internal_Shdr *hdr)
fa152c49 1449{
800eeca4 1450 if (hdr->sh_flags & SHF_IA_64_SHORT)
fa152c49 1451 *flags |= SEC_SMALL_DATA;
800eeca4 1452
b34976b6 1453 return TRUE;
800eeca4
JW
1454}
1455
1456/* Set the correct type for an IA-64 ELF section. We do this by the
1457 section name, which is a hack, but ought to work. */
1458
b34976b6 1459static bfd_boolean
eae50df2
L
1460elfNN_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
1461 asection *sec)
800eeca4 1462{
91d6fa6a 1463 const char *name;
800eeca4
JW
1464
1465 name = bfd_get_section_name (abfd, sec);
1466
d9cf1b54 1467 if (is_unwind_section_name (abfd, name))
81545d45
RH
1468 {
1469 /* We don't have the sections numbered at this point, so sh_info
1470 is set later, in elfNN_ia64_final_write_processing. */
1471 hdr->sh_type = SHT_IA_64_UNWIND;
1472 hdr->sh_flags |= SHF_LINK_ORDER;
1473 }
800eeca4
JW
1474 else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
1475 hdr->sh_type = SHT_IA_64_EXT;
d9cf1b54
AM
1476 else if (strcmp (name, ".HP.opt_annot") == 0)
1477 hdr->sh_type = SHT_IA_64_HP_OPT_ANOT;
800eeca4 1478 else if (strcmp (name, ".reloc") == 0)
5e8d7549
NC
1479 /* This is an ugly, but unfortunately necessary hack that is
1480 needed when producing EFI binaries on IA-64. It tells
1481 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1482 containing ELF relocation info. We need this hack in order to
1483 be able to generate ELF binaries that can be translated into
1484 EFI applications (which are essentially COFF objects). Those
1485 files contain a COFF ".reloc" section inside an ELFNN object,
1486 which would normally cause BFD to segfault because it would
1487 attempt to interpret this section as containing relocation
1488 entries for section "oc". With this hack enabled, ".reloc"
1489 will be treated as a normal data section, which will avoid the
1490 segfault. However, you won't be able to create an ELFNN binary
1491 with a section named "oc" that needs relocations, but that's
1492 the kind of ugly side-effects you get when detecting section
1493 types based on their names... In practice, this limitation is
1494 unlikely to bite. */
800eeca4
JW
1495 hdr->sh_type = SHT_PROGBITS;
1496
1497 if (sec->flags & SEC_SMALL_DATA)
1498 hdr->sh_flags |= SHF_IA_64_SHORT;
1499
75eb734c
SE
1500 /* Some HP linkers look for the SHF_IA_64_HP_TLS flag instead of SHF_TLS. */
1501
1502 if (elfNN_ia64_hpux_vec (abfd->xvec) && (sec->flags & SHF_TLS))
1503 hdr->sh_flags |= SHF_IA_64_HP_TLS;
1504
b34976b6 1505 return TRUE;
800eeca4
JW
1506}
1507
81545d45
RH
1508/* The final processing done just before writing out an IA-64 ELF
1509 object file. */
1510
1511static void
eae50df2
L
1512elfNN_ia64_final_write_processing (bfd *abfd,
1513 bfd_boolean linker ATTRIBUTE_UNUSED)
81545d45
RH
1514{
1515 Elf_Internal_Shdr *hdr;
38ce5b11 1516 asection *s;
81545d45
RH
1517
1518 for (s = abfd->sections; s; s = s->next)
1519 {
1520 hdr = &elf_section_data (s)->this_hdr;
1521 switch (hdr->sh_type)
1522 {
1523 case SHT_IA_64_UNWIND:
38ce5b11
L
1524 /* The IA-64 processor-specific ABI requires setting sh_link
1525 to the unwind section, whereas HP-UX requires sh_info to
1526 do so. For maximum compatibility, we'll set both for
1527 now... */
1528 hdr->sh_info = hdr->sh_link;
81545d45
RH
1529 break;
1530 }
1531 }
9d46020e
AM
1532
1533 if (! elf_flags_init (abfd))
1534 {
1535 unsigned long flags = 0;
1536
1537 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
1538 flags |= EF_IA_64_BE;
1539 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
1540 flags |= EF_IA_64_ABI64;
1541
1542 elf_elfheader(abfd)->e_flags = flags;
b34976b6 1543 elf_flags_init (abfd) = TRUE;
9d46020e 1544 }
81545d45
RH
1545}
1546
800eeca4
JW
1547/* Hook called by the linker routine which adds symbols from an object
1548 file. We use it to put .comm items in .sbss, and not .bss. */
1549
b34976b6 1550static bfd_boolean
a30e5f5f
AM
1551elfNN_ia64_add_symbol_hook (bfd *abfd,
1552 struct bfd_link_info *info,
eae50df2 1553 Elf_Internal_Sym *sym,
a30e5f5f
AM
1554 const char **namep ATTRIBUTE_UNUSED,
1555 flagword *flagsp ATTRIBUTE_UNUSED,
1556 asection **secp,
1557 bfd_vma *valp)
800eeca4
JW
1558{
1559 if (sym->st_shndx == SHN_COMMON
1049f94e 1560 && !info->relocatable
c0846b23 1561 && sym->st_size <= elf_gp_size (abfd))
800eeca4
JW
1562 {
1563 /* Common symbols less than or equal to -G nn bytes are
1564 automatically put into .sbss. */
1565
1566 asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
1567
1568 if (scomm == NULL)
1569 {
3496cb2a
L
1570 scomm = bfd_make_section_with_flags (abfd, ".scommon",
1571 (SEC_ALLOC
1572 | SEC_IS_COMMON
1573 | SEC_LINKER_CREATED));
1574 if (scomm == NULL)
b34976b6 1575 return FALSE;
800eeca4
JW
1576 }
1577
1578 *secp = scomm;
1579 *valp = sym->st_size;
1580 }
1581
b34976b6 1582 return TRUE;
800eeca4
JW
1583}
1584
1585/* Return the number of additional phdrs we will need. */
1586
1587static int
a6b96beb
AM
1588elfNN_ia64_additional_program_headers (bfd *abfd,
1589 struct bfd_link_info *info ATTRIBUTE_UNUSED)
800eeca4
JW
1590{
1591 asection *s;
1592 int ret = 0;
1593
1594 /* See if we need a PT_IA_64_ARCHEXT segment. */
1595 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1596 if (s && (s->flags & SEC_LOAD))
1597 ++ret;
1598
81545d45
RH
1599 /* Count how many PT_IA_64_UNWIND segments we need. */
1600 for (s = abfd->sections; s; s = s->next)
d9cf1b54 1601 if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
81545d45 1602 ++ret;
800eeca4
JW
1603
1604 return ret;
1605}
1606
b34976b6 1607static bfd_boolean
8ded5a0f
AM
1608elfNN_ia64_modify_segment_map (bfd *abfd,
1609 struct bfd_link_info *info ATTRIBUTE_UNUSED)
800eeca4
JW
1610{
1611 struct elf_segment_map *m, **pm;
81545d45 1612 Elf_Internal_Shdr *hdr;
800eeca4
JW
1613 asection *s;
1614
1615 /* If we need a PT_IA_64_ARCHEXT segment, it must come before
1616 all PT_LOAD segments. */
1617 s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
1618 if (s && (s->flags & SEC_LOAD))
1619 {
1620 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1621 if (m->p_type == PT_IA_64_ARCHEXT)
1622 break;
1623 if (m == NULL)
1624 {
dc810e39
AM
1625 m = ((struct elf_segment_map *)
1626 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
800eeca4 1627 if (m == NULL)
b34976b6 1628 return FALSE;
800eeca4
JW
1629
1630 m->p_type = PT_IA_64_ARCHEXT;
1631 m->count = 1;
1632 m->sections[0] = s;
1633
1634 /* We want to put it after the PHDR and INTERP segments. */
1635 pm = &elf_tdata (abfd)->segment_map;
1636 while (*pm != NULL
1637 && ((*pm)->p_type == PT_PHDR
1638 || (*pm)->p_type == PT_INTERP))
1639 pm = &(*pm)->next;
1640
1641 m->next = *pm;
1642 *pm = m;
1643 }
1644 }
1645
81545d45
RH
1646 /* Install PT_IA_64_UNWIND segments, if needed. */
1647 for (s = abfd->sections; s; s = s->next)
800eeca4 1648 {
81545d45
RH
1649 hdr = &elf_section_data (s)->this_hdr;
1650 if (hdr->sh_type != SHT_IA_64_UNWIND)
1651 continue;
1652
1653 if (s && (s->flags & SEC_LOAD))
800eeca4 1654 {
81545d45 1655 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
d9cf1b54
AM
1656 if (m->p_type == PT_IA_64_UNWIND)
1657 {
40c97fc6
AM
1658 int i;
1659
d9cf1b54
AM
1660 /* Look through all sections in the unwind segment
1661 for a match since there may be multiple sections
1662 to a segment. */
40c97fc6
AM
1663 for (i = m->count - 1; i >= 0; --i)
1664 if (m->sections[i] == s)
1665 break;
d9cf1b54 1666
40c97fc6 1667 if (i >= 0)
d9cf1b54
AM
1668 break;
1669 }
81545d45 1670
800eeca4 1671 if (m == NULL)
81545d45 1672 {
dc810e39
AM
1673 m = ((struct elf_segment_map *)
1674 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
81545d45 1675 if (m == NULL)
b34976b6 1676 return FALSE;
800eeca4 1677
81545d45
RH
1678 m->p_type = PT_IA_64_UNWIND;
1679 m->count = 1;
1680 m->sections[0] = s;
1681 m->next = NULL;
800eeca4 1682
81545d45
RH
1683 /* We want to put it last. */
1684 pm = &elf_tdata (abfd)->segment_map;
1685 while (*pm != NULL)
1686 pm = &(*pm)->next;
1687 *pm = m;
1688 }
800eeca4
JW
1689 }
1690 }
1691
e36284ab
AM
1692 return TRUE;
1693}
1694
1695/* Turn on PF_IA_64_NORECOV if needed. This involves traversing all of
1696 the input sections for each output section in the segment and testing
1697 for SHF_IA_64_NORECOV on each. */
1698
1699static bfd_boolean
1700elfNN_ia64_modify_program_headers (bfd *abfd,
1701 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1702{
1703 struct elf_obj_tdata *tdata = elf_tdata (abfd);
1704 struct elf_segment_map *m;
1705 Elf_Internal_Phdr *p;
1706
1707 for (p = tdata->phdr, m = tdata->segment_map; m != NULL; m = m->next, p++)
800eeca4
JW
1708 if (m->p_type == PT_LOAD)
1709 {
1710 int i;
1711 for (i = m->count - 1; i >= 0; --i)
1712 {
8423293d 1713 struct bfd_link_order *order = m->sections[i]->map_head.link_order;
e36284ab
AM
1714
1715 while (order != NULL)
800eeca4
JW
1716 {
1717 if (order->type == bfd_indirect_link_order)
1718 {
1719 asection *is = order->u.indirect.section;
1720 bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
1721 if (flags & SHF_IA_64_NORECOV)
1722 {
e36284ab 1723 p->p_flags |= PF_IA_64_NORECOV;
800eeca4
JW
1724 goto found;
1725 }
1726 }
1727 order = order->next;
1728 }
1729 }
1730 found:;
1731 }
1732
b34976b6 1733 return TRUE;
800eeca4
JW
1734}
1735
800eeca4
JW
1736/* According to the Tahoe assembler spec, all labels starting with a
1737 '.' are local. */
1738
b34976b6 1739static bfd_boolean
eae50df2
L
1740elfNN_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1741 const char *name)
800eeca4
JW
1742{
1743 return name[0] == '.';
1744}
1745
1746/* Should we do dynamic things to this symbol? */
1747
b34976b6 1748static bfd_boolean
eae50df2
L
1749elfNN_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h,
1750 struct bfd_link_info *info, int r_type)
800eeca4 1751{
986a241f
RH
1752 bfd_boolean ignore_protected
1753 = ((r_type & 0xf8) == 0x40 /* FPTR relocs */
1754 || (r_type & 0xf8) == 0x50); /* LTOFF_FPTR relocs */
800eeca4 1755
986a241f 1756 return _bfd_elf_dynamic_symbol_p (h, info, ignore_protected);
800eeca4
JW
1757}
1758\f
800eeca4 1759static struct bfd_hash_entry*
eae50df2
L
1760elfNN_ia64_new_elf_hash_entry (struct bfd_hash_entry *entry,
1761 struct bfd_hash_table *table,
1762 const char *string)
800eeca4 1763{
bbe66d08
JW
1764 struct elfNN_ia64_link_hash_entry *ret;
1765 ret = (struct elfNN_ia64_link_hash_entry *) entry;
800eeca4
JW
1766
1767 /* Allocate the structure if it has not already been allocated by a
1768 subclass. */
1769 if (!ret)
1770 ret = bfd_hash_allocate (table, sizeof (*ret));
1771
1772 if (!ret)
1773 return 0;
1774
800eeca4 1775 /* Call the allocation method of the superclass. */
bbe66d08 1776 ret = ((struct elfNN_ia64_link_hash_entry *)
800eeca4
JW
1777 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1778 table, string));
1779
4f40114d 1780 ret->info = NULL;
396a682d
L
1781 ret->count = 0;
1782 ret->sorted_count = 0;
1783 ret->size = 0;
800eeca4
JW
1784 return (struct bfd_hash_entry *) ret;
1785}
1786
1787static void
eae50df2
L
1788elfNN_ia64_hash_copy_indirect (struct bfd_link_info *info,
1789 struct elf_link_hash_entry *xdir,
1790 struct elf_link_hash_entry *xind)
800eeca4 1791{
bbe66d08 1792 struct elfNN_ia64_link_hash_entry *dir, *ind;
800eeca4 1793
57c7194e
AM
1794 dir = (struct elfNN_ia64_link_hash_entry *) xdir;
1795 ind = (struct elfNN_ia64_link_hash_entry *) xind;
800eeca4 1796
3e932841 1797 /* Copy down any references that we may have already seen to the
800eeca4
JW
1798 symbol which just became indirect. */
1799
f5385ebf
AM
1800 dir->root.ref_dynamic |= ind->root.ref_dynamic;
1801 dir->root.ref_regular |= ind->root.ref_regular;
1802 dir->root.ref_regular_nonweak |= ind->root.ref_regular_nonweak;
1803 dir->root.needs_plt |= ind->root.needs_plt;
800eeca4 1804
1e370bd2 1805 if (ind->root.root.type != bfd_link_hash_indirect)
0a991dfe
AM
1806 return;
1807
800eeca4
JW
1808 /* Copy over the got and plt data. This would have been done
1809 by check_relocs. */
1810
fcfa13d2 1811 if (ind->info != NULL)
800eeca4 1812 {
bbe66d08 1813 struct elfNN_ia64_dyn_sym_info *dyn_i;
396a682d
L
1814 unsigned int count;
1815
1816 if (dir->info)
1817 free (dir->info);
1818
1819 dir->info = ind->info;
1820 dir->count = ind->count;
1821 dir->sorted_count = ind->sorted_count;
1822 dir->size = ind->size;
800eeca4 1823
800eeca4 1824 ind->info = NULL;
396a682d
L
1825 ind->count = 0;
1826 ind->sorted_count = 0;
1827 ind->size = 0;
800eeca4
JW
1828
1829 /* Fix up the dyn_sym_info pointers to the global symbol. */
396a682d
L
1830 for (count = dir->count, dyn_i = dir->info;
1831 count != 0;
1832 count--, dyn_i++)
800eeca4
JW
1833 dyn_i->h = &dir->root;
1834 }
800eeca4
JW
1835
1836 /* Copy over the dynindx. */
1837
fcfa13d2 1838 if (ind->root.dynindx != -1)
800eeca4 1839 {
fcfa13d2
AM
1840 if (dir->root.dynindx != -1)
1841 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1842 dir->root.dynstr_index);
800eeca4
JW
1843 dir->root.dynindx = ind->root.dynindx;
1844 dir->root.dynstr_index = ind->root.dynstr_index;
1845 ind->root.dynindx = -1;
1846 ind->root.dynstr_index = 0;
1847 }
800eeca4
JW
1848}
1849
1850static void
eae50df2
L
1851elfNN_ia64_hash_hide_symbol (struct bfd_link_info *info,
1852 struct elf_link_hash_entry *xh,
1853 bfd_boolean force_local)
800eeca4 1854{
bbe66d08
JW
1855 struct elfNN_ia64_link_hash_entry *h;
1856 struct elfNN_ia64_dyn_sym_info *dyn_i;
396a682d 1857 unsigned int count;
800eeca4 1858
bbe66d08 1859 h = (struct elfNN_ia64_link_hash_entry *)xh;
800eeca4 1860
e5094212 1861 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
800eeca4 1862
396a682d
L
1863 for (count = h->count, dyn_i = h->info;
1864 count != 0;
1865 count--, dyn_i++)
6a32c710
L
1866 {
1867 dyn_i->want_plt2 = 0;
1868 dyn_i->want_plt = 0;
1869 }
800eeca4
JW
1870}
1871
0aa92b58
JJ
1872/* Compute a hash of a local hash entry. */
1873
1874static hashval_t
eae50df2 1875elfNN_ia64_local_htab_hash (const void *ptr)
0aa92b58
JJ
1876{
1877 struct elfNN_ia64_local_hash_entry *entry
1878 = (struct elfNN_ia64_local_hash_entry *) ptr;
1879
d2149d72 1880 return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
0aa92b58
JJ
1881}
1882
1883/* Compare local hash entries. */
1884
1885static int
eae50df2 1886elfNN_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
0aa92b58
JJ
1887{
1888 struct elfNN_ia64_local_hash_entry *entry1
1889 = (struct elfNN_ia64_local_hash_entry *) ptr1;
1890 struct elfNN_ia64_local_hash_entry *entry2
1891 = (struct elfNN_ia64_local_hash_entry *) ptr2;
1892
1893 return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
1894}
1895
800eeca4
JW
1896/* Create the derived linker hash table. The IA-64 ELF port uses this
1897 derived hash table to keep information specific to the IA-64 ElF
1898 linker (without using static variables). */
1899
4dfe6ac6 1900static struct bfd_link_hash_table *
eae50df2 1901elfNN_ia64_hash_table_create (bfd *abfd)
800eeca4 1902{
bbe66d08 1903 struct elfNN_ia64_link_hash_table *ret;
800eeca4 1904
6e84a906 1905 ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
800eeca4 1906 if (!ret)
4dfe6ac6 1907 return NULL;
6e84a906 1908
800eeca4 1909 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
66eb6687 1910 elfNN_ia64_new_elf_hash_entry,
4dfe6ac6
NC
1911 sizeof (struct elfNN_ia64_link_hash_entry),
1912 IA64_ELF_DATA))
800eeca4 1913 {
6e84a906 1914 free (ret);
4dfe6ac6 1915 return NULL;
800eeca4
JW
1916 }
1917
0aa92b58
JJ
1918 ret->loc_hash_table = htab_try_create (1024, elfNN_ia64_local_htab_hash,
1919 elfNN_ia64_local_htab_eq, NULL);
1920 ret->loc_hash_memory = objalloc_create ();
1921 if (!ret->loc_hash_table || !ret->loc_hash_memory)
6e84a906
DJ
1922 {
1923 free (ret);
4dfe6ac6 1924 return NULL;
6e84a906
DJ
1925 }
1926
800eeca4
JW
1927 return &ret->root.root;
1928}
1929
396a682d
L
1930/* Free the global elfNN_ia64_dyn_sym_info array. */
1931
1932static bfd_boolean
1933elfNN_ia64_global_dyn_info_free (void **xentry,
1934 PTR unused ATTRIBUTE_UNUSED)
1935{
1936 struct elfNN_ia64_link_hash_entry *entry
1937 = (struct elfNN_ia64_link_hash_entry *) xentry;
1938
1939 if (entry->root.root.type == bfd_link_hash_warning)
1940 entry = (struct elfNN_ia64_link_hash_entry *) entry->root.root.u.i.link;
1941
1942 if (entry->info)
1943 {
1944 free (entry->info);
1945 entry->info = NULL;
1946 entry->count = 0;
1947 entry->sorted_count = 0;
1948 entry->size = 0;
1949 }
1950
1951 return TRUE;
1952}
1953
1954/* Free the local elfNN_ia64_dyn_sym_info array. */
1955
1956static bfd_boolean
1957elfNN_ia64_local_dyn_info_free (void **slot,
1958 PTR unused ATTRIBUTE_UNUSED)
1959{
1960 struct elfNN_ia64_local_hash_entry *entry
1961 = (struct elfNN_ia64_local_hash_entry *) *slot;
1962
1963 if (entry->info)
1964 {
1965 free (entry->info);
1966 entry->info = NULL;
1967 entry->count = 0;
1968 entry->sorted_count = 0;
1969 entry->size = 0;
1970 }
1971
1972 return TRUE;
1973}
1974
0aa92b58 1975/* Destroy IA-64 linker hash table. */
800eeca4 1976
0aa92b58 1977static void
eae50df2 1978elfNN_ia64_hash_table_free (struct bfd_link_hash_table *hash)
800eeca4 1979{
0aa92b58
JJ
1980 struct elfNN_ia64_link_hash_table *ia64_info
1981 = (struct elfNN_ia64_link_hash_table *) hash;
1982 if (ia64_info->loc_hash_table)
396a682d
L
1983 {
1984 htab_traverse (ia64_info->loc_hash_table,
1985 elfNN_ia64_local_dyn_info_free, NULL);
1986 htab_delete (ia64_info->loc_hash_table);
1987 }
0aa92b58
JJ
1988 if (ia64_info->loc_hash_memory)
1989 objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
396a682d
L
1990 elf_link_hash_traverse (&ia64_info->root,
1991 elfNN_ia64_global_dyn_info_free, NULL);
0aa92b58 1992 _bfd_generic_link_hash_table_free (hash);
800eeca4
JW
1993}
1994
1995/* Traverse both local and global hash tables. */
1996
bbe66d08 1997struct elfNN_ia64_dyn_sym_traverse_data
800eeca4 1998{
eae50df2 1999 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR);
800eeca4
JW
2000 PTR data;
2001};
2002
b34976b6 2003static bfd_boolean
eae50df2
L
2004elfNN_ia64_global_dyn_sym_thunk (struct bfd_hash_entry *xentry,
2005 PTR xdata)
800eeca4 2006{
bbe66d08
JW
2007 struct elfNN_ia64_link_hash_entry *entry
2008 = (struct elfNN_ia64_link_hash_entry *) xentry;
2009 struct elfNN_ia64_dyn_sym_traverse_data *data
2010 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
2011 struct elfNN_ia64_dyn_sym_info *dyn_i;
396a682d 2012 unsigned int count;
800eeca4 2013
e92d460e
AM
2014 if (entry->root.root.type == bfd_link_hash_warning)
2015 entry = (struct elfNN_ia64_link_hash_entry *) entry->root.root.u.i.link;
2016
396a682d
L
2017 for (count = entry->count, dyn_i = entry->info;
2018 count != 0;
2019 count--, dyn_i++)
800eeca4 2020 if (! (*data->func) (dyn_i, data->data))
b34976b6
AM
2021 return FALSE;
2022 return TRUE;
800eeca4
JW
2023}
2024
b34976b6 2025static bfd_boolean
eae50df2 2026elfNN_ia64_local_dyn_sym_thunk (void **slot, PTR xdata)
800eeca4 2027{
bbe66d08 2028 struct elfNN_ia64_local_hash_entry *entry
0aa92b58 2029 = (struct elfNN_ia64_local_hash_entry *) *slot;
bbe66d08
JW
2030 struct elfNN_ia64_dyn_sym_traverse_data *data
2031 = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
2032 struct elfNN_ia64_dyn_sym_info *dyn_i;
396a682d 2033 unsigned int count;
800eeca4 2034
396a682d
L
2035 for (count = entry->count, dyn_i = entry->info;
2036 count != 0;
2037 count--, dyn_i++)
800eeca4 2038 if (! (*data->func) (dyn_i, data->data))
396a682d
L
2039 return FALSE;
2040 return TRUE;
800eeca4
JW
2041}
2042
2043static void
eae50df2
L
2044elfNN_ia64_dyn_sym_traverse (struct elfNN_ia64_link_hash_table *ia64_info,
2045 bfd_boolean (*func) (struct elfNN_ia64_dyn_sym_info *, PTR),
2046 PTR data)
800eeca4 2047{
bbe66d08 2048 struct elfNN_ia64_dyn_sym_traverse_data xdata;
800eeca4
JW
2049
2050 xdata.func = func;
2051 xdata.data = data;
2052
2053 elf_link_hash_traverse (&ia64_info->root,
bbe66d08 2054 elfNN_ia64_global_dyn_sym_thunk, &xdata);
0aa92b58
JJ
2055 htab_traverse (ia64_info->loc_hash_table,
2056 elfNN_ia64_local_dyn_sym_thunk, &xdata);
800eeca4
JW
2057}
2058\f
b34976b6 2059static bfd_boolean
eae50df2
L
2060elfNN_ia64_create_dynamic_sections (bfd *abfd,
2061 struct bfd_link_info *info)
800eeca4 2062{
bbe66d08 2063 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
2064 asection *s;
2065
2066 if (! _bfd_elf_create_dynamic_sections (abfd, info))
b34976b6 2067 return FALSE;
800eeca4 2068
bbe66d08 2069 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
2070 if (ia64_info == NULL)
2071 return FALSE;
800eeca4 2072
800eeca4 2073 {
cc3bd654
L
2074 flagword flags = bfd_get_section_flags (abfd, ia64_info->root.sgot);
2075 bfd_set_section_flags (abfd, ia64_info->root.sgot,
2076 SEC_SMALL_DATA | flags);
69bbc4c0 2077 /* The .got section is always aligned at 8 bytes. */
cc3bd654 2078 bfd_set_section_alignment (abfd, ia64_info->root.sgot, 3);
800eeca4
JW
2079 }
2080
2081 if (!get_pltoff (abfd, info, ia64_info))
b34976b6 2082 return FALSE;
800eeca4 2083
3496cb2a
L
2084 s = bfd_make_section_with_flags (abfd, ".rela.IA_64.pltoff",
2085 (SEC_ALLOC | SEC_LOAD
2086 | SEC_HAS_CONTENTS
2087 | SEC_IN_MEMORY
2088 | SEC_LINKER_CREATED
2089 | SEC_READONLY));
800eeca4 2090 if (s == NULL
5a260b66 2091 || !bfd_set_section_alignment (abfd, s, LOG_SECTION_ALIGN))
b34976b6 2092 return FALSE;
800eeca4
JW
2093 ia64_info->rel_pltoff_sec = s;
2094
b34976b6 2095 return TRUE;
800eeca4
JW
2096}
2097
f7460f5f
JJ
2098/* Find and/or create a hash entry for local symbol. */
2099static struct elfNN_ia64_local_hash_entry *
eae50df2
L
2100get_local_sym_hash (struct elfNN_ia64_link_hash_table *ia64_info,
2101 bfd *abfd, const Elf_Internal_Rela *rel,
2102 bfd_boolean create)
f7460f5f 2103{
0aa92b58 2104 struct elfNN_ia64_local_hash_entry e, *ret;
d48770d4 2105 asection *sec = abfd->sections;
d2149d72
L
2106 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
2107 ELFNN_R_SYM (rel->r_info));
0aa92b58 2108 void **slot;
d48770d4 2109
0aa92b58
JJ
2110 e.id = sec->id;
2111 e.r_sym = ELFNN_R_SYM (rel->r_info);
2112 slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
2113 create ? INSERT : NO_INSERT);
f7460f5f 2114
0aa92b58
JJ
2115 if (!slot)
2116 return NULL;
f7460f5f 2117
0aa92b58
JJ
2118 if (*slot)
2119 return (struct elfNN_ia64_local_hash_entry *) *slot;
f7460f5f 2120
0aa92b58
JJ
2121 ret = (struct elfNN_ia64_local_hash_entry *)
2122 objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
2123 sizeof (struct elfNN_ia64_local_hash_entry));
2124 if (ret)
2125 {
2126 memset (ret, 0, sizeof (*ret));
2127 ret->id = sec->id;
2128 ret->r_sym = ELFNN_R_SYM (rel->r_info);
2129 *slot = ret;
2130 }
fcf12726 2131 return ret;
f7460f5f
JJ
2132}
2133
396a682d
L
2134/* Used to sort elfNN_ia64_dyn_sym_info array. */
2135
2136static int
2137addend_compare (const void *xp, const void *yp)
2138{
2139 const struct elfNN_ia64_dyn_sym_info *x
2140 = (const struct elfNN_ia64_dyn_sym_info *) xp;
2141 const struct elfNN_ia64_dyn_sym_info *y
2142 = (const struct elfNN_ia64_dyn_sym_info *) yp;
2143
c26620e3 2144 return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
396a682d
L
2145}
2146
2147/* Sort elfNN_ia64_dyn_sym_info array and remove duplicates. */
2148
2149static unsigned int
2150sort_dyn_sym_info (struct elfNN_ia64_dyn_sym_info *info,
2151 unsigned int count)
2152{
293a0124 2153 bfd_vma curr, prev, got_offset;
91d6fa6a 2154 unsigned int i, kept, dupes, diff, dest, src, len;
396a682d
L
2155
2156 qsort (info, count, sizeof (*info), addend_compare);
2157
2158 /* Find the first duplicate. */
2159 prev = info [0].addend;
293a0124 2160 got_offset = info [0].got_offset;
396a682d
L
2161 for (i = 1; i < count; i++)
2162 {
2163 curr = info [i].addend;
2164 if (curr == prev)
293a0124
L
2165 {
2166 /* For duplicates, make sure that GOT_OFFSET is valid. */
2167 if (got_offset == (bfd_vma) -1)
2168 got_offset = info [i].got_offset;
2169 break;
2170 }
2171 got_offset = info [i].got_offset;
396a682d
L
2172 prev = curr;
2173 }
2174
293a0124
L
2175 /* We may move a block of elements to here. */
2176 dest = i++;
2177
396a682d
L
2178 /* Remove duplicates. */
2179 if (i < count)
2180 {
396a682d
L
2181 while (i < count)
2182 {
293a0124
L
2183 /* For duplicates, make sure that the kept one has a valid
2184 got_offset. */
2185 kept = dest - 1;
2186 if (got_offset != (bfd_vma) -1)
2187 info [kept].got_offset = got_offset;
2188
396a682d 2189 curr = info [i].addend;
293a0124 2190 got_offset = info [i].got_offset;
396a682d
L
2191
2192 /* Move a block of elements whose first one is different from
2193 the previous. */
2194 if (curr == prev)
2195 {
2196 for (src = i + 1; src < count; src++)
293a0124
L
2197 {
2198 if (info [src].addend != curr)
2199 break;
2200 /* For duplicates, make sure that GOT_OFFSET is
2201 valid. */
2202 if (got_offset == (bfd_vma) -1)
2203 got_offset = info [src].got_offset;
2204 }
2205
2206 /* Make sure that the kept one has a valid got_offset. */
2207 if (got_offset != (bfd_vma) -1)
2208 info [kept].got_offset = got_offset;
396a682d
L
2209 }
2210 else
2211 src = i;
2212
2213 if (src >= count)
2214 break;
2215
293a0124 2216 /* Find the next duplicate. SRC will be kept. */
396a682d 2217 prev = info [src].addend;
293a0124 2218 got_offset = info [src].got_offset;
91d6fa6a 2219 for (dupes = src + 1; dupes < count; dupes ++)
396a682d 2220 {
91d6fa6a 2221 curr = info [dupes].addend;
396a682d 2222 if (curr == prev)
293a0124
L
2223 {
2224 /* Make sure that got_offset is valid. */
2225 if (got_offset == (bfd_vma) -1)
91d6fa6a 2226 got_offset = info [dupes].got_offset;
293a0124
L
2227
2228 /* For duplicates, make sure that the kept one has
2229 a valid got_offset. */
2230 if (got_offset != (bfd_vma) -1)
91d6fa6a 2231 info [dupes - 1].got_offset = got_offset;
293a0124
L
2232 break;
2233 }
91d6fa6a 2234 got_offset = info [dupes].got_offset;
396a682d
L
2235 prev = curr;
2236 }
2237
2238 /* How much to move. */
91d6fa6a
NC
2239 len = dupes - src;
2240 i = dupes + 1;
396a682d 2241
91d6fa6a 2242 if (len == 1 && dupes < count)
396a682d
L
2243 {
2244 /* If we only move 1 element, we combine it with the next
293a0124
L
2245 one. There must be at least a duplicate. Find the
2246 next different one. */
91d6fa6a 2247 for (diff = dupes + 1, src++; diff < count; diff++, src++)
293a0124
L
2248 {
2249 if (info [diff].addend != curr)
2250 break;
2251 /* Make sure that got_offset is valid. */
2252 if (got_offset == (bfd_vma) -1)
2253 got_offset = info [diff].got_offset;
2254 }
2255
2256 /* Makre sure that the last duplicated one has an valid
2257 offset. */
2258 BFD_ASSERT (curr == prev);
2259 if (got_offset != (bfd_vma) -1)
2260 info [diff - 1].got_offset = got_offset;
396a682d
L
2261
2262 if (diff < count)
2263 {
293a0124
L
2264 /* Find the next duplicate. Track the current valid
2265 offset. */
396a682d 2266 prev = info [diff].addend;
293a0124 2267 got_offset = info [diff].got_offset;
91d6fa6a 2268 for (dupes = diff + 1; dupes < count; dupes ++)
396a682d 2269 {
91d6fa6a 2270 curr = info [dupes].addend;
396a682d 2271 if (curr == prev)
293a0124
L
2272 {
2273 /* For duplicates, make sure that GOT_OFFSET
2274 is valid. */
2275 if (got_offset == (bfd_vma) -1)
91d6fa6a 2276 got_offset = info [dupes].got_offset;
293a0124
L
2277 break;
2278 }
91d6fa6a 2279 got_offset = info [dupes].got_offset;
396a682d
L
2280 prev = curr;
2281 diff++;
2282 }
2283
2284 len = diff - src + 1;
2285 i = diff + 1;
2286 }
2287 }
2288
2289 memmove (&info [dest], &info [src], len * sizeof (*info));
2290
2291 dest += len;
2292 }
2293
2294 count = dest;
2295 }
293a0124
L
2296 else
2297 {
2298 /* When we get here, either there is no duplicate at all or
2299 the only duplicate is the last element. */
2300 if (dest < count)
2301 {
2302 /* If the last element is a duplicate, make sure that the
2303 kept one has a valid got_offset. We also update count. */
2304 if (got_offset != (bfd_vma) -1)
2305 info [dest - 1].got_offset = got_offset;
2306 count = dest;
2307 }
2308 }
396a682d
L
2309
2310 return count;
2311}
2312
800eeca4 2313/* Find and/or create a descriptor for dynamic symbol info. This will
396a682d
L
2314 vary based on global or local symbol, and the addend to the reloc.
2315
2316 We don't sort when inserting. Also, we sort and eliminate
2317 duplicates if there is an unsorted section. Typically, this will
2318 only happen once, because we do all insertions before lookups. We
2319 then use bsearch to do a lookup. This also allows lookups to be
2320 fast. So we have fast insertion (O(log N) due to duplicate check),
2321 fast lookup (O(log N)) and one sort (O(N log N) expected time).
2322 Previously, all lookups were O(N) because of the use of the linked
2323 list and also all insertions were O(N) because of the check for
2324 duplicates. There are some complications here because the array
2325 size grows occasionally, which may add an O(N) factor, but this
2326 should be rare. Also, we free the excess array allocation, which
2327 requires a copy which is O(N), but this only happens once. */
800eeca4 2328
bbe66d08 2329static struct elfNN_ia64_dyn_sym_info *
eae50df2
L
2330get_dyn_sym_info (struct elfNN_ia64_link_hash_table *ia64_info,
2331 struct elf_link_hash_entry *h, bfd *abfd,
2332 const Elf_Internal_Rela *rel, bfd_boolean create)
800eeca4 2333{
396a682d
L
2334 struct elfNN_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
2335 unsigned int *count_p, *sorted_count_p, *size_p;
2336 unsigned int count, sorted_count, size;
800eeca4 2337 bfd_vma addend = rel ? rel->r_addend : 0;
396a682d 2338 bfd_size_type amt;
3e932841 2339
800eeca4 2340 if (h)
396a682d
L
2341 {
2342 struct elfNN_ia64_link_hash_entry *global_h;
2343
2344 global_h = (struct elfNN_ia64_link_hash_entry *) h;
2345 info_p = &global_h->info;
2346 count_p = &global_h->count;
2347 sorted_count_p = &global_h->sorted_count;
2348 size_p = &global_h->size;
2349 }
800eeca4
JW
2350 else
2351 {
bbe66d08 2352 struct elfNN_ia64_local_hash_entry *loc_h;
800eeca4 2353
f7460f5f 2354 loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
f86b235a
RH
2355 if (!loc_h)
2356 {
2357 BFD_ASSERT (!create);
2358 return NULL;
2359 }
800eeca4 2360
396a682d
L
2361 info_p = &loc_h->info;
2362 count_p = &loc_h->count;
2363 sorted_count_p = &loc_h->sorted_count;
2364 size_p = &loc_h->size;
3e932841 2365 }
800eeca4 2366
396a682d
L
2367 count = *count_p;
2368 sorted_count = *sorted_count_p;
2369 size = *size_p;
2370 info = *info_p;
2371 if (create)
800eeca4 2372 {
396a682d
L
2373 /* When we create the array, we don't check for duplicates,
2374 except in the previously sorted section if one exists, and
2375 against the last inserted entry. This allows insertions to
2376 be fast. */
2377 if (info)
2378 {
2379 if (sorted_count)
2380 {
2381 /* Try bsearch first on the sorted section. */
2382 key.addend = addend;
2383 dyn_i = bsearch (&key, info, sorted_count,
2384 sizeof (*info), addend_compare);
2385
2386 if (dyn_i)
2387 {
2388 return dyn_i;
2389 }
2390 }
2391
2392 /* Do a quick check for the last inserted entry. */
2393 dyn_i = info + count - 1;
2394 if (dyn_i->addend == addend)
2395 {
2396 return dyn_i;
2397 }
2398 }
2399
2400 if (size == 0)
2401 {
2402 /* It is the very first element. We create the array of size
2403 1. */
2404 size = 1;
2405 amt = size * sizeof (*info);
2406 info = bfd_malloc (amt);
2407 }
2408 else if (size <= count)
2409 {
2410 /* We double the array size every time when we reach the
2411 size limit. */
2412 size += size;
2413 amt = size * sizeof (*info);
2414 info = bfd_realloc (info, amt);
2415 }
2416 else
2417 goto has_space;
2418
2419 if (info == NULL)
2420 return NULL;
2421 *size_p = size;
2422 *info_p = info;
2423
2424has_space:
2425 /* Append the new one to the array. */
2426 dyn_i = info + count;
2427 memset (dyn_i, 0, sizeof (*dyn_i));
293a0124 2428 dyn_i->got_offset = (bfd_vma) -1;
800eeca4 2429 dyn_i->addend = addend;
9a2e389a 2430
396a682d
L
2431 /* We increment count only since the new ones are unsorted and
2432 may have duplicate. */
2433 (*count_p)++;
2434 }
2435 else
2436 {
2437 /* It is a lookup without insertion. Sort array if part of the
2438 array isn't sorted. */
2439 if (count != sorted_count)
2440 {
2441 count = sort_dyn_sym_info (info, count);
2442 *count_p = count;
2443 *sorted_count_p = count;
2444 }
2445
2446 /* Free unused memory. */
2447 if (size != count)
2448 {
2449 amt = count * sizeof (*info);
2450 info = bfd_malloc (amt);
2451 if (info != NULL)
2452 {
2453 memcpy (info, *info_p, amt);
2454 free (*info_p);
2455 *size_p = count;
2456 *info_p = info;
2457 }
2458 }
2459
2460 key.addend = addend;
2461 dyn_i = bsearch (&key, info, count,
2462 sizeof (*info), addend_compare);
800eeca4
JW
2463 }
2464
2465 return dyn_i;
2466}
2467
2468static asection *
eae50df2
L
2469get_got (bfd *abfd, struct bfd_link_info *info,
2470 struct elfNN_ia64_link_hash_table *ia64_info)
800eeca4 2471{
64bf6ae6 2472 asection *got;
800eeca4
JW
2473 bfd *dynobj;
2474
cc3bd654 2475 got = ia64_info->root.sgot;
800eeca4
JW
2476 if (!got)
2477 {
2478 flagword flags;
2479
2480 dynobj = ia64_info->root.dynobj;
2481 if (!dynobj)
2482 ia64_info->root.dynobj = dynobj = abfd;
2483 if (!_bfd_elf_create_got_section (dynobj, info))
2484 return 0;
2485
cc3bd654 2486 got = ia64_info->root.sgot;
800eeca4 2487
8651fcf9
L
2488 /* The .got section is always aligned at 8 bytes. */
2489 if (!bfd_set_section_alignment (abfd, got, 3))
2490 return 0;
2491
800eeca4
JW
2492 flags = bfd_get_section_flags (abfd, got);
2493 bfd_set_section_flags (abfd, got, SEC_SMALL_DATA | flags);
2494 }
2495
2496 return got;
2497}
2498
2499/* Create function descriptor section (.opd). This section is called .opd
4cc11e76 2500 because it contains "official procedure descriptors". The "official"
800eeca4
JW
2501 refers to the fact that these descriptors are used when taking the address
2502 of a procedure, thus ensuring a unique address for each procedure. */
2503
2504static asection *
eae50df2
L
2505get_fptr (bfd *abfd, struct bfd_link_info *info,
2506 struct elfNN_ia64_link_hash_table *ia64_info)
800eeca4
JW
2507{
2508 asection *fptr;
2509 bfd *dynobj;
2510
2511 fptr = ia64_info->fptr_sec;
2512 if (!fptr)
2513 {
2514 dynobj = ia64_info->root.dynobj;
2515 if (!dynobj)
2516 ia64_info->root.dynobj = dynobj = abfd;
2517
3496cb2a
L
2518 fptr = bfd_make_section_with_flags (dynobj, ".opd",
2519 (SEC_ALLOC
2520 | SEC_LOAD
2521 | SEC_HAS_CONTENTS
2522 | SEC_IN_MEMORY
2523 | (info->pie ? 0 : SEC_READONLY)
2524 | SEC_LINKER_CREATED));
800eeca4 2525 if (!fptr
800eeca4
JW
2526 || !bfd_set_section_alignment (abfd, fptr, 4))
2527 {
2528 BFD_ASSERT (0);
2529 return NULL;
2530 }
2531
2532 ia64_info->fptr_sec = fptr;
9203ba99
JJ
2533
2534 if (info->pie)
2535 {
2536 asection *fptr_rel;
3496cb2a
L
2537 fptr_rel = bfd_make_section_with_flags (dynobj, ".rela.opd",
2538 (SEC_ALLOC | SEC_LOAD
2539 | SEC_HAS_CONTENTS
2540 | SEC_IN_MEMORY
2541 | SEC_LINKER_CREATED
2542 | SEC_READONLY));
9203ba99 2543 if (fptr_rel == NULL
5a260b66
L
2544 || !bfd_set_section_alignment (abfd, fptr_rel,
2545 LOG_SECTION_ALIGN))
9203ba99
JJ
2546 {
2547 BFD_ASSERT (0);
2548 return NULL;
2549 }
2550
2551 ia64_info->rel_fptr_sec = fptr_rel;
2552 }
800eeca4
JW
2553 }
2554
2555 return fptr;
2556}
2557
2558static asection *
eae50df2
L
2559get_pltoff (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED,
2560 struct elfNN_ia64_link_hash_table *ia64_info)
800eeca4
JW
2561{
2562 asection *pltoff;
2563 bfd *dynobj;
2564
2565 pltoff = ia64_info->pltoff_sec;
2566 if (!pltoff)
2567 {
2568 dynobj = ia64_info->root.dynobj;
2569 if (!dynobj)
2570 ia64_info->root.dynobj = dynobj = abfd;
2571
3496cb2a
L
2572 pltoff = bfd_make_section_with_flags (dynobj,
2573 ELF_STRING_ia64_pltoff,
2574 (SEC_ALLOC
2575 | SEC_LOAD
2576 | SEC_HAS_CONTENTS
2577 | SEC_IN_MEMORY
2578 | SEC_SMALL_DATA
2579 | SEC_LINKER_CREATED));
800eeca4 2580 if (!pltoff
800eeca4
JW
2581 || !bfd_set_section_alignment (abfd, pltoff, 4))
2582 {
2583 BFD_ASSERT (0);
2584 return NULL;
2585 }
2586
2587 ia64_info->pltoff_sec = pltoff;
2588 }
2589
2590 return pltoff;
2591}
2592
2593static asection *
eae50df2
L
2594get_reloc_section (bfd *abfd,
2595 struct elfNN_ia64_link_hash_table *ia64_info,
2596 asection *sec, bfd_boolean create)
800eeca4
JW
2597{
2598 const char *srel_name;
2599 asection *srel;
2600 bfd *dynobj;
2601
2602 srel_name = (bfd_elf_string_from_elf_section
2603 (abfd, elf_elfheader(abfd)->e_shstrndx,
2604 elf_section_data(sec)->rel_hdr.sh_name));
2605 if (srel_name == NULL)
2606 return NULL;
2607
0112cd26 2608 BFD_ASSERT ((CONST_STRNEQ (srel_name, ".rela")
800eeca4
JW
2609 && strcmp (bfd_get_section_name (abfd, sec),
2610 srel_name+5) == 0)
0112cd26 2611 || (CONST_STRNEQ (srel_name, ".rel")
800eeca4
JW
2612 && strcmp (bfd_get_section_name (abfd, sec),
2613 srel_name+4) == 0));
2614
2615 dynobj = ia64_info->root.dynobj;
2616 if (!dynobj)
2617 ia64_info->root.dynobj = dynobj = abfd;
2618
2619 srel = bfd_get_section_by_name (dynobj, srel_name);
2620 if (srel == NULL && create)
2621 {
3496cb2a
L
2622 srel = bfd_make_section_with_flags (dynobj, srel_name,
2623 (SEC_ALLOC | SEC_LOAD
2624 | SEC_HAS_CONTENTS
2625 | SEC_IN_MEMORY
2626 | SEC_LINKER_CREATED
2627 | SEC_READONLY));
800eeca4 2628 if (srel == NULL
5a260b66
L
2629 || !bfd_set_section_alignment (dynobj, srel,
2630 LOG_SECTION_ALIGN))
800eeca4
JW
2631 return NULL;
2632 }
2633
2634 return srel;
2635}
2636
b34976b6 2637static bfd_boolean
ac33696c
L
2638count_dyn_reloc (bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
2639 asection *srel, int type, bfd_boolean reltext)
800eeca4 2640{
bbe66d08 2641 struct elfNN_ia64_dyn_reloc_entry *rent;
800eeca4
JW
2642
2643 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
2644 if (rent->srel == srel && rent->type == type)
2645 break;
2646
2647 if (!rent)
2648 {
dc810e39
AM
2649 rent = ((struct elfNN_ia64_dyn_reloc_entry *)
2650 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
800eeca4 2651 if (!rent)
b34976b6 2652 return FALSE;
800eeca4
JW
2653
2654 rent->next = dyn_i->reloc_entries;
2655 rent->srel = srel;
2656 rent->type = type;
2657 rent->count = 0;
2658 dyn_i->reloc_entries = rent;
2659 }
ac33696c 2660 rent->reltext = reltext;
800eeca4
JW
2661 rent->count++;
2662
b34976b6 2663 return TRUE;
800eeca4
JW
2664}
2665
b34976b6 2666static bfd_boolean
eae50df2
L
2667elfNN_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2668 asection *sec,
2669 const Elf_Internal_Rela *relocs)
800eeca4 2670{
bbe66d08 2671 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
2672 const Elf_Internal_Rela *relend;
2673 Elf_Internal_Shdr *symtab_hdr;
2674 const Elf_Internal_Rela *rel;
21a8f7fa 2675 asection *got, *fptr, *srel, *pltoff;
396a682d
L
2676 enum {
2677 NEED_GOT = 1,
2678 NEED_GOTX = 2,
2679 NEED_FPTR = 4,
2680 NEED_PLTOFF = 8,
2681 NEED_MIN_PLT = 16,
2682 NEED_FULL_PLT = 32,
2683 NEED_DYNREL = 64,
2684 NEED_LTOFF_FPTR = 128,
2685 NEED_TPREL = 256,
2686 NEED_DTPMOD = 512,
2687 NEED_DTPREL = 1024
2688 };
2689 int need_entry;
2690 struct elf_link_hash_entry *h;
2691 unsigned long r_symndx;
2692 bfd_boolean maybe_dynamic;
800eeca4 2693
1049f94e 2694 if (info->relocatable)
b34976b6 2695 return TRUE;
800eeca4
JW
2696
2697 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
bbe66d08 2698 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
2699 if (ia64_info == NULL)
2700 return FALSE;
800eeca4 2701
21a8f7fa 2702 got = fptr = srel = pltoff = NULL;
800eeca4
JW
2703
2704 relend = relocs + sec->reloc_count;
396a682d
L
2705
2706 /* We scan relocations first to create dynamic relocation arrays. We
2707 modified get_dyn_sym_info to allow fast insertion and support fast
2708 lookup in the next loop. */
2709 for (rel = relocs; rel < relend; ++rel)
2710 {
2711 r_symndx = ELFNN_R_SYM (rel->r_info);
2712 if (r_symndx >= symtab_hdr->sh_info)
2713 {
2714 long indx = r_symndx - symtab_hdr->sh_info;
2715 h = elf_sym_hashes (abfd)[indx];
2716 while (h->root.type == bfd_link_hash_indirect
2717 || h->root.type == bfd_link_hash_warning)
2718 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2719 }
2720 else
2721 h = NULL;
2722
2723 /* We can only get preliminary data on whether a symbol is
2724 locally or externally defined, as not all of the input files
2725 have yet been processed. Do something with what we know, as
2726 this may help reduce memory usage and processing time later. */
2727 maybe_dynamic = (h && ((!info->executable
55255dae 2728 && (!SYMBOLIC_BIND (info, h)
396a682d
L
2729 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2730 || !h->def_regular
2731 || h->root.type == bfd_link_hash_defweak));
2732
2733 need_entry = 0;
2734 switch (ELFNN_R_TYPE (rel->r_info))
2735 {
2736 case R_IA64_TPREL64MSB:
2737 case R_IA64_TPREL64LSB:
2738 if (info->shared || maybe_dynamic)
2739 need_entry = NEED_DYNREL;
2740 break;
2741
2742 case R_IA64_LTOFF_TPREL22:
2743 need_entry = NEED_TPREL;
2744 if (info->shared)
2745 info->flags |= DF_STATIC_TLS;
2746 break;
2747
2748 case R_IA64_DTPREL32MSB:
2749 case R_IA64_DTPREL32LSB:
2750 case R_IA64_DTPREL64MSB:
2751 case R_IA64_DTPREL64LSB:
2752 if (info->shared || maybe_dynamic)
2753 need_entry = NEED_DYNREL;
2754 break;
2755
2756 case R_IA64_LTOFF_DTPREL22:
2757 need_entry = NEED_DTPREL;
2758 break;
2759
2760 case R_IA64_DTPMOD64MSB:
2761 case R_IA64_DTPMOD64LSB:
2762 if (info->shared || maybe_dynamic)
2763 need_entry = NEED_DYNREL;
2764 break;
2765
2766 case R_IA64_LTOFF_DTPMOD22:
2767 need_entry = NEED_DTPMOD;
2768 break;
2769
2770 case R_IA64_LTOFF_FPTR22:
2771 case R_IA64_LTOFF_FPTR64I:
2772 case R_IA64_LTOFF_FPTR32MSB:
2773 case R_IA64_LTOFF_FPTR32LSB:
2774 case R_IA64_LTOFF_FPTR64MSB:
2775 case R_IA64_LTOFF_FPTR64LSB:
2776 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2777 break;
2778
2779 case R_IA64_FPTR64I:
2780 case R_IA64_FPTR32MSB:
2781 case R_IA64_FPTR32LSB:
2782 case R_IA64_FPTR64MSB:
2783 case R_IA64_FPTR64LSB:
2784 if (info->shared || h)
2785 need_entry = NEED_FPTR | NEED_DYNREL;
2786 else
2787 need_entry = NEED_FPTR;
2788 break;
2789
2790 case R_IA64_LTOFF22:
2791 case R_IA64_LTOFF64I:
2792 need_entry = NEED_GOT;
2793 break;
2794
2795 case R_IA64_LTOFF22X:
2796 need_entry = NEED_GOTX;
2797 break;
2798
2799 case R_IA64_PLTOFF22:
2800 case R_IA64_PLTOFF64I:
2801 case R_IA64_PLTOFF64MSB:
2802 case R_IA64_PLTOFF64LSB:
2803 need_entry = NEED_PLTOFF;
2804 if (h)
2805 {
2806 if (maybe_dynamic)
2807 need_entry |= NEED_MIN_PLT;
2808 }
2809 else
2810 {
2811 (*info->callbacks->warning)
2812 (info, _("@pltoff reloc against local symbol"), 0,
2813 abfd, 0, (bfd_vma) 0);
2814 }
2815 break;
2816
2817 case R_IA64_PCREL21B:
2818 case R_IA64_PCREL60B:
2819 /* Depending on where this symbol is defined, we may or may not
2820 need a full plt entry. Only skip if we know we'll not need
2821 the entry -- static or symbolic, and the symbol definition
2822 has already been seen. */
2823 if (maybe_dynamic && rel->r_addend == 0)
2824 need_entry = NEED_FULL_PLT;
2825 break;
2826
2827 case R_IA64_IMM14:
2828 case R_IA64_IMM22:
2829 case R_IA64_IMM64:
2830 case R_IA64_DIR32MSB:
2831 case R_IA64_DIR32LSB:
2832 case R_IA64_DIR64MSB:
2833 case R_IA64_DIR64LSB:
2834 /* Shared objects will always need at least a REL relocation. */
2835 if (info->shared || maybe_dynamic)
2836 need_entry = NEED_DYNREL;
2837 break;
2838
2839 case R_IA64_IPLTMSB:
2840 case R_IA64_IPLTLSB:
2841 /* Shared objects will always need at least a REL relocation. */
2842 if (info->shared || maybe_dynamic)
2843 need_entry = NEED_DYNREL;
2844 break;
2845
2846 case R_IA64_PCREL22:
2847 case R_IA64_PCREL64I:
2848 case R_IA64_PCREL32MSB:
2849 case R_IA64_PCREL32LSB:
2850 case R_IA64_PCREL64MSB:
2851 case R_IA64_PCREL64LSB:
2852 if (maybe_dynamic)
2853 need_entry = NEED_DYNREL;
2854 break;
2855 }
2856
2857 if (!need_entry)
2858 continue;
2859
2860 if ((need_entry & NEED_FPTR) != 0
2861 && rel->r_addend)
2862 {
2863 (*info->callbacks->warning)
2864 (info, _("non-zero addend in @fptr reloc"), 0,
2865 abfd, 0, (bfd_vma) 0);
2866 }
2867
2868 if (get_dyn_sym_info (ia64_info, h, abfd, rel, TRUE) == NULL)
2869 return FALSE;
2870 }
2871
2872 /* Now, we only do lookup without insertion, which is very fast
9a2e389a 2873 with the modified get_dyn_sym_info. */
800eeca4
JW
2874 for (rel = relocs; rel < relend; ++rel)
2875 {
bbe66d08 2876 struct elfNN_ia64_dyn_sym_info *dyn_i;
64bf6ae6 2877 int dynrel_type = R_IA64_NONE;
800eeca4 2878
396a682d 2879 r_symndx = ELFNN_R_SYM (rel->r_info);
800eeca4
JW
2880 if (r_symndx >= symtab_hdr->sh_info)
2881 {
2882 /* We're dealing with a global symbol -- find its hash entry
2883 and mark it as being referenced. */
2884 long indx = r_symndx - symtab_hdr->sh_info;
2885 h = elf_sym_hashes (abfd)[indx];
2886 while (h->root.type == bfd_link_hash_indirect
2887 || h->root.type == bfd_link_hash_warning)
2888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2889
f5385ebf 2890 h->ref_regular = 1;
800eeca4 2891 }
396a682d
L
2892 else
2893 h = NULL;
800eeca4
JW
2894
2895 /* We can only get preliminary data on whether a symbol is
2896 locally or externally defined, as not all of the input files
2897 have yet been processed. Do something with what we know, as
2898 this may help reduce memory usage and processing time later. */
396a682d 2899 maybe_dynamic = (h && ((!info->executable
55255dae 2900 && (!SYMBOLIC_BIND (info, h)
396a682d
L
2901 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
2902 || !h->def_regular
2903 || h->root.type == bfd_link_hash_defweak));
800eeca4
JW
2904
2905 need_entry = 0;
bbe66d08 2906 switch (ELFNN_R_TYPE (rel->r_info))
800eeca4 2907 {
800eeca4
JW
2908 case R_IA64_TPREL64MSB:
2909 case R_IA64_TPREL64LSB:
13ae64f3
JJ
2910 if (info->shared || maybe_dynamic)
2911 need_entry = NEED_DYNREL;
2912 dynrel_type = R_IA64_TPREL64LSB;
2913 if (info->shared)
2914 info->flags |= DF_STATIC_TLS;
2915 break;
2916
2917 case R_IA64_LTOFF_TPREL22:
2918 need_entry = NEED_TPREL;
2919 if (info->shared)
2920 info->flags |= DF_STATIC_TLS;
2921 break;
2922
5a260b66
L
2923 case R_IA64_DTPREL32MSB:
2924 case R_IA64_DTPREL32LSB:
13ae64f3
JJ
2925 case R_IA64_DTPREL64MSB:
2926 case R_IA64_DTPREL64LSB:
2927 if (info->shared || maybe_dynamic)
2928 need_entry = NEED_DYNREL;
5a260b66 2929 dynrel_type = R_IA64_DTPRELNNLSB;
13ae64f3
JJ
2930 break;
2931
2932 case R_IA64_LTOFF_DTPREL22:
2933 need_entry = NEED_DTPREL;
2934 break;
2935
2936 case R_IA64_DTPMOD64MSB:
2937 case R_IA64_DTPMOD64LSB:
2938 if (info->shared || maybe_dynamic)
2939 need_entry = NEED_DYNREL;
2940 dynrel_type = R_IA64_DTPMOD64LSB;
2941 break;
2942
2943 case R_IA64_LTOFF_DTPMOD22:
2944 need_entry = NEED_DTPMOD;
2945 break;
800eeca4
JW
2946
2947 case R_IA64_LTOFF_FPTR22:
2948 case R_IA64_LTOFF_FPTR64I:
a4bd8390
JW
2949 case R_IA64_LTOFF_FPTR32MSB:
2950 case R_IA64_LTOFF_FPTR32LSB:
800eeca4
JW
2951 case R_IA64_LTOFF_FPTR64MSB:
2952 case R_IA64_LTOFF_FPTR64LSB:
2953 need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
2954 break;
2955
2956 case R_IA64_FPTR64I:
2957 case R_IA64_FPTR32MSB:
2958 case R_IA64_FPTR32LSB:
2959 case R_IA64_FPTR64MSB:
2960 case R_IA64_FPTR64LSB:
02e6ad56 2961 if (info->shared || h)
800eeca4
JW
2962 need_entry = NEED_FPTR | NEED_DYNREL;
2963 else
2964 need_entry = NEED_FPTR;
5a260b66 2965 dynrel_type = R_IA64_FPTRNNLSB;
800eeca4
JW
2966 break;
2967
2968 case R_IA64_LTOFF22:
800eeca4
JW
2969 case R_IA64_LTOFF64I:
2970 need_entry = NEED_GOT;
2971 break;
2972
2c4c2bc0
RH
2973 case R_IA64_LTOFF22X:
2974 need_entry = NEED_GOTX;
2975 break;
2976
800eeca4
JW
2977 case R_IA64_PLTOFF22:
2978 case R_IA64_PLTOFF64I:
2979 case R_IA64_PLTOFF64MSB:
2980 case R_IA64_PLTOFF64LSB:
2981 need_entry = NEED_PLTOFF;
2982 if (h)
2983 {
2984 if (maybe_dynamic)
2985 need_entry |= NEED_MIN_PLT;
2986 }
800eeca4
JW
2987 break;
2988
2989 case R_IA64_PCREL21B:
748abff6 2990 case R_IA64_PCREL60B:
800eeca4
JW
2991 /* Depending on where this symbol is defined, we may or may not
2992 need a full plt entry. Only skip if we know we'll not need
2993 the entry -- static or symbolic, and the symbol definition
2994 has already been seen. */
2995 if (maybe_dynamic && rel->r_addend == 0)
2996 need_entry = NEED_FULL_PLT;
2997 break;
2998
2999 case R_IA64_IMM14:
3000 case R_IA64_IMM22:
3001 case R_IA64_IMM64:
3002 case R_IA64_DIR32MSB:
3003 case R_IA64_DIR32LSB:
3004 case R_IA64_DIR64MSB:
3005 case R_IA64_DIR64LSB:
3006 /* Shared objects will always need at least a REL relocation. */
02e6ad56 3007 if (info->shared || maybe_dynamic)
800eeca4 3008 need_entry = NEED_DYNREL;
5a260b66 3009 dynrel_type = R_IA64_DIRNNLSB;
800eeca4
JW
3010 break;
3011
18b27f17
RH
3012 case R_IA64_IPLTMSB:
3013 case R_IA64_IPLTLSB:
3014 /* Shared objects will always need at least a REL relocation. */
3015 if (info->shared || maybe_dynamic)
3016 need_entry = NEED_DYNREL;
3017 dynrel_type = R_IA64_IPLTLSB;
3018 break;
3019
748abff6
RH
3020 case R_IA64_PCREL22:
3021 case R_IA64_PCREL64I:
800eeca4
JW
3022 case R_IA64_PCREL32MSB:
3023 case R_IA64_PCREL32LSB:
3024 case R_IA64_PCREL64MSB:
3025 case R_IA64_PCREL64LSB:
3026 if (maybe_dynamic)
3027 need_entry = NEED_DYNREL;
5a260b66 3028 dynrel_type = R_IA64_PCRELNNLSB;
800eeca4
JW
3029 break;
3030 }
3031
3032 if (!need_entry)
3033 continue;
3034
396a682d 3035 dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, FALSE);
800eeca4
JW
3036
3037 /* Record whether or not this is a local symbol. */
3038 dyn_i->h = h;
3039
3040 /* Create what's needed. */
2c4c2bc0
RH
3041 if (need_entry & (NEED_GOT | NEED_GOTX | NEED_TPREL
3042 | NEED_DTPMOD | NEED_DTPREL))
800eeca4
JW
3043 {
3044 if (!got)
3045 {
3046 got = get_got (abfd, info, ia64_info);
3047 if (!got)
b34976b6 3048 return FALSE;
800eeca4 3049 }
13ae64f3
JJ
3050 if (need_entry & NEED_GOT)
3051 dyn_i->want_got = 1;
2c4c2bc0
RH
3052 if (need_entry & NEED_GOTX)
3053 dyn_i->want_gotx = 1;
13ae64f3
JJ
3054 if (need_entry & NEED_TPREL)
3055 dyn_i->want_tprel = 1;
3056 if (need_entry & NEED_DTPMOD)
3057 dyn_i->want_dtpmod = 1;
3058 if (need_entry & NEED_DTPREL)
3059 dyn_i->want_dtprel = 1;
800eeca4
JW
3060 }
3061 if (need_entry & NEED_FPTR)
3062 {
3063 if (!fptr)
3064 {
3065 fptr = get_fptr (abfd, info, ia64_info);
3066 if (!fptr)
b34976b6 3067 return FALSE;
800eeca4
JW
3068 }
3069
3070 /* FPTRs for shared libraries are allocated by the dynamic
3071 linker. Make sure this local symbol will appear in the
3072 dynamic symbol table. */
02e6ad56 3073 if (!h && info->shared)
800eeca4 3074 {
c152c796 3075 if (! (bfd_elf_link_record_local_dynamic_symbol
dc810e39 3076 (info, abfd, (long) r_symndx)))
b34976b6 3077 return FALSE;
800eeca4
JW
3078 }
3079
3080 dyn_i->want_fptr = 1;
3081 }
3082 if (need_entry & NEED_LTOFF_FPTR)
3083 dyn_i->want_ltoff_fptr = 1;
3084 if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
3085 {
3086 if (!ia64_info->root.dynobj)
3087 ia64_info->root.dynobj = abfd;
f5385ebf 3088 h->needs_plt = 1;
800eeca4
JW
3089 dyn_i->want_plt = 1;
3090 }
3091 if (need_entry & NEED_FULL_PLT)
3092 dyn_i->want_plt2 = 1;
3093 if (need_entry & NEED_PLTOFF)
21a8f7fa
JW
3094 {
3095 /* This is needed here, in case @pltoff is used in a non-shared
3096 link. */
3097 if (!pltoff)
3098 {
3099 pltoff = get_pltoff (abfd, info, ia64_info);
3100 if (!pltoff)
3101 return FALSE;
3102 }
f12123c0 3103
21a8f7fa
JW
3104 dyn_i->want_pltoff = 1;
3105 }
800eeca4
JW
3106 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
3107 {
3108 if (!srel)
3109 {
b34976b6 3110 srel = get_reloc_section (abfd, ia64_info, sec, TRUE);
800eeca4 3111 if (!srel)
b34976b6 3112 return FALSE;
800eeca4 3113 }
ac33696c 3114 if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type,
de9811af 3115 (sec->flags & SEC_READONLY) != 0))
b34976b6 3116 return FALSE;
800eeca4
JW
3117 }
3118 }
3119
b34976b6 3120 return TRUE;
800eeca4
JW
3121}
3122
800eeca4
JW
3123/* For cleanliness, and potentially faster dynamic loading, allocate
3124 external GOT entries first. */
3125
b34976b6 3126static bfd_boolean
eae50df2 3127allocate_global_data_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
4dfe6ac6 3128 void * data)
800eeca4 3129{
bbe66d08 3130 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4 3131
2c4c2bc0 3132 if ((dyn_i->want_got || dyn_i->want_gotx)
800eeca4 3133 && ! dyn_i->want_fptr
986a241f 3134 && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
800eeca4
JW
3135 {
3136 dyn_i->got_offset = x->ofs;
3137 x->ofs += 8;
3138 }
13ae64f3
JJ
3139 if (dyn_i->want_tprel)
3140 {
3141 dyn_i->tprel_offset = x->ofs;
3142 x->ofs += 8;
3143 }
3144 if (dyn_i->want_dtpmod)
3145 {
986a241f 3146 if (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
b3dfd7fe
JJ
3147 {
3148 dyn_i->dtpmod_offset = x->ofs;
3149 x->ofs += 8;
3150 }
3151 else
3152 {
3153 struct elfNN_ia64_link_hash_table *ia64_info;
3154
3155 ia64_info = elfNN_ia64_hash_table (x->info);
4dfe6ac6
NC
3156 if (ia64_info == NULL)
3157 return FALSE;
3158
b3dfd7fe
JJ
3159 if (ia64_info->self_dtpmod_offset == (bfd_vma) -1)
3160 {
3161 ia64_info->self_dtpmod_offset = x->ofs;
3162 x->ofs += 8;
3163 }
3164 dyn_i->dtpmod_offset = ia64_info->self_dtpmod_offset;
3165 }
13ae64f3
JJ
3166 }
3167 if (dyn_i->want_dtprel)
3168 {
3169 dyn_i->dtprel_offset = x->ofs;
3170 x->ofs += 8;
3171 }
b34976b6 3172 return TRUE;
800eeca4
JW
3173}
3174
3175/* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
3176
b34976b6 3177static bfd_boolean
eae50df2 3178allocate_global_fptr_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
4dfe6ac6 3179 void * data)
800eeca4 3180{
bbe66d08 3181 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
3182
3183 if (dyn_i->want_got
3184 && dyn_i->want_fptr
5a260b66 3185 && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, R_IA64_FPTRNNLSB))
800eeca4
JW
3186 {
3187 dyn_i->got_offset = x->ofs;
3188 x->ofs += 8;
3189 }
b34976b6 3190 return TRUE;
800eeca4
JW
3191}
3192
3193/* Lastly, allocate all the GOT entries for local data. */
3194
b34976b6 3195static bfd_boolean
eae50df2
L
3196allocate_local_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
3197 PTR data)
800eeca4 3198{
bbe66d08 3199 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4 3200
2c4c2bc0 3201 if ((dyn_i->want_got || dyn_i->want_gotx)
986a241f 3202 && !elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
800eeca4
JW
3203 {
3204 dyn_i->got_offset = x->ofs;
3205 x->ofs += 8;
3206 }
b34976b6 3207 return TRUE;
800eeca4
JW
3208}
3209
3210/* Search for the index of a global symbol in it's defining object file. */
3211
dc810e39 3212static long
eae50df2 3213global_sym_index (struct elf_link_hash_entry *h)
800eeca4
JW
3214{
3215 struct elf_link_hash_entry **p;
3216 bfd *obj;
3217
3218 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3219 || h->root.type == bfd_link_hash_defweak);
3220
3221 obj = h->root.u.def.section->owner;
3222 for (p = elf_sym_hashes (obj); *p != h; ++p)
3223 continue;
3224
3225 return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
3226}
3227
3228/* Allocate function descriptors. We can do these for every function
3229 in a main executable that is not exported. */
3230
b34976b6 3231static bfd_boolean
eae50df2 3232allocate_fptr (struct elfNN_ia64_dyn_sym_info *dyn_i, PTR data)
800eeca4 3233{
bbe66d08 3234 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
3235
3236 if (dyn_i->want_fptr)
3237 {
3238 struct elf_link_hash_entry *h = dyn_i->h;
3e932841 3239
800eeca4
JW
3240 if (h)
3241 while (h->root.type == bfd_link_hash_indirect
3242 || h->root.type == bfd_link_hash_warning)
3243 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3244
02e6ad56
RH
3245 if (!x->info->executable
3246 && (!h
3247 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1faab634
L
3248 || (h->root.type != bfd_link_hash_undefweak
3249 && h->root.type != bfd_link_hash_undefined)))
800eeca4
JW
3250 {
3251 if (h && h->dynindx == -1)
3252 {
3253 BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
3254 || (h->root.type == bfd_link_hash_defweak));
3255
c152c796 3256 if (!bfd_elf_link_record_local_dynamic_symbol
800eeca4
JW
3257 (x->info, h->root.u.def.section->owner,
3258 global_sym_index (h)))
b34976b6 3259 return FALSE;
800eeca4
JW
3260 }
3261
3262 dyn_i->want_fptr = 0;
3263 }
3264 else if (h == NULL || h->dynindx == -1)
3265 {
3266 dyn_i->fptr_offset = x->ofs;
3267 x->ofs += 16;
3268 }
3269 else
3270 dyn_i->want_fptr = 0;
3271 }
b34976b6 3272 return TRUE;
800eeca4
JW
3273}
3274
3275/* Allocate all the minimal PLT entries. */
3276
b34976b6 3277static bfd_boolean
eae50df2
L
3278allocate_plt_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3279 PTR data)
800eeca4 3280{
bbe66d08 3281 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
3282
3283 if (dyn_i->want_plt)
3284 {
3285 struct elf_link_hash_entry *h = dyn_i->h;
3286
3287 if (h)
3288 while (h->root.type == bfd_link_hash_indirect
3289 || h->root.type == bfd_link_hash_warning)
3290 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3291
f5385ebf 3292 /* ??? Versioned symbols seem to lose NEEDS_PLT. */
986a241f 3293 if (elfNN_ia64_dynamic_symbol_p (h, x->info, 0))
800eeca4
JW
3294 {
3295 bfd_size_type offset = x->ofs;
3296 if (offset == 0)
3297 offset = PLT_HEADER_SIZE;
3298 dyn_i->plt_offset = offset;
3299 x->ofs = offset + PLT_MIN_ENTRY_SIZE;
3300
3301 dyn_i->want_pltoff = 1;
3302 }
3303 else
3304 {
3305 dyn_i->want_plt = 0;
3306 dyn_i->want_plt2 = 0;
3307 }
3308 }
b34976b6 3309 return TRUE;
800eeca4
JW
3310}
3311
3312/* Allocate all the full PLT entries. */
3313
b34976b6 3314static bfd_boolean
eae50df2
L
3315allocate_plt2_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3316 PTR data)
800eeca4 3317{
bbe66d08 3318 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
3319
3320 if (dyn_i->want_plt2)
3321 {
3322 struct elf_link_hash_entry *h = dyn_i->h;
3323 bfd_size_type ofs = x->ofs;
3324
3325 dyn_i->plt2_offset = ofs;
3326 x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
3327
3328 while (h->root.type == bfd_link_hash_indirect
3329 || h->root.type == bfd_link_hash_warning)
3330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3331 dyn_i->h->plt.offset = ofs;
3332 }
b34976b6 3333 return TRUE;
800eeca4
JW
3334}
3335
3336/* Allocate all the PLTOFF entries requested by relocations and
3337 plt entries. We can't share space with allocated FPTR entries,
3338 because the latter are not necessarily addressable by the GP.
3339 ??? Relaxation might be able to determine that they are. */
3340
b34976b6 3341static bfd_boolean
eae50df2
L
3342allocate_pltoff_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3343 PTR data)
800eeca4 3344{
bbe66d08 3345 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
800eeca4
JW
3346
3347 if (dyn_i->want_pltoff)
3348 {
3349 dyn_i->pltoff_offset = x->ofs;
3350 x->ofs += 16;
3351 }
b34976b6 3352 return TRUE;
800eeca4
JW
3353}
3354
3355/* Allocate dynamic relocations for those symbols that turned out
3356 to be dynamic. */
3357
b34976b6 3358static bfd_boolean
eae50df2
L
3359allocate_dynrel_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
3360 PTR data)
800eeca4 3361{
bbe66d08
JW
3362 struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
3363 struct elfNN_ia64_link_hash_table *ia64_info;
3364 struct elfNN_ia64_dyn_reloc_entry *rent;
ef5aade5 3365 bfd_boolean dynamic_symbol, shared, resolved_zero;
800eeca4 3366
bbe66d08 3367 ia64_info = elfNN_ia64_hash_table (x->info);
4dfe6ac6
NC
3368 if (ia64_info == NULL)
3369 return FALSE;
986a241f
RH
3370
3371 /* Note that this can't be used in relation to FPTR relocs below. */
3372 dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0);
3373
800eeca4 3374 shared = x->info->shared;
ef5aade5
L
3375 resolved_zero = (dyn_i->h
3376 && ELF_ST_VISIBILITY (dyn_i->h->other)
3377 && dyn_i->h->root.type == bfd_link_hash_undefweak);
800eeca4 3378
4a78a1f4
AS
3379 /* Take care of the GOT and PLT relocations. */
3380
3381 if ((!resolved_zero
3382 && (dynamic_symbol || shared)
3383 && (dyn_i->want_got || dyn_i->want_gotx))
3384 || (dyn_i->want_ltoff_fptr
3385 && dyn_i->h
3386 && dyn_i->h->dynindx != -1))
3387 {
3388 if (!dyn_i->want_ltoff_fptr
3389 || !x->info->pie
3390 || dyn_i->h == NULL
3391 || dyn_i->h->root.type != bfd_link_hash_undefweak)
cc3bd654 3392 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
4a78a1f4
AS
3393 }
3394 if ((dynamic_symbol || shared) && dyn_i->want_tprel)
cc3bd654 3395 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
4a78a1f4 3396 if (dynamic_symbol && dyn_i->want_dtpmod)
cc3bd654 3397 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
4a78a1f4 3398 if (dynamic_symbol && dyn_i->want_dtprel)
cc3bd654 3399 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
4a78a1f4
AS
3400
3401 if (x->only_got)
3402 return TRUE;
3403
3404 if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
3405 {
3406 if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
3407 ia64_info->rel_fptr_sec->size += sizeof (ElfNN_External_Rela);
3408 }
3409
3410 if (!resolved_zero && dyn_i->want_pltoff)
3411 {
3412 bfd_size_type t = 0;
3413
3414 /* Dynamic symbols get one IPLT relocation. Local symbols in
3415 shared libraries get two REL relocations. Local symbols in
3416 main applications get nothing. */
3417 if (dynamic_symbol)
3418 t = sizeof (ElfNN_External_Rela);
3419 else if (shared)
3420 t = 2 * sizeof (ElfNN_External_Rela);
3421
3422 ia64_info->rel_pltoff_sec->size += t;
3423 }
3424
800eeca4
JW
3425 /* Take care of the normal data relocations. */
3426
3427 for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
3428 {
18b27f17
RH
3429 int count = rent->count;
3430
800eeca4
JW
3431 switch (rent->type)
3432 {
5a260b66 3433 case R_IA64_FPTR32LSB:
800eeca4 3434 case R_IA64_FPTR64LSB:
9203ba99
JJ
3435 /* Allocate one iff !want_fptr and not PIE, which by this point
3436 will be true only if we're actually allocating one statically
3437 in the main executable. Position independent executables
3438 need a relative reloc. */
3439 if (dyn_i->want_fptr && !x->info->pie)
800eeca4
JW
3440 continue;
3441 break;
5a260b66 3442 case R_IA64_PCREL32LSB:
800eeca4
JW
3443 case R_IA64_PCREL64LSB:
3444 if (!dynamic_symbol)
3445 continue;
3446 break;
5a260b66 3447 case R_IA64_DIR32LSB:
800eeca4
JW
3448 case R_IA64_DIR64LSB:
3449 if (!dynamic_symbol && !shared)
3450 continue;
3451 break;
18b27f17
RH
3452 case R_IA64_IPLTLSB:
3453 if (!dynamic_symbol && !shared)
3454 continue;
3455 /* Use two REL relocations for IPLT relocations
3456 against local symbols. */
3457 if (!dynamic_symbol)
3458 count *= 2;
3459 break;
5a260b66 3460 case R_IA64_DTPREL32LSB:
13ae64f3
JJ
3461 case R_IA64_TPREL64LSB:
3462 case R_IA64_DTPREL64LSB:
3463 case R_IA64_DTPMOD64LSB:
3464 break;
18b27f17
RH
3465 default:
3466 abort ();
800eeca4 3467 }
ac33696c
L
3468 if (rent->reltext)
3469 ia64_info->reltext = 1;
eea6121a 3470 rent->srel->size += sizeof (ElfNN_External_Rela) * count;
800eeca4
JW
3471 }
3472
b34976b6 3473 return TRUE;
800eeca4
JW
3474}
3475
b34976b6 3476static bfd_boolean
eae50df2
L
3477elfNN_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3478 struct elf_link_hash_entry *h)
800eeca4
JW
3479{
3480 /* ??? Undefined symbols with PLT entries should be re-defined
3481 to be the PLT entry. */
3482
3483 /* If this is a weak symbol, and there is a real definition, the
3484 processor independent code will have arranged for us to see the
3485 real definition first, and we can just use the same value. */
f6e332e6 3486 if (h->u.weakdef != NULL)
800eeca4 3487 {
f6e332e6
AM
3488 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3489 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3490 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3491 h->root.u.def.value = h->u.weakdef->root.u.def.value;
b34976b6 3492 return TRUE;
800eeca4
JW
3493 }
3494
3495 /* If this is a reference to a symbol defined by a dynamic object which
3496 is not a function, we might allocate the symbol in our .dynbss section
3497 and allocate a COPY dynamic relocation.
3498
3499 But IA-64 code is canonically PIC, so as a rule we can avoid this sort
3500 of hackery. */
3501
b34976b6 3502 return TRUE;
800eeca4
JW
3503}
3504
b34976b6 3505static bfd_boolean
eae50df2
L
3506elfNN_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3507 struct bfd_link_info *info)
800eeca4 3508{
bbe66d08
JW
3509 struct elfNN_ia64_allocate_data data;
3510 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
3511 asection *sec;
3512 bfd *dynobj;
b34976b6 3513 bfd_boolean relplt = FALSE;
800eeca4
JW
3514
3515 dynobj = elf_hash_table(info)->dynobj;
bbe66d08 3516 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
3517 if (ia64_info == NULL)
3518 return FALSE;
b3dfd7fe 3519 ia64_info->self_dtpmod_offset = (bfd_vma) -1;
800eeca4
JW
3520 BFD_ASSERT(dynobj != NULL);
3521 data.info = info;
3522
3523 /* Set the contents of the .interp section to the interpreter. */
3524 if (ia64_info->root.dynamic_sections_created
36af4a4e 3525 && info->executable)
800eeca4
JW
3526 {
3527 sec = bfd_get_section_by_name (dynobj, ".interp");
3528 BFD_ASSERT (sec != NULL);
02e6ad56 3529 sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
eea6121a 3530 sec->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
800eeca4
JW
3531 }
3532
800eeca4
JW
3533 /* Allocate the GOT entries. */
3534
cc3bd654 3535 if (ia64_info->root.sgot)
800eeca4
JW
3536 {
3537 data.ofs = 0;
bbe66d08
JW
3538 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
3539 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
3540 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
cc3bd654 3541 ia64_info->root.sgot->size = data.ofs;
800eeca4
JW
3542 }
3543
3544 /* Allocate the FPTR entries. */
3545
3546 if (ia64_info->fptr_sec)
3547 {
3548 data.ofs = 0;
bbe66d08 3549 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
eea6121a 3550 ia64_info->fptr_sec->size = data.ofs;
800eeca4
JW
3551 }
3552
3553 /* Now that we've seen all of the input files, we can decide which
3554 symbols need plt entries. Allocate the minimal PLT entries first.
b34976b6 3555 We do this even though dynamic_sections_created may be FALSE, because
800eeca4
JW
3556 this has the side-effect of clearing want_plt and want_plt2. */
3557
3558 data.ofs = 0;
bbe66d08 3559 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
800eeca4
JW
3560
3561 ia64_info->minplt_entries = 0;
3562 if (data.ofs)
3563 {
3564 ia64_info->minplt_entries
3565 = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
3566 }
3567
3568 /* Align the pointer for the plt2 entries. */
dc810e39 3569 data.ofs = (data.ofs + 31) & (bfd_vma) -32;
800eeca4 3570
bbe66d08 3571 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
a5a58ba4 3572 if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
800eeca4 3573 {
a5a58ba4
L
3574 /* FIXME: we always reserve the memory for dynamic linker even if
3575 there are no PLT entries since dynamic linker may assume the
3576 reserved memory always exists. */
3577
800eeca4
JW
3578 BFD_ASSERT (ia64_info->root.dynamic_sections_created);
3579
cc3bd654 3580 ia64_info->root.splt->size = data.ofs;
800eeca4
JW
3581
3582 /* If we've got a .plt, we need some extra memory for the dynamic
3583 linker. We stuff these in .got.plt. */
3584 sec = bfd_get_section_by_name (dynobj, ".got.plt");
eea6121a 3585 sec->size = 8 * PLT_RESERVED_WORDS;
800eeca4
JW
3586 }
3587
3588 /* Allocate the PLTOFF entries. */
3589
3590 if (ia64_info->pltoff_sec)
3591 {
3592 data.ofs = 0;
bbe66d08 3593 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
eea6121a 3594 ia64_info->pltoff_sec->size = data.ofs;
800eeca4
JW
3595 }
3596
3597 if (ia64_info->root.dynamic_sections_created)
3598 {
3599 /* Allocate space for the dynamic relocations that turned out to be
3600 required. */
3601
b3dfd7fe 3602 if (info->shared && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
cc3bd654 3603 ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
4a78a1f4 3604 data.only_got = FALSE;
bbe66d08 3605 elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
800eeca4
JW
3606 }
3607
3608 /* We have now determined the sizes of the various dynamic sections.
3609 Allocate memory for them. */
3610 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
3611 {
b34976b6 3612 bfd_boolean strip;
800eeca4
JW
3613
3614 if (!(sec->flags & SEC_LINKER_CREATED))
3615 continue;
3616
3617 /* If we don't need this section, strip it from the output file.
3618 There were several sections primarily related to dynamic
3619 linking that must be create before the linker maps input
3620 sections to output sections. The linker does that before
3621 bfd_elf_size_dynamic_sections is called, and it is that
3622 function which decides whether anything needs to go into
3623 these sections. */
3624
eea6121a 3625 strip = (sec->size == 0);
800eeca4 3626
cc3bd654 3627 if (sec == ia64_info->root.sgot)
b34976b6 3628 strip = FALSE;
cc3bd654 3629 else if (sec == ia64_info->root.srelgot)
800eeca4
JW
3630 {
3631 if (strip)
cc3bd654 3632 ia64_info->root.srelgot = NULL;
800eeca4
JW
3633 else
3634 /* We use the reloc_count field as a counter if we need to
3635 copy relocs into the output file. */
3636 sec->reloc_count = 0;
3637 }
3638 else if (sec == ia64_info->fptr_sec)
3639 {
3640 if (strip)
3641 ia64_info->fptr_sec = NULL;
3642 }
55936540
JW
3643 else if (sec == ia64_info->rel_fptr_sec)
3644 {
3645 if (strip)
3646 ia64_info->rel_fptr_sec = NULL;
3647 else
3648 /* We use the reloc_count field as a counter if we need to
3649 copy relocs into the output file. */
3650 sec->reloc_count = 0;
3651 }
cc3bd654 3652 else if (sec == ia64_info->root.splt)
800eeca4
JW
3653 {
3654 if (strip)
cc3bd654 3655 ia64_info->root.splt = NULL;
800eeca4
JW
3656 }
3657 else if (sec == ia64_info->pltoff_sec)
3658 {
3659 if (strip)
3660 ia64_info->pltoff_sec = NULL;
3661 }
3662 else if (sec == ia64_info->rel_pltoff_sec)
3663 {
3664 if (strip)
3665 ia64_info->rel_pltoff_sec = NULL;
3666 else
3667 {
b34976b6 3668 relplt = TRUE;
800eeca4
JW
3669 /* We use the reloc_count field as a counter if we need to
3670 copy relocs into the output file. */
3671 sec->reloc_count = 0;
3672 }
3673 }
3674 else
3675 {
3676 const char *name;
3677
3678 /* It's OK to base decisions on the section name, because none
3679 of the dynobj section names depend upon the input files. */
3680 name = bfd_get_section_name (dynobj, sec);
3681
3682 if (strcmp (name, ".got.plt") == 0)
b34976b6 3683 strip = FALSE;
0112cd26 3684 else if (CONST_STRNEQ (name, ".rel"))
800eeca4
JW
3685 {
3686 if (!strip)
3687 {
800eeca4
JW
3688 /* We use the reloc_count field as a counter if we need to
3689 copy relocs into the output file. */
3690 sec->reloc_count = 0;
3691 }
3692 }
3693 else
3694 continue;
3695 }
3696
3697 if (strip)
8423293d 3698 sec->flags |= SEC_EXCLUDE;
800eeca4
JW
3699 else
3700 {
3701 /* Allocate memory for the section contents. */
eea6121a
AM
3702 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
3703 if (sec->contents == NULL && sec->size != 0)
b34976b6 3704 return FALSE;
800eeca4
JW
3705 }
3706 }
3707
3708 if (elf_hash_table (info)->dynamic_sections_created)
3709 {
3710 /* Add some entries to the .dynamic section. We fill in the values
3711 later (in finish_dynamic_sections) but we must add the entries now
3712 so that we get the correct size for the .dynamic section. */
3713
36af4a4e 3714 if (info->executable)
800eeca4
JW
3715 {
3716 /* The DT_DEBUG entry is filled in by the dynamic linker and used
3717 by the debugger. */
dc810e39 3718#define add_dynamic_entry(TAG, VAL) \
5a580b3a 3719 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39
AM
3720
3721 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3722 return FALSE;
800eeca4
JW
3723 }
3724
dc810e39 3725 if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
b34976b6 3726 return FALSE;
dc810e39 3727 if (!add_dynamic_entry (DT_PLTGOT, 0))
b34976b6 3728 return FALSE;
800eeca4
JW
3729
3730 if (relplt)
3731 {
dc810e39
AM
3732 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3733 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3734 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3735 return FALSE;
800eeca4
JW
3736 }
3737
dc810e39
AM
3738 if (!add_dynamic_entry (DT_RELA, 0)
3739 || !add_dynamic_entry (DT_RELASZ, 0)
3740 || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
b34976b6 3741 return FALSE;
800eeca4 3742
db6751f2 3743 if (ia64_info->reltext)
800eeca4 3744 {
dc810e39 3745 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3746 return FALSE;
d6cf2879 3747 info->flags |= DF_TEXTREL;
800eeca4
JW
3748 }
3749 }
3750
3751 /* ??? Perhaps force __gp local. */
3752
b34976b6 3753 return TRUE;
800eeca4
JW
3754}
3755
3756static bfd_reloc_status_type
eae50df2
L
3757elfNN_ia64_install_value (bfd_byte *hit_addr, bfd_vma v,
3758 unsigned int r_type)
800eeca4
JW
3759{
3760 const struct ia64_operand *op;
3761 int bigendian = 0, shift = 0;
b4677f03
AS
3762 bfd_vma t0, t1, dword;
3763 ia64_insn insn;
800eeca4
JW
3764 enum ia64_opnd opnd;
3765 const char *err;
3766 size_t size = 8;
1e738b87
NC
3767#ifdef BFD_HOST_U_64_BIT
3768 BFD_HOST_U_64_BIT val = (BFD_HOST_U_64_BIT) v;
3769#else
3770 bfd_vma val = v;
3771#endif
800eeca4
JW
3772
3773 opnd = IA64_OPND_NIL;
3774 switch (r_type)
3775 {
3776 case R_IA64_NONE:
3777 case R_IA64_LDXMOV:
3778 return bfd_reloc_ok;
3779
3e932841 3780 /* Instruction relocations. */
800eeca4 3781
13ae64f3
JJ
3782 case R_IA64_IMM14:
3783 case R_IA64_TPREL14:
3784 case R_IA64_DTPREL14:
3785 opnd = IA64_OPND_IMM14;
3786 break;
748abff6 3787
800eeca4
JW
3788 case R_IA64_PCREL21F: opnd = IA64_OPND_TGT25; break;
3789 case R_IA64_PCREL21M: opnd = IA64_OPND_TGT25b; break;
748abff6
RH
3790 case R_IA64_PCREL60B: opnd = IA64_OPND_TGT64; break;
3791 case R_IA64_PCREL21B:
3792 case R_IA64_PCREL21BI:
3793 opnd = IA64_OPND_TGT25c;
3794 break;
800eeca4
JW
3795
3796 case R_IA64_IMM22:
3797 case R_IA64_GPREL22:
3798 case R_IA64_LTOFF22:
3799 case R_IA64_LTOFF22X:
3800 case R_IA64_PLTOFF22:
748abff6 3801 case R_IA64_PCREL22:
800eeca4 3802 case R_IA64_LTOFF_FPTR22:
13ae64f3
JJ
3803 case R_IA64_TPREL22:
3804 case R_IA64_DTPREL22:
3805 case R_IA64_LTOFF_TPREL22:
3806 case R_IA64_LTOFF_DTPMOD22:
3807 case R_IA64_LTOFF_DTPREL22:
800eeca4
JW
3808 opnd = IA64_OPND_IMM22;
3809 break;
3810
3811 case R_IA64_IMM64:
3812 case R_IA64_GPREL64I:
3813 case R_IA64_LTOFF64I:
3814 case R_IA64_PLTOFF64I:
748abff6 3815 case R_IA64_PCREL64I:
800eeca4
JW
3816 case R_IA64_FPTR64I:
3817 case R_IA64_LTOFF_FPTR64I:
13ae64f3
JJ
3818 case R_IA64_TPREL64I:
3819 case R_IA64_DTPREL64I:
800eeca4
JW
3820 opnd = IA64_OPND_IMMU64;
3821 break;
3822
3823 /* Data relocations. */
3824
3825 case R_IA64_DIR32MSB:
3826 case R_IA64_GPREL32MSB:
3827 case R_IA64_FPTR32MSB:
3828 case R_IA64_PCREL32MSB:
a4bd8390 3829 case R_IA64_LTOFF_FPTR32MSB:
800eeca4
JW
3830 case R_IA64_SEGREL32MSB:
3831 case R_IA64_SECREL32MSB:
3832 case R_IA64_LTV32MSB:
13ae64f3 3833 case R_IA64_DTPREL32MSB:
800eeca4
JW
3834 size = 4; bigendian = 1;
3835 break;
3836
3837 case R_IA64_DIR32LSB:
3838 case R_IA64_GPREL32LSB:
3839 case R_IA64_FPTR32LSB:
3840 case R_IA64_PCREL32LSB:
a4bd8390 3841 case R_IA64_LTOFF_FPTR32LSB:
800eeca4
JW
3842 case R_IA64_SEGREL32LSB:
3843 case R_IA64_SECREL32LSB:
3844 case R_IA64_LTV32LSB:
13ae64f3 3845 case R_IA64_DTPREL32LSB:
800eeca4
JW
3846 size = 4; bigendian = 0;
3847 break;
3848
3849 case R_IA64_DIR64MSB:
3850 case R_IA64_GPREL64MSB:
3851 case R_IA64_PLTOFF64MSB:
3852 case R_IA64_FPTR64MSB:
3853 case R_IA64_PCREL64MSB:
3854 case R_IA64_LTOFF_FPTR64MSB:
3855 case R_IA64_SEGREL64MSB:
3856 case R_IA64_SECREL64MSB:
3857 case R_IA64_LTV64MSB:
13ae64f3
JJ
3858 case R_IA64_TPREL64MSB:
3859 case R_IA64_DTPMOD64MSB:
3860 case R_IA64_DTPREL64MSB:
800eeca4
JW
3861 size = 8; bigendian = 1;
3862 break;
3863
3864 case R_IA64_DIR64LSB:
3865 case R_IA64_GPREL64LSB:
3866 case R_IA64_PLTOFF64LSB:
3867 case R_IA64_FPTR64LSB:
3868 case R_IA64_PCREL64LSB:
3869 case R_IA64_LTOFF_FPTR64LSB:
3870 case R_IA64_SEGREL64LSB:
3871 case R_IA64_SECREL64LSB:
3872 case R_IA64_LTV64LSB:
13ae64f3
JJ
3873 case R_IA64_TPREL64LSB:
3874 case R_IA64_DTPMOD64LSB:
3875 case R_IA64_DTPREL64LSB:
800eeca4
JW
3876 size = 8; bigendian = 0;
3877 break;
3878
3879 /* Unsupported / Dynamic relocations. */
800eeca4
JW
3880 default:
3881 return bfd_reloc_notsupported;
3882 }
3883
3884 switch (opnd)
3885 {
3886 case IA64_OPND_IMMU64:
3887 hit_addr -= (long) hit_addr & 0x3;
bbb268c3
JW
3888 t0 = bfd_getl64 (hit_addr);
3889 t1 = bfd_getl64 (hit_addr + 8);
800eeca4
JW
3890
3891 /* tmpl/s: bits 0.. 5 in t0
3892 slot 0: bits 5..45 in t0
3893 slot 1: bits 46..63 in t0, bits 0..22 in t1
3894 slot 2: bits 23..63 in t1 */
3895
3896 /* First, clear the bits that form the 64 bit constant. */
3897 t0 &= ~(0x3ffffLL << 46);
3898 t1 &= ~(0x7fffffLL
3899 | (( (0x07fLL << 13) | (0x1ffLL << 27)
3900 | (0x01fLL << 22) | (0x001LL << 21)
3901 | (0x001LL << 36)) << 23));
3902
3903 t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
3904 t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
3905 t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
3906 | (((val >> 7) & 0x1ff) << 27) /* imm9d */
3907 | (((val >> 16) & 0x01f) << 22) /* imm5c */
3908 | (((val >> 21) & 0x001) << 21) /* ic */
3909 | (((val >> 63) & 0x001) << 36)) << 23; /* i */
3910
bbb268c3
JW
3911 bfd_putl64 (t0, hit_addr);
3912 bfd_putl64 (t1, hit_addr + 8);
800eeca4
JW
3913 break;
3914
748abff6
RH
3915 case IA64_OPND_TGT64:
3916 hit_addr -= (long) hit_addr & 0x3;
bbb268c3
JW
3917 t0 = bfd_getl64 (hit_addr);
3918 t1 = bfd_getl64 (hit_addr + 8);
748abff6
RH
3919
3920 /* tmpl/s: bits 0.. 5 in t0
3921 slot 0: bits 5..45 in t0
3922 slot 1: bits 46..63 in t0, bits 0..22 in t1
3923 slot 2: bits 23..63 in t1 */
3924
3925 /* First, clear the bits that form the 64 bit constant. */
3926 t0 &= ~(0x3ffffLL << 46);
3927 t1 &= ~(0x7fffffLL
3928 | ((1LL << 36 | 0xfffffLL << 13) << 23));
3929
3930 val >>= 4;
3931 t0 |= ((val >> 20) & 0xffffLL) << 2 << 46; /* 16 lsbs of imm39 */
3932 t1 |= ((val >> 36) & 0x7fffffLL) << 0; /* 23 msbs of imm39 */
3933 t1 |= ((((val >> 0) & 0xfffffLL) << 13) /* imm20b */
3934 | (((val >> 59) & 0x1LL) << 36)) << 23; /* i */
3935
bbb268c3
JW
3936 bfd_putl64 (t0, hit_addr);
3937 bfd_putl64 (t1, hit_addr + 8);
748abff6
RH
3938 break;
3939
800eeca4
JW
3940 default:
3941 switch ((long) hit_addr & 0x3)
3942 {
3943 case 0: shift = 5; break;
3944 case 1: shift = 14; hit_addr += 3; break;
3945 case 2: shift = 23; hit_addr += 6; break;
3e932841 3946 case 3: return bfd_reloc_notsupported; /* shouldn't happen... */
800eeca4 3947 }
bbb268c3 3948 dword = bfd_getl64 (hit_addr);
800eeca4
JW
3949 insn = (dword >> shift) & 0x1ffffffffffLL;
3950
3951 op = elf64_ia64_operands + opnd;
b4677f03 3952 err = (*op->insert) (op, val, &insn);
800eeca4
JW
3953 if (err)
3954 return bfd_reloc_overflow;
3955
3956 dword &= ~(0x1ffffffffffLL << shift);
3957 dword |= (insn << shift);
bbb268c3 3958 bfd_putl64 (dword, hit_addr);
800eeca4
JW
3959 break;
3960
3961 case IA64_OPND_NIL:
3962 /* A data relocation. */
3963 if (bigendian)
3964 if (size == 4)
3965 bfd_putb32 (val, hit_addr);
3966 else
3967 bfd_putb64 (val, hit_addr);
3968 else
3969 if (size == 4)
3970 bfd_putl32 (val, hit_addr);
3971 else
3972 bfd_putl64 (val, hit_addr);
3973 break;
3974 }
3975
3976 return bfd_reloc_ok;
3977}
3978
3979static void
eae50df2
L
3980elfNN_ia64_install_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
3981 asection *sec, asection *srel,
3982 bfd_vma offset, unsigned int type,
3983 long dynindx, bfd_vma addend)
800eeca4
JW
3984{
3985 Elf_Internal_Rela outrel;
947216bf 3986 bfd_byte *loc;
800eeca4 3987
800eeca4 3988 BFD_ASSERT (dynindx != -1);
bbe66d08 3989 outrel.r_info = ELFNN_R_INFO (dynindx, type);
800eeca4 3990 outrel.r_addend = addend;
c629eae0 3991 outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
99eb2ac8 3992 if (outrel.r_offset >= (bfd_vma) -2)
800eeca4 3993 {
c629eae0
JJ
3994 /* Run for the hills. We shouldn't be outputting a relocation
3995 for this. So do what everyone else does and output a no-op. */
3996 outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
3997 outrel.r_addend = 0;
3998 outrel.r_offset = 0;
800eeca4 3999 }
99eb2ac8
AM
4000 else
4001 outrel.r_offset += sec->output_section->vma + sec->output_offset;
800eeca4 4002
947216bf
AM
4003 loc = srel->contents;
4004 loc += srel->reloc_count++ * sizeof (ElfNN_External_Rela);
4005 bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
eea6121a 4006 BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count <= srel->size);
800eeca4
JW
4007}
4008
4009/* Store an entry for target address TARGET_ADDR in the linkage table
4010 and return the gp-relative address of the linkage table entry. */
4011
4012static bfd_vma
eae50df2
L
4013set_got_entry (bfd *abfd, struct bfd_link_info *info,
4014 struct elfNN_ia64_dyn_sym_info *dyn_i,
4015 long dynindx, bfd_vma addend, bfd_vma value,
4016 unsigned int dyn_r_type)
800eeca4 4017{
bbe66d08 4018 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4 4019 asection *got_sec;
b34976b6 4020 bfd_boolean done;
13ae64f3 4021 bfd_vma got_offset;
800eeca4 4022
bbe66d08 4023 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
4024 if (ia64_info == NULL)
4025 return 0;
4026
cc3bd654 4027 got_sec = ia64_info->root.sgot;
800eeca4 4028
13ae64f3 4029 switch (dyn_r_type)
800eeca4 4030 {
13ae64f3
JJ
4031 case R_IA64_TPREL64LSB:
4032 done = dyn_i->tprel_done;
b34976b6 4033 dyn_i->tprel_done = TRUE;
13ae64f3
JJ
4034 got_offset = dyn_i->tprel_offset;
4035 break;
4036 case R_IA64_DTPMOD64LSB:
b3dfd7fe
JJ
4037 if (dyn_i->dtpmod_offset != ia64_info->self_dtpmod_offset)
4038 {
4039 done = dyn_i->dtpmod_done;
4040 dyn_i->dtpmod_done = TRUE;
4041 }
4042 else
4043 {
4044 done = ia64_info->self_dtpmod_done;
4045 ia64_info->self_dtpmod_done = TRUE;
4046 dynindx = 0;
4047 }
13ae64f3
JJ
4048 got_offset = dyn_i->dtpmod_offset;
4049 break;
5a260b66 4050 case R_IA64_DTPREL32LSB:
13ae64f3
JJ
4051 case R_IA64_DTPREL64LSB:
4052 done = dyn_i->dtprel_done;
b34976b6 4053 dyn_i->dtprel_done = TRUE;
13ae64f3
JJ
4054 got_offset = dyn_i->dtprel_offset;
4055 break;
4056 default:
4057 done = dyn_i->got_done;
b34976b6 4058 dyn_i->got_done = TRUE;
13ae64f3
JJ
4059 got_offset = dyn_i->got_offset;
4060 break;
4061 }
800eeca4 4062
13ae64f3
JJ
4063 BFD_ASSERT ((got_offset & 7) == 0);
4064
4065 if (! done)
4066 {
800eeca4 4067 /* Store the target address in the linkage table entry. */
13ae64f3 4068 bfd_put_64 (abfd, value, got_sec->contents + got_offset);
800eeca4
JW
4069
4070 /* Install a dynamic relocation if needed. */
9203ba99
JJ
4071 if (((info->shared
4072 && (!dyn_i->h
4073 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
4074 || dyn_i->h->root.type != bfd_link_hash_undefweak)
5a260b66 4075 && dyn_r_type != R_IA64_DTPREL32LSB
9203ba99 4076 && dyn_r_type != R_IA64_DTPREL64LSB)
986a241f 4077 || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info, dyn_r_type)
5a260b66
L
4078 || (dynindx != -1
4079 && (dyn_r_type == R_IA64_FPTR32LSB
4080 || dyn_r_type == R_IA64_FPTR64LSB)))
9203ba99
JJ
4081 && (!dyn_i->want_ltoff_fptr
4082 || !info->pie
4083 || !dyn_i->h
4084 || dyn_i->h->root.type != bfd_link_hash_undefweak))
800eeca4 4085 {
13ae64f3
JJ
4086 if (dynindx == -1
4087 && dyn_r_type != R_IA64_TPREL64LSB
4088 && dyn_r_type != R_IA64_DTPMOD64LSB
5a260b66 4089 && dyn_r_type != R_IA64_DTPREL32LSB
13ae64f3 4090 && dyn_r_type != R_IA64_DTPREL64LSB)
800eeca4 4091 {
5a260b66 4092 dyn_r_type = R_IA64_RELNNLSB;
800eeca4
JW
4093 dynindx = 0;
4094 addend = value;
4095 }
4096
4097 if (bfd_big_endian (abfd))
4098 {
4099 switch (dyn_r_type)
4100 {
5a260b66
L
4101 case R_IA64_REL32LSB:
4102 dyn_r_type = R_IA64_REL32MSB;
4103 break;
4104 case R_IA64_DIR32LSB:
4105 dyn_r_type = R_IA64_DIR32MSB;
4106 break;
4107 case R_IA64_FPTR32LSB:
4108 dyn_r_type = R_IA64_FPTR32MSB;
4109 break;
4110 case R_IA64_DTPREL32LSB:
4111 dyn_r_type = R_IA64_DTPREL32MSB;
4112 break;
800eeca4
JW
4113 case R_IA64_REL64LSB:
4114 dyn_r_type = R_IA64_REL64MSB;
4115 break;
4116 case R_IA64_DIR64LSB:
4117 dyn_r_type = R_IA64_DIR64MSB;
4118 break;
4119 case R_IA64_FPTR64LSB:
4120 dyn_r_type = R_IA64_FPTR64MSB;
4121 break;
13ae64f3
JJ
4122 case R_IA64_TPREL64LSB:
4123 dyn_r_type = R_IA64_TPREL64MSB;
4124 break;
4125 case R_IA64_DTPMOD64LSB:
4126 dyn_r_type = R_IA64_DTPMOD64MSB;
4127 break;
4128 case R_IA64_DTPREL64LSB:
4129 dyn_r_type = R_IA64_DTPREL64MSB;
4130 break;
800eeca4 4131 default:
b34976b6 4132 BFD_ASSERT (FALSE);
800eeca4
JW
4133 break;
4134 }
4135 }
4136
bbe66d08 4137 elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
cc3bd654 4138 ia64_info->root.srelgot,
13ae64f3 4139 got_offset, dyn_r_type,
800eeca4
JW
4140 dynindx, addend);
4141 }
4142 }
4143
4144 /* Return the address of the linkage table entry. */
4145 value = (got_sec->output_section->vma
4146 + got_sec->output_offset
13ae64f3 4147 + got_offset);
800eeca4
JW
4148
4149 return value;
4150}
4151
4152/* Fill in a function descriptor consisting of the function's code
4153 address and its global pointer. Return the descriptor's address. */
4154
4155static bfd_vma
eae50df2
L
4156set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
4157 struct elfNN_ia64_dyn_sym_info *dyn_i,
4158 bfd_vma value)
800eeca4 4159{
bbe66d08 4160 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
4161 asection *fptr_sec;
4162
bbe66d08 4163 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
4164 if (ia64_info == NULL)
4165 return 0;
4166
800eeca4
JW
4167 fptr_sec = ia64_info->fptr_sec;
4168
4169 if (!dyn_i->fptr_done)
4170 {
4171 dyn_i->fptr_done = 1;
4172
4173 /* Fill in the function descriptor. */
4174 bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
4175 bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
4176 fptr_sec->contents + dyn_i->fptr_offset + 8);
9203ba99
JJ
4177 if (ia64_info->rel_fptr_sec)
4178 {
4179 Elf_Internal_Rela outrel;
4180 bfd_byte *loc;
4181
4182 if (bfd_little_endian (abfd))
4183 outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTLSB);
4184 else
4185 outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTMSB);
4186 outrel.r_addend = value;
4187 outrel.r_offset = (fptr_sec->output_section->vma
4188 + fptr_sec->output_offset
4189 + dyn_i->fptr_offset);
4190 loc = ia64_info->rel_fptr_sec->contents;
4191 loc += ia64_info->rel_fptr_sec->reloc_count++
4192 * sizeof (ElfNN_External_Rela);
4193 bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
4194 }
800eeca4
JW
4195 }
4196
4197 /* Return the descriptor's address. */
4198 value = (fptr_sec->output_section->vma
4199 + fptr_sec->output_offset
4200 + dyn_i->fptr_offset);
4201
4202 return value;
4203}
4204
4205/* Fill in a PLTOFF entry consisting of the function's code address
4206 and its global pointer. Return the descriptor's address. */
4207
4208static bfd_vma
eae50df2
L
4209set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
4210 struct elfNN_ia64_dyn_sym_info *dyn_i,
4211 bfd_vma value, bfd_boolean is_plt)
800eeca4 4212{
bbe66d08 4213 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
4214 asection *pltoff_sec;
4215
bbe66d08 4216 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
4217 if (ia64_info == NULL)
4218 return 0;
4219
800eeca4
JW
4220 pltoff_sec = ia64_info->pltoff_sec;
4221
4222 /* Don't do anything if this symbol uses a real PLT entry. In
4223 that case, we'll fill this in during finish_dynamic_symbol. */
4224 if ((! dyn_i->want_plt || is_plt)
4225 && !dyn_i->pltoff_done)
4226 {
18b27f17
RH
4227 bfd_vma gp = _bfd_get_gp_value (abfd);
4228
800eeca4
JW
4229 /* Fill in the function descriptor. */
4230 bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
18b27f17 4231 bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
800eeca4
JW
4232
4233 /* Install dynamic relocations if needed. */
ef5aade5
L
4234 if (!is_plt
4235 && info->shared
4236 && (!dyn_i->h
4237 || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
4238 || dyn_i->h->root.type != bfd_link_hash_undefweak))
800eeca4
JW
4239 {
4240 unsigned int dyn_r_type;
4241
4242 if (bfd_big_endian (abfd))
5a260b66 4243 dyn_r_type = R_IA64_RELNNMSB;
800eeca4 4244 else
5a260b66 4245 dyn_r_type = R_IA64_RELNNLSB;
800eeca4 4246
bbe66d08 4247 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
800eeca4
JW
4248 ia64_info->rel_pltoff_sec,
4249 dyn_i->pltoff_offset,
18b27f17 4250 dyn_r_type, 0, value);
bbe66d08 4251 elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
800eeca4 4252 ia64_info->rel_pltoff_sec,
5a260b66 4253 dyn_i->pltoff_offset + ARCH_SIZE / 8,
18b27f17 4254 dyn_r_type, 0, gp);
800eeca4
JW
4255 }
4256
4257 dyn_i->pltoff_done = 1;
4258 }
4259
4260 /* Return the descriptor's address. */
4261 value = (pltoff_sec->output_section->vma
4262 + pltoff_sec->output_offset
4263 + dyn_i->pltoff_offset);
4264
4265 return value;
4266}
4267
13ae64f3
JJ
4268/* Return the base VMA address which should be subtracted from real addresses
4269 when resolving @tprel() relocation.
4270 Main program TLS (whose template starts at PT_TLS p_vaddr)
5a260b66 4271 is assigned offset round(2 * size of pointer, PT_TLS p_align). */
13ae64f3
JJ
4272
4273static bfd_vma
eae50df2 4274elfNN_ia64_tprel_base (struct bfd_link_info *info)
13ae64f3 4275{
e1918d23 4276 asection *tls_sec = elf_hash_table (info)->tls_sec;
5a260b66
L
4277 return tls_sec->vma - align_power ((bfd_vma) ARCH_SIZE / 4,
4278 tls_sec->alignment_power);
13ae64f3
JJ
4279}
4280
4281/* Return the base VMA address which should be subtracted from real addresses
4282 when resolving @dtprel() relocation.
4283 This is PT_TLS segment p_vaddr. */
4284
4285static bfd_vma
eae50df2 4286elfNN_ia64_dtprel_base (struct bfd_link_info *info)
13ae64f3 4287{
e1918d23 4288 return elf_hash_table (info)->tls_sec->vma;
13ae64f3
JJ
4289}
4290
f3b6f7c3 4291/* Called through qsort to sort the .IA_64.unwind section during a
bbe66d08 4292 non-relocatable link. Set elfNN_ia64_unwind_entry_compare_bfd
f3b6f7c3
RH
4293 to the output bfd so we can do proper endianness frobbing. */
4294
bbe66d08 4295static bfd *elfNN_ia64_unwind_entry_compare_bfd;
f3b6f7c3
RH
4296
4297static int
eae50df2 4298elfNN_ia64_unwind_entry_compare (const PTR a, const PTR b)
f3b6f7c3
RH
4299{
4300 bfd_vma av, bv;
4301
bbe66d08
JW
4302 av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
4303 bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
f3b6f7c3
RH
4304
4305 return (av < bv ? -1 : av > bv ? 1 : 0);
4306}
4307
2c4c2bc0 4308/* Make sure we've got ourselves a nice fat __gp value. */
b34976b6 4309static bfd_boolean
eae50df2 4310elfNN_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info)
800eeca4 4311{
2c4c2bc0
RH
4312 bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
4313 bfd_vma min_short_vma = min_vma, max_short_vma = 0;
4314 struct elf_link_hash_entry *gp;
4315 bfd_vma gp_val;
4316 asection *os;
bbe66d08 4317 struct elfNN_ia64_link_hash_table *ia64_info;
9a951beb 4318
bbe66d08 4319 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
4320 if (ia64_info == NULL)
4321 return FALSE;
800eeca4 4322
2c4c2bc0
RH
4323 /* Find the min and max vma of all sections marked short. Also collect
4324 min and max vma of any type, for use in selecting a nice gp. */
4325 for (os = abfd->sections; os ; os = os->next)
800eeca4 4326 {
2c4c2bc0 4327 bfd_vma lo, hi;
800eeca4 4328
2c4c2bc0
RH
4329 if ((os->flags & SEC_ALLOC) == 0)
4330 continue;
4331
4332 lo = os->vma;
f72c3e6b 4333 hi = os->vma + (os->rawsize ? os->rawsize : os->size);
2c4c2bc0
RH
4334 if (hi < lo)
4335 hi = (bfd_vma) -1;
4336
4337 if (min_vma > lo)
4338 min_vma = lo;
4339 if (max_vma < hi)
4340 max_vma = hi;
4341 if (os->flags & SEC_SMALL_DATA)
800eeca4 4342 {
2c4c2bc0
RH
4343 if (min_short_vma > lo)
4344 min_short_vma = lo;
4345 if (max_short_vma < hi)
4346 max_short_vma = hi;
4347 }
4348 }
800eeca4 4349
a231ddc0
L
4350 if (ia64_info->min_short_sec)
4351 {
4352 if (min_short_vma
affb4bf3 4353 > (ia64_info->min_short_sec->vma
a231ddc0 4354 + ia64_info->min_short_offset))
affb4bf3 4355 min_short_vma = (ia64_info->min_short_sec->vma
a231ddc0
L
4356 + ia64_info->min_short_offset);
4357 if (max_short_vma
affb4bf3 4358 < (ia64_info->max_short_sec->vma
a231ddc0 4359 + ia64_info->max_short_offset))
affb4bf3 4360 max_short_vma = (ia64_info->max_short_sec->vma
a231ddc0
L
4361 + ia64_info->max_short_offset);
4362 }
4363
2c4c2bc0
RH
4364 /* See if the user wants to force a value. */
4365 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
4366 FALSE, FALSE);
800eeca4 4367
2c4c2bc0
RH
4368 if (gp
4369 && (gp->root.type == bfd_link_hash_defined
4370 || gp->root.type == bfd_link_hash_defweak))
4371 {
4372 asection *gp_sec = gp->root.u.def.section;
4373 gp_val = (gp->root.u.def.value
4374 + gp_sec->output_section->vma
4375 + gp_sec->output_offset);
4376 }
4377 else
4378 {
4379 /* Pick a sensible value. */
800eeca4 4380
a231ddc0
L
4381 if (ia64_info->min_short_sec)
4382 {
4383 bfd_vma short_range = max_short_vma - min_short_vma;
2c4c2bc0 4384
a231ddc0
L
4385 /* If min_short_sec is set, pick one in the middle bewteen
4386 min_short_vma and max_short_vma. */
4387 if (short_range >= 0x400000)
4388 goto overflow;
4389 gp_val = min_short_vma + short_range / 2;
4390 }
6d2cf7d8 4391 else
a231ddc0
L
4392 {
4393 asection *got_sec = ia64_info->root.sgot;
4394
4395 /* Start with just the address of the .got. */
4396 if (got_sec)
4397 gp_val = got_sec->output_section->vma;
4398 else if (max_short_vma != 0)
4399 gp_val = min_short_vma;
4400 else if (max_vma - min_vma < 0x200000)
4401 gp_val = min_vma;
4402 else
4403 gp_val = max_vma - 0x200000 + 8;
4404 }
2c4c2bc0
RH
4405
4406 /* If it is possible to address the entire image, but we
4407 don't with the choice above, adjust. */
4408 if (max_vma - min_vma < 0x400000
6d2cf7d8
L
4409 && (max_vma - gp_val >= 0x200000
4410 || gp_val - min_vma > 0x200000))
2c4c2bc0
RH
4411 gp_val = min_vma + 0x200000;
4412 else if (max_short_vma != 0)
4413 {
4414 /* If we don't cover all the short data, adjust. */
4415 if (max_short_vma - gp_val >= 0x200000)
4416 gp_val = min_short_vma + 0x200000;
4417
4418 /* If we're addressing stuff past the end, adjust back. */
4419 if (gp_val > max_vma)
4420 gp_val = max_vma - 0x200000 + 8;
800eeca4 4421 }
2c4c2bc0 4422 }
800eeca4 4423
2c4c2bc0
RH
4424 /* Validate whether all SHF_IA_64_SHORT sections are within
4425 range of the chosen GP. */
800eeca4 4426
2c4c2bc0
RH
4427 if (max_short_vma != 0)
4428 {
4429 if (max_short_vma - min_short_vma >= 0x400000)
800eeca4 4430 {
a231ddc0 4431overflow:
2c4c2bc0
RH
4432 (*_bfd_error_handler)
4433 (_("%s: short data segment overflowed (0x%lx >= 0x400000)"),
4434 bfd_get_filename (abfd),
4435 (unsigned long) (max_short_vma - min_short_vma));
4436 return FALSE;
800eeca4 4437 }
2c4c2bc0
RH
4438 else if ((gp_val > min_short_vma
4439 && gp_val - min_short_vma > 0x200000)
4440 || (gp_val < max_short_vma
4441 && max_short_vma - gp_val >= 0x200000))
800eeca4 4442 {
2c4c2bc0
RH
4443 (*_bfd_error_handler)
4444 (_("%s: __gp does not cover short data segment"),
4445 bfd_get_filename (abfd));
4446 return FALSE;
4447 }
4448 }
800eeca4 4449
2c4c2bc0 4450 _bfd_set_gp_value (abfd, gp_val);
800eeca4 4451
2c4c2bc0
RH
4452 return TRUE;
4453}
800eeca4 4454
2c4c2bc0 4455static bfd_boolean
eae50df2 4456elfNN_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
2c4c2bc0
RH
4457{
4458 struct elfNN_ia64_link_hash_table *ia64_info;
4459 asection *unwind_output_sec;
800eeca4 4460
2c4c2bc0 4461 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
4462 if (ia64_info == NULL)
4463 return FALSE;
800eeca4 4464
2c4c2bc0 4465 /* Make sure we've got ourselves a nice fat __gp value. */
1049f94e 4466 if (!info->relocatable)
2c4c2bc0 4467 {
a38a2e96 4468 bfd_vma gp_val;
2c4c2bc0
RH
4469 struct elf_link_hash_entry *gp;
4470
a38a2e96
L
4471 /* We assume after gp is set, section size will only decrease. We
4472 need to adjust gp for it. */
4473 _bfd_set_gp_value (abfd, 0);
4474 if (! elfNN_ia64_choose_gp (abfd, info))
4475 return FALSE;
4476 gp_val = _bfd_get_gp_value (abfd);
800eeca4 4477
2c4c2bc0
RH
4478 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
4479 FALSE, FALSE);
b4adccfd
RH
4480 if (gp)
4481 {
4482 gp->root.type = bfd_link_hash_defined;
4483 gp->root.u.def.value = gp_val;
4484 gp->root.u.def.section = bfd_abs_section_ptr;
4485 }
800eeca4
JW
4486 }
4487
f3b6f7c3 4488 /* If we're producing a final executable, we need to sort the contents
9a951beb
RH
4489 of the .IA_64.unwind section. Force this section to be relocated
4490 into memory rather than written immediately to the output file. */
4491 unwind_output_sec = NULL;
1049f94e 4492 if (!info->relocatable)
f3b6f7c3
RH
4493 {
4494 asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
4495 if (s)
4496 {
9a951beb
RH
4497 unwind_output_sec = s->output_section;
4498 unwind_output_sec->contents
eea6121a 4499 = bfd_malloc (unwind_output_sec->size);
9a951beb 4500 if (unwind_output_sec->contents == NULL)
b34976b6 4501 return FALSE;
9a951beb
RH
4502 }
4503 }
f3b6f7c3 4504
9a951beb 4505 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 4506 if (!bfd_elf_final_link (abfd, info))
b34976b6 4507 return FALSE;
f3b6f7c3 4508
9a951beb
RH
4509 if (unwind_output_sec)
4510 {
4511 elfNN_ia64_unwind_entry_compare_bfd = abfd;
dc810e39 4512 qsort (unwind_output_sec->contents,
eea6121a 4513 (size_t) (unwind_output_sec->size / 24),
dc810e39
AM
4514 24,
4515 elfNN_ia64_unwind_entry_compare);
9a951beb
RH
4516
4517 if (! bfd_set_section_contents (abfd, unwind_output_sec,
dc810e39 4518 unwind_output_sec->contents, (bfd_vma) 0,
eea6121a 4519 unwind_output_sec->size))
b34976b6 4520 return FALSE;
f3b6f7c3
RH
4521 }
4522
b34976b6 4523 return TRUE;
800eeca4
JW
4524}
4525
b34976b6 4526static bfd_boolean
eae50df2
L
4527elfNN_ia64_relocate_section (bfd *output_bfd,
4528 struct bfd_link_info *info,
4529 bfd *input_bfd,
4530 asection *input_section,
4531 bfd_byte *contents,
4532 Elf_Internal_Rela *relocs,
4533 Elf_Internal_Sym *local_syms,
4534 asection **local_sections)
800eeca4 4535{
bbe66d08 4536 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
4537 Elf_Internal_Shdr *symtab_hdr;
4538 Elf_Internal_Rela *rel;
4539 Elf_Internal_Rela *relend;
4540 asection *srel;
b34976b6 4541 bfd_boolean ret_val = TRUE; /* for non-fatal errors */
800eeca4
JW
4542 bfd_vma gp_val;
4543
4544 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
bbe66d08 4545 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
4546 if (ia64_info == NULL)
4547 return FALSE;
800eeca4
JW
4548
4549 /* Infect various flags from the input section to the output section. */
1049f94e 4550 if (info->relocatable)
800eeca4
JW
4551 {
4552 bfd_vma flags;
4553
4554 flags = elf_section_data(input_section)->this_hdr.sh_flags;
4555 flags &= SHF_IA_64_NORECOV;
4556
4557 elf_section_data(input_section->output_section)
4558 ->this_hdr.sh_flags |= flags;
4559 }
4560
4561 gp_val = _bfd_get_gp_value (output_bfd);
b34976b6 4562 srel = get_reloc_section (input_bfd, ia64_info, input_section, FALSE);
800eeca4
JW
4563
4564 rel = relocs;
4565 relend = relocs + input_section->reloc_count;
4566 for (; rel < relend; ++rel)
4567 {
4568 struct elf_link_hash_entry *h;
bbe66d08 4569 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4
JW
4570 bfd_reloc_status_type r;
4571 reloc_howto_type *howto;
4572 unsigned long r_symndx;
4573 Elf_Internal_Sym *sym;
4574 unsigned int r_type;
4575 bfd_vma value;
4576 asection *sym_sec;
4577 bfd_byte *hit_addr;
b34976b6
AM
4578 bfd_boolean dynamic_symbol_p;
4579 bfd_boolean undef_weak_ref;
800eeca4 4580
bbe66d08 4581 r_type = ELFNN_R_TYPE (rel->r_info);
800eeca4
JW
4582 if (r_type > R_IA64_MAX_RELOC_CODE)
4583 {
4584 (*_bfd_error_handler)
d003868e
AM
4585 (_("%B: unknown relocation type %d"),
4586 input_bfd, (int) r_type);
800eeca4 4587 bfd_set_error (bfd_error_bad_value);
b34976b6 4588 ret_val = FALSE;
800eeca4
JW
4589 continue;
4590 }
b491616a 4591
800eeca4 4592 howto = lookup_howto (r_type);
bbe66d08 4593 r_symndx = ELFNN_R_SYM (rel->r_info);
800eeca4
JW
4594 h = NULL;
4595 sym = NULL;
4596 sym_sec = NULL;
b34976b6 4597 undef_weak_ref = FALSE;
800eeca4
JW
4598
4599 if (r_symndx < symtab_hdr->sh_info)
4600 {
4601 /* Reloc against local symbol. */
8517fae7 4602 asection *msec;
800eeca4
JW
4603 sym = local_syms + r_symndx;
4604 sym_sec = local_sections[r_symndx];
8517fae7
AM
4605 msec = sym_sec;
4606 value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
ab96bf03
AM
4607 if (!info->relocatable
4608 && (sym_sec->flags & SEC_MERGE) != 0
f7460f5f 4609 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
68bfbfcc 4610 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
f7460f5f
JJ
4611 {
4612 struct elfNN_ia64_local_hash_entry *loc_h;
b34976b6
AM
4613
4614 loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, FALSE);
f7460f5f
JJ
4615 if (loc_h && ! loc_h->sec_merge_done)
4616 {
4617 struct elfNN_ia64_dyn_sym_info *dynent;
396a682d 4618 unsigned int count;
f7460f5f 4619
396a682d
L
4620 for (count = loc_h->count, dynent = loc_h->info;
4621 count != 0;
4622 count--, dynent++)
f7460f5f
JJ
4623 {
4624 msec = sym_sec;
4625 dynent->addend =
4626 _bfd_merged_section_offset (output_bfd, &msec,
4627 elf_section_data (msec)->
65765700 4628 sec_info,
f7460f5f 4629 sym->st_value
753731ee 4630 + dynent->addend);
f7460f5f
JJ
4631 dynent->addend -= sym->st_value;
4632 dynent->addend += msec->output_section->vma
4633 + msec->output_offset
4634 - sym_sec->output_section->vma
4635 - sym_sec->output_offset;
4636 }
293a0124
L
4637
4638 /* We may have introduced duplicated entries. We need
4639 to remove them properly. */
4640 count = sort_dyn_sym_info (loc_h->info, loc_h->count);
4641 if (count != loc_h->count)
4642 {
4643 loc_h->count = count;
4644 loc_h->sorted_count = count;
4645 }
396a682d 4646
f7460f5f
JJ
4647 loc_h->sec_merge_done = 1;
4648 }
4649 }
800eeca4
JW
4650 }
4651 else
4652 {
560e09e9
NC
4653 bfd_boolean unresolved_reloc;
4654 bfd_boolean warned;
b2a8e766 4655 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
800eeca4 4656
b2a8e766
AM
4657 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4658 r_symndx, symtab_hdr, sym_hashes,
4659 h, sym_sec, value,
4660 unresolved_reloc, warned);
800eeca4 4661
560e09e9 4662 if (h->root.type == bfd_link_hash_undefweak)
b34976b6 4663 undef_weak_ref = TRUE;
560e09e9
NC
4664 else if (warned)
4665 continue;
800eeca4
JW
4666 }
4667
ab96bf03
AM
4668 /* For relocs against symbols from removed linkonce sections,
4669 or sections discarded by a linker script, we just want the
4670 section contents zeroed. Avoid any special processing. */
4671 if (sym_sec != NULL && elf_discarded_section (sym_sec))
4672 {
4673 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
4674 rel->r_info = 0;
4675 rel->r_addend = 0;
4676 continue;
4677 }
4678
4679 if (info->relocatable)
4680 continue;
4681
800eeca4
JW
4682 hit_addr = contents + rel->r_offset;
4683 value += rel->r_addend;
986a241f 4684 dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info, r_type);
800eeca4
JW
4685
4686 switch (r_type)
4687 {
4688 case R_IA64_NONE:
4689 case R_IA64_LDXMOV:
4690 continue;
4691
4692 case R_IA64_IMM14:
4693 case R_IA64_IMM22:
4694 case R_IA64_IMM64:
4695 case R_IA64_DIR32MSB:
4696 case R_IA64_DIR32LSB:
4697 case R_IA64_DIR64MSB:
4698 case R_IA64_DIR64LSB:
4699 /* Install a dynamic relocation for this reloc. */
02e6ad56 4700 if ((dynamic_symbol_p || info->shared)
ec338859 4701 && r_symndx != 0
800eeca4
JW
4702 && (input_section->flags & SEC_ALLOC) != 0)
4703 {
4704 unsigned int dyn_r_type;
4705 long dynindx;
18b27f17 4706 bfd_vma addend;
800eeca4
JW
4707
4708 BFD_ASSERT (srel != NULL);
4709
838e70c5
L
4710 switch (r_type)
4711 {
4712 case R_IA64_IMM14:
4713 case R_IA64_IMM22:
4714 case R_IA64_IMM64:
4715 /* ??? People shouldn't be doing non-pic code in
4716 shared libraries nor dynamic executables. */
4717 (*_bfd_error_handler)
d003868e
AM
4718 (_("%B: non-pic code with imm relocation against dynamic symbol `%s'"),
4719 input_bfd,
26c61ae5
L
4720 h ? h->root.root.string
4721 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4722 sym_sec));
838e70c5
L
4723 ret_val = FALSE;
4724 continue;
4725
4726 default:
4727 break;
4728 }
4729
800eeca4
JW
4730 /* If we don't need dynamic symbol lookup, find a
4731 matching RELATIVE relocation. */
4732 dyn_r_type = r_type;
986a241f 4733 if (dynamic_symbol_p)
18b27f17
RH
4734 {
4735 dynindx = h->dynindx;
4736 addend = rel->r_addend;
4737 value = 0;
4738 }
800eeca4
JW
4739 else
4740 {
4741 switch (r_type)
4742 {
4743 case R_IA64_DIR32MSB:
4744 dyn_r_type = R_IA64_REL32MSB;
4745 break;
4746 case R_IA64_DIR32LSB:
4747 dyn_r_type = R_IA64_REL32LSB;
4748 break;
4749 case R_IA64_DIR64MSB:
4750 dyn_r_type = R_IA64_REL64MSB;
4751 break;
4752 case R_IA64_DIR64LSB:
4753 dyn_r_type = R_IA64_REL64LSB;
4754 break;
4755
4756 default:
838e70c5 4757 break;
800eeca4
JW
4758 }
4759 dynindx = 0;
18b27f17 4760 addend = value;
800eeca4
JW
4761 }
4762
bbe66d08 4763 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
800eeca4 4764 srel, rel->r_offset, dyn_r_type,
18b27f17 4765 dynindx, addend);
800eeca4 4766 }
ae9a127f 4767 /* Fall through. */
800eeca4
JW
4768
4769 case R_IA64_LTV32MSB:
4770 case R_IA64_LTV32LSB:
4771 case R_IA64_LTV64MSB:
4772 case R_IA64_LTV64LSB:
bbb268c3 4773 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
4774 break;
4775
4776 case R_IA64_GPREL22:
4777 case R_IA64_GPREL64I:
4778 case R_IA64_GPREL32MSB:
4779 case R_IA64_GPREL32LSB:
4780 case R_IA64_GPREL64MSB:
4781 case R_IA64_GPREL64LSB:
4782 if (dynamic_symbol_p)
4783 {
4784 (*_bfd_error_handler)
d003868e 4785 (_("%B: @gprel relocation against dynamic symbol %s"),
26c61ae5
L
4786 input_bfd,
4787 h ? h->root.root.string
4788 : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4789 sym_sec));
b34976b6 4790 ret_val = FALSE;
800eeca4
JW
4791 continue;
4792 }
4793 value -= gp_val;
bbb268c3 4794 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
4795 break;
4796
4797 case R_IA64_LTOFF22:
4798 case R_IA64_LTOFF22X:
4799 case R_IA64_LTOFF64I:
b34976b6 4800 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
800eeca4 4801 value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
5a260b66 4802 rel->r_addend, value, R_IA64_DIRNNLSB);
800eeca4 4803 value -= gp_val;
bbb268c3 4804 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
4805 break;
4806
4807 case R_IA64_PLTOFF22:
4808 case R_IA64_PLTOFF64I:
4809 case R_IA64_PLTOFF64MSB:
4810 case R_IA64_PLTOFF64LSB:
b34976b6
AM
4811 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
4812 value = set_pltoff_entry (output_bfd, info, dyn_i, value, FALSE);
800eeca4 4813 value -= gp_val;
bbb268c3 4814 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
4815 break;
4816
4817 case R_IA64_FPTR64I:
4818 case R_IA64_FPTR32MSB:
4819 case R_IA64_FPTR32LSB:
4820 case R_IA64_FPTR64MSB:
4821 case R_IA64_FPTR64LSB:
b34976b6 4822 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
800eeca4
JW
4823 if (dyn_i->want_fptr)
4824 {
4825 if (!undef_weak_ref)
4826 value = set_fptr_entry (output_bfd, info, dyn_i, value);
4827 }
9203ba99 4828 if (!dyn_i->want_fptr || info->pie)
800eeca4
JW
4829 {
4830 long dynindx;
9203ba99
JJ
4831 unsigned int dyn_r_type = r_type;
4832 bfd_vma addend = rel->r_addend;
800eeca4
JW
4833
4834 /* Otherwise, we expect the dynamic linker to create
4835 the entry. */
4836
9203ba99
JJ
4837 if (dyn_i->want_fptr)
4838 {
4839 if (r_type == R_IA64_FPTR64I)
4840 {
4841 /* We can't represent this without a dynamic symbol.
4842 Adjust the relocation to be against an output
4843 section symbol, which are always present in the
4844 dynamic symbol table. */
4845 /* ??? People shouldn't be doing non-pic code in
4846 shared libraries. Hork. */
4847 (*_bfd_error_handler)
d003868e
AM
4848 (_("%B: linking non-pic code in a position independent executable"),
4849 input_bfd);
9203ba99
JJ
4850 ret_val = FALSE;
4851 continue;
4852 }
4853 dynindx = 0;
4854 addend = value;
5a260b66 4855 dyn_r_type = r_type + R_IA64_RELNNLSB - R_IA64_FPTRNNLSB;
9203ba99
JJ
4856 }
4857 else if (h)
800eeca4
JW
4858 {
4859 if (h->dynindx != -1)
4860 dynindx = h->dynindx;
4861 else
4862 dynindx = (_bfd_elf_link_lookup_local_dynindx
4863 (info, h->root.u.def.section->owner,
4864 global_sym_index (h)));
9203ba99 4865 value = 0;
800eeca4
JW
4866 }
4867 else
4868 {
4869 dynindx = (_bfd_elf_link_lookup_local_dynindx
dc810e39 4870 (info, input_bfd, (long) r_symndx));
9203ba99 4871 value = 0;
800eeca4
JW
4872 }
4873
bbe66d08 4874 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
9203ba99
JJ
4875 srel, rel->r_offset, dyn_r_type,
4876 dynindx, addend);
800eeca4
JW
4877 }
4878
bbb268c3 4879 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
4880 break;
4881
4882 case R_IA64_LTOFF_FPTR22:
4883 case R_IA64_LTOFF_FPTR64I:
a4bd8390
JW
4884 case R_IA64_LTOFF_FPTR32MSB:
4885 case R_IA64_LTOFF_FPTR32LSB:
800eeca4
JW
4886 case R_IA64_LTOFF_FPTR64MSB:
4887 case R_IA64_LTOFF_FPTR64LSB:
4888 {
4889 long dynindx;
4890
b34976b6 4891 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
800eeca4
JW
4892 if (dyn_i->want_fptr)
4893 {
f12123c0 4894 BFD_ASSERT (h == NULL || h->dynindx == -1);
800eeca4
JW
4895 if (!undef_weak_ref)
4896 value = set_fptr_entry (output_bfd, info, dyn_i, value);
4897 dynindx = -1;
4898 }
4899 else
4900 {
4901 /* Otherwise, we expect the dynamic linker to create
4902 the entry. */
4903 if (h)
4904 {
4905 if (h->dynindx != -1)
4906 dynindx = h->dynindx;
4907 else
4908 dynindx = (_bfd_elf_link_lookup_local_dynindx
4909 (info, h->root.u.def.section->owner,
4910 global_sym_index (h)));
4911 }
4912 else
4913 dynindx = (_bfd_elf_link_lookup_local_dynindx
dc810e39 4914 (info, input_bfd, (long) r_symndx));
800eeca4
JW
4915 value = 0;
4916 }
4917
4918 value = set_got_entry (output_bfd, info, dyn_i, dynindx,
5a260b66 4919 rel->r_addend, value, R_IA64_FPTRNNLSB);
800eeca4 4920 value -= gp_val;
bbb268c3 4921 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
4922 }
4923 break;
4924
4925 case R_IA64_PCREL32MSB:
4926 case R_IA64_PCREL32LSB:
4927 case R_IA64_PCREL64MSB:
4928 case R_IA64_PCREL64LSB:
4929 /* Install a dynamic relocation for this reloc. */
02e6ad56 4930 if (dynamic_symbol_p && r_symndx != 0)
800eeca4
JW
4931 {
4932 BFD_ASSERT (srel != NULL);
4933
bbe66d08 4934 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
800eeca4
JW
4935 srel, rel->r_offset, r_type,
4936 h->dynindx, rel->r_addend);
4937 }
4938 goto finish_pcrel;
4939
800eeca4 4940 case R_IA64_PCREL21B:
748abff6 4941 case R_IA64_PCREL60B:
800eeca4 4942 /* We should have created a PLT entry for any dynamic symbol. */
800eeca4
JW
4943 dyn_i = NULL;
4944 if (h)
b34976b6 4945 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
800eeca4
JW
4946
4947 if (dyn_i && dyn_i->want_plt2)
4948 {
4949 /* Should have caught this earlier. */
4950 BFD_ASSERT (rel->r_addend == 0);
4951
cc3bd654
L
4952 value = (ia64_info->root.splt->output_section->vma
4953 + ia64_info->root.splt->output_offset
800eeca4
JW
4954 + dyn_i->plt2_offset);
4955 }
4956 else
4957 {
4958 /* Since there's no PLT entry, Validate that this is
4959 locally defined. */
4960 BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
4961
4962 /* If the symbol is undef_weak, we shouldn't be trying
4963 to call it. There's every chance that we'd wind up
4964 with an out-of-range fixup here. Don't bother setting
4965 any value at all. */
4966 if (undef_weak_ref)
4967 continue;
4968 }
4969 goto finish_pcrel;
4970
2f9bd3f6
RH
4971 case R_IA64_PCREL21BI:
4972 case R_IA64_PCREL21F:
4973 case R_IA64_PCREL21M:
748abff6
RH
4974 case R_IA64_PCREL22:
4975 case R_IA64_PCREL64I:
2f9bd3f6
RH
4976 /* The PCREL21BI reloc is specifically not intended for use with
4977 dynamic relocs. PCREL21F and PCREL21M are used for speculation
f12123c0 4978 fixup code, and thus probably ought not be dynamic. The
2f9bd3f6
RH
4979 PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs. */
4980 if (dynamic_symbol_p)
4981 {
4982 const char *msg;
4983
4984 if (r_type == R_IA64_PCREL21BI)
d003868e 4985 msg = _("%B: @internal branch to dynamic symbol %s");
2f9bd3f6 4986 else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
d003868e 4987 msg = _("%B: speculation fixup to dynamic symbol %s");
2f9bd3f6 4988 else
d003868e 4989 msg = _("%B: @pcrel relocation against dynamic symbol %s");
26c61ae5
L
4990 (*_bfd_error_handler) (msg, input_bfd,
4991 h ? h->root.root.string
4992 : bfd_elf_sym_name (input_bfd,
4993 symtab_hdr,
4994 sym,
4995 sym_sec));
2f9bd3f6
RH
4996 ret_val = FALSE;
4997 continue;
4998 }
4999 goto finish_pcrel;
5000
800eeca4
JW
5001 finish_pcrel:
5002 /* Make pc-relative. */
5003 value -= (input_section->output_section->vma
5004 + input_section->output_offset
5005 + rel->r_offset) & ~ (bfd_vma) 0x3;
bbb268c3 5006 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
5007 break;
5008
5009 case R_IA64_SEGREL32MSB:
5010 case R_IA64_SEGREL32LSB:
5011 case R_IA64_SEGREL64MSB:
5012 case R_IA64_SEGREL64LSB:
d7458677 5013 {
d7458677 5014 /* Find the segment that contains the output_section. */
2ea37f1c 5015 Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
992824d5 5016 (output_bfd, input_section->output_section);
800eeca4 5017
2ea37f1c 5018 if (p == NULL)
d7458677 5019 {
800eeca4 5020 r = bfd_reloc_notsupported;
d7458677
AM
5021 }
5022 else
5023 {
5024 /* The VMA of the segment is the vaddr of the associated
5025 program header. */
5026 if (value > p->p_vaddr)
5027 value -= p->p_vaddr;
5028 else
5029 value = 0;
bbb268c3 5030 r = elfNN_ia64_install_value (hit_addr, value, r_type);
d7458677
AM
5031 }
5032 break;
5033 }
800eeca4
JW
5034
5035 case R_IA64_SECREL32MSB:
5036 case R_IA64_SECREL32LSB:
5037 case R_IA64_SECREL64MSB:
5038 case R_IA64_SECREL64LSB:
97ecf322
L
5039 /* Make output-section relative to section where the symbol
5040 is defined. PR 475 */
bf718458
L
5041 if (sym_sec)
5042 value -= sym_sec->output_section->vma;
bbb268c3 5043 r = elfNN_ia64_install_value (hit_addr, value, r_type);
800eeca4
JW
5044 break;
5045
800eeca4
JW
5046 case R_IA64_IPLTMSB:
5047 case R_IA64_IPLTLSB:
18b27f17
RH
5048 /* Install a dynamic relocation for this reloc. */
5049 if ((dynamic_symbol_p || info->shared)
5050 && (input_section->flags & SEC_ALLOC) != 0)
5051 {
18b27f17
RH
5052 BFD_ASSERT (srel != NULL);
5053
5054 /* If we don't need dynamic symbol lookup, install two
5055 RELATIVE relocations. */
986a241f 5056 if (!dynamic_symbol_p)
18b27f17
RH
5057 {
5058 unsigned int dyn_r_type;
3e932841 5059
18b27f17
RH
5060 if (r_type == R_IA64_IPLTMSB)
5061 dyn_r_type = R_IA64_REL64MSB;
5062 else
5063 dyn_r_type = R_IA64_REL64LSB;
5064
5065 elfNN_ia64_install_dyn_reloc (output_bfd, info,
5066 input_section,
5067 srel, rel->r_offset,
5068 dyn_r_type, 0, value);
5069 elfNN_ia64_install_dyn_reloc (output_bfd, info,
5070 input_section,
5071 srel, rel->r_offset + 8,
5072 dyn_r_type, 0, gp_val);
5073 }
5074 else
5075 elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
5076 srel, rel->r_offset, r_type,
5077 h->dynindx, rel->r_addend);
5078 }
5079
5080 if (r_type == R_IA64_IPLTMSB)
5081 r_type = R_IA64_DIR64MSB;
5082 else
5083 r_type = R_IA64_DIR64LSB;
bbb268c3
JW
5084 elfNN_ia64_install_value (hit_addr, value, r_type);
5085 r = elfNN_ia64_install_value (hit_addr + 8, gp_val, r_type);
18b27f17 5086 break;
800eeca4 5087
13ae64f3
JJ
5088 case R_IA64_TPREL14:
5089 case R_IA64_TPREL22:
5090 case R_IA64_TPREL64I:
0f2830ff
L
5091 if (elf_hash_table (info)->tls_sec == NULL)
5092 goto missing_tls_sec;
13ae64f3 5093 value -= elfNN_ia64_tprel_base (info);
bbb268c3 5094 r = elfNN_ia64_install_value (hit_addr, value, r_type);
13ae64f3
JJ
5095 break;
5096
5097 case R_IA64_DTPREL14:
5098 case R_IA64_DTPREL22:
5099 case R_IA64_DTPREL64I:
5a260b66
L
5100 case R_IA64_DTPREL32LSB:
5101 case R_IA64_DTPREL32MSB:
b3dfd7fe
JJ
5102 case R_IA64_DTPREL64LSB:
5103 case R_IA64_DTPREL64MSB:
0f2830ff
L
5104 if (elf_hash_table (info)->tls_sec == NULL)
5105 goto missing_tls_sec;
13ae64f3 5106 value -= elfNN_ia64_dtprel_base (info);
bbb268c3 5107 r = elfNN_ia64_install_value (hit_addr, value, r_type);
13ae64f3
JJ
5108 break;
5109
5110 case R_IA64_LTOFF_TPREL22:
5111 case R_IA64_LTOFF_DTPMOD22:
5112 case R_IA64_LTOFF_DTPREL22:
5113 {
5114 int got_r_type;
a823975a
JJ
5115 long dynindx = h ? h->dynindx : -1;
5116 bfd_vma r_addend = rel->r_addend;
13ae64f3
JJ
5117
5118 switch (r_type)
5119 {
5120 default:
5121 case R_IA64_LTOFF_TPREL22:
a823975a
JJ
5122 if (!dynamic_symbol_p)
5123 {
0f2830ff
L
5124 if (elf_hash_table (info)->tls_sec == NULL)
5125 goto missing_tls_sec;
a823975a
JJ
5126 if (!info->shared)
5127 value -= elfNN_ia64_tprel_base (info);
5128 else
5129 {
5130 r_addend += value - elfNN_ia64_dtprel_base (info);
5131 dynindx = 0;
5132 }
5133 }
13ae64f3
JJ
5134 got_r_type = R_IA64_TPREL64LSB;
5135 break;
5136 case R_IA64_LTOFF_DTPMOD22:
5137 if (!dynamic_symbol_p && !info->shared)
5138 value = 1;
5139 got_r_type = R_IA64_DTPMOD64LSB;
5140 break;
5141 case R_IA64_LTOFF_DTPREL22:
5142 if (!dynamic_symbol_p)
0f2830ff
L
5143 {
5144 if (elf_hash_table (info)->tls_sec == NULL)
5145 goto missing_tls_sec;
5146 value -= elfNN_ia64_dtprel_base (info);
5147 }
5a260b66 5148 got_r_type = R_IA64_DTPRELNNLSB;
13ae64f3
JJ
5149 break;
5150 }
b34976b6 5151 dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, FALSE);
a823975a 5152 value = set_got_entry (input_bfd, info, dyn_i, dynindx, r_addend,
13ae64f3
JJ
5153 value, got_r_type);
5154 value -= gp_val;
bbb268c3 5155 r = elfNN_ia64_install_value (hit_addr, value, r_type);
13ae64f3
JJ
5156 }
5157 break;
5158
800eeca4
JW
5159 default:
5160 r = bfd_reloc_notsupported;
5161 break;
5162 }
5163
5164 switch (r)
5165 {
5166 case bfd_reloc_ok:
5167 break;
5168
5169 case bfd_reloc_undefined:
5170 /* This can happen for global table relative relocs if
5171 __gp is undefined. This is a panic situation so we
5172 don't try to continue. */
5173 (*info->callbacks->undefined_symbol)
5174 (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
b34976b6 5175 return FALSE;
800eeca4
JW
5176
5177 case bfd_reloc_notsupported:
5178 {
5179 const char *name;
5180
5181 if (h)
5182 name = h->root.root.string;
5183 else
26c61ae5
L
5184 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
5185 sym_sec);
800eeca4
JW
5186 if (!(*info->callbacks->warning) (info, _("unsupported reloc"),
5187 name, input_bfd,
5188 input_section, rel->r_offset))
b34976b6
AM
5189 return FALSE;
5190 ret_val = FALSE;
800eeca4
JW
5191 }
5192 break;
5193
5194 case bfd_reloc_dangerous:
5195 case bfd_reloc_outofrange:
5196 case bfd_reloc_overflow:
5197 default:
0f2830ff 5198missing_tls_sec:
800eeca4
JW
5199 {
5200 const char *name;
5201
5202 if (h)
f0581930 5203 name = h->root.root.string;
800eeca4 5204 else
26c61ae5
L
5205 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
5206 sym_sec);
c5509b92
L
5207
5208 switch (r_type)
5209 {
0f2830ff
L
5210 case R_IA64_TPREL14:
5211 case R_IA64_TPREL22:
5212 case R_IA64_TPREL64I:
5213 case R_IA64_DTPREL14:
5214 case R_IA64_DTPREL22:
5215 case R_IA64_DTPREL64I:
5216 case R_IA64_DTPREL32LSB:
5217 case R_IA64_DTPREL32MSB:
5218 case R_IA64_DTPREL64LSB:
5219 case R_IA64_DTPREL64MSB:
5220 case R_IA64_LTOFF_TPREL22:
5221 case R_IA64_LTOFF_DTPMOD22:
5222 case R_IA64_LTOFF_DTPREL22:
5223 (*_bfd_error_handler)
5224 (_("%B: missing TLS section for relocation %s against `%s' at 0x%lx in section `%A'."),
5225 input_bfd, input_section, howto->name, name,
5226 rel->r_offset);
5227 break;
5228
c5509b92
L
5229 case R_IA64_PCREL21B:
5230 case R_IA64_PCREL21BI:
5231 case R_IA64_PCREL21M:
5232 case R_IA64_PCREL21F:
5233 if (is_elf_hash_table (info->hash))
5234 {
5235 /* Relaxtion is always performed for ELF output.
5236 Overflow failures for those relocations mean
5237 that the section is too big to relax. */
5238 (*_bfd_error_handler)
5239 (_("%B: Can't relax br (%s) to `%s' at 0x%lx in section `%A' with size 0x%lx (> 0x1000000)."),
5240 input_bfd, input_section, howto->name, name,
5241 rel->r_offset, input_section->size);
5242 break;
5243 }
5244 default:
5245 if (!(*info->callbacks->reloc_overflow) (info,
5246 &h->root,
5247 name,
5248 howto->name,
5249 (bfd_vma) 0,
5250 input_bfd,
5251 input_section,
5252 rel->r_offset))
5253 return FALSE;
5254 break;
5255 }
5256
b34976b6 5257 ret_val = FALSE;
800eeca4
JW
5258 }
5259 break;
5260 }
5261 }
5262
5263 return ret_val;
5264}
5265
b34976b6 5266static bfd_boolean
eae50df2
L
5267elfNN_ia64_finish_dynamic_symbol (bfd *output_bfd,
5268 struct bfd_link_info *info,
5269 struct elf_link_hash_entry *h,
5270 Elf_Internal_Sym *sym)
800eeca4 5271{
bbe66d08
JW
5272 struct elfNN_ia64_link_hash_table *ia64_info;
5273 struct elfNN_ia64_dyn_sym_info *dyn_i;
800eeca4 5274
bbe66d08 5275 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
5276 if (ia64_info == NULL)
5277 return FALSE;
5278
b34976b6 5279 dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, FALSE);
800eeca4
JW
5280
5281 /* Fill in the PLT data, if required. */
5282 if (dyn_i && dyn_i->want_plt)
5283 {
5284 Elf_Internal_Rela outrel;
5285 bfd_byte *loc;
5286 asection *plt_sec;
91d6fa6a 5287 bfd_vma plt_addr, pltoff_addr, gp_val, plt_index;
800eeca4
JW
5288
5289 gp_val = _bfd_get_gp_value (output_bfd);
5290
5291 /* Initialize the minimal PLT entry. */
5292
91d6fa6a 5293 plt_index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
cc3bd654 5294 plt_sec = ia64_info->root.splt;
800eeca4
JW
5295 loc = plt_sec->contents + dyn_i->plt_offset;
5296
5297 memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
91d6fa6a 5298 elfNN_ia64_install_value (loc, plt_index, R_IA64_IMM22);
bbb268c3 5299 elfNN_ia64_install_value (loc+2, -dyn_i->plt_offset, R_IA64_PCREL21B);
800eeca4
JW
5300
5301 plt_addr = (plt_sec->output_section->vma
5302 + plt_sec->output_offset
5303 + dyn_i->plt_offset);
b34976b6 5304 pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, TRUE);
800eeca4
JW
5305
5306 /* Initialize the FULL PLT entry, if needed. */
5307 if (dyn_i->want_plt2)
5308 {
5309 loc = plt_sec->contents + dyn_i->plt2_offset;
5310
5311 memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
bbb268c3 5312 elfNN_ia64_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
800eeca4
JW
5313
5314 /* Mark the symbol as undefined, rather than as defined in the
5315 plt section. Leave the value alone. */
5316 /* ??? We didn't redefine it in adjust_dynamic_symbol in the
c152c796 5317 first place. But perhaps elflink.c did some for us. */
f5385ebf 5318 if (!h->def_regular)
800eeca4
JW
5319 sym->st_shndx = SHN_UNDEF;
5320 }
5321
5322 /* Create the dynamic relocation. */
5323 outrel.r_offset = pltoff_addr;
5324 if (bfd_little_endian (output_bfd))
bbe66d08 5325 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
800eeca4 5326 else
bbe66d08 5327 outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
800eeca4
JW
5328 outrel.r_addend = 0;
5329
5330 /* This is fun. In the .IA_64.pltoff section, we've got entries
5331 that correspond both to real PLT entries, and those that
5332 happened to resolve to local symbols but need to be created
5333 to satisfy @pltoff relocations. The .rela.IA_64.pltoff
5334 relocations for the real PLT should come at the end of the
5335 section, so that they can be indexed by plt entry at runtime.
5336
5337 We emitted all of the relocations for the non-PLT @pltoff
5338 entries during relocate_section. So we can consider the
5339 existing sec->reloc_count to be the base of the array of
5340 PLT relocations. */
5341
947216bf 5342 loc = ia64_info->rel_pltoff_sec->contents;
91d6fa6a 5343 loc += ((ia64_info->rel_pltoff_sec->reloc_count + plt_index)
37cd2629 5344 * sizeof (ElfNN_External_Rela));
947216bf 5345 bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
800eeca4
JW
5346 }
5347
5348 /* Mark some specially defined symbols as absolute. */
5349 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1
RS
5350 || h == ia64_info->root.hgot
5351 || h == ia64_info->root.hplt)
800eeca4
JW
5352 sym->st_shndx = SHN_ABS;
5353
b34976b6 5354 return TRUE;
800eeca4
JW
5355}
5356
b34976b6 5357static bfd_boolean
eae50df2
L
5358elfNN_ia64_finish_dynamic_sections (bfd *abfd,
5359 struct bfd_link_info *info)
800eeca4 5360{
bbe66d08 5361 struct elfNN_ia64_link_hash_table *ia64_info;
800eeca4
JW
5362 bfd *dynobj;
5363
bbe66d08 5364 ia64_info = elfNN_ia64_hash_table (info);
4dfe6ac6
NC
5365 if (ia64_info == NULL)
5366 return FALSE;
5367
800eeca4
JW
5368 dynobj = ia64_info->root.dynobj;
5369
5370 if (elf_hash_table (info)->dynamic_sections_created)
5371 {
bbe66d08 5372 ElfNN_External_Dyn *dyncon, *dynconend;
800eeca4
JW
5373 asection *sdyn, *sgotplt;
5374 bfd_vma gp_val;
5375
5376 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5377 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
5378 BFD_ASSERT (sdyn != NULL);
bbe66d08 5379 dyncon = (ElfNN_External_Dyn *) sdyn->contents;
eea6121a 5380 dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
800eeca4
JW
5381
5382 gp_val = _bfd_get_gp_value (abfd);
5383
5384 for (; dyncon < dynconend; dyncon++)
5385 {
5386 Elf_Internal_Dyn dyn;
800eeca4 5387
bbe66d08 5388 bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
800eeca4
JW
5389
5390 switch (dyn.d_tag)
5391 {
5392 case DT_PLTGOT:
5393 dyn.d_un.d_ptr = gp_val;
5394 break;
5395
5396 case DT_PLTRELSZ:
5397 dyn.d_un.d_val = (ia64_info->minplt_entries
bbe66d08 5398 * sizeof (ElfNN_External_Rela));
800eeca4
JW
5399 break;
5400
5401 case DT_JMPREL:
5402 /* See the comment above in finish_dynamic_symbol. */
5403 dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
5404 + ia64_info->rel_pltoff_sec->output_offset
5405 + (ia64_info->rel_pltoff_sec->reloc_count
bbe66d08 5406 * sizeof (ElfNN_External_Rela)));
800eeca4
JW
5407 break;
5408
5409 case DT_IA_64_PLT_RESERVE:
5410 dyn.d_un.d_ptr = (sgotplt->output_section->vma
5411 + sgotplt->output_offset);
5412 break;
5413
5414 case DT_RELASZ:
5415 /* Do not have RELASZ include JMPREL. This makes things
3e932841 5416 easier on ld.so. This is not what the rest of BFD set up. */
800eeca4 5417 dyn.d_un.d_val -= (ia64_info->minplt_entries
bbe66d08 5418 * sizeof (ElfNN_External_Rela));
800eeca4 5419 break;
800eeca4
JW
5420 }
5421
bbe66d08 5422 bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
800eeca4
JW
5423 }
5424
ae9a127f 5425 /* Initialize the PLT0 entry. */
cc3bd654 5426 if (ia64_info->root.splt)
800eeca4 5427 {
cc3bd654 5428 bfd_byte *loc = ia64_info->root.splt->contents;
800eeca4
JW
5429 bfd_vma pltres;
5430
5431 memcpy (loc, plt_header, PLT_HEADER_SIZE);
5432
5433 pltres = (sgotplt->output_section->vma
5434 + sgotplt->output_offset
5435 - gp_val);
5436
bbb268c3 5437 elfNN_ia64_install_value (loc+1, pltres, R_IA64_GPREL22);
800eeca4
JW
5438 }
5439 }
5440
b34976b6 5441 return TRUE;
800eeca4
JW
5442}
5443\f
ae9a127f 5444/* ELF file flag handling: */
800eeca4 5445
3e932841 5446/* Function to keep IA-64 specific file flags. */
b34976b6 5447static bfd_boolean
eae50df2 5448elfNN_ia64_set_private_flags (bfd *abfd, flagword flags)
800eeca4
JW
5449{
5450 BFD_ASSERT (!elf_flags_init (abfd)
5451 || elf_elfheader (abfd)->e_flags == flags);
5452
5453 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
5454 elf_flags_init (abfd) = TRUE;
5455 return TRUE;
800eeca4
JW
5456}
5457
800eeca4
JW
5458/* Merge backend specific data from an object file to the output
5459 object file when linking. */
b34976b6 5460static bfd_boolean
eae50df2 5461elfNN_ia64_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
800eeca4
JW
5462{
5463 flagword out_flags;
5464 flagword in_flags;
b34976b6 5465 bfd_boolean ok = TRUE;
800eeca4
JW
5466
5467 /* Don't even pretend to support mixed-format linking. */
5468 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5469 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 5470 return FALSE;
800eeca4
JW
5471
5472 in_flags = elf_elfheader (ibfd)->e_flags;
5473 out_flags = elf_elfheader (obfd)->e_flags;
5474
5475 if (! elf_flags_init (obfd))
5476 {
b34976b6 5477 elf_flags_init (obfd) = TRUE;
800eeca4
JW
5478 elf_elfheader (obfd)->e_flags = in_flags;
5479
5480 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5481 && bfd_get_arch_info (obfd)->the_default)
5482 {
5483 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
5484 bfd_get_mach (ibfd));
5485 }
5486
b34976b6 5487 return TRUE;
800eeca4
JW
5488 }
5489
5490 /* Check flag compatibility. */
5491 if (in_flags == out_flags)
b34976b6 5492 return TRUE;
800eeca4 5493
c43c2cc5
JW
5494 /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
5495 if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
5496 elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
5497
800eeca4
JW
5498 if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
5499 {
5500 (*_bfd_error_handler)
d003868e
AM
5501 (_("%B: linking trap-on-NULL-dereference with non-trapping files"),
5502 ibfd);
800eeca4
JW
5503
5504 bfd_set_error (bfd_error_bad_value);
b34976b6 5505 ok = FALSE;
800eeca4
JW
5506 }
5507 if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
5508 {
5509 (*_bfd_error_handler)
d003868e
AM
5510 (_("%B: linking big-endian files with little-endian files"),
5511 ibfd);
800eeca4
JW
5512
5513 bfd_set_error (bfd_error_bad_value);
b34976b6 5514 ok = FALSE;
800eeca4
JW
5515 }
5516 if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
5517 {
5518 (*_bfd_error_handler)
d003868e
AM
5519 (_("%B: linking 64-bit files with 32-bit files"),
5520 ibfd);
800eeca4
JW
5521
5522 bfd_set_error (bfd_error_bad_value);
b34976b6 5523 ok = FALSE;
800eeca4 5524 }
c43c2cc5
JW
5525 if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
5526 {
5527 (*_bfd_error_handler)
d003868e
AM
5528 (_("%B: linking constant-gp files with non-constant-gp files"),
5529 ibfd);
c43c2cc5
JW
5530
5531 bfd_set_error (bfd_error_bad_value);
b34976b6 5532 ok = FALSE;
c43c2cc5
JW
5533 }
5534 if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
5535 != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
5536 {
5537 (*_bfd_error_handler)
d003868e
AM
5538 (_("%B: linking auto-pic files with non-auto-pic files"),
5539 ibfd);
c43c2cc5
JW
5540
5541 bfd_set_error (bfd_error_bad_value);
b34976b6 5542 ok = FALSE;
c43c2cc5 5543 }
800eeca4
JW
5544
5545 return ok;
5546}
5547
b34976b6 5548static bfd_boolean
eae50df2 5549elfNN_ia64_print_private_bfd_data (bfd *abfd, PTR ptr)
800eeca4
JW
5550{
5551 FILE *file = (FILE *) ptr;
5552 flagword flags = elf_elfheader (abfd)->e_flags;
5553
5554 BFD_ASSERT (abfd != NULL && ptr != NULL);
5555
c43c2cc5 5556 fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
800eeca4
JW
5557 (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
5558 (flags & EF_IA_64_EXT) ? "EXT, " : "",
5559 (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
c43c2cc5
JW
5560 (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
5561 (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
5562 (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
5563 (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
800eeca4 5564 (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
3e932841 5565
800eeca4 5566 _bfd_elf_print_private_bfd_data (abfd, ptr);
b34976b6 5567 return TRUE;
800eeca4 5568}
db6751f2
JJ
5569
5570static enum elf_reloc_type_class
eae50df2 5571elfNN_ia64_reloc_type_class (const Elf_Internal_Rela *rela)
db6751f2 5572{
f51e552e 5573 switch ((int) ELFNN_R_TYPE (rela->r_info))
db6751f2
JJ
5574 {
5575 case R_IA64_REL32MSB:
5576 case R_IA64_REL32LSB:
5577 case R_IA64_REL64MSB:
5578 case R_IA64_REL64LSB:
5579 return reloc_class_relative;
5580 case R_IA64_IPLTMSB:
5581 case R_IA64_IPLTLSB:
5582 return reloc_class_plt;
5583 case R_IA64_COPY:
5584 return reloc_class_copy;
5585 default:
5586 return reloc_class_normal;
5587 }
5588}
fcf12726 5589
b35d266b 5590static const struct bfd_elf_special_section elfNN_ia64_special_sections[] =
2f89ff8d 5591{
0112cd26
NC
5592 { STRING_COMMA_LEN (".sbss"), -1, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
5593 { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
5594 { NULL, 0, 0, 0, 0 }
7f4d3958
L
5595};
5596
da9f89d4
L
5597static bfd_boolean
5598elfNN_ia64_object_p (bfd *abfd)
5599{
5600 asection *sec;
da9f89d4
L
5601 asection *group, *unwi, *unw;
5602 flagword flags;
5603 const char *name;
5604 char *unwi_name, *unw_name;
5605 bfd_size_type amt;
5606
5607 if (abfd->flags & DYNAMIC)
5608 return TRUE;
5609
5610 /* Flags for fake group section. */
5611 flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
5612 | SEC_EXCLUDE);
5613
5614 /* We add a fake section group for each .gnu.linkonce.t.* section,
5615 which isn't in a section group, and its unwind sections. */
5616 for (sec = abfd->sections; sec != NULL; sec = sec->next)
5617 {
5618 if (elf_sec_group (sec) == NULL
5619 && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
5620 == (SEC_LINK_ONCE | SEC_CODE))
0112cd26 5621 && CONST_STRNEQ (sec->name, ".gnu.linkonce.t."))
da9f89d4
L
5622 {
5623 name = sec->name + 16;
5624
5625 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
5626 unwi_name = bfd_alloc (abfd, amt);
5627 if (!unwi_name)
5628 return FALSE;
5629
5630 strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
5631 unwi = bfd_get_section_by_name (abfd, unwi_name);
5632
5633 amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
5634 unw_name = bfd_alloc (abfd, amt);
5635 if (!unw_name)
5636 return FALSE;
5637
5638 strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
5639 unw = bfd_get_section_by_name (abfd, unw_name);
5640
da9f89d4
L
5641 /* We need to create a fake group section for it and its
5642 unwind sections. */
3496cb2a
L
5643 group = bfd_make_section_anyway_with_flags (abfd, name,
5644 flags);
5645 if (group == NULL)
da9f89d4
L
5646 return FALSE;
5647
5648 /* Move the fake group section to the beginning. */
5daa8fe7 5649 bfd_section_list_remove (abfd, group);
04dd1667 5650 bfd_section_list_prepend (abfd, group);
da9f89d4
L
5651
5652 elf_next_in_group (group) = sec;
5653
5654 elf_group_name (sec) = name;
5655 elf_next_in_group (sec) = sec;
5656 elf_sec_group (sec) = group;
5657
5658 if (unwi)
5659 {
5660 elf_group_name (unwi) = name;
5661 elf_next_in_group (unwi) = sec;
5662 elf_next_in_group (sec) = unwi;
5663 elf_sec_group (unwi) = group;
5664 }
5665
5666 if (unw)
5667 {
5668 elf_group_name (unw) = name;
5669 if (unwi)
5670 {
5671 elf_next_in_group (unw) = elf_next_in_group (unwi);
5672 elf_next_in_group (unwi) = unw;
5673 }
5674 else
5675 {
5676 elf_next_in_group (unw) = sec;
5677 elf_next_in_group (sec) = unw;
5678 }
5679 elf_sec_group (unw) = group;
5680 }
5681
5682 /* Fake SHT_GROUP section header. */
5683 elf_section_data (group)->this_hdr.bfd_section = group;
5684 elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
5685 }
5686 }
5687 return TRUE;
5688}
5689
b34976b6 5690static bfd_boolean
d9cf1b54
AM
5691elfNN_ia64_hpux_vec (const bfd_target *vec)
5692{
5693 extern const bfd_target bfd_elfNN_ia64_hpux_big_vec;
5694 return (vec == & bfd_elfNN_ia64_hpux_big_vec);
5695}
5696
fcf12726 5697static void
eae50df2
L
5698elfNN_hpux_post_process_headers (bfd *abfd,
5699 struct bfd_link_info *info ATTRIBUTE_UNUSED)
fcf12726
AM
5700{
5701 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5702
d1036acb 5703 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
fcf12726
AM
5704 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
5705}
d9cf1b54 5706
eae50df2
L
5707static bfd_boolean
5708elfNN_hpux_backend_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5709 asection *sec, int *retval)
d9cf1b54
AM
5710{
5711 if (bfd_is_com_section (sec))
5712 {
5713 *retval = SHN_IA_64_ANSI_COMMON;
b34976b6 5714 return TRUE;
d9cf1b54 5715 }
b34976b6 5716 return FALSE;
d9cf1b54 5717}
b59dd4a5
L
5718
5719static void
5720elfNN_hpux_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5721 asymbol *asym)
5722{
5f1cb353 5723 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
b59dd4a5
L
5724
5725 switch (elfsym->internal_elf_sym.st_shndx)
5726 {
5727 case SHN_IA_64_ANSI_COMMON:
5728 asym->section = bfd_com_section_ptr;
5729 asym->value = elfsym->internal_elf_sym.st_size;
5730 asym->flags &= ~BSF_GLOBAL;
5731 break;
5732 }
5733}
5734
7256a114
TG
5735#ifdef INCLUDE_IA64_VMS
5736
01e1a5bc
NC
5737static bfd_boolean
5738elfNN_vms_section_from_shdr (bfd *abfd,
5739 Elf_Internal_Shdr *hdr,
5740 const char *name,
5741 int shindex)
5742{
5743 asection *newsect;
5744
5745 switch (hdr->sh_type)
5746 {
5747 case SHT_IA_64_VMS_TRACE:
5748 case SHT_IA_64_VMS_DEBUG:
5749 case SHT_IA_64_VMS_DEBUG_STR:
5750 break;
5751
5752 default:
5753 return elfNN_ia64_section_from_shdr (abfd, hdr, name, shindex);
5754 }
5755
5756 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5757 return FALSE;
5758 newsect = hdr->bfd_section;
5759
5760 return TRUE;
5761}
5762
5763static bfd_boolean
5764elfNN_vms_object_p (bfd *abfd)
5765{
5766 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5767 Elf_Internal_Phdr *i_phdr = elf_tdata (abfd)->phdr;
5768 unsigned int i;
5769 unsigned int num_text = 0;
5770 unsigned int num_data = 0;
5771 unsigned int num_rodata = 0;
5772 char name[16];
5773
5774 if (!elfNN_ia64_object_p (abfd))
5775 return FALSE;
5776
5777 for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
5778 {
5779 /* Is there a section for this segment? */
5780 bfd_vma base_vma = i_phdr->p_vaddr;
5781 bfd_vma limit_vma = base_vma + i_phdr->p_filesz;
5782
5783 if (i_phdr->p_type != PT_LOAD)
5784 continue;
5785
5786 again:
5787 while (base_vma < limit_vma)
5788 {
5789 bfd_vma next_vma = limit_vma;
5790 asection *nsec;
5791 asection *sec;
5792 flagword flags;
5793 char *nname = NULL;
5794
5795 /* Find a section covering base_vma. */
5796 for (sec = abfd->sections; sec != NULL; sec = sec->next)
5797 {
5798 if ((sec->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
5799 continue;
5800 if (sec->vma <= base_vma && sec->vma + sec->size > base_vma)
5801 {
5802 base_vma = sec->vma + sec->size;
5803 goto again;
5804 }
5805 if (sec->vma < next_vma && sec->vma + sec->size >= base_vma)
5806 next_vma = sec->vma;
5807 }
5808
5809 /* No section covering [base_vma; next_vma). Create a fake one. */
5810 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
5811 if (i_phdr->p_flags & PF_X)
5812 {
5813 flags |= SEC_CODE;
5814 if (num_text++ == 0)
5815 nname = ".text";
5816 else
5817 sprintf (name, ".text$%u", num_text);
5818 }
5819 else if ((i_phdr->p_flags & (PF_R | PF_W)) == PF_R)
5820 {
5821 flags |= SEC_READONLY;
5822 sprintf (name, ".rodata$%u", num_rodata++);
5823 }
5824 else
5825 {
5826 flags |= SEC_DATA;
5827 sprintf (name, ".data$%u", num_data++);
5828 }
5829
5830 /* Allocate name. */
5831 if (nname == NULL)
5832 {
5833 size_t name_len = strlen (name) + 1;
5834 nname = bfd_alloc (abfd, name_len);
5835 if (nname == NULL)
5836 return FALSE;
5837 memcpy (nname, name, name_len);
5838 }
5839
5840 /* Create and fill new section. */
5841 nsec = bfd_make_section_anyway_with_flags (abfd, nname, flags);
5842 if (nsec == NULL)
5843 return FALSE;
5844 nsec->vma = base_vma;
5845 nsec->size = next_vma - base_vma;
5846 nsec->filepos = i_phdr->p_offset + (base_vma - i_phdr->p_vaddr);
5847
5848 base_vma = next_vma;
5849 }
5850 }
5851 return TRUE;
5852}
5853
5854static void
5855elfNN_vms_post_process_headers (bfd *abfd,
5856 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5857{
5858 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5859
5860 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_OPENVMS;
5861 i_ehdrp->e_ident[EI_ABIVERSION] = 2;
5862}
5863
5864static bfd_boolean
5865elfNN_vms_section_processing (bfd *abfd ATTRIBUTE_UNUSED,
5866 Elf_Internal_Shdr *hdr)
5867{
5868 if (hdr->bfd_section != NULL)
5869 {
5870 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5871
5872 if (strcmp (name, ".text") == 0)
5873 hdr->sh_flags |= SHF_IA_64_VMS_SHARED;
5874 else if ((strcmp (name, ".debug") == 0)
5875 || (strcmp (name, ".debug_abbrev") == 0)
5876 || (strcmp (name, ".debug_aranges") == 0)
5877 || (strcmp (name, ".debug_frame") == 0)
5878 || (strcmp (name, ".debug_info") == 0)
5879 || (strcmp (name, ".debug_loc") == 0)
5880 || (strcmp (name, ".debug_macinfo") == 0)
5881 || (strcmp (name, ".debug_pubnames") == 0)
5882 || (strcmp (name, ".debug_pubtypes") == 0))
5883 hdr->sh_type = SHT_IA_64_VMS_DEBUG;
5884 else if ((strcmp (name, ".debug_line") == 0)
5885 || (strcmp (name, ".debug_ranges") == 0))
5886 hdr->sh_type = SHT_IA_64_VMS_TRACE;
5887 else if (strcmp (name, ".debug_str") == 0)
5888 hdr->sh_type = SHT_IA_64_VMS_DEBUG_STR;
5889 else if (strcmp (name, ".vms_display_name_info") == 0)
5890 {
5891 int idx, symcount;
5892 asymbol **syms;
5893 struct elf_obj_tdata *t = elf_tdata (abfd);
5894 int buf[2];
5895 int demangler_sym_idx = -1;
5896
5897 symcount = bfd_get_symcount (abfd);
5898 syms = bfd_get_outsymbols (abfd);
5899 for (idx = 0; idx < symcount; idx++)
5900 {
5901 asymbol *sym;
5902 sym = syms[idx];
5903 if ((sym->flags & (BSF_DEBUGGING | BSF_DYNAMIC))
5904 && strchr (sym->name, '@')
5905 && (strcmp (sym->section->name, BFD_ABS_SECTION_NAME) == 0))
5906 {
5907 demangler_sym_idx = sym->udata.i;
5908 break;
5909 }
5910 }
5911
5912 hdr->sh_type = SHT_IA_64_VMS_DISPLAY_NAME_INFO;
5913 hdr->sh_entsize = 4;
5914 hdr->sh_addralign = 0;
5915 hdr->sh_link = t->symtab_section;
5916
5917 /* Find symtab index of demangler routine and stuff it in
5918 the second long word of section data. */
5919
5920 if (demangler_sym_idx > -1)
5921 {
5922 bfd_seek (abfd, hdr->sh_offset, SEEK_SET);
5923 bfd_bread (buf, hdr->sh_size, abfd);
5924 buf [1] = demangler_sym_idx;
5925 bfd_seek (abfd, hdr->sh_offset, SEEK_SET);
5926 bfd_bwrite (buf, hdr->sh_size, abfd);
5927 }
5928 }
5929 }
5930
5931 return TRUE;
5932}
5933
5934/* The final processing done just before writing out a VMS IA-64 ELF
5935 object file. */
5936
5937static void
5938elfNN_vms_final_write_processing (bfd *abfd,
5939 bfd_boolean linker ATTRIBUTE_UNUSED)
5940{
5941 Elf_Internal_Shdr *hdr;
5942 asection *s;
5943 int unwind_info_sect_idx = 0;
5944
5945 for (s = abfd->sections; s; s = s->next)
5946 {
5947 hdr = &elf_section_data (s)->this_hdr;
5948
5949 if (strcmp (bfd_get_section_name (abfd, hdr->bfd_section),
5950 ".IA_64.unwind_info") == 0)
5951 unwind_info_sect_idx = elf_section_data (s)->this_idx;
5952
5953 switch (hdr->sh_type)
5954 {
5955 case SHT_IA_64_UNWIND:
5956 /* VMS requires sh_info to point to the unwind info section. */
5957 hdr->sh_info = unwind_info_sect_idx;
5958 break;
5959 }
5960 }
5961
5962 if (! elf_flags_init (abfd))
5963 {
5964 unsigned long flags = 0;
5965
5966 if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
5967 flags |= EF_IA_64_BE;
5968 if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
5969 flags |= EF_IA_64_ABI64;
5970
5971 elf_elfheader(abfd)->e_flags = flags;
5972 elf_flags_init (abfd) = TRUE;
5973 }
5974}
5975
5976static bfd_boolean
5977elfNN_vms_close_and_cleanup (bfd *abfd)
5978{
5979 if (bfd_get_format (abfd) == bfd_object)
5980 {
5981 long isize, irsize;
5982
5983 if (elf_shstrtab (abfd) != NULL)
5984 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5985
5986 /* Pad to 8 byte boundary for IPF/VMS. */
5987 isize = bfd_get_size (abfd);
5988 if ((irsize = isize/8*8) < isize)
5989 {
5990 int ishort = (irsize + 8) - isize;
5991 bfd_seek (abfd, isize, SEEK_SET);
5992 bfd_bwrite (bfd_zmalloc (ishort), ishort, abfd);
5993 }
5994 }
5995
5996 return _bfd_generic_close_and_cleanup (abfd);
5997}
7256a114 5998#endif /* INCLUDE_IA64_VMS */
800eeca4 5999\f
bbe66d08
JW
6000#define TARGET_LITTLE_SYM bfd_elfNN_ia64_little_vec
6001#define TARGET_LITTLE_NAME "elfNN-ia64-little"
6002#define TARGET_BIG_SYM bfd_elfNN_ia64_big_vec
6003#define TARGET_BIG_NAME "elfNN-ia64-big"
800eeca4
JW
6004#define ELF_ARCH bfd_arch_ia64
6005#define ELF_MACHINE_CODE EM_IA_64
6006#define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
6007#define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
6008#define ELF_MAXPAGESIZE 0x10000 /* 64KB */
24718e3b 6009#define ELF_COMMONPAGESIZE 0x4000 /* 16KB */
800eeca4
JW
6010
6011#define elf_backend_section_from_shdr \
bbe66d08 6012 elfNN_ia64_section_from_shdr
fa152c49 6013#define elf_backend_section_flags \
bbe66d08 6014 elfNN_ia64_section_flags
800eeca4 6015#define elf_backend_fake_sections \
bbe66d08 6016 elfNN_ia64_fake_sections
81545d45
RH
6017#define elf_backend_final_write_processing \
6018 elfNN_ia64_final_write_processing
800eeca4 6019#define elf_backend_add_symbol_hook \
bbe66d08 6020 elfNN_ia64_add_symbol_hook
800eeca4 6021#define elf_backend_additional_program_headers \
bbe66d08 6022 elfNN_ia64_additional_program_headers
800eeca4 6023#define elf_backend_modify_segment_map \
bbe66d08 6024 elfNN_ia64_modify_segment_map
e36284ab
AM
6025#define elf_backend_modify_program_headers \
6026 elfNN_ia64_modify_program_headers
800eeca4 6027#define elf_info_to_howto \
bbe66d08 6028 elfNN_ia64_info_to_howto
800eeca4 6029
bbe66d08
JW
6030#define bfd_elfNN_bfd_reloc_type_lookup \
6031 elfNN_ia64_reloc_type_lookup
157090f7
AM
6032#define bfd_elfNN_bfd_reloc_name_lookup \
6033 elfNN_ia64_reloc_name_lookup
bbe66d08
JW
6034#define bfd_elfNN_bfd_is_local_label_name \
6035 elfNN_ia64_is_local_label_name
6036#define bfd_elfNN_bfd_relax_section \
6037 elfNN_ia64_relax_section
800eeca4 6038
da9f89d4
L
6039#define elf_backend_object_p \
6040 elfNN_ia64_object_p
6041
800eeca4 6042/* Stuff for the BFD linker: */
bbe66d08
JW
6043#define bfd_elfNN_bfd_link_hash_table_create \
6044 elfNN_ia64_hash_table_create
0aa92b58
JJ
6045#define bfd_elfNN_bfd_link_hash_table_free \
6046 elfNN_ia64_hash_table_free
800eeca4 6047#define elf_backend_create_dynamic_sections \
bbe66d08 6048 elfNN_ia64_create_dynamic_sections
800eeca4 6049#define elf_backend_check_relocs \
bbe66d08 6050 elfNN_ia64_check_relocs
800eeca4 6051#define elf_backend_adjust_dynamic_symbol \
bbe66d08 6052 elfNN_ia64_adjust_dynamic_symbol
800eeca4 6053#define elf_backend_size_dynamic_sections \
bbe66d08 6054 elfNN_ia64_size_dynamic_sections
74541ad4
AM
6055#define elf_backend_omit_section_dynsym \
6056 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
800eeca4 6057#define elf_backend_relocate_section \
bbe66d08 6058 elfNN_ia64_relocate_section
800eeca4 6059#define elf_backend_finish_dynamic_symbol \
bbe66d08 6060 elfNN_ia64_finish_dynamic_symbol
800eeca4 6061#define elf_backend_finish_dynamic_sections \
bbe66d08
JW
6062 elfNN_ia64_finish_dynamic_sections
6063#define bfd_elfNN_bfd_final_link \
6064 elfNN_ia64_final_link
6065
bbe66d08
JW
6066#define bfd_elfNN_bfd_merge_private_bfd_data \
6067 elfNN_ia64_merge_private_bfd_data
6068#define bfd_elfNN_bfd_set_private_flags \
6069 elfNN_ia64_set_private_flags
6070#define bfd_elfNN_bfd_print_private_bfd_data \
6071 elfNN_ia64_print_private_bfd_data
800eeca4
JW
6072
6073#define elf_backend_plt_readonly 1
6074#define elf_backend_want_plt_sym 0
6075#define elf_backend_plt_alignment 5
6076#define elf_backend_got_header_size 0
800eeca4
JW
6077#define elf_backend_want_got_plt 1
6078#define elf_backend_may_use_rel_p 1
6079#define elf_backend_may_use_rela_p 1
6080#define elf_backend_default_use_rela_p 1
6081#define elf_backend_want_dynbss 0
bbe66d08
JW
6082#define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
6083#define elf_backend_hide_symbol elfNN_ia64_hash_hide_symbol
508c3946 6084#define elf_backend_fixup_symbol _bfd_elf_link_hash_fixup_symbol
db6751f2 6085#define elf_backend_reloc_type_class elfNN_ia64_reloc_type_class
b491616a 6086#define elf_backend_rela_normal 1
29ef7005 6087#define elf_backend_special_sections elfNN_ia64_special_sections
d4d2b80b 6088#define elf_backend_default_execstack 0
800eeca4 6089
185d09ad 6090/* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
e04bcc6d 6091 SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
185d09ad
L
6092 We don't want to flood users with so many error messages. We turn
6093 off the warning for now. It will be turned on later when the Intel
6094 compiler is fixed. */
6095#define elf_backend_link_order_error_handler NULL
6096
bbe66d08 6097#include "elfNN-target.h"
7b6dab7f 6098
fcf12726
AM
6099/* HPUX-specific vectors. */
6100
6101#undef TARGET_LITTLE_SYM
6102#undef TARGET_LITTLE_NAME
6103#undef TARGET_BIG_SYM
6104#define TARGET_BIG_SYM bfd_elfNN_ia64_hpux_big_vec
6105#undef TARGET_BIG_NAME
6106#define TARGET_BIG_NAME "elfNN-ia64-hpux-big"
6107
254ed743
NC
6108/* These are HP-UX specific functions. */
6109
fcf12726
AM
6110#undef elf_backend_post_process_headers
6111#define elf_backend_post_process_headers elfNN_hpux_post_process_headers
6112
d9cf1b54
AM
6113#undef elf_backend_section_from_bfd_section
6114#define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
6115
b59dd4a5
L
6116#undef elf_backend_symbol_processing
6117#define elf_backend_symbol_processing elfNN_hpux_backend_symbol_processing
6118
5e8d7549
NC
6119#undef elf_backend_want_p_paddr_set_to_zero
6120#define elf_backend_want_p_paddr_set_to_zero 1
6121
24718e3b 6122#undef ELF_COMMONPAGESIZE
d1036acb
L
6123#undef ELF_OSABI
6124#define ELF_OSABI ELFOSABI_HPUX
fcf12726
AM
6125
6126#undef elfNN_bed
6127#define elfNN_bed elfNN_ia64_hpux_bed
6128
6129#include "elfNN-target.h"
5e8d7549 6130
01e1a5bc 6131/* VMS-specific vectors. */
7256a114 6132#ifdef INCLUDE_IA64_VMS
01e1a5bc
NC
6133
6134#undef TARGET_LITTLE_SYM
6135#define TARGET_LITTLE_SYM bfd_elfNN_ia64_vms_vec
6136#undef TARGET_LITTLE_NAME
6137#define TARGET_LITTLE_NAME "elfNN-ia64-vms"
6138#undef TARGET_BIG_SYM
6139#undef TARGET_BIG_NAME
6140
6141/* These are VMS specific functions. */
6142
6143#undef elf_backend_object_p
6144#define elf_backend_object_p elfNN_vms_object_p
6145
6146#undef elf_backend_section_from_shdr
6147#define elf_backend_section_from_shdr elfNN_vms_section_from_shdr
6148
6149#undef elf_backend_post_process_headers
6150#define elf_backend_post_process_headers elfNN_vms_post_process_headers
6151
6152#undef elf_backend_section_processing
6153#define elf_backend_section_processing elfNN_vms_section_processing
6154
6155#undef elf_backend_final_write_processing
6156#define elf_backend_final_write_processing elfNN_vms_final_write_processing
6157
6158#undef bfd_elfNN_close_and_cleanup
6159#define bfd_elfNN_close_and_cleanup elfNN_vms_close_and_cleanup
6160
6161#undef elf_backend_section_from_bfd_section
6162
6163#undef elf_backend_symbol_processing
6164
5e8d7549 6165#undef elf_backend_want_p_paddr_set_to_zero
01e1a5bc 6166
11d73275
TG
6167#undef ELF_OSABI
6168#define ELF_OSABI ELFOSABI_OPENVMS
6169
01e1a5bc
NC
6170#undef ELF_MAXPAGESIZE
6171#define ELF_MAXPAGESIZE 0x10000 /* 64KB */
6172
6173#undef elfNN_bed
6174#define elfNN_bed elfNN_ia64_vms_bed
6175
7256a114
TG
6176/* Use VMS-style archives (in particular, don't use the standard coff
6177 archive format). */
6178#define bfd_elfNN_archive_functions
6179
6180#undef bfd_elfNN_archive_p
6181#define bfd_elfNN_archive_p _bfd_vms_lib_ia64_archive_p
6182
6183#define bfd_elfNN_archive_slurp_armap \
6184 _bfd_vms_lib_slurp_armap
6185#define bfd_elfNN_archive_slurp_extended_name_table \
6186 _bfd_vms_lib_slurp_extended_name_table
6187#define bfd_elfNN_archive_construct_extended_name_table \
6188 _bfd_vms_lib_construct_extended_name_table
6189#define bfd_elfNN_archive_truncate_arname \
6190 _bfd_vms_lib_truncate_arname
6191#define bfd_elfNN_archive_write_armap \
6192 _bfd_vms_lib_write_armap
6193#define bfd_elfNN_archive_read_ar_hdr \
6194 _bfd_vms_lib_read_ar_hdr
6195#define bfd_elfNN_archive_write_ar_hdr \
6196 _bfd_vms_lib_write_ar_hdr
6197#define bfd_elfNN_archive_openr_next_archived_file \
6198 _bfd_vms_lib_openr_next_archived_file
6199#define bfd_elfNN_archive_get_elt_at_index \
6200 _bfd_vms_lib_get_elt_at_index
6201#define bfd_elfNN_archive_generic_stat_arch_elt \
6202 _bfd_vms_lib_generic_stat_arch_elt
6203#define bfd_elfNN_archive_update_armap_timestamp \
6204 _bfd_vms_lib_update_armap_timestamp
6205
01e1a5bc 6206#include "elfNN-target.h"
7256a114
TG
6207
6208#endif /* INCLUDE_IA64_VMS */
This page took 1.128154 seconds and 4 git commands to generate.