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