* elf32-bfin.c (bfd_const_reloc, bfd_oper_reloc, bfin_push_reloc,
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
64543e1a 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
66eb6687 3 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
b49e97c9
TS
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
ae9a127f 12 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 13
ae9a127f
NC
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
b49e97c9 18
ae9a127f
NC
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
b49e97c9 23
ae9a127f
NC
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
3e110533 26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
b49e97c9
TS
27
28/* This file handles functionality common to the different MIPS ABI's. */
29
30#include "bfd.h"
31#include "sysdep.h"
32#include "libbfd.h"
64543e1a 33#include "libiberty.h"
b49e97c9
TS
34#include "elf-bfd.h"
35#include "elfxx-mips.h"
36#include "elf/mips.h"
0a44bf69 37#include "elf-vxworks.h"
b49e97c9
TS
38
39/* Get the ECOFF swapping routines. */
40#include "coff/sym.h"
41#include "coff/symconst.h"
42#include "coff/ecoff.h"
43#include "coff/mips.h"
44
b15e6682
AO
45#include "hashtab.h"
46
47/* This structure is used to hold .got entries while estimating got
48 sizes. */
49struct mips_got_entry
50{
51 /* The input bfd in which the symbol is defined. */
52 bfd *abfd;
f4416af6
AO
53 /* The index of the symbol, as stored in the relocation r_info, if
54 we have a local symbol; -1 otherwise. */
55 long symndx;
56 union
57 {
58 /* If abfd == NULL, an address that must be stored in the got. */
59 bfd_vma address;
60 /* If abfd != NULL && symndx != -1, the addend of the relocation
61 that should be added to the symbol value. */
62 bfd_vma addend;
63 /* If abfd != NULL && symndx == -1, the hash table entry
64 corresponding to a global symbol in the got (or, local, if
65 h->forced_local). */
66 struct mips_elf_link_hash_entry *h;
67 } d;
0f20cc35
DJ
68
69 /* The TLS types included in this GOT entry (specifically, GD and
70 IE). The GD and IE flags can be added as we encounter new
71 relocations. LDM can also be set; it will always be alone, not
72 combined with any GD or IE flags. An LDM GOT entry will be
73 a local symbol entry with r_symndx == 0. */
74 unsigned char tls_type;
75
b15e6682 76 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
77 corresponding to this symbol+addend. If it's a global symbol
78 whose offset is yet to be decided, it's going to be -1. */
79 long gotidx;
b15e6682
AO
80};
81
f0abc2a1 82/* This structure is used to hold .got information when linking. */
b49e97c9
TS
83
84struct mips_got_info
85{
86 /* The global symbol in the GOT with the lowest index in the dynamic
87 symbol table. */
88 struct elf_link_hash_entry *global_gotsym;
89 /* The number of global .got entries. */
90 unsigned int global_gotno;
0f20cc35
DJ
91 /* The number of .got slots used for TLS. */
92 unsigned int tls_gotno;
93 /* The first unused TLS .got entry. Used only during
94 mips_elf_initialize_tls_index. */
95 unsigned int tls_assigned_gotno;
b49e97c9
TS
96 /* The number of local .got entries. */
97 unsigned int local_gotno;
98 /* The number of local .got entries we have used. */
99 unsigned int assigned_gotno;
b15e6682
AO
100 /* A hash table holding members of the got. */
101 struct htab *got_entries;
f4416af6
AO
102 /* A hash table mapping input bfds to other mips_got_info. NULL
103 unless multi-got was necessary. */
104 struct htab *bfd2got;
105 /* In multi-got links, a pointer to the next got (err, rather, most
106 of the time, it points to the previous got). */
107 struct mips_got_info *next;
0f20cc35
DJ
108 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
109 for none, or MINUS_TWO for not yet assigned. This is needed
110 because a single-GOT link may have multiple hash table entries
111 for the LDM. It does not get initialized in multi-GOT mode. */
112 bfd_vma tls_ldm_offset;
f4416af6
AO
113};
114
115/* Map an input bfd to a got in a multi-got link. */
116
117struct mips_elf_bfd2got_hash {
118 bfd *bfd;
119 struct mips_got_info *g;
120};
121
122/* Structure passed when traversing the bfd2got hash table, used to
123 create and merge bfd's gots. */
124
125struct mips_elf_got_per_bfd_arg
126{
127 /* A hashtable that maps bfds to gots. */
128 htab_t bfd2got;
129 /* The output bfd. */
130 bfd *obfd;
131 /* The link information. */
132 struct bfd_link_info *info;
133 /* A pointer to the primary got, i.e., the one that's going to get
134 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
135 DT_MIPS_GOTSYM. */
136 struct mips_got_info *primary;
137 /* A non-primary got we're trying to merge with other input bfd's
138 gots. */
139 struct mips_got_info *current;
140 /* The maximum number of got entries that can be addressed with a
141 16-bit offset. */
142 unsigned int max_count;
143 /* The number of local and global entries in the primary got. */
144 unsigned int primary_count;
145 /* The number of local and global entries in the current got. */
146 unsigned int current_count;
0f20cc35
DJ
147 /* The total number of global entries which will live in the
148 primary got and be automatically relocated. This includes
149 those not referenced by the primary GOT but included in
150 the "master" GOT. */
151 unsigned int global_count;
f4416af6
AO
152};
153
154/* Another structure used to pass arguments for got entries traversal. */
155
156struct mips_elf_set_global_got_offset_arg
157{
158 struct mips_got_info *g;
159 int value;
160 unsigned int needed_relocs;
161 struct bfd_link_info *info;
b49e97c9
TS
162};
163
0f20cc35
DJ
164/* A structure used to count TLS relocations or GOT entries, for GOT
165 entry or ELF symbol table traversal. */
166
167struct mips_elf_count_tls_arg
168{
169 struct bfd_link_info *info;
170 unsigned int needed;
171};
172
f0abc2a1
AM
173struct _mips_elf_section_data
174{
175 struct bfd_elf_section_data elf;
176 union
177 {
178 struct mips_got_info *got_info;
179 bfd_byte *tdata;
180 } u;
181};
182
183#define mips_elf_section_data(sec) \
68bfbfcc 184 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 185
b49e97c9
TS
186/* This structure is passed to mips_elf_sort_hash_table_f when sorting
187 the dynamic symbols. */
188
189struct mips_elf_hash_sort_data
190{
191 /* The symbol in the global GOT with the lowest dynamic symbol table
192 index. */
193 struct elf_link_hash_entry *low;
0f20cc35
DJ
194 /* The least dynamic symbol table index corresponding to a non-TLS
195 symbol with a GOT entry. */
b49e97c9 196 long min_got_dynindx;
f4416af6
AO
197 /* The greatest dynamic symbol table index corresponding to a symbol
198 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 199 with dynamic relocations pointing to it from non-primary GOTs). */
f4416af6 200 long max_unref_got_dynindx;
b49e97c9
TS
201 /* The greatest dynamic symbol table index not corresponding to a
202 symbol without a GOT entry. */
203 long max_non_got_dynindx;
204};
205
206/* The MIPS ELF linker needs additional information for each symbol in
207 the global hash table. */
208
209struct mips_elf_link_hash_entry
210{
211 struct elf_link_hash_entry root;
212
213 /* External symbol information. */
214 EXTR esym;
215
216 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
217 this symbol. */
218 unsigned int possibly_dynamic_relocs;
219
220 /* If the R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 reloc is against
221 a readonly section. */
b34976b6 222 bfd_boolean readonly_reloc;
b49e97c9 223
b49e97c9
TS
224 /* We must not create a stub for a symbol that has relocations
225 related to taking the function's address, i.e. any but
226 R_MIPS_CALL*16 ones -- see "MIPS ABI Supplement, 3rd Edition",
227 p. 4-20. */
b34976b6 228 bfd_boolean no_fn_stub;
b49e97c9
TS
229
230 /* If there is a stub that 32 bit functions should use to call this
231 16 bit function, this points to the section containing the stub. */
232 asection *fn_stub;
233
234 /* Whether we need the fn_stub; this is set if this symbol appears
235 in any relocs other than a 16 bit call. */
b34976b6 236 bfd_boolean need_fn_stub;
b49e97c9
TS
237
238 /* If there is a stub that 16 bit functions should use to call this
239 32 bit function, this points to the section containing the stub. */
240 asection *call_stub;
241
242 /* This is like the call_stub field, but it is used if the function
243 being called returns a floating point value. */
244 asection *call_fp_stub;
7c5fcef7 245
a008ac03
DJ
246 /* Are we forced local? This will only be set if we have converted
247 the initial global GOT entry to a local GOT entry. */
b34976b6 248 bfd_boolean forced_local;
0f20cc35 249
0a44bf69
RS
250 /* Are we referenced by some kind of relocation? */
251 bfd_boolean is_relocation_target;
252
253 /* Are we referenced by branch relocations? */
254 bfd_boolean is_branch_target;
255
0f20cc35
DJ
256#define GOT_NORMAL 0
257#define GOT_TLS_GD 1
258#define GOT_TLS_LDM 2
259#define GOT_TLS_IE 4
260#define GOT_TLS_OFFSET_DONE 0x40
261#define GOT_TLS_DONE 0x80
262 unsigned char tls_type;
263 /* This is only used in single-GOT mode; in multi-GOT mode there
264 is one mips_got_entry per GOT entry, so the offset is stored
265 there. In single-GOT mode there may be many mips_got_entry
266 structures all referring to the same GOT slot. It might be
267 possible to use root.got.offset instead, but that field is
268 overloaded already. */
269 bfd_vma tls_got_offset;
b49e97c9
TS
270};
271
272/* MIPS ELF linker hash table. */
273
274struct mips_elf_link_hash_table
275{
276 struct elf_link_hash_table root;
277#if 0
278 /* We no longer use this. */
279 /* String section indices for the dynamic section symbols. */
280 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
281#endif
282 /* The number of .rtproc entries. */
283 bfd_size_type procedure_count;
284 /* The size of the .compact_rel section (if SGI_COMPAT). */
285 bfd_size_type compact_rel_size;
286 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
8dc1a139 287 entry is set to the address of __rld_obj_head as in IRIX5. */
b34976b6 288 bfd_boolean use_rld_obj_head;
b49e97c9
TS
289 /* This is the value of the __rld_map or __rld_obj_head symbol. */
290 bfd_vma rld_value;
291 /* This is set if we see any mips16 stub sections. */
b34976b6 292 bfd_boolean mips16_stubs_seen;
0a44bf69
RS
293 /* True if we're generating code for VxWorks. */
294 bfd_boolean is_vxworks;
295 /* Shortcuts to some dynamic sections, or NULL if they are not
296 being used. */
297 asection *srelbss;
298 asection *sdynbss;
299 asection *srelplt;
300 asection *srelplt2;
301 asection *sgotplt;
302 asection *splt;
303 /* The size of the PLT header in bytes (VxWorks only). */
304 bfd_vma plt_header_size;
305 /* The size of a PLT entry in bytes (VxWorks only). */
306 bfd_vma plt_entry_size;
b49e97c9
TS
307};
308
0f20cc35
DJ
309#define TLS_RELOC_P(r_type) \
310 (r_type == R_MIPS_TLS_DTPMOD32 \
311 || r_type == R_MIPS_TLS_DTPMOD64 \
312 || r_type == R_MIPS_TLS_DTPREL32 \
313 || r_type == R_MIPS_TLS_DTPREL64 \
314 || r_type == R_MIPS_TLS_GD \
315 || r_type == R_MIPS_TLS_LDM \
316 || r_type == R_MIPS_TLS_DTPREL_HI16 \
317 || r_type == R_MIPS_TLS_DTPREL_LO16 \
318 || r_type == R_MIPS_TLS_GOTTPREL \
319 || r_type == R_MIPS_TLS_TPREL32 \
320 || r_type == R_MIPS_TLS_TPREL64 \
321 || r_type == R_MIPS_TLS_TPREL_HI16 \
322 || r_type == R_MIPS_TLS_TPREL_LO16)
323
b49e97c9
TS
324/* Structure used to pass information to mips_elf_output_extsym. */
325
326struct extsym_info
327{
9e4aeb93
RS
328 bfd *abfd;
329 struct bfd_link_info *info;
b49e97c9
TS
330 struct ecoff_debug_info *debug;
331 const struct ecoff_debug_swap *swap;
b34976b6 332 bfd_boolean failed;
b49e97c9
TS
333};
334
8dc1a139 335/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
336
337static const char * const mips_elf_dynsym_rtproc_names[] =
338{
339 "_procedure_table",
340 "_procedure_string_table",
341 "_procedure_table_size",
342 NULL
343};
344
345/* These structures are used to generate the .compact_rel section on
8dc1a139 346 IRIX5. */
b49e97c9
TS
347
348typedef struct
349{
350 unsigned long id1; /* Always one? */
351 unsigned long num; /* Number of compact relocation entries. */
352 unsigned long id2; /* Always two? */
353 unsigned long offset; /* The file offset of the first relocation. */
354 unsigned long reserved0; /* Zero? */
355 unsigned long reserved1; /* Zero? */
356} Elf32_compact_rel;
357
358typedef struct
359{
360 bfd_byte id1[4];
361 bfd_byte num[4];
362 bfd_byte id2[4];
363 bfd_byte offset[4];
364 bfd_byte reserved0[4];
365 bfd_byte reserved1[4];
366} Elf32_External_compact_rel;
367
368typedef struct
369{
370 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
371 unsigned int rtype : 4; /* Relocation types. See below. */
372 unsigned int dist2to : 8;
373 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
374 unsigned long konst; /* KONST field. See below. */
375 unsigned long vaddr; /* VADDR to be relocated. */
376} Elf32_crinfo;
377
378typedef struct
379{
380 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
381 unsigned int rtype : 4; /* Relocation types. See below. */
382 unsigned int dist2to : 8;
383 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
384 unsigned long konst; /* KONST field. See below. */
385} Elf32_crinfo2;
386
387typedef struct
388{
389 bfd_byte info[4];
390 bfd_byte konst[4];
391 bfd_byte vaddr[4];
392} Elf32_External_crinfo;
393
394typedef struct
395{
396 bfd_byte info[4];
397 bfd_byte konst[4];
398} Elf32_External_crinfo2;
399
400/* These are the constants used to swap the bitfields in a crinfo. */
401
402#define CRINFO_CTYPE (0x1)
403#define CRINFO_CTYPE_SH (31)
404#define CRINFO_RTYPE (0xf)
405#define CRINFO_RTYPE_SH (27)
406#define CRINFO_DIST2TO (0xff)
407#define CRINFO_DIST2TO_SH (19)
408#define CRINFO_RELVADDR (0x7ffff)
409#define CRINFO_RELVADDR_SH (0)
410
411/* A compact relocation info has long (3 words) or short (2 words)
412 formats. A short format doesn't have VADDR field and relvaddr
413 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
414#define CRF_MIPS_LONG 1
415#define CRF_MIPS_SHORT 0
416
417/* There are 4 types of compact relocation at least. The value KONST
418 has different meaning for each type:
419
420 (type) (konst)
421 CT_MIPS_REL32 Address in data
422 CT_MIPS_WORD Address in word (XXX)
423 CT_MIPS_GPHI_LO GP - vaddr
424 CT_MIPS_JMPAD Address to jump
425 */
426
427#define CRT_MIPS_REL32 0xa
428#define CRT_MIPS_WORD 0xb
429#define CRT_MIPS_GPHI_LO 0xc
430#define CRT_MIPS_JMPAD 0xd
431
432#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
433#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
434#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
435#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
436\f
437/* The structure of the runtime procedure descriptor created by the
438 loader for use by the static exception system. */
439
440typedef struct runtime_pdr {
ae9a127f
NC
441 bfd_vma adr; /* Memory address of start of procedure. */
442 long regmask; /* Save register mask. */
443 long regoffset; /* Save register offset. */
444 long fregmask; /* Save floating point register mask. */
445 long fregoffset; /* Save floating point register offset. */
446 long frameoffset; /* Frame size. */
447 short framereg; /* Frame pointer register. */
448 short pcreg; /* Offset or reg of return pc. */
449 long irpss; /* Index into the runtime string table. */
b49e97c9 450 long reserved;
ae9a127f 451 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
452} RPDR, *pRPDR;
453#define cbRPDR sizeof (RPDR)
454#define rpdNil ((pRPDR) 0)
455\f
b15e6682 456static struct mips_got_entry *mips_elf_create_local_got_entry
0a44bf69
RS
457 (bfd *, struct bfd_link_info *, bfd *, struct mips_got_info *, asection *,
458 asection *, bfd_vma, unsigned long, struct mips_elf_link_hash_entry *, int);
b34976b6 459static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 460 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
461static bfd_vma mips_elf_high
462 (bfd_vma);
b34976b6 463static bfd_boolean mips_elf_stub_section_p
9719ad41 464 (bfd *, asection *);
b34976b6 465static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
466 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
467 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
468 bfd_vma *, asection *);
9719ad41
RS
469static hashval_t mips_elf_got_entry_hash
470 (const void *);
f4416af6 471static bfd_vma mips_elf_adjust_gp
9719ad41 472 (bfd *, struct mips_got_info *, bfd *);
f4416af6 473static struct mips_got_info *mips_elf_got_for_ibfd
9719ad41 474 (struct mips_got_info *, bfd *);
f4416af6 475
b49e97c9
TS
476/* This will be used when we sort the dynamic relocation records. */
477static bfd *reldyn_sorting_bfd;
478
479/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
480#define ABI_N32_P(abfd) \
481 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
482
4a14403c 483/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 484#define ABI_64_P(abfd) \
141ff970 485 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 486
4a14403c
TS
487/* Nonzero if ABFD is using NewABI conventions. */
488#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
489
490/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
491#define IRIX_COMPAT(abfd) \
492 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
493
b49e97c9
TS
494/* Whether we are trying to be compatible with IRIX at all. */
495#define SGI_COMPAT(abfd) \
496 (IRIX_COMPAT (abfd) != ict_none)
497
498/* The name of the options section. */
499#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 500 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 501
cc2e31b9
RS
502/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
503 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
504#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
505 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
506
b49e97c9 507/* The name of the stub section. */
ca07892d 508#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
509
510/* The size of an external REL relocation. */
511#define MIPS_ELF_REL_SIZE(abfd) \
512 (get_elf_backend_data (abfd)->s->sizeof_rel)
513
0a44bf69
RS
514/* The size of an external RELA relocation. */
515#define MIPS_ELF_RELA_SIZE(abfd) \
516 (get_elf_backend_data (abfd)->s->sizeof_rela)
517
b49e97c9
TS
518/* The size of an external dynamic table entry. */
519#define MIPS_ELF_DYN_SIZE(abfd) \
520 (get_elf_backend_data (abfd)->s->sizeof_dyn)
521
522/* The size of a GOT entry. */
523#define MIPS_ELF_GOT_SIZE(abfd) \
524 (get_elf_backend_data (abfd)->s->arch_size / 8)
525
526/* The size of a symbol-table entry. */
527#define MIPS_ELF_SYM_SIZE(abfd) \
528 (get_elf_backend_data (abfd)->s->sizeof_sym)
529
530/* The default alignment for sections, as a power of two. */
531#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 532 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
533
534/* Get word-sized data. */
535#define MIPS_ELF_GET_WORD(abfd, ptr) \
536 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
537
538/* Put out word-sized data. */
539#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
540 (ABI_64_P (abfd) \
541 ? bfd_put_64 (abfd, val, ptr) \
542 : bfd_put_32 (abfd, val, ptr))
543
544/* Add a dynamic symbol table-entry. */
9719ad41 545#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 546 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
547
548#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
549 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
550
4ffba85c
AO
551/* Determine whether the internal relocation of index REL_IDX is REL
552 (zero) or RELA (non-zero). The assumption is that, if there are
553 two relocation sections for this section, one of them is REL and
554 the other is RELA. If the index of the relocation we're testing is
555 in range for the first relocation section, check that the external
556 relocation size is that for RELA. It is also assumed that, if
557 rel_idx is not in range for the first section, and this first
558 section contains REL relocs, then the relocation is in the second
559 section, that is RELA. */
560#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx) \
561 ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr) \
562 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel \
563 > (bfd_vma)(rel_idx)) \
564 == (elf_section_data (sec)->rel_hdr.sh_entsize \
565 == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela) \
566 : sizeof (Elf32_External_Rela))))
567
0a44bf69
RS
568/* The name of the dynamic relocation section. */
569#define MIPS_ELF_REL_DYN_NAME(INFO) \
570 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
571
b49e97c9
TS
572/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
573 from smaller values. Start with zero, widen, *then* decrement. */
574#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 575#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9
TS
576
577/* The number of local .got entries we reserve. */
0a44bf69
RS
578#define MIPS_RESERVED_GOTNO(INFO) \
579 (mips_elf_hash_table (INFO)->is_vxworks ? 3 : 2)
b49e97c9 580
f4416af6 581/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
582#define ELF_MIPS_GP_OFFSET(INFO) \
583 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
584
585/* The maximum size of the GOT for it to be addressable using 16-bit
586 offsets from $gp. */
0a44bf69 587#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 588
6a691779 589/* Instructions which appear in a stub. */
b49e97c9 590#define STUB_LW(abfd) \
f4416af6
AO
591 ((ABI_64_P (abfd) \
592 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
593 : 0x8f998010)) /* lw t9,0x8010(gp) */
b49e97c9 594#define STUB_MOVE(abfd) \
6a691779
TS
595 ((ABI_64_P (abfd) \
596 ? 0x03e0782d /* daddu t7,ra */ \
597 : 0x03e07821)) /* addu t7,ra */
598#define STUB_JALR 0x0320f809 /* jalr t9,ra */
b49e97c9 599#define STUB_LI16(abfd) \
6a691779
TS
600 ((ABI_64_P (abfd) \
601 ? 0x64180000 /* daddiu t8,zero,0 */ \
602 : 0x24180000)) /* addiu t8,zero,0 */
b49e97c9
TS
603#define MIPS_FUNCTION_STUB_SIZE (16)
604
605/* The name of the dynamic interpreter. This is put in the .interp
606 section. */
607
608#define ELF_DYNAMIC_INTERPRETER(abfd) \
609 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
610 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
611 : "/usr/lib/libc.so.1")
612
613#ifdef BFD64
ee6423ed
AO
614#define MNAME(bfd,pre,pos) \
615 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
616#define ELF_R_SYM(bfd, i) \
617 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
618#define ELF_R_TYPE(bfd, i) \
619 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
620#define ELF_R_INFO(bfd, s, t) \
621 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
622#else
ee6423ed 623#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
624#define ELF_R_SYM(bfd, i) \
625 (ELF32_R_SYM (i))
626#define ELF_R_TYPE(bfd, i) \
627 (ELF32_R_TYPE (i))
628#define ELF_R_INFO(bfd, s, t) \
629 (ELF32_R_INFO (s, t))
630#endif
631\f
632 /* The mips16 compiler uses a couple of special sections to handle
633 floating point arguments.
634
635 Section names that look like .mips16.fn.FNNAME contain stubs that
636 copy floating point arguments from the fp regs to the gp regs and
637 then jump to FNNAME. If any 32 bit function calls FNNAME, the
638 call should be redirected to the stub instead. If no 32 bit
639 function calls FNNAME, the stub should be discarded. We need to
640 consider any reference to the function, not just a call, because
641 if the address of the function is taken we will need the stub,
642 since the address might be passed to a 32 bit function.
643
644 Section names that look like .mips16.call.FNNAME contain stubs
645 that copy floating point arguments from the gp regs to the fp
646 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
647 then any 16 bit function that calls FNNAME should be redirected
648 to the stub instead. If FNNAME is not a 32 bit function, the
649 stub should be discarded.
650
651 .mips16.call.fp.FNNAME sections are similar, but contain stubs
652 which call FNNAME and then copy the return value from the fp regs
653 to the gp regs. These stubs store the return value in $18 while
654 calling FNNAME; any function which might call one of these stubs
655 must arrange to save $18 around the call. (This case is not
656 needed for 32 bit functions that call 16 bit functions, because
657 16 bit functions always return floating point values in both
658 $f0/$f1 and $2/$3.)
659
660 Note that in all cases FNNAME might be defined statically.
661 Therefore, FNNAME is not used literally. Instead, the relocation
662 information will indicate which symbol the section is for.
663
664 We record any stubs that we find in the symbol table. */
665
666#define FN_STUB ".mips16.fn."
667#define CALL_STUB ".mips16.call."
668#define CALL_FP_STUB ".mips16.call.fp."
669\f
0a44bf69
RS
670/* The format of the first PLT entry in a VxWorks executable. */
671static const bfd_vma mips_vxworks_exec_plt0_entry[] = {
672 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
673 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
674 0x8f390008, /* lw t9, 8(t9) */
675 0x00000000, /* nop */
676 0x03200008, /* jr t9 */
677 0x00000000 /* nop */
678};
679
680/* The format of subsequent PLT entries. */
681static const bfd_vma mips_vxworks_exec_plt_entry[] = {
682 0x10000000, /* b .PLT_resolver */
683 0x24180000, /* li t8, <pltindex> */
684 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
685 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
686 0x8f390000, /* lw t9, 0(t9) */
687 0x00000000, /* nop */
688 0x03200008, /* jr t9 */
689 0x00000000 /* nop */
690};
691
692/* The format of the first PLT entry in a VxWorks shared object. */
693static const bfd_vma mips_vxworks_shared_plt0_entry[] = {
694 0x8f990008, /* lw t9, 8(gp) */
695 0x00000000, /* nop */
696 0x03200008, /* jr t9 */
697 0x00000000, /* nop */
698 0x00000000, /* nop */
699 0x00000000 /* nop */
700};
701
702/* The format of subsequent PLT entries. */
703static const bfd_vma mips_vxworks_shared_plt_entry[] = {
704 0x10000000, /* b .PLT_resolver */
705 0x24180000 /* li t8, <pltindex> */
706};
707\f
b49e97c9
TS
708/* Look up an entry in a MIPS ELF linker hash table. */
709
710#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
711 ((struct mips_elf_link_hash_entry *) \
712 elf_link_hash_lookup (&(table)->root, (string), (create), \
713 (copy), (follow)))
714
715/* Traverse a MIPS ELF linker hash table. */
716
717#define mips_elf_link_hash_traverse(table, func, info) \
718 (elf_link_hash_traverse \
719 (&(table)->root, \
9719ad41 720 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
721 (info)))
722
723/* Get the MIPS ELF linker hash table from a link_info structure. */
724
725#define mips_elf_hash_table(p) \
726 ((struct mips_elf_link_hash_table *) ((p)->hash))
727
0f20cc35
DJ
728/* Find the base offsets for thread-local storage in this object,
729 for GD/LD and IE/LE respectively. */
730
731#define TP_OFFSET 0x7000
732#define DTP_OFFSET 0x8000
733
734static bfd_vma
735dtprel_base (struct bfd_link_info *info)
736{
737 /* If tls_sec is NULL, we should have signalled an error already. */
738 if (elf_hash_table (info)->tls_sec == NULL)
739 return 0;
740 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
741}
742
743static bfd_vma
744tprel_base (struct bfd_link_info *info)
745{
746 /* If tls_sec is NULL, we should have signalled an error already. */
747 if (elf_hash_table (info)->tls_sec == NULL)
748 return 0;
749 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
750}
751
b49e97c9
TS
752/* Create an entry in a MIPS ELF linker hash table. */
753
754static struct bfd_hash_entry *
9719ad41
RS
755mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
756 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
757{
758 struct mips_elf_link_hash_entry *ret =
759 (struct mips_elf_link_hash_entry *) entry;
760
761 /* Allocate the structure if it has not already been allocated by a
762 subclass. */
9719ad41
RS
763 if (ret == NULL)
764 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
765 if (ret == NULL)
b49e97c9
TS
766 return (struct bfd_hash_entry *) ret;
767
768 /* Call the allocation method of the superclass. */
769 ret = ((struct mips_elf_link_hash_entry *)
770 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
771 table, string));
9719ad41 772 if (ret != NULL)
b49e97c9
TS
773 {
774 /* Set local fields. */
775 memset (&ret->esym, 0, sizeof (EXTR));
776 /* We use -2 as a marker to indicate that the information has
777 not been set. -1 means there is no associated ifd. */
778 ret->esym.ifd = -2;
779 ret->possibly_dynamic_relocs = 0;
b34976b6 780 ret->readonly_reloc = FALSE;
b34976b6 781 ret->no_fn_stub = FALSE;
b49e97c9 782 ret->fn_stub = NULL;
b34976b6 783 ret->need_fn_stub = FALSE;
b49e97c9
TS
784 ret->call_stub = NULL;
785 ret->call_fp_stub = NULL;
b34976b6 786 ret->forced_local = FALSE;
0a44bf69
RS
787 ret->is_branch_target = FALSE;
788 ret->is_relocation_target = FALSE;
0f20cc35 789 ret->tls_type = GOT_NORMAL;
b49e97c9
TS
790 }
791
792 return (struct bfd_hash_entry *) ret;
793}
f0abc2a1
AM
794
795bfd_boolean
9719ad41 796_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1
AM
797{
798 struct _mips_elf_section_data *sdata;
799 bfd_size_type amt = sizeof (*sdata);
800
9719ad41 801 sdata = bfd_zalloc (abfd, amt);
f0abc2a1
AM
802 if (sdata == NULL)
803 return FALSE;
9719ad41 804 sec->used_by_bfd = sdata;
f0abc2a1
AM
805
806 return _bfd_elf_new_section_hook (abfd, sec);
807}
b49e97c9
TS
808\f
809/* Read ECOFF debugging information from a .mdebug section into a
810 ecoff_debug_info structure. */
811
b34976b6 812bfd_boolean
9719ad41
RS
813_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
814 struct ecoff_debug_info *debug)
b49e97c9
TS
815{
816 HDRR *symhdr;
817 const struct ecoff_debug_swap *swap;
9719ad41 818 char *ext_hdr;
b49e97c9
TS
819
820 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
821 memset (debug, 0, sizeof (*debug));
822
9719ad41 823 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
824 if (ext_hdr == NULL && swap->external_hdr_size != 0)
825 goto error_return;
826
9719ad41 827 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 828 swap->external_hdr_size))
b49e97c9
TS
829 goto error_return;
830
831 symhdr = &debug->symbolic_header;
832 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
833
834 /* The symbolic header contains absolute file offsets and sizes to
835 read. */
836#define READ(ptr, offset, count, size, type) \
837 if (symhdr->count == 0) \
838 debug->ptr = NULL; \
839 else \
840 { \
841 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 842 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
843 if (debug->ptr == NULL) \
844 goto error_return; \
9719ad41 845 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
846 || bfd_bread (debug->ptr, amt, abfd) != amt) \
847 goto error_return; \
848 }
849
850 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
851 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
852 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
853 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
854 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
855 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
856 union aux_ext *);
857 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
858 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
859 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
860 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
861 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
862#undef READ
863
864 debug->fdr = NULL;
b49e97c9 865
b34976b6 866 return TRUE;
b49e97c9
TS
867
868 error_return:
869 if (ext_hdr != NULL)
870 free (ext_hdr);
871 if (debug->line != NULL)
872 free (debug->line);
873 if (debug->external_dnr != NULL)
874 free (debug->external_dnr);
875 if (debug->external_pdr != NULL)
876 free (debug->external_pdr);
877 if (debug->external_sym != NULL)
878 free (debug->external_sym);
879 if (debug->external_opt != NULL)
880 free (debug->external_opt);
881 if (debug->external_aux != NULL)
882 free (debug->external_aux);
883 if (debug->ss != NULL)
884 free (debug->ss);
885 if (debug->ssext != NULL)
886 free (debug->ssext);
887 if (debug->external_fdr != NULL)
888 free (debug->external_fdr);
889 if (debug->external_rfd != NULL)
890 free (debug->external_rfd);
891 if (debug->external_ext != NULL)
892 free (debug->external_ext);
b34976b6 893 return FALSE;
b49e97c9
TS
894}
895\f
896/* Swap RPDR (runtime procedure table entry) for output. */
897
898static void
9719ad41 899ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
900{
901 H_PUT_S32 (abfd, in->adr, ex->p_adr);
902 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
903 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
904 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
905 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
906 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
907
908 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
909 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
910
911 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
912}
913
914/* Create a runtime procedure table from the .mdebug section. */
915
b34976b6 916static bfd_boolean
9719ad41
RS
917mips_elf_create_procedure_table (void *handle, bfd *abfd,
918 struct bfd_link_info *info, asection *s,
919 struct ecoff_debug_info *debug)
b49e97c9
TS
920{
921 const struct ecoff_debug_swap *swap;
922 HDRR *hdr = &debug->symbolic_header;
923 RPDR *rpdr, *rp;
924 struct rpdr_ext *erp;
9719ad41 925 void *rtproc;
b49e97c9
TS
926 struct pdr_ext *epdr;
927 struct sym_ext *esym;
928 char *ss, **sv;
929 char *str;
930 bfd_size_type size;
931 bfd_size_type count;
932 unsigned long sindex;
933 unsigned long i;
934 PDR pdr;
935 SYMR sym;
936 const char *no_name_func = _("static procedure (no name)");
937
938 epdr = NULL;
939 rpdr = NULL;
940 esym = NULL;
941 ss = NULL;
942 sv = NULL;
943
944 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
945
946 sindex = strlen (no_name_func) + 1;
947 count = hdr->ipdMax;
948 if (count > 0)
949 {
950 size = swap->external_pdr_size;
951
9719ad41 952 epdr = bfd_malloc (size * count);
b49e97c9
TS
953 if (epdr == NULL)
954 goto error_return;
955
9719ad41 956 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
957 goto error_return;
958
959 size = sizeof (RPDR);
9719ad41 960 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
961 if (rpdr == NULL)
962 goto error_return;
963
964 size = sizeof (char *);
9719ad41 965 sv = bfd_malloc (size * count);
b49e97c9
TS
966 if (sv == NULL)
967 goto error_return;
968
969 count = hdr->isymMax;
970 size = swap->external_sym_size;
9719ad41 971 esym = bfd_malloc (size * count);
b49e97c9
TS
972 if (esym == NULL)
973 goto error_return;
974
9719ad41 975 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
976 goto error_return;
977
978 count = hdr->issMax;
9719ad41 979 ss = bfd_malloc (count);
b49e97c9
TS
980 if (ss == NULL)
981 goto error_return;
f075ee0c 982 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
983 goto error_return;
984
985 count = hdr->ipdMax;
986 for (i = 0; i < (unsigned long) count; i++, rp++)
987 {
9719ad41
RS
988 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
989 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
990 rp->adr = sym.value;
991 rp->regmask = pdr.regmask;
992 rp->regoffset = pdr.regoffset;
993 rp->fregmask = pdr.fregmask;
994 rp->fregoffset = pdr.fregoffset;
995 rp->frameoffset = pdr.frameoffset;
996 rp->framereg = pdr.framereg;
997 rp->pcreg = pdr.pcreg;
998 rp->irpss = sindex;
999 sv[i] = ss + sym.iss;
1000 sindex += strlen (sv[i]) + 1;
1001 }
1002 }
1003
1004 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1005 size = BFD_ALIGN (size, 16);
9719ad41 1006 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1007 if (rtproc == NULL)
1008 {
1009 mips_elf_hash_table (info)->procedure_count = 0;
1010 goto error_return;
1011 }
1012
1013 mips_elf_hash_table (info)->procedure_count = count + 2;
1014
9719ad41 1015 erp = rtproc;
b49e97c9
TS
1016 memset (erp, 0, sizeof (struct rpdr_ext));
1017 erp++;
1018 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1019 strcpy (str, no_name_func);
1020 str += strlen (no_name_func) + 1;
1021 for (i = 0; i < count; i++)
1022 {
1023 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1024 strcpy (str, sv[i]);
1025 str += strlen (sv[i]) + 1;
1026 }
1027 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1028
1029 /* Set the size and contents of .rtproc section. */
eea6121a 1030 s->size = size;
9719ad41 1031 s->contents = rtproc;
b49e97c9
TS
1032
1033 /* Skip this section later on (I don't think this currently
1034 matters, but someday it might). */
8423293d 1035 s->map_head.link_order = NULL;
b49e97c9
TS
1036
1037 if (epdr != NULL)
1038 free (epdr);
1039 if (rpdr != NULL)
1040 free (rpdr);
1041 if (esym != NULL)
1042 free (esym);
1043 if (ss != NULL)
1044 free (ss);
1045 if (sv != NULL)
1046 free (sv);
1047
b34976b6 1048 return TRUE;
b49e97c9
TS
1049
1050 error_return:
1051 if (epdr != NULL)
1052 free (epdr);
1053 if (rpdr != NULL)
1054 free (rpdr);
1055 if (esym != NULL)
1056 free (esym);
1057 if (ss != NULL)
1058 free (ss);
1059 if (sv != NULL)
1060 free (sv);
b34976b6 1061 return FALSE;
b49e97c9
TS
1062}
1063
1064/* Check the mips16 stubs for a particular symbol, and see if we can
1065 discard them. */
1066
b34976b6 1067static bfd_boolean
9719ad41
RS
1068mips_elf_check_mips16_stubs (struct mips_elf_link_hash_entry *h,
1069 void *data ATTRIBUTE_UNUSED)
b49e97c9
TS
1070{
1071 if (h->root.root.type == bfd_link_hash_warning)
1072 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1073
1074 if (h->fn_stub != NULL
1075 && ! h->need_fn_stub)
1076 {
1077 /* We don't need the fn_stub; the only references to this symbol
1078 are 16 bit calls. Clobber the size to 0 to prevent it from
1079 being included in the link. */
eea6121a 1080 h->fn_stub->size = 0;
b49e97c9
TS
1081 h->fn_stub->flags &= ~SEC_RELOC;
1082 h->fn_stub->reloc_count = 0;
1083 h->fn_stub->flags |= SEC_EXCLUDE;
1084 }
1085
1086 if (h->call_stub != NULL
1087 && h->root.other == STO_MIPS16)
1088 {
1089 /* We don't need the call_stub; this is a 16 bit function, so
1090 calls from other 16 bit functions are OK. Clobber the size
1091 to 0 to prevent it from being included in the link. */
eea6121a 1092 h->call_stub->size = 0;
b49e97c9
TS
1093 h->call_stub->flags &= ~SEC_RELOC;
1094 h->call_stub->reloc_count = 0;
1095 h->call_stub->flags |= SEC_EXCLUDE;
1096 }
1097
1098 if (h->call_fp_stub != NULL
1099 && h->root.other == STO_MIPS16)
1100 {
1101 /* We don't need the call_stub; this is a 16 bit function, so
1102 calls from other 16 bit functions are OK. Clobber the size
1103 to 0 to prevent it from being included in the link. */
eea6121a 1104 h->call_fp_stub->size = 0;
b49e97c9
TS
1105 h->call_fp_stub->flags &= ~SEC_RELOC;
1106 h->call_fp_stub->reloc_count = 0;
1107 h->call_fp_stub->flags |= SEC_EXCLUDE;
1108 }
1109
b34976b6 1110 return TRUE;
b49e97c9
TS
1111}
1112\f
d6f16593
MR
1113/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1114 Most mips16 instructions are 16 bits, but these instructions
1115 are 32 bits.
1116
1117 The format of these instructions is:
1118
1119 +--------------+--------------------------------+
1120 | JALX | X| Imm 20:16 | Imm 25:21 |
1121 +--------------+--------------------------------+
1122 | Immediate 15:0 |
1123 +-----------------------------------------------+
1124
1125 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1126 Note that the immediate value in the first word is swapped.
1127
1128 When producing a relocatable object file, R_MIPS16_26 is
1129 handled mostly like R_MIPS_26. In particular, the addend is
1130 stored as a straight 26-bit value in a 32-bit instruction.
1131 (gas makes life simpler for itself by never adjusting a
1132 R_MIPS16_26 reloc to be against a section, so the addend is
1133 always zero). However, the 32 bit instruction is stored as 2
1134 16-bit values, rather than a single 32-bit value. In a
1135 big-endian file, the result is the same; in a little-endian
1136 file, the two 16-bit halves of the 32 bit value are swapped.
1137 This is so that a disassembler can recognize the jal
1138 instruction.
1139
1140 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1141 instruction stored as two 16-bit values. The addend A is the
1142 contents of the targ26 field. The calculation is the same as
1143 R_MIPS_26. When storing the calculated value, reorder the
1144 immediate value as shown above, and don't forget to store the
1145 value as two 16-bit values.
1146
1147 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1148 defined as
1149
1150 big-endian:
1151 +--------+----------------------+
1152 | | |
1153 | | targ26-16 |
1154 |31 26|25 0|
1155 +--------+----------------------+
1156
1157 little-endian:
1158 +----------+------+-------------+
1159 | | | |
1160 | sub1 | | sub2 |
1161 |0 9|10 15|16 31|
1162 +----------+--------------------+
1163 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1164 ((sub1 << 16) | sub2)).
1165
1166 When producing a relocatable object file, the calculation is
1167 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1168 When producing a fully linked file, the calculation is
1169 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1170 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1171
1172 R_MIPS16_GPREL is used for GP-relative addressing in mips16
1173 mode. A typical instruction will have a format like this:
1174
1175 +--------------+--------------------------------+
1176 | EXTEND | Imm 10:5 | Imm 15:11 |
1177 +--------------+--------------------------------+
1178 | Major | rx | ry | Imm 4:0 |
1179 +--------------+--------------------------------+
1180
1181 EXTEND is the five bit value 11110. Major is the instruction
1182 opcode.
1183
1184 This is handled exactly like R_MIPS_GPREL16, except that the
1185 addend is retrieved and stored as shown in this diagram; that
1186 is, the Imm fields above replace the V-rel16 field.
1187
1188 All we need to do here is shuffle the bits appropriately. As
1189 above, the two 16-bit halves must be swapped on a
1190 little-endian system.
1191
1192 R_MIPS16_HI16 and R_MIPS16_LO16 are used in mips16 mode to
1193 access data when neither GP-relative nor PC-relative addressing
1194 can be used. They are handled like R_MIPS_HI16 and R_MIPS_LO16,
1195 except that the addend is retrieved and stored as shown above
1196 for R_MIPS16_GPREL.
1197 */
1198void
1199_bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1200 bfd_boolean jal_shuffle, bfd_byte *data)
1201{
1202 bfd_vma extend, insn, val;
1203
1204 if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1205 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1206 return;
1207
1208 /* Pick up the mips16 extend instruction and the real instruction. */
1209 extend = bfd_get_16 (abfd, data);
1210 insn = bfd_get_16 (abfd, data + 2);
1211 if (r_type == R_MIPS16_26)
1212 {
1213 if (jal_shuffle)
1214 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1215 | ((extend & 0x1f) << 21) | insn;
1216 else
1217 val = extend << 16 | insn;
1218 }
1219 else
1220 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1221 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1222 bfd_put_32 (abfd, val, data);
1223}
1224
1225void
1226_bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1227 bfd_boolean jal_shuffle, bfd_byte *data)
1228{
1229 bfd_vma extend, insn, val;
1230
1231 if (r_type != R_MIPS16_26 && r_type != R_MIPS16_GPREL
1232 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
1233 return;
1234
1235 val = bfd_get_32 (abfd, data);
1236 if (r_type == R_MIPS16_26)
1237 {
1238 if (jal_shuffle)
1239 {
1240 insn = val & 0xffff;
1241 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1242 | ((val >> 21) & 0x1f);
1243 }
1244 else
1245 {
1246 insn = val & 0xffff;
1247 extend = val >> 16;
1248 }
1249 }
1250 else
1251 {
1252 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1253 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1254 }
1255 bfd_put_16 (abfd, insn, data + 2);
1256 bfd_put_16 (abfd, extend, data);
1257}
1258
b49e97c9 1259bfd_reloc_status_type
9719ad41
RS
1260_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1261 arelent *reloc_entry, asection *input_section,
1262 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
1263{
1264 bfd_vma relocation;
a7ebbfdf 1265 bfd_signed_vma val;
30ac9238 1266 bfd_reloc_status_type status;
b49e97c9
TS
1267
1268 if (bfd_is_com_section (symbol->section))
1269 relocation = 0;
1270 else
1271 relocation = symbol->value;
1272
1273 relocation += symbol->section->output_section->vma;
1274 relocation += symbol->section->output_offset;
1275
07515404 1276 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
1277 return bfd_reloc_outofrange;
1278
b49e97c9 1279 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
1280 val = reloc_entry->addend;
1281
30ac9238 1282 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 1283
b49e97c9 1284 /* Adjust val for the final section location and GP value. If we
1049f94e 1285 are producing relocatable output, we don't want to do this for
b49e97c9 1286 an external symbol. */
1049f94e 1287 if (! relocatable
b49e97c9
TS
1288 || (symbol->flags & BSF_SECTION_SYM) != 0)
1289 val += relocation - gp;
1290
a7ebbfdf
TS
1291 if (reloc_entry->howto->partial_inplace)
1292 {
30ac9238
RS
1293 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1294 (bfd_byte *) data
1295 + reloc_entry->address);
1296 if (status != bfd_reloc_ok)
1297 return status;
a7ebbfdf
TS
1298 }
1299 else
1300 reloc_entry->addend = val;
b49e97c9 1301
1049f94e 1302 if (relocatable)
b49e97c9 1303 reloc_entry->address += input_section->output_offset;
30ac9238
RS
1304
1305 return bfd_reloc_ok;
1306}
1307
1308/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
1309 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
1310 that contains the relocation field and DATA points to the start of
1311 INPUT_SECTION. */
1312
1313struct mips_hi16
1314{
1315 struct mips_hi16 *next;
1316 bfd_byte *data;
1317 asection *input_section;
1318 arelent rel;
1319};
1320
1321/* FIXME: This should not be a static variable. */
1322
1323static struct mips_hi16 *mips_hi16_list;
1324
1325/* A howto special_function for REL *HI16 relocations. We can only
1326 calculate the correct value once we've seen the partnering
1327 *LO16 relocation, so just save the information for later.
1328
1329 The ABI requires that the *LO16 immediately follow the *HI16.
1330 However, as a GNU extension, we permit an arbitrary number of
1331 *HI16s to be associated with a single *LO16. This significantly
1332 simplies the relocation handling in gcc. */
1333
1334bfd_reloc_status_type
1335_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1336 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
1337 asection *input_section, bfd *output_bfd,
1338 char **error_message ATTRIBUTE_UNUSED)
1339{
1340 struct mips_hi16 *n;
1341
07515404 1342 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
1343 return bfd_reloc_outofrange;
1344
1345 n = bfd_malloc (sizeof *n);
1346 if (n == NULL)
1347 return bfd_reloc_outofrange;
1348
1349 n->next = mips_hi16_list;
1350 n->data = data;
1351 n->input_section = input_section;
1352 n->rel = *reloc_entry;
1353 mips_hi16_list = n;
1354
1355 if (output_bfd != NULL)
1356 reloc_entry->address += input_section->output_offset;
1357
1358 return bfd_reloc_ok;
1359}
1360
1361/* A howto special_function for REL R_MIPS_GOT16 relocations. This is just
1362 like any other 16-bit relocation when applied to global symbols, but is
1363 treated in the same as R_MIPS_HI16 when applied to local symbols. */
1364
1365bfd_reloc_status_type
1366_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1367 void *data, asection *input_section,
1368 bfd *output_bfd, char **error_message)
1369{
1370 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1371 || bfd_is_und_section (bfd_get_section (symbol))
1372 || bfd_is_com_section (bfd_get_section (symbol)))
1373 /* The relocation is against a global symbol. */
1374 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1375 input_section, output_bfd,
1376 error_message);
1377
1378 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1379 input_section, output_bfd, error_message);
1380}
1381
1382/* A howto special_function for REL *LO16 relocations. The *LO16 itself
1383 is a straightforward 16 bit inplace relocation, but we must deal with
1384 any partnering high-part relocations as well. */
1385
1386bfd_reloc_status_type
1387_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1388 void *data, asection *input_section,
1389 bfd *output_bfd, char **error_message)
1390{
1391 bfd_vma vallo;
d6f16593 1392 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 1393
07515404 1394 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
1395 return bfd_reloc_outofrange;
1396
d6f16593
MR
1397 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1398 location);
1399 vallo = bfd_get_32 (abfd, location);
1400 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1401 location);
1402
30ac9238
RS
1403 while (mips_hi16_list != NULL)
1404 {
1405 bfd_reloc_status_type ret;
1406 struct mips_hi16 *hi;
1407
1408 hi = mips_hi16_list;
1409
1410 /* R_MIPS_GOT16 relocations are something of a special case. We
1411 want to install the addend in the same way as for a R_MIPS_HI16
1412 relocation (with a rightshift of 16). However, since GOT16
1413 relocations can also be used with global symbols, their howto
1414 has a rightshift of 0. */
1415 if (hi->rel.howto->type == R_MIPS_GOT16)
1416 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
1417
1418 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
1419 carry or borrow will induce a change of +1 or -1 in the high part. */
1420 hi->rel.addend += (vallo + 0x8000) & 0xffff;
1421
30ac9238
RS
1422 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
1423 hi->input_section, output_bfd,
1424 error_message);
1425 if (ret != bfd_reloc_ok)
1426 return ret;
1427
1428 mips_hi16_list = hi->next;
1429 free (hi);
1430 }
1431
1432 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1433 input_section, output_bfd,
1434 error_message);
1435}
1436
1437/* A generic howto special_function. This calculates and installs the
1438 relocation itself, thus avoiding the oft-discussed problems in
1439 bfd_perform_relocation and bfd_install_relocation. */
1440
1441bfd_reloc_status_type
1442_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1443 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1444 asection *input_section, bfd *output_bfd,
1445 char **error_message ATTRIBUTE_UNUSED)
1446{
1447 bfd_signed_vma val;
1448 bfd_reloc_status_type status;
1449 bfd_boolean relocatable;
1450
1451 relocatable = (output_bfd != NULL);
1452
07515404 1453 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
1454 return bfd_reloc_outofrange;
1455
1456 /* Build up the field adjustment in VAL. */
1457 val = 0;
1458 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
1459 {
1460 /* Either we're calculating the final field value or we have a
1461 relocation against a section symbol. Add in the section's
1462 offset or address. */
1463 val += symbol->section->output_section->vma;
1464 val += symbol->section->output_offset;
1465 }
1466
1467 if (!relocatable)
1468 {
1469 /* We're calculating the final field value. Add in the symbol's value
1470 and, if pc-relative, subtract the address of the field itself. */
1471 val += symbol->value;
1472 if (reloc_entry->howto->pc_relative)
1473 {
1474 val -= input_section->output_section->vma;
1475 val -= input_section->output_offset;
1476 val -= reloc_entry->address;
1477 }
1478 }
1479
1480 /* VAL is now the final adjustment. If we're keeping this relocation
1481 in the output file, and if the relocation uses a separate addend,
1482 we just need to add VAL to that addend. Otherwise we need to add
1483 VAL to the relocation field itself. */
1484 if (relocatable && !reloc_entry->howto->partial_inplace)
1485 reloc_entry->addend += val;
1486 else
1487 {
d6f16593
MR
1488 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
1489
30ac9238
RS
1490 /* Add in the separate addend, if any. */
1491 val += reloc_entry->addend;
1492
1493 /* Add VAL to the relocation field. */
d6f16593
MR
1494 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
1495 location);
30ac9238 1496 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593
MR
1497 location);
1498 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
1499 location);
1500
30ac9238
RS
1501 if (status != bfd_reloc_ok)
1502 return status;
1503 }
1504
1505 if (relocatable)
1506 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
1507
1508 return bfd_reloc_ok;
1509}
1510\f
1511/* Swap an entry in a .gptab section. Note that these routines rely
1512 on the equivalence of the two elements of the union. */
1513
1514static void
9719ad41
RS
1515bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
1516 Elf32_gptab *in)
b49e97c9
TS
1517{
1518 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
1519 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
1520}
1521
1522static void
9719ad41
RS
1523bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
1524 Elf32_External_gptab *ex)
b49e97c9
TS
1525{
1526 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
1527 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
1528}
1529
1530static void
9719ad41
RS
1531bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
1532 Elf32_External_compact_rel *ex)
b49e97c9
TS
1533{
1534 H_PUT_32 (abfd, in->id1, ex->id1);
1535 H_PUT_32 (abfd, in->num, ex->num);
1536 H_PUT_32 (abfd, in->id2, ex->id2);
1537 H_PUT_32 (abfd, in->offset, ex->offset);
1538 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
1539 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
1540}
1541
1542static void
9719ad41
RS
1543bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
1544 Elf32_External_crinfo *ex)
b49e97c9
TS
1545{
1546 unsigned long l;
1547
1548 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1549 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1550 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1551 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1552 H_PUT_32 (abfd, l, ex->info);
1553 H_PUT_32 (abfd, in->konst, ex->konst);
1554 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
1555}
b49e97c9
TS
1556\f
1557/* A .reginfo section holds a single Elf32_RegInfo structure. These
1558 routines swap this structure in and out. They are used outside of
1559 BFD, so they are globally visible. */
1560
1561void
9719ad41
RS
1562bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
1563 Elf32_RegInfo *in)
b49e97c9
TS
1564{
1565 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1566 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1567 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1568 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1569 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1570 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
1571}
1572
1573void
9719ad41
RS
1574bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
1575 Elf32_External_RegInfo *ex)
b49e97c9
TS
1576{
1577 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1578 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1579 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1580 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1581 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1582 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
1583}
1584
1585/* In the 64 bit ABI, the .MIPS.options section holds register
1586 information in an Elf64_Reginfo structure. These routines swap
1587 them in and out. They are globally visible because they are used
1588 outside of BFD. These routines are here so that gas can call them
1589 without worrying about whether the 64 bit ABI has been included. */
1590
1591void
9719ad41
RS
1592bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
1593 Elf64_Internal_RegInfo *in)
b49e97c9
TS
1594{
1595 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
1596 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
1597 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
1598 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
1599 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
1600 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
1601 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
1602}
1603
1604void
9719ad41
RS
1605bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
1606 Elf64_External_RegInfo *ex)
b49e97c9
TS
1607{
1608 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
1609 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
1610 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
1611 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
1612 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
1613 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
1614 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
1615}
1616
1617/* Swap in an options header. */
1618
1619void
9719ad41
RS
1620bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
1621 Elf_Internal_Options *in)
b49e97c9
TS
1622{
1623 in->kind = H_GET_8 (abfd, ex->kind);
1624 in->size = H_GET_8 (abfd, ex->size);
1625 in->section = H_GET_16 (abfd, ex->section);
1626 in->info = H_GET_32 (abfd, ex->info);
1627}
1628
1629/* Swap out an options header. */
1630
1631void
9719ad41
RS
1632bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
1633 Elf_External_Options *ex)
b49e97c9
TS
1634{
1635 H_PUT_8 (abfd, in->kind, ex->kind);
1636 H_PUT_8 (abfd, in->size, ex->size);
1637 H_PUT_16 (abfd, in->section, ex->section);
1638 H_PUT_32 (abfd, in->info, ex->info);
1639}
1640\f
1641/* This function is called via qsort() to sort the dynamic relocation
1642 entries by increasing r_symndx value. */
1643
1644static int
9719ad41 1645sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 1646{
947216bf
AM
1647 Elf_Internal_Rela int_reloc1;
1648 Elf_Internal_Rela int_reloc2;
b49e97c9 1649
947216bf
AM
1650 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
1651 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 1652
947216bf 1653 return ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
b49e97c9
TS
1654}
1655
f4416af6
AO
1656/* Like sort_dynamic_relocs, but used for elf64 relocations. */
1657
1658static int
7e3102a7
AM
1659sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
1660 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 1661{
7e3102a7 1662#ifdef BFD64
f4416af6
AO
1663 Elf_Internal_Rela int_reloc1[3];
1664 Elf_Internal_Rela int_reloc2[3];
1665
1666 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1667 (reldyn_sorting_bfd, arg1, int_reloc1);
1668 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
1669 (reldyn_sorting_bfd, arg2, int_reloc2);
1670
1671 return (ELF64_R_SYM (int_reloc1[0].r_info)
1672 - ELF64_R_SYM (int_reloc2[0].r_info));
7e3102a7
AM
1673#else
1674 abort ();
1675#endif
f4416af6
AO
1676}
1677
1678
b49e97c9
TS
1679/* This routine is used to write out ECOFF debugging external symbol
1680 information. It is called via mips_elf_link_hash_traverse. The
1681 ECOFF external symbol information must match the ELF external
1682 symbol information. Unfortunately, at this point we don't know
1683 whether a symbol is required by reloc information, so the two
1684 tables may wind up being different. We must sort out the external
1685 symbol information before we can set the final size of the .mdebug
1686 section, and we must set the size of the .mdebug section before we
1687 can relocate any sections, and we can't know which symbols are
1688 required by relocation until we relocate the sections.
1689 Fortunately, it is relatively unlikely that any symbol will be
1690 stripped but required by a reloc. In particular, it can not happen
1691 when generating a final executable. */
1692
b34976b6 1693static bfd_boolean
9719ad41 1694mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 1695{
9719ad41 1696 struct extsym_info *einfo = data;
b34976b6 1697 bfd_boolean strip;
b49e97c9
TS
1698 asection *sec, *output_section;
1699
1700 if (h->root.root.type == bfd_link_hash_warning)
1701 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1702
1703 if (h->root.indx == -2)
b34976b6 1704 strip = FALSE;
f5385ebf 1705 else if ((h->root.def_dynamic
77cfaee6
AM
1706 || h->root.ref_dynamic
1707 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
1708 && !h->root.def_regular
1709 && !h->root.ref_regular)
b34976b6 1710 strip = TRUE;
b49e97c9
TS
1711 else if (einfo->info->strip == strip_all
1712 || (einfo->info->strip == strip_some
1713 && bfd_hash_lookup (einfo->info->keep_hash,
1714 h->root.root.root.string,
b34976b6
AM
1715 FALSE, FALSE) == NULL))
1716 strip = TRUE;
b49e97c9 1717 else
b34976b6 1718 strip = FALSE;
b49e97c9
TS
1719
1720 if (strip)
b34976b6 1721 return TRUE;
b49e97c9
TS
1722
1723 if (h->esym.ifd == -2)
1724 {
1725 h->esym.jmptbl = 0;
1726 h->esym.cobol_main = 0;
1727 h->esym.weakext = 0;
1728 h->esym.reserved = 0;
1729 h->esym.ifd = ifdNil;
1730 h->esym.asym.value = 0;
1731 h->esym.asym.st = stGlobal;
1732
1733 if (h->root.root.type == bfd_link_hash_undefined
1734 || h->root.root.type == bfd_link_hash_undefweak)
1735 {
1736 const char *name;
1737
1738 /* Use undefined class. Also, set class and type for some
1739 special symbols. */
1740 name = h->root.root.root.string;
1741 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
1742 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
1743 {
1744 h->esym.asym.sc = scData;
1745 h->esym.asym.st = stLabel;
1746 h->esym.asym.value = 0;
1747 }
1748 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
1749 {
1750 h->esym.asym.sc = scAbs;
1751 h->esym.asym.st = stLabel;
1752 h->esym.asym.value =
1753 mips_elf_hash_table (einfo->info)->procedure_count;
1754 }
4a14403c 1755 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
b49e97c9
TS
1756 {
1757 h->esym.asym.sc = scAbs;
1758 h->esym.asym.st = stLabel;
1759 h->esym.asym.value = elf_gp (einfo->abfd);
1760 }
1761 else
1762 h->esym.asym.sc = scUndefined;
1763 }
1764 else if (h->root.root.type != bfd_link_hash_defined
1765 && h->root.root.type != bfd_link_hash_defweak)
1766 h->esym.asym.sc = scAbs;
1767 else
1768 {
1769 const char *name;
1770
1771 sec = h->root.root.u.def.section;
1772 output_section = sec->output_section;
1773
1774 /* When making a shared library and symbol h is the one from
1775 the another shared library, OUTPUT_SECTION may be null. */
1776 if (output_section == NULL)
1777 h->esym.asym.sc = scUndefined;
1778 else
1779 {
1780 name = bfd_section_name (output_section->owner, output_section);
1781
1782 if (strcmp (name, ".text") == 0)
1783 h->esym.asym.sc = scText;
1784 else if (strcmp (name, ".data") == 0)
1785 h->esym.asym.sc = scData;
1786 else if (strcmp (name, ".sdata") == 0)
1787 h->esym.asym.sc = scSData;
1788 else if (strcmp (name, ".rodata") == 0
1789 || strcmp (name, ".rdata") == 0)
1790 h->esym.asym.sc = scRData;
1791 else if (strcmp (name, ".bss") == 0)
1792 h->esym.asym.sc = scBss;
1793 else if (strcmp (name, ".sbss") == 0)
1794 h->esym.asym.sc = scSBss;
1795 else if (strcmp (name, ".init") == 0)
1796 h->esym.asym.sc = scInit;
1797 else if (strcmp (name, ".fini") == 0)
1798 h->esym.asym.sc = scFini;
1799 else
1800 h->esym.asym.sc = scAbs;
1801 }
1802 }
1803
1804 h->esym.asym.reserved = 0;
1805 h->esym.asym.index = indexNil;
1806 }
1807
1808 if (h->root.root.type == bfd_link_hash_common)
1809 h->esym.asym.value = h->root.root.u.c.size;
1810 else if (h->root.root.type == bfd_link_hash_defined
1811 || h->root.root.type == bfd_link_hash_defweak)
1812 {
1813 if (h->esym.asym.sc == scCommon)
1814 h->esym.asym.sc = scBss;
1815 else if (h->esym.asym.sc == scSCommon)
1816 h->esym.asym.sc = scSBss;
1817
1818 sec = h->root.root.u.def.section;
1819 output_section = sec->output_section;
1820 if (output_section != NULL)
1821 h->esym.asym.value = (h->root.root.u.def.value
1822 + sec->output_offset
1823 + output_section->vma);
1824 else
1825 h->esym.asym.value = 0;
1826 }
f5385ebf 1827 else if (h->root.needs_plt)
b49e97c9
TS
1828 {
1829 struct mips_elf_link_hash_entry *hd = h;
b34976b6 1830 bfd_boolean no_fn_stub = h->no_fn_stub;
b49e97c9
TS
1831
1832 while (hd->root.root.type == bfd_link_hash_indirect)
1833 {
1834 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
1835 no_fn_stub = no_fn_stub || hd->no_fn_stub;
1836 }
1837
1838 if (!no_fn_stub)
1839 {
1840 /* Set type and value for a symbol with a function stub. */
1841 h->esym.asym.st = stProc;
1842 sec = hd->root.root.u.def.section;
1843 if (sec == NULL)
1844 h->esym.asym.value = 0;
1845 else
1846 {
1847 output_section = sec->output_section;
1848 if (output_section != NULL)
1849 h->esym.asym.value = (hd->root.plt.offset
1850 + sec->output_offset
1851 + output_section->vma);
1852 else
1853 h->esym.asym.value = 0;
1854 }
b49e97c9
TS
1855 }
1856 }
1857
1858 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
1859 h->root.root.root.string,
1860 &h->esym))
1861 {
b34976b6
AM
1862 einfo->failed = TRUE;
1863 return FALSE;
b49e97c9
TS
1864 }
1865
b34976b6 1866 return TRUE;
b49e97c9
TS
1867}
1868
1869/* A comparison routine used to sort .gptab entries. */
1870
1871static int
9719ad41 1872gptab_compare (const void *p1, const void *p2)
b49e97c9 1873{
9719ad41
RS
1874 const Elf32_gptab *a1 = p1;
1875 const Elf32_gptab *a2 = p2;
b49e97c9
TS
1876
1877 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
1878}
1879\f
b15e6682 1880/* Functions to manage the got entry hash table. */
f4416af6
AO
1881
1882/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
1883 hash number. */
1884
1885static INLINE hashval_t
9719ad41 1886mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
1887{
1888#ifdef BFD64
1889 return addr + (addr >> 32);
1890#else
1891 return addr;
1892#endif
1893}
1894
1895/* got_entries only match if they're identical, except for gotidx, so
1896 use all fields to compute the hash, and compare the appropriate
1897 union members. */
1898
b15e6682 1899static hashval_t
9719ad41 1900mips_elf_got_entry_hash (const void *entry_)
b15e6682
AO
1901{
1902 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1903
38985a1c 1904 return entry->symndx
0f20cc35 1905 + ((entry->tls_type & GOT_TLS_LDM) << 17)
f4416af6 1906 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
38985a1c
AO
1907 : entry->abfd->id
1908 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
1909 : entry->d.h->root.root.root.hash));
b15e6682
AO
1910}
1911
1912static int
9719ad41 1913mips_elf_got_entry_eq (const void *entry1, const void *entry2)
b15e6682
AO
1914{
1915 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1916 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1917
0f20cc35
DJ
1918 /* An LDM entry can only match another LDM entry. */
1919 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1920 return 0;
1921
b15e6682 1922 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
f4416af6
AO
1923 && (! e1->abfd ? e1->d.address == e2->d.address
1924 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
1925 : e1->d.h == e2->d.h);
1926}
1927
1928/* multi_got_entries are still a match in the case of global objects,
1929 even if the input bfd in which they're referenced differs, so the
1930 hash computation and compare functions are adjusted
1931 accordingly. */
1932
1933static hashval_t
9719ad41 1934mips_elf_multi_got_entry_hash (const void *entry_)
f4416af6
AO
1935{
1936 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
1937
1938 return entry->symndx
1939 + (! entry->abfd
1940 ? mips_elf_hash_bfd_vma (entry->d.address)
1941 : entry->symndx >= 0
0f20cc35
DJ
1942 ? ((entry->tls_type & GOT_TLS_LDM)
1943 ? (GOT_TLS_LDM << 17)
1944 : (entry->abfd->id
1945 + mips_elf_hash_bfd_vma (entry->d.addend)))
f4416af6
AO
1946 : entry->d.h->root.root.root.hash);
1947}
1948
1949static int
9719ad41 1950mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
1951{
1952 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
1953 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
1954
0f20cc35
DJ
1955 /* Any two LDM entries match. */
1956 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
1957 return 1;
1958
1959 /* Nothing else matches an LDM entry. */
1960 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
1961 return 0;
1962
f4416af6
AO
1963 return e1->symndx == e2->symndx
1964 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
1965 : e1->abfd == NULL || e2->abfd == NULL
1966 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
1967 : e1->d.h == e2->d.h);
b15e6682
AO
1968}
1969\f
0a44bf69
RS
1970/* Return the dynamic relocation section. If it doesn't exist, try to
1971 create a new it if CREATE_P, otherwise return NULL. Also return NULL
1972 if creation fails. */
f4416af6
AO
1973
1974static asection *
0a44bf69 1975mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 1976{
0a44bf69 1977 const char *dname;
f4416af6 1978 asection *sreloc;
0a44bf69 1979 bfd *dynobj;
f4416af6 1980
0a44bf69
RS
1981 dname = MIPS_ELF_REL_DYN_NAME (info);
1982 dynobj = elf_hash_table (info)->dynobj;
f4416af6
AO
1983 sreloc = bfd_get_section_by_name (dynobj, dname);
1984 if (sreloc == NULL && create_p)
1985 {
3496cb2a
L
1986 sreloc = bfd_make_section_with_flags (dynobj, dname,
1987 (SEC_ALLOC
1988 | SEC_LOAD
1989 | SEC_HAS_CONTENTS
1990 | SEC_IN_MEMORY
1991 | SEC_LINKER_CREATED
1992 | SEC_READONLY));
f4416af6 1993 if (sreloc == NULL
f4416af6 1994 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 1995 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
1996 return NULL;
1997 }
1998 return sreloc;
1999}
2000
b49e97c9
TS
2001/* Returns the GOT section for ABFD. */
2002
2003static asection *
9719ad41 2004mips_elf_got_section (bfd *abfd, bfd_boolean maybe_excluded)
b49e97c9 2005{
f4416af6
AO
2006 asection *sgot = bfd_get_section_by_name (abfd, ".got");
2007 if (sgot == NULL
2008 || (! maybe_excluded && (sgot->flags & SEC_EXCLUDE) != 0))
2009 return NULL;
2010 return sgot;
b49e97c9
TS
2011}
2012
2013/* Returns the GOT information associated with the link indicated by
2014 INFO. If SGOTP is non-NULL, it is filled in with the GOT
2015 section. */
2016
2017static struct mips_got_info *
9719ad41 2018mips_elf_got_info (bfd *abfd, asection **sgotp)
b49e97c9
TS
2019{
2020 asection *sgot;
2021 struct mips_got_info *g;
2022
f4416af6 2023 sgot = mips_elf_got_section (abfd, TRUE);
b49e97c9 2024 BFD_ASSERT (sgot != NULL);
f0abc2a1
AM
2025 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
2026 g = mips_elf_section_data (sgot)->u.got_info;
b49e97c9
TS
2027 BFD_ASSERT (g != NULL);
2028
2029 if (sgotp)
f4416af6
AO
2030 *sgotp = (sgot->flags & SEC_EXCLUDE) == 0 ? sgot : NULL;
2031
b49e97c9
TS
2032 return g;
2033}
2034
0f20cc35
DJ
2035/* Count the number of relocations needed for a TLS GOT entry, with
2036 access types from TLS_TYPE, and symbol H (or a local symbol if H
2037 is NULL). */
2038
2039static int
2040mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2041 struct elf_link_hash_entry *h)
2042{
2043 int indx = 0;
2044 int ret = 0;
2045 bfd_boolean need_relocs = FALSE;
2046 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2047
2048 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2049 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2050 indx = h->dynindx;
2051
2052 if ((info->shared || indx != 0)
2053 && (h == NULL
2054 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2055 || h->root.type != bfd_link_hash_undefweak))
2056 need_relocs = TRUE;
2057
2058 if (!need_relocs)
2059 return FALSE;
2060
2061 if (tls_type & GOT_TLS_GD)
2062 {
2063 ret++;
2064 if (indx != 0)
2065 ret++;
2066 }
2067
2068 if (tls_type & GOT_TLS_IE)
2069 ret++;
2070
2071 if ((tls_type & GOT_TLS_LDM) && info->shared)
2072 ret++;
2073
2074 return ret;
2075}
2076
2077/* Count the number of TLS relocations required for the GOT entry in
2078 ARG1, if it describes a local symbol. */
2079
2080static int
2081mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2082{
2083 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2084 struct mips_elf_count_tls_arg *arg = arg2;
2085
2086 if (entry->abfd != NULL && entry->symndx != -1)
2087 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2088
2089 return 1;
2090}
2091
2092/* Count the number of TLS GOT entries required for the global (or
2093 forced-local) symbol in ARG1. */
2094
2095static int
2096mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2097{
2098 struct mips_elf_link_hash_entry *hm
2099 = (struct mips_elf_link_hash_entry *) arg1;
2100 struct mips_elf_count_tls_arg *arg = arg2;
2101
2102 if (hm->tls_type & GOT_TLS_GD)
2103 arg->needed += 2;
2104 if (hm->tls_type & GOT_TLS_IE)
2105 arg->needed += 1;
2106
2107 return 1;
2108}
2109
2110/* Count the number of TLS relocations required for the global (or
2111 forced-local) symbol in ARG1. */
2112
2113static int
2114mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2115{
2116 struct mips_elf_link_hash_entry *hm
2117 = (struct mips_elf_link_hash_entry *) arg1;
2118 struct mips_elf_count_tls_arg *arg = arg2;
2119
2120 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2121
2122 return 1;
2123}
2124
2125/* Output a simple dynamic relocation into SRELOC. */
2126
2127static void
2128mips_elf_output_dynamic_relocation (bfd *output_bfd,
2129 asection *sreloc,
2130 unsigned long indx,
2131 int r_type,
2132 bfd_vma offset)
2133{
2134 Elf_Internal_Rela rel[3];
2135
2136 memset (rel, 0, sizeof (rel));
2137
2138 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2139 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2140
2141 if (ABI_64_P (output_bfd))
2142 {
2143 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2144 (output_bfd, &rel[0],
2145 (sreloc->contents
2146 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2147 }
2148 else
2149 bfd_elf32_swap_reloc_out
2150 (output_bfd, &rel[0],
2151 (sreloc->contents
2152 + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
2153 ++sreloc->reloc_count;
2154}
2155
2156/* Initialize a set of TLS GOT entries for one symbol. */
2157
2158static void
2159mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2160 unsigned char *tls_type_p,
2161 struct bfd_link_info *info,
2162 struct mips_elf_link_hash_entry *h,
2163 bfd_vma value)
2164{
2165 int indx;
2166 asection *sreloc, *sgot;
2167 bfd_vma offset, offset2;
2168 bfd *dynobj;
2169 bfd_boolean need_relocs = FALSE;
2170
2171 dynobj = elf_hash_table (info)->dynobj;
2172 sgot = mips_elf_got_section (dynobj, FALSE);
2173
2174 indx = 0;
2175 if (h != NULL)
2176 {
2177 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2178
2179 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2180 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2181 indx = h->root.dynindx;
2182 }
2183
2184 if (*tls_type_p & GOT_TLS_DONE)
2185 return;
2186
2187 if ((info->shared || indx != 0)
2188 && (h == NULL
2189 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2190 || h->root.type != bfd_link_hash_undefweak))
2191 need_relocs = TRUE;
2192
2193 /* MINUS_ONE means the symbol is not defined in this object. It may not
2194 be defined at all; assume that the value doesn't matter in that
2195 case. Otherwise complain if we would use the value. */
2196 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2197 || h->root.root.type == bfd_link_hash_undefweak);
2198
2199 /* Emit necessary relocations. */
0a44bf69 2200 sreloc = mips_elf_rel_dyn_section (info, FALSE);
0f20cc35
DJ
2201
2202 /* General Dynamic. */
2203 if (*tls_type_p & GOT_TLS_GD)
2204 {
2205 offset = got_offset;
2206 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2207
2208 if (need_relocs)
2209 {
2210 mips_elf_output_dynamic_relocation
2211 (abfd, sreloc, indx,
2212 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2213 sgot->output_offset + sgot->output_section->vma + offset);
2214
2215 if (indx)
2216 mips_elf_output_dynamic_relocation
2217 (abfd, sreloc, indx,
2218 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2219 sgot->output_offset + sgot->output_section->vma + offset2);
2220 else
2221 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2222 sgot->contents + offset2);
2223 }
2224 else
2225 {
2226 MIPS_ELF_PUT_WORD (abfd, 1,
2227 sgot->contents + offset);
2228 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2229 sgot->contents + offset2);
2230 }
2231
2232 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2233 }
2234
2235 /* Initial Exec model. */
2236 if (*tls_type_p & GOT_TLS_IE)
2237 {
2238 offset = got_offset;
2239
2240 if (need_relocs)
2241 {
2242 if (indx == 0)
2243 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2244 sgot->contents + offset);
2245 else
2246 MIPS_ELF_PUT_WORD (abfd, 0,
2247 sgot->contents + offset);
2248
2249 mips_elf_output_dynamic_relocation
2250 (abfd, sreloc, indx,
2251 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2252 sgot->output_offset + sgot->output_section->vma + offset);
2253 }
2254 else
2255 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2256 sgot->contents + offset);
2257 }
2258
2259 if (*tls_type_p & GOT_TLS_LDM)
2260 {
2261 /* The initial offset is zero, and the LD offsets will include the
2262 bias by DTP_OFFSET. */
2263 MIPS_ELF_PUT_WORD (abfd, 0,
2264 sgot->contents + got_offset
2265 + MIPS_ELF_GOT_SIZE (abfd));
2266
2267 if (!info->shared)
2268 MIPS_ELF_PUT_WORD (abfd, 1,
2269 sgot->contents + got_offset);
2270 else
2271 mips_elf_output_dynamic_relocation
2272 (abfd, sreloc, indx,
2273 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2274 sgot->output_offset + sgot->output_section->vma + got_offset);
2275 }
2276
2277 *tls_type_p |= GOT_TLS_DONE;
2278}
2279
2280/* Return the GOT index to use for a relocation of type R_TYPE against
2281 a symbol accessed using TLS_TYPE models. The GOT entries for this
2282 symbol in this GOT start at GOT_INDEX. This function initializes the
2283 GOT entries and corresponding relocations. */
2284
2285static bfd_vma
2286mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2287 int r_type, struct bfd_link_info *info,
2288 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2289{
2290 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2291 || r_type == R_MIPS_TLS_LDM);
2292
2293 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2294
2295 if (r_type == R_MIPS_TLS_GOTTPREL)
2296 {
2297 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2298 if (*tls_type & GOT_TLS_GD)
2299 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2300 else
2301 return got_index;
2302 }
2303
2304 if (r_type == R_MIPS_TLS_GD)
2305 {
2306 BFD_ASSERT (*tls_type & GOT_TLS_GD);
2307 return got_index;
2308 }
2309
2310 if (r_type == R_MIPS_TLS_LDM)
2311 {
2312 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
2313 return got_index;
2314 }
2315
2316 return got_index;
2317}
2318
0a44bf69
RS
2319/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
2320 for global symbol H. .got.plt comes before the GOT, so the offset
2321 will be negative. */
2322
2323static bfd_vma
2324mips_elf_gotplt_index (struct bfd_link_info *info,
2325 struct elf_link_hash_entry *h)
2326{
2327 bfd_vma plt_index, got_address, got_value;
2328 struct mips_elf_link_hash_table *htab;
2329
2330 htab = mips_elf_hash_table (info);
2331 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
2332
2333 /* Calculate the index of the symbol's PLT entry. */
2334 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
2335
2336 /* Calculate the address of the associated .got.plt entry. */
2337 got_address = (htab->sgotplt->output_section->vma
2338 + htab->sgotplt->output_offset
2339 + plt_index * 4);
2340
2341 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
2342 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
2343 + htab->root.hgot->root.u.def.section->output_offset
2344 + htab->root.hgot->root.u.def.value);
2345
2346 return got_address - got_value;
2347}
2348
2349/* Return the GOT offset for address VALUE, which was derived from
2350 a symbol belonging to INPUT_SECTION. If there is not yet a GOT
2351 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
2352 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
2353 offset can be found. */
b49e97c9
TS
2354
2355static bfd_vma
9719ad41 2356mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
0a44bf69
RS
2357 asection *input_section, bfd_vma value,
2358 unsigned long r_symndx,
0f20cc35 2359 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9
TS
2360{
2361 asection *sgot;
2362 struct mips_got_info *g;
b15e6682 2363 struct mips_got_entry *entry;
b49e97c9
TS
2364
2365 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2366
0a44bf69
RS
2367 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2368 input_section, value,
0f20cc35
DJ
2369 r_symndx, h, r_type);
2370 if (!entry)
b15e6682 2371 return MINUS_ONE;
0f20cc35
DJ
2372
2373 if (TLS_RELOC_P (r_type))
2374 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type, r_type,
2375 info, h, value);
2376 else
2377 return entry->gotidx;
b49e97c9
TS
2378}
2379
2380/* Returns the GOT index for the global symbol indicated by H. */
2381
2382static bfd_vma
0f20cc35
DJ
2383mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
2384 int r_type, struct bfd_link_info *info)
b49e97c9
TS
2385{
2386 bfd_vma index;
2387 asection *sgot;
f4416af6 2388 struct mips_got_info *g, *gg;
d0c7ff07 2389 long global_got_dynindx = 0;
b49e97c9 2390
f4416af6
AO
2391 gg = g = mips_elf_got_info (abfd, &sgot);
2392 if (g->bfd2got && ibfd)
2393 {
2394 struct mips_got_entry e, *p;
143d77c5 2395
f4416af6
AO
2396 BFD_ASSERT (h->dynindx >= 0);
2397
2398 g = mips_elf_got_for_ibfd (g, ibfd);
0f20cc35 2399 if (g->next != gg || TLS_RELOC_P (r_type))
f4416af6
AO
2400 {
2401 e.abfd = ibfd;
2402 e.symndx = -1;
2403 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 2404 e.tls_type = 0;
f4416af6 2405
9719ad41 2406 p = htab_find (g->got_entries, &e);
f4416af6
AO
2407
2408 BFD_ASSERT (p->gotidx > 0);
0f20cc35
DJ
2409
2410 if (TLS_RELOC_P (r_type))
2411 {
2412 bfd_vma value = MINUS_ONE;
2413 if ((h->root.type == bfd_link_hash_defined
2414 || h->root.type == bfd_link_hash_defweak)
2415 && h->root.u.def.section->output_section)
2416 value = (h->root.u.def.value
2417 + h->root.u.def.section->output_offset
2418 + h->root.u.def.section->output_section->vma);
2419
2420 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
2421 info, e.d.h, value);
2422 }
2423 else
2424 return p->gotidx;
f4416af6
AO
2425 }
2426 }
2427
2428 if (gg->global_gotsym != NULL)
2429 global_got_dynindx = gg->global_gotsym->dynindx;
b49e97c9 2430
0f20cc35
DJ
2431 if (TLS_RELOC_P (r_type))
2432 {
2433 struct mips_elf_link_hash_entry *hm
2434 = (struct mips_elf_link_hash_entry *) h;
2435 bfd_vma value = MINUS_ONE;
2436
2437 if ((h->root.type == bfd_link_hash_defined
2438 || h->root.type == bfd_link_hash_defweak)
2439 && h->root.u.def.section->output_section)
2440 value = (h->root.u.def.value
2441 + h->root.u.def.section->output_offset
2442 + h->root.u.def.section->output_section->vma);
2443
2444 index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
2445 r_type, info, hm, value);
2446 }
2447 else
2448 {
2449 /* Once we determine the global GOT entry with the lowest dynamic
2450 symbol table index, we must put all dynamic symbols with greater
2451 indices into the GOT. That makes it easy to calculate the GOT
2452 offset. */
2453 BFD_ASSERT (h->dynindx >= global_got_dynindx);
2454 index = ((h->dynindx - global_got_dynindx + g->local_gotno)
2455 * MIPS_ELF_GOT_SIZE (abfd));
2456 }
eea6121a 2457 BFD_ASSERT (index < sgot->size);
b49e97c9
TS
2458
2459 return index;
2460}
2461
0a44bf69
RS
2462/* Find a GOT page entry that points to within 32KB of VALUE, which was
2463 calculated from a symbol belonging to INPUT_SECTION. These entries
2464 are supposed to be placed at small offsets in the GOT, i.e., within
2465 32KB of GP. Return the index of the GOT entry, or -1 if no entry
2466 could be created. If OFFSETP is nonnull, use it to return the
2467 offset of the GOT entry from VALUE. */
b49e97c9
TS
2468
2469static bfd_vma
9719ad41 2470mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
0a44bf69 2471 asection *input_section, bfd_vma value, bfd_vma *offsetp)
b49e97c9
TS
2472{
2473 asection *sgot;
2474 struct mips_got_info *g;
0a44bf69 2475 bfd_vma page, index;
b15e6682 2476 struct mips_got_entry *entry;
b49e97c9
TS
2477
2478 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2479
0a44bf69
RS
2480 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
2481 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2482 input_section, page, 0,
0f20cc35 2483 NULL, R_MIPS_GOT_PAGE);
b49e97c9 2484
b15e6682
AO
2485 if (!entry)
2486 return MINUS_ONE;
143d77c5 2487
b15e6682 2488 index = entry->gotidx;
b49e97c9
TS
2489
2490 if (offsetp)
f4416af6 2491 *offsetp = value - entry->d.address;
b49e97c9
TS
2492
2493 return index;
2494}
2495
0a44bf69
RS
2496/* Find a local GOT entry for an R_MIPS_GOT16 relocation against VALUE,
2497 which was calculated from a symbol belonging to INPUT_SECTION.
2498 EXTERNAL is true if the relocation was against a global symbol
2499 that has been forced local. */
b49e97c9
TS
2500
2501static bfd_vma
9719ad41 2502mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
0a44bf69
RS
2503 asection *input_section, bfd_vma value,
2504 bfd_boolean external)
b49e97c9
TS
2505{
2506 asection *sgot;
2507 struct mips_got_info *g;
b15e6682 2508 struct mips_got_entry *entry;
b49e97c9 2509
0a44bf69
RS
2510 /* GOT16 relocations against local symbols are followed by a LO16
2511 relocation; those against global symbols are not. Thus if the
2512 symbol was originally local, the GOT16 relocation should load the
2513 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 2514 if (! external)
0a44bf69 2515 value = mips_elf_high (value) << 16;
b49e97c9
TS
2516
2517 g = mips_elf_got_info (elf_hash_table (info)->dynobj, &sgot);
2518
0a44bf69
RS
2519 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, g, sgot,
2520 input_section, value, 0,
2521 NULL, R_MIPS_GOT16);
b15e6682
AO
2522 if (entry)
2523 return entry->gotidx;
2524 else
2525 return MINUS_ONE;
b49e97c9
TS
2526}
2527
2528/* Returns the offset for the entry at the INDEXth position
2529 in the GOT. */
2530
2531static bfd_vma
9719ad41
RS
2532mips_elf_got_offset_from_index (bfd *dynobj, bfd *output_bfd,
2533 bfd *input_bfd, bfd_vma index)
b49e97c9
TS
2534{
2535 asection *sgot;
2536 bfd_vma gp;
f4416af6 2537 struct mips_got_info *g;
b49e97c9 2538
f4416af6
AO
2539 g = mips_elf_got_info (dynobj, &sgot);
2540 gp = _bfd_get_gp_value (output_bfd)
2541 + mips_elf_adjust_gp (output_bfd, g, input_bfd);
143d77c5 2542
f4416af6 2543 return sgot->output_section->vma + sgot->output_offset + index - gp;
b49e97c9
TS
2544}
2545
0a44bf69
RS
2546/* Create and return a local GOT entry for VALUE, which was calculated
2547 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
2548 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
2549 instead. */
b49e97c9 2550
b15e6682 2551static struct mips_got_entry *
0a44bf69
RS
2552mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
2553 bfd *ibfd, struct mips_got_info *gg,
2554 asection *sgot, asection *input_section,
2555 bfd_vma value, unsigned long r_symndx,
0f20cc35
DJ
2556 struct mips_elf_link_hash_entry *h,
2557 int r_type)
b49e97c9 2558{
b15e6682 2559 struct mips_got_entry entry, **loc;
f4416af6 2560 struct mips_got_info *g;
0a44bf69
RS
2561 struct mips_elf_link_hash_table *htab;
2562
2563 htab = mips_elf_hash_table (info);
b15e6682 2564
f4416af6
AO
2565 entry.abfd = NULL;
2566 entry.symndx = -1;
2567 entry.d.address = value;
0f20cc35 2568 entry.tls_type = 0;
f4416af6
AO
2569
2570 g = mips_elf_got_for_ibfd (gg, ibfd);
2571 if (g == NULL)
2572 {
2573 g = mips_elf_got_for_ibfd (gg, abfd);
2574 BFD_ASSERT (g != NULL);
2575 }
b15e6682 2576
0f20cc35
DJ
2577 /* We might have a symbol, H, if it has been forced local. Use the
2578 global entry then. It doesn't matter whether an entry is local
2579 or global for TLS, since the dynamic linker does not
2580 automatically relocate TLS GOT entries. */
a008ac03 2581 BFD_ASSERT (h == NULL || h->root.forced_local);
0f20cc35
DJ
2582 if (TLS_RELOC_P (r_type))
2583 {
2584 struct mips_got_entry *p;
2585
2586 entry.abfd = ibfd;
2587 if (r_type == R_MIPS_TLS_LDM)
2588 {
2589 entry.tls_type = GOT_TLS_LDM;
2590 entry.symndx = 0;
2591 entry.d.addend = 0;
2592 }
2593 else if (h == NULL)
2594 {
2595 entry.symndx = r_symndx;
2596 entry.d.addend = 0;
2597 }
2598 else
2599 entry.d.h = h;
2600
2601 p = (struct mips_got_entry *)
2602 htab_find (g->got_entries, &entry);
2603
2604 BFD_ASSERT (p);
2605 return p;
2606 }
2607
b15e6682
AO
2608 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2609 INSERT);
2610 if (*loc)
2611 return *loc;
143d77c5 2612
b15e6682 2613 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
0f20cc35 2614 entry.tls_type = 0;
b15e6682
AO
2615
2616 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2617
2618 if (! *loc)
2619 return NULL;
143d77c5 2620
b15e6682
AO
2621 memcpy (*loc, &entry, sizeof entry);
2622
b49e97c9
TS
2623 if (g->assigned_gotno >= g->local_gotno)
2624 {
f4416af6 2625 (*loc)->gotidx = -1;
b49e97c9
TS
2626 /* We didn't allocate enough space in the GOT. */
2627 (*_bfd_error_handler)
2628 (_("not enough GOT space for local GOT entries"));
2629 bfd_set_error (bfd_error_bad_value);
b15e6682 2630 return NULL;
b49e97c9
TS
2631 }
2632
2633 MIPS_ELF_PUT_WORD (abfd, value,
b15e6682
AO
2634 (sgot->contents + entry.gotidx));
2635
0a44bf69
RS
2636 /* These GOT entries need a dynamic relocation on VxWorks. Because
2637 the offset between segments is not fixed, the relocation must be
2638 against a symbol in the same segment as the original symbol.
2639 The easiest way to do this is to take INPUT_SECTION's output
2640 section and emit a relocation against its section symbol. */
2641 if (htab->is_vxworks)
2642 {
2643 Elf_Internal_Rela outrel;
2644 asection *s, *output_section;
2645 bfd_byte *loc;
2646 bfd_vma got_address;
2647 int dynindx;
2648
2649 s = mips_elf_rel_dyn_section (info, FALSE);
2650 output_section = input_section->output_section;
2651 dynindx = elf_section_data (output_section)->dynindx;
2652 got_address = (sgot->output_section->vma
2653 + sgot->output_offset
2654 + entry.gotidx);
2655
2656 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
2657 outrel.r_offset = got_address;
2658 outrel.r_info = ELF32_R_INFO (dynindx, R_MIPS_32);
2659 outrel.r_addend = value - output_section->vma;
2660 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
2661 }
2662
b15e6682 2663 return *loc;
b49e97c9
TS
2664}
2665
2666/* Sort the dynamic symbol table so that symbols that need GOT entries
2667 appear towards the end. This reduces the amount of GOT space
2668 required. MAX_LOCAL is used to set the number of local symbols
2669 known to be in the dynamic symbol table. During
2670 _bfd_mips_elf_size_dynamic_sections, this value is 1. Afterward, the
2671 section symbols are added and the count is higher. */
2672
b34976b6 2673static bfd_boolean
9719ad41 2674mips_elf_sort_hash_table (struct bfd_link_info *info, unsigned long max_local)
b49e97c9
TS
2675{
2676 struct mips_elf_hash_sort_data hsd;
2677 struct mips_got_info *g;
2678 bfd *dynobj;
2679
2680 dynobj = elf_hash_table (info)->dynobj;
2681
f4416af6
AO
2682 g = mips_elf_got_info (dynobj, NULL);
2683
b49e97c9 2684 hsd.low = NULL;
143d77c5 2685 hsd.max_unref_got_dynindx =
f4416af6
AO
2686 hsd.min_got_dynindx = elf_hash_table (info)->dynsymcount
2687 /* In the multi-got case, assigned_gotno of the master got_info
2688 indicate the number of entries that aren't referenced in the
2689 primary GOT, but that must have entries because there are
2690 dynamic relocations that reference it. Since they aren't
2691 referenced, we move them to the end of the GOT, so that they
2692 don't prevent other entries that are referenced from getting
2693 too large offsets. */
2694 - (g->next ? g->assigned_gotno : 0);
b49e97c9
TS
2695 hsd.max_non_got_dynindx = max_local;
2696 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
2697 elf_hash_table (info)),
2698 mips_elf_sort_hash_table_f,
2699 &hsd);
2700
2701 /* There should have been enough room in the symbol table to
44c410de 2702 accommodate both the GOT and non-GOT symbols. */
b49e97c9 2703 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
f4416af6
AO
2704 BFD_ASSERT ((unsigned long)hsd.max_unref_got_dynindx
2705 <= elf_hash_table (info)->dynsymcount);
b49e97c9
TS
2706
2707 /* Now we know which dynamic symbol has the lowest dynamic symbol
2708 table index in the GOT. */
b49e97c9
TS
2709 g->global_gotsym = hsd.low;
2710
b34976b6 2711 return TRUE;
b49e97c9
TS
2712}
2713
2714/* If H needs a GOT entry, assign it the highest available dynamic
2715 index. Otherwise, assign it the lowest available dynamic
2716 index. */
2717
b34976b6 2718static bfd_boolean
9719ad41 2719mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2720{
9719ad41 2721 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9
TS
2722
2723 if (h->root.root.type == bfd_link_hash_warning)
2724 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2725
2726 /* Symbols without dynamic symbol table entries aren't interesting
2727 at all. */
2728 if (h->root.dynindx == -1)
b34976b6 2729 return TRUE;
b49e97c9 2730
f4416af6
AO
2731 /* Global symbols that need GOT entries that are not explicitly
2732 referenced are marked with got offset 2. Those that are
2733 referenced get a 1, and those that don't need GOT entries get
2734 -1. */
2735 if (h->root.got.offset == 2)
2736 {
0f20cc35
DJ
2737 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2738
f4416af6
AO
2739 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
2740 hsd->low = (struct elf_link_hash_entry *) h;
2741 h->root.dynindx = hsd->max_unref_got_dynindx++;
2742 }
2743 else if (h->root.got.offset != 1)
b49e97c9
TS
2744 h->root.dynindx = hsd->max_non_got_dynindx++;
2745 else
2746 {
0f20cc35
DJ
2747 BFD_ASSERT (h->tls_type == GOT_NORMAL);
2748
b49e97c9
TS
2749 h->root.dynindx = --hsd->min_got_dynindx;
2750 hsd->low = (struct elf_link_hash_entry *) h;
2751 }
2752
b34976b6 2753 return TRUE;
b49e97c9
TS
2754}
2755
2756/* If H is a symbol that needs a global GOT entry, but has a dynamic
2757 symbol table index lower than any we've seen to date, record it for
2758 posterity. */
2759
b34976b6 2760static bfd_boolean
9719ad41
RS
2761mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
2762 bfd *abfd, struct bfd_link_info *info,
0f20cc35
DJ
2763 struct mips_got_info *g,
2764 unsigned char tls_flag)
b49e97c9 2765{
f4416af6
AO
2766 struct mips_got_entry entry, **loc;
2767
b49e97c9
TS
2768 /* A global symbol in the GOT must also be in the dynamic symbol
2769 table. */
7c5fcef7
L
2770 if (h->dynindx == -1)
2771 {
2772 switch (ELF_ST_VISIBILITY (h->other))
2773 {
2774 case STV_INTERNAL:
2775 case STV_HIDDEN:
b34976b6 2776 _bfd_mips_elf_hide_symbol (info, h, TRUE);
7c5fcef7
L
2777 break;
2778 }
c152c796 2779 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2780 return FALSE;
7c5fcef7 2781 }
b49e97c9 2782
86324f90
EC
2783 /* Make sure we have a GOT to put this entry into. */
2784 BFD_ASSERT (g != NULL);
2785
f4416af6
AO
2786 entry.abfd = abfd;
2787 entry.symndx = -1;
2788 entry.d.h = (struct mips_elf_link_hash_entry *) h;
0f20cc35 2789 entry.tls_type = 0;
f4416af6
AO
2790
2791 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
2792 INSERT);
2793
b49e97c9
TS
2794 /* If we've already marked this entry as needing GOT space, we don't
2795 need to do it again. */
f4416af6 2796 if (*loc)
0f20cc35
DJ
2797 {
2798 (*loc)->tls_type |= tls_flag;
2799 return TRUE;
2800 }
f4416af6
AO
2801
2802 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2803
2804 if (! *loc)
2805 return FALSE;
143d77c5 2806
f4416af6 2807 entry.gotidx = -1;
0f20cc35
DJ
2808 entry.tls_type = tls_flag;
2809
f4416af6
AO
2810 memcpy (*loc, &entry, sizeof entry);
2811
b49e97c9 2812 if (h->got.offset != MINUS_ONE)
b34976b6 2813 return TRUE;
b49e97c9
TS
2814
2815 /* By setting this to a value other than -1, we are indicating that
2816 there needs to be a GOT entry for H. Avoid using zero, as the
2817 generic ELF copy_indirect_symbol tests for <= 0. */
0f20cc35
DJ
2818 if (tls_flag == 0)
2819 h->got.offset = 1;
b49e97c9 2820
b34976b6 2821 return TRUE;
b49e97c9 2822}
f4416af6
AO
2823
2824/* Reserve space in G for a GOT entry containing the value of symbol
2825 SYMNDX in input bfd ABDF, plus ADDEND. */
2826
2827static bfd_boolean
9719ad41 2828mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
0f20cc35
DJ
2829 struct mips_got_info *g,
2830 unsigned char tls_flag)
f4416af6
AO
2831{
2832 struct mips_got_entry entry, **loc;
2833
2834 entry.abfd = abfd;
2835 entry.symndx = symndx;
2836 entry.d.addend = addend;
0f20cc35 2837 entry.tls_type = tls_flag;
f4416af6
AO
2838 loc = (struct mips_got_entry **)
2839 htab_find_slot (g->got_entries, &entry, INSERT);
2840
2841 if (*loc)
0f20cc35
DJ
2842 {
2843 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
2844 {
2845 g->tls_gotno += 2;
2846 (*loc)->tls_type |= tls_flag;
2847 }
2848 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
2849 {
2850 g->tls_gotno += 1;
2851 (*loc)->tls_type |= tls_flag;
2852 }
2853 return TRUE;
2854 }
f4416af6 2855
0f20cc35
DJ
2856 if (tls_flag != 0)
2857 {
2858 entry.gotidx = -1;
2859 entry.tls_type = tls_flag;
2860 if (tls_flag == GOT_TLS_IE)
2861 g->tls_gotno += 1;
2862 else if (tls_flag == GOT_TLS_GD)
2863 g->tls_gotno += 2;
2864 else if (g->tls_ldm_offset == MINUS_ONE)
2865 {
2866 g->tls_ldm_offset = MINUS_TWO;
2867 g->tls_gotno += 2;
2868 }
2869 }
2870 else
2871 {
2872 entry.gotidx = g->local_gotno++;
2873 entry.tls_type = 0;
2874 }
f4416af6
AO
2875
2876 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
2877
2878 if (! *loc)
2879 return FALSE;
143d77c5 2880
f4416af6
AO
2881 memcpy (*loc, &entry, sizeof entry);
2882
2883 return TRUE;
2884}
2885\f
2886/* Compute the hash value of the bfd in a bfd2got hash entry. */
2887
2888static hashval_t
9719ad41 2889mips_elf_bfd2got_entry_hash (const void *entry_)
f4416af6
AO
2890{
2891 const struct mips_elf_bfd2got_hash *entry
2892 = (struct mips_elf_bfd2got_hash *)entry_;
2893
2894 return entry->bfd->id;
2895}
2896
2897/* Check whether two hash entries have the same bfd. */
2898
2899static int
9719ad41 2900mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
2901{
2902 const struct mips_elf_bfd2got_hash *e1
2903 = (const struct mips_elf_bfd2got_hash *)entry1;
2904 const struct mips_elf_bfd2got_hash *e2
2905 = (const struct mips_elf_bfd2got_hash *)entry2;
2906
2907 return e1->bfd == e2->bfd;
2908}
2909
bad36eac 2910/* In a multi-got link, determine the GOT to be used for IBFD. G must
f4416af6
AO
2911 be the master GOT data. */
2912
2913static struct mips_got_info *
9719ad41 2914mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
2915{
2916 struct mips_elf_bfd2got_hash e, *p;
2917
2918 if (! g->bfd2got)
2919 return g;
2920
2921 e.bfd = ibfd;
9719ad41 2922 p = htab_find (g->bfd2got, &e);
f4416af6
AO
2923 return p ? p->g : NULL;
2924}
2925
2926/* Create one separate got for each bfd that has entries in the global
2927 got, such that we can tell how many local and global entries each
2928 bfd requires. */
2929
2930static int
9719ad41 2931mips_elf_make_got_per_bfd (void **entryp, void *p)
f4416af6
AO
2932{
2933 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
2934 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
2935 htab_t bfd2got = arg->bfd2got;
2936 struct mips_got_info *g;
2937 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
2938 void **bfdgotp;
143d77c5 2939
f4416af6
AO
2940 /* Find the got_info for this GOT entry's input bfd. Create one if
2941 none exists. */
2942 bfdgot_entry.bfd = entry->abfd;
2943 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
2944 bfdgot = (struct mips_elf_bfd2got_hash *)*bfdgotp;
2945
2946 if (bfdgot != NULL)
2947 g = bfdgot->g;
2948 else
2949 {
2950 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
2951 (arg->obfd, sizeof (struct mips_elf_bfd2got_hash));
2952
2953 if (bfdgot == NULL)
2954 {
2955 arg->obfd = 0;
2956 return 0;
2957 }
2958
2959 *bfdgotp = bfdgot;
2960
2961 bfdgot->bfd = entry->abfd;
2962 bfdgot->g = g = (struct mips_got_info *)
2963 bfd_alloc (arg->obfd, sizeof (struct mips_got_info));
2964 if (g == NULL)
2965 {
2966 arg->obfd = 0;
2967 return 0;
2968 }
2969
2970 g->global_gotsym = NULL;
2971 g->global_gotno = 0;
2972 g->local_gotno = 0;
2973 g->assigned_gotno = -1;
0f20cc35
DJ
2974 g->tls_gotno = 0;
2975 g->tls_assigned_gotno = 0;
2976 g->tls_ldm_offset = MINUS_ONE;
f4416af6 2977 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
9719ad41 2978 mips_elf_multi_got_entry_eq, NULL);
f4416af6
AO
2979 if (g->got_entries == NULL)
2980 {
2981 arg->obfd = 0;
2982 return 0;
2983 }
2984
2985 g->bfd2got = NULL;
2986 g->next = NULL;
2987 }
2988
2989 /* Insert the GOT entry in the bfd's got entry hash table. */
2990 entryp = htab_find_slot (g->got_entries, entry, INSERT);
2991 if (*entryp != NULL)
2992 return 1;
143d77c5 2993
f4416af6
AO
2994 *entryp = entry;
2995
0f20cc35
DJ
2996 if (entry->tls_type)
2997 {
2998 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
2999 g->tls_gotno += 2;
3000 if (entry->tls_type & GOT_TLS_IE)
3001 g->tls_gotno += 1;
3002 }
3003 else if (entry->symndx >= 0 || entry->d.h->forced_local)
f4416af6
AO
3004 ++g->local_gotno;
3005 else
3006 ++g->global_gotno;
3007
3008 return 1;
3009}
3010
3011/* Attempt to merge gots of different input bfds. Try to use as much
3012 as possible of the primary got, since it doesn't require explicit
3013 dynamic relocations, but don't use bfds that would reference global
3014 symbols out of the addressable range. Failing the primary got,
3015 attempt to merge with the current got, or finish the current got
3016 and then make make the new got current. */
3017
3018static int
9719ad41 3019mips_elf_merge_gots (void **bfd2got_, void *p)
f4416af6
AO
3020{
3021 struct mips_elf_bfd2got_hash *bfd2got
3022 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
3023 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
3024 unsigned int lcount = bfd2got->g->local_gotno;
3025 unsigned int gcount = bfd2got->g->global_gotno;
0f20cc35 3026 unsigned int tcount = bfd2got->g->tls_gotno;
f4416af6 3027 unsigned int maxcnt = arg->max_count;
0f20cc35
DJ
3028 bfd_boolean too_many_for_tls = FALSE;
3029
3030 /* We place TLS GOT entries after both locals and globals. The globals
3031 for the primary GOT may overflow the normal GOT size limit, so be
3032 sure not to merge a GOT which requires TLS with the primary GOT in that
3033 case. This doesn't affect non-primary GOTs. */
3034 if (tcount > 0)
3035 {
3036 unsigned int primary_total = lcount + tcount + arg->global_count;
3037 if (primary_total * MIPS_ELF_GOT_SIZE (bfd2got->bfd)
0a44bf69 3038 >= MIPS_ELF_GOT_MAX_SIZE (arg->info))
0f20cc35
DJ
3039 too_many_for_tls = TRUE;
3040 }
143d77c5 3041
f4416af6
AO
3042 /* If we don't have a primary GOT and this is not too big, use it as
3043 a starting point for the primary GOT. */
0f20cc35
DJ
3044 if (! arg->primary && lcount + gcount + tcount <= maxcnt
3045 && ! too_many_for_tls)
f4416af6
AO
3046 {
3047 arg->primary = bfd2got->g;
3048 arg->primary_count = lcount + gcount;
3049 }
3050 /* If it looks like we can merge this bfd's entries with those of
3051 the primary, merge them. The heuristics is conservative, but we
3052 don't have to squeeze it too hard. */
0f20cc35
DJ
3053 else if (arg->primary && ! too_many_for_tls
3054 && (arg->primary_count + lcount + gcount + tcount) <= maxcnt)
f4416af6
AO
3055 {
3056 struct mips_got_info *g = bfd2got->g;
3057 int old_lcount = arg->primary->local_gotno;
3058 int old_gcount = arg->primary->global_gotno;
0f20cc35 3059 int old_tcount = arg->primary->tls_gotno;
f4416af6
AO
3060
3061 bfd2got->g = arg->primary;
3062
3063 htab_traverse (g->got_entries,
3064 mips_elf_make_got_per_bfd,
3065 arg);
3066 if (arg->obfd == NULL)
3067 return 0;
3068
3069 htab_delete (g->got_entries);
3070 /* We don't have to worry about releasing memory of the actual
3071 got entries, since they're all in the master got_entries hash
3072 table anyway. */
3073
caec41ff 3074 BFD_ASSERT (old_lcount + lcount >= arg->primary->local_gotno);
f4416af6 3075 BFD_ASSERT (old_gcount + gcount >= arg->primary->global_gotno);
0f20cc35 3076 BFD_ASSERT (old_tcount + tcount >= arg->primary->tls_gotno);
f4416af6
AO
3077
3078 arg->primary_count = arg->primary->local_gotno
0f20cc35 3079 + arg->primary->global_gotno + arg->primary->tls_gotno;
f4416af6
AO
3080 }
3081 /* If we can merge with the last-created got, do it. */
3082 else if (arg->current
0f20cc35 3083 && arg->current_count + lcount + gcount + tcount <= maxcnt)
f4416af6
AO
3084 {
3085 struct mips_got_info *g = bfd2got->g;
3086 int old_lcount = arg->current->local_gotno;
3087 int old_gcount = arg->current->global_gotno;
0f20cc35 3088 int old_tcount = arg->current->tls_gotno;
f4416af6
AO
3089
3090 bfd2got->g = arg->current;
3091
3092 htab_traverse (g->got_entries,
3093 mips_elf_make_got_per_bfd,
3094 arg);
3095 if (arg->obfd == NULL)
3096 return 0;
3097
3098 htab_delete (g->got_entries);
3099
caec41ff 3100 BFD_ASSERT (old_lcount + lcount >= arg->current->local_gotno);
f4416af6 3101 BFD_ASSERT (old_gcount + gcount >= arg->current->global_gotno);
0f20cc35 3102 BFD_ASSERT (old_tcount + tcount >= arg->current->tls_gotno);
f4416af6
AO
3103
3104 arg->current_count = arg->current->local_gotno
0f20cc35 3105 + arg->current->global_gotno + arg->current->tls_gotno;
f4416af6
AO
3106 }
3107 /* Well, we couldn't merge, so create a new GOT. Don't check if it
3108 fits; if it turns out that it doesn't, we'll get relocation
3109 overflows anyway. */
3110 else
3111 {
3112 bfd2got->g->next = arg->current;
3113 arg->current = bfd2got->g;
143d77c5 3114
0f20cc35
DJ
3115 arg->current_count = lcount + gcount + 2 * tcount;
3116 }
3117
3118 return 1;
3119}
3120
3121/* Set the TLS GOT index for the GOT entry in ENTRYP. */
3122
3123static int
3124mips_elf_initialize_tls_index (void **entryp, void *p)
3125{
3126 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3127 struct mips_got_info *g = p;
3128
3129 /* We're only interested in TLS symbols. */
3130 if (entry->tls_type == 0)
3131 return 1;
3132
3133 if (entry->symndx == -1)
3134 {
3135 /* There may be multiple mips_got_entry structs for a global variable
3136 if there is just one GOT. Just do this once. */
3137 if (g->next == NULL)
3138 {
3139 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4005427f
RS
3140 {
3141 entry->gotidx = entry->d.h->tls_got_offset;
3142 return 1;
3143 }
0f20cc35
DJ
3144 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
3145 }
3146 }
3147 else if (entry->tls_type & GOT_TLS_LDM)
3148 {
3149 /* Similarly, there may be multiple structs for the LDM entry. */
3150 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
3151 {
3152 entry->gotidx = g->tls_ldm_offset;
3153 return 1;
3154 }
f4416af6
AO
3155 }
3156
0f20cc35
DJ
3157 /* Initialize the GOT offset. */
3158 entry->gotidx = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
3159 if (g->next == NULL && entry->symndx == -1)
3160 entry->d.h->tls_got_offset = entry->gotidx;
3161
3162 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
3163 g->tls_assigned_gotno += 2;
3164 if (entry->tls_type & GOT_TLS_IE)
3165 g->tls_assigned_gotno += 1;
3166
3167 if (entry->tls_type & GOT_TLS_LDM)
3168 g->tls_ldm_offset = entry->gotidx;
3169
f4416af6
AO
3170 return 1;
3171}
3172
3173/* If passed a NULL mips_got_info in the argument, set the marker used
3174 to tell whether a global symbol needs a got entry (in the primary
3175 got) to the given VALUE.
3176
3177 If passed a pointer G to a mips_got_info in the argument (it must
3178 not be the primary GOT), compute the offset from the beginning of
3179 the (primary) GOT section to the entry in G corresponding to the
3180 global symbol. G's assigned_gotno must contain the index of the
3181 first available global GOT entry in G. VALUE must contain the size
3182 of a GOT entry in bytes. For each global GOT entry that requires a
3183 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4cc11e76 3184 marked as not eligible for lazy resolution through a function
f4416af6
AO
3185 stub. */
3186static int
9719ad41 3187mips_elf_set_global_got_offset (void **entryp, void *p)
f4416af6
AO
3188{
3189 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3190 struct mips_elf_set_global_got_offset_arg *arg
3191 = (struct mips_elf_set_global_got_offset_arg *)p;
3192 struct mips_got_info *g = arg->g;
3193
0f20cc35
DJ
3194 if (g && entry->tls_type != GOT_NORMAL)
3195 arg->needed_relocs +=
3196 mips_tls_got_relocs (arg->info, entry->tls_type,
3197 entry->symndx == -1 ? &entry->d.h->root : NULL);
3198
f4416af6 3199 if (entry->abfd != NULL && entry->symndx == -1
0f20cc35
DJ
3200 && entry->d.h->root.dynindx != -1
3201 && entry->d.h->tls_type == GOT_NORMAL)
f4416af6
AO
3202 {
3203 if (g)
3204 {
3205 BFD_ASSERT (g->global_gotsym == NULL);
3206
3207 entry->gotidx = arg->value * (long) g->assigned_gotno++;
f4416af6
AO
3208 if (arg->info->shared
3209 || (elf_hash_table (arg->info)->dynamic_sections_created
f5385ebf
AM
3210 && entry->d.h->root.def_dynamic
3211 && !entry->d.h->root.def_regular))
f4416af6
AO
3212 ++arg->needed_relocs;
3213 }
3214 else
3215 entry->d.h->root.got.offset = arg->value;
3216 }
3217
3218 return 1;
3219}
3220
0626d451
RS
3221/* Mark any global symbols referenced in the GOT we are iterating over
3222 as inelligible for lazy resolution stubs. */
3223static int
9719ad41 3224mips_elf_set_no_stub (void **entryp, void *p ATTRIBUTE_UNUSED)
0626d451
RS
3225{
3226 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3227
3228 if (entry->abfd != NULL
3229 && entry->symndx == -1
3230 && entry->d.h->root.dynindx != -1)
3231 entry->d.h->no_fn_stub = TRUE;
3232
3233 return 1;
3234}
3235
f4416af6
AO
3236/* Follow indirect and warning hash entries so that each got entry
3237 points to the final symbol definition. P must point to a pointer
3238 to the hash table we're traversing. Since this traversal may
3239 modify the hash table, we set this pointer to NULL to indicate
3240 we've made a potentially-destructive change to the hash table, so
3241 the traversal must be restarted. */
3242static int
9719ad41 3243mips_elf_resolve_final_got_entry (void **entryp, void *p)
f4416af6
AO
3244{
3245 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
3246 htab_t got_entries = *(htab_t *)p;
3247
3248 if (entry->abfd != NULL && entry->symndx == -1)
3249 {
3250 struct mips_elf_link_hash_entry *h = entry->d.h;
3251
3252 while (h->root.root.type == bfd_link_hash_indirect
3253 || h->root.root.type == bfd_link_hash_warning)
3254 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3255
3256 if (entry->d.h == h)
3257 return 1;
143d77c5 3258
f4416af6
AO
3259 entry->d.h = h;
3260
3261 /* If we can't find this entry with the new bfd hash, re-insert
3262 it, and get the traversal restarted. */
3263 if (! htab_find (got_entries, entry))
3264 {
3265 htab_clear_slot (got_entries, entryp);
3266 entryp = htab_find_slot (got_entries, entry, INSERT);
3267 if (! *entryp)
3268 *entryp = entry;
3269 /* Abort the traversal, since the whole table may have
3270 moved, and leave it up to the parent to restart the
3271 process. */
3272 *(htab_t *)p = NULL;
3273 return 0;
3274 }
3275 /* We might want to decrement the global_gotno count, but it's
3276 either too early or too late for that at this point. */
3277 }
143d77c5 3278
f4416af6
AO
3279 return 1;
3280}
3281
3282/* Turn indirect got entries in a got_entries table into their final
3283 locations. */
3284static void
9719ad41 3285mips_elf_resolve_final_got_entries (struct mips_got_info *g)
f4416af6
AO
3286{
3287 htab_t got_entries;
3288
3289 do
3290 {
3291 got_entries = g->got_entries;
3292
3293 htab_traverse (got_entries,
3294 mips_elf_resolve_final_got_entry,
3295 &got_entries);
3296 }
3297 while (got_entries == NULL);
3298}
3299
3300/* Return the offset of an input bfd IBFD's GOT from the beginning of
3301 the primary GOT. */
3302static bfd_vma
9719ad41 3303mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6
AO
3304{
3305 if (g->bfd2got == NULL)
3306 return 0;
3307
3308 g = mips_elf_got_for_ibfd (g, ibfd);
3309 if (! g)
3310 return 0;
3311
3312 BFD_ASSERT (g->next);
3313
3314 g = g->next;
143d77c5 3315
0f20cc35
DJ
3316 return (g->local_gotno + g->global_gotno + g->tls_gotno)
3317 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
3318}
3319
3320/* Turn a single GOT that is too big for 16-bit addressing into
3321 a sequence of GOTs, each one 16-bit addressable. */
3322
3323static bfd_boolean
9719ad41
RS
3324mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
3325 struct mips_got_info *g, asection *got,
3326 bfd_size_type pages)
f4416af6
AO
3327{
3328 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
3329 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
3330 struct mips_got_info *gg;
3331 unsigned int assign;
3332
3333 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
9719ad41 3334 mips_elf_bfd2got_entry_eq, NULL);
f4416af6
AO
3335 if (g->bfd2got == NULL)
3336 return FALSE;
3337
3338 got_per_bfd_arg.bfd2got = g->bfd2got;
3339 got_per_bfd_arg.obfd = abfd;
3340 got_per_bfd_arg.info = info;
3341
3342 /* Count how many GOT entries each input bfd requires, creating a
3343 map from bfd to got info while at that. */
f4416af6
AO
3344 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
3345 if (got_per_bfd_arg.obfd == NULL)
3346 return FALSE;
3347
3348 got_per_bfd_arg.current = NULL;
3349 got_per_bfd_arg.primary = NULL;
3350 /* Taking out PAGES entries is a worst-case estimate. We could
3351 compute the maximum number of pages that each separate input bfd
3352 uses, but it's probably not worth it. */
0a44bf69 3353 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 3354 / MIPS_ELF_GOT_SIZE (abfd))
0a44bf69 3355 - MIPS_RESERVED_GOTNO (info) - pages);
0f20cc35
DJ
3356 /* The number of globals that will be included in the primary GOT.
3357 See the calls to mips_elf_set_global_got_offset below for more
3358 information. */
3359 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
3360
3361 /* Try to merge the GOTs of input bfds together, as long as they
3362 don't seem to exceed the maximum GOT size, choosing one of them
3363 to be the primary GOT. */
3364 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
3365 if (got_per_bfd_arg.obfd == NULL)
3366 return FALSE;
3367
0f20cc35 3368 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6
AO
3369 if (got_per_bfd_arg.primary == NULL)
3370 {
3371 g->next = (struct mips_got_info *)
3372 bfd_alloc (abfd, sizeof (struct mips_got_info));
3373 if (g->next == NULL)
3374 return FALSE;
3375
3376 g->next->global_gotsym = NULL;
3377 g->next->global_gotno = 0;
3378 g->next->local_gotno = 0;
0f20cc35 3379 g->next->tls_gotno = 0;
f4416af6 3380 g->next->assigned_gotno = 0;
0f20cc35
DJ
3381 g->next->tls_assigned_gotno = 0;
3382 g->next->tls_ldm_offset = MINUS_ONE;
f4416af6
AO
3383 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3384 mips_elf_multi_got_entry_eq,
9719ad41 3385 NULL);
f4416af6
AO
3386 if (g->next->got_entries == NULL)
3387 return FALSE;
3388 g->next->bfd2got = NULL;
3389 }
3390 else
3391 g->next = got_per_bfd_arg.primary;
3392 g->next->next = got_per_bfd_arg.current;
3393
3394 /* GG is now the master GOT, and G is the primary GOT. */
3395 gg = g;
3396 g = g->next;
3397
3398 /* Map the output bfd to the primary got. That's what we're going
3399 to use for bfds that use GOT16 or GOT_PAGE relocations that we
3400 didn't mark in check_relocs, and we want a quick way to find it.
3401 We can't just use gg->next because we're going to reverse the
3402 list. */
3403 {
3404 struct mips_elf_bfd2got_hash *bfdgot;
3405 void **bfdgotp;
143d77c5 3406
f4416af6
AO
3407 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
3408 (abfd, sizeof (struct mips_elf_bfd2got_hash));
3409
3410 if (bfdgot == NULL)
3411 return FALSE;
3412
3413 bfdgot->bfd = abfd;
3414 bfdgot->g = g;
3415 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
3416
3417 BFD_ASSERT (*bfdgotp == NULL);
3418 *bfdgotp = bfdgot;
3419 }
3420
3421 /* The IRIX dynamic linker requires every symbol that is referenced
3422 in a dynamic relocation to be present in the primary GOT, so
3423 arrange for them to appear after those that are actually
3424 referenced.
3425
3426 GNU/Linux could very well do without it, but it would slow down
3427 the dynamic linker, since it would have to resolve every dynamic
3428 symbol referenced in other GOTs more than once, without help from
3429 the cache. Also, knowing that every external symbol has a GOT
3430 helps speed up the resolution of local symbols too, so GNU/Linux
3431 follows IRIX's practice.
143d77c5 3432
f4416af6
AO
3433 The number 2 is used by mips_elf_sort_hash_table_f to count
3434 global GOT symbols that are unreferenced in the primary GOT, with
3435 an initial dynamic index computed from gg->assigned_gotno, where
3436 the number of unreferenced global entries in the primary GOT is
3437 preserved. */
3438 if (1)
3439 {
3440 gg->assigned_gotno = gg->global_gotno - g->global_gotno;
3441 g->global_gotno = gg->global_gotno;
3442 set_got_offset_arg.value = 2;
3443 }
3444 else
3445 {
3446 /* This could be used for dynamic linkers that don't optimize
3447 symbol resolution while applying relocations so as to use
3448 primary GOT entries or assuming the symbol is locally-defined.
3449 With this code, we assign lower dynamic indices to global
3450 symbols that are not referenced in the primary GOT, so that
3451 their entries can be omitted. */
3452 gg->assigned_gotno = 0;
3453 set_got_offset_arg.value = -1;
3454 }
3455
3456 /* Reorder dynamic symbols as described above (which behavior
3457 depends on the setting of VALUE). */
3458 set_got_offset_arg.g = NULL;
3459 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
3460 &set_got_offset_arg);
3461 set_got_offset_arg.value = 1;
3462 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
3463 &set_got_offset_arg);
3464 if (! mips_elf_sort_hash_table (info, 1))
3465 return FALSE;
3466
3467 /* Now go through the GOTs assigning them offset ranges.
3468 [assigned_gotno, local_gotno[ will be set to the range of local
3469 entries in each GOT. We can then compute the end of a GOT by
3470 adding local_gotno to global_gotno. We reverse the list and make
3471 it circular since then we'll be able to quickly compute the
3472 beginning of a GOT, by computing the end of its predecessor. To
3473 avoid special cases for the primary GOT, while still preserving
3474 assertions that are valid for both single- and multi-got links,
3475 we arrange for the main got struct to have the right number of
3476 global entries, but set its local_gotno such that the initial
3477 offset of the primary GOT is zero. Remember that the primary GOT
3478 will become the last item in the circular linked list, so it
3479 points back to the master GOT. */
3480 gg->local_gotno = -g->global_gotno;
3481 gg->global_gotno = g->global_gotno;
0f20cc35 3482 gg->tls_gotno = 0;
f4416af6
AO
3483 assign = 0;
3484 gg->next = gg;
3485
3486 do
3487 {
3488 struct mips_got_info *gn;
3489
0a44bf69 3490 assign += MIPS_RESERVED_GOTNO (info);
f4416af6
AO
3491 g->assigned_gotno = assign;
3492 g->local_gotno += assign + pages;
0f20cc35
DJ
3493 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
3494
3495 /* Set up any TLS entries. We always place the TLS entries after
3496 all non-TLS entries. */
3497 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
3498 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
f4416af6
AO
3499
3500 /* Take g out of the direct list, and push it onto the reversed
3501 list that gg points to. */
3502 gn = g->next;
3503 g->next = gg->next;
3504 gg->next = g;
3505 g = gn;
0626d451
RS
3506
3507 /* Mark global symbols in every non-primary GOT as ineligible for
3508 stubs. */
3509 if (g)
3510 htab_traverse (g->got_entries, mips_elf_set_no_stub, NULL);
f4416af6
AO
3511 }
3512 while (g);
3513
eea6121a 3514 got->size = (gg->next->local_gotno
0f20cc35
DJ
3515 + gg->next->global_gotno
3516 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
143d77c5 3517
f4416af6
AO
3518 return TRUE;
3519}
143d77c5 3520
b49e97c9
TS
3521\f
3522/* Returns the first relocation of type r_type found, beginning with
3523 RELOCATION. RELEND is one-past-the-end of the relocation table. */
3524
3525static const Elf_Internal_Rela *
9719ad41
RS
3526mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
3527 const Elf_Internal_Rela *relocation,
3528 const Elf_Internal_Rela *relend)
b49e97c9 3529{
b49e97c9
TS
3530 while (relocation < relend)
3531 {
3532 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type)
3533 return relocation;
3534
3535 ++relocation;
3536 }
3537
3538 /* We didn't find it. */
3539 bfd_set_error (bfd_error_bad_value);
3540 return NULL;
3541}
3542
3543/* Return whether a relocation is against a local symbol. */
3544
b34976b6 3545static bfd_boolean
9719ad41
RS
3546mips_elf_local_relocation_p (bfd *input_bfd,
3547 const Elf_Internal_Rela *relocation,
3548 asection **local_sections,
3549 bfd_boolean check_forced)
b49e97c9
TS
3550{
3551 unsigned long r_symndx;
3552 Elf_Internal_Shdr *symtab_hdr;
3553 struct mips_elf_link_hash_entry *h;
3554 size_t extsymoff;
3555
3556 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3557 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3558 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
3559
3560 if (r_symndx < extsymoff)
b34976b6 3561 return TRUE;
b49e97c9 3562 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 3563 return TRUE;
b49e97c9
TS
3564
3565 if (check_forced)
3566 {
3567 /* Look up the hash table to check whether the symbol
3568 was forced local. */
3569 h = (struct mips_elf_link_hash_entry *)
3570 elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
3571 /* Find the real hash-table entry for this symbol. */
3572 while (h->root.root.type == bfd_link_hash_indirect
3573 || h->root.root.type == bfd_link_hash_warning)
3574 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
f5385ebf 3575 if (h->root.forced_local)
b34976b6 3576 return TRUE;
b49e97c9
TS
3577 }
3578
b34976b6 3579 return FALSE;
b49e97c9
TS
3580}
3581\f
3582/* Sign-extend VALUE, which has the indicated number of BITS. */
3583
a7ebbfdf 3584bfd_vma
9719ad41 3585_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
3586{
3587 if (value & ((bfd_vma) 1 << (bits - 1)))
3588 /* VALUE is negative. */
3589 value |= ((bfd_vma) - 1) << bits;
3590
3591 return value;
3592}
3593
3594/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 3595 range expressible by a signed number with the indicated number of
b49e97c9
TS
3596 BITS. */
3597
b34976b6 3598static bfd_boolean
9719ad41 3599mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
3600{
3601 bfd_signed_vma svalue = (bfd_signed_vma) value;
3602
3603 if (svalue > (1 << (bits - 1)) - 1)
3604 /* The value is too big. */
b34976b6 3605 return TRUE;
b49e97c9
TS
3606 else if (svalue < -(1 << (bits - 1)))
3607 /* The value is too small. */
b34976b6 3608 return TRUE;
b49e97c9
TS
3609
3610 /* All is well. */
b34976b6 3611 return FALSE;
b49e97c9
TS
3612}
3613
3614/* Calculate the %high function. */
3615
3616static bfd_vma
9719ad41 3617mips_elf_high (bfd_vma value)
b49e97c9
TS
3618{
3619 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
3620}
3621
3622/* Calculate the %higher function. */
3623
3624static bfd_vma
9719ad41 3625mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
3626{
3627#ifdef BFD64
3628 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
3629#else
3630 abort ();
c5ae1840 3631 return MINUS_ONE;
b49e97c9
TS
3632#endif
3633}
3634
3635/* Calculate the %highest function. */
3636
3637static bfd_vma
9719ad41 3638mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
3639{
3640#ifdef BFD64
b15e6682 3641 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
3642#else
3643 abort ();
c5ae1840 3644 return MINUS_ONE;
b49e97c9
TS
3645#endif
3646}
3647\f
3648/* Create the .compact_rel section. */
3649
b34976b6 3650static bfd_boolean
9719ad41
RS
3651mips_elf_create_compact_rel_section
3652 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
3653{
3654 flagword flags;
3655 register asection *s;
3656
3657 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
3658 {
3659 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
3660 | SEC_READONLY);
3661
3496cb2a 3662 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
b49e97c9 3663 if (s == NULL
b49e97c9
TS
3664 || ! bfd_set_section_alignment (abfd, s,
3665 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 3666 return FALSE;
b49e97c9 3667
eea6121a 3668 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
3669 }
3670
b34976b6 3671 return TRUE;
b49e97c9
TS
3672}
3673
3674/* Create the .got section to hold the global offset table. */
3675
b34976b6 3676static bfd_boolean
9719ad41
RS
3677mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info,
3678 bfd_boolean maybe_exclude)
b49e97c9
TS
3679{
3680 flagword flags;
3681 register asection *s;
3682 struct elf_link_hash_entry *h;
14a793b2 3683 struct bfd_link_hash_entry *bh;
b49e97c9
TS
3684 struct mips_got_info *g;
3685 bfd_size_type amt;
0a44bf69
RS
3686 struct mips_elf_link_hash_table *htab;
3687
3688 htab = mips_elf_hash_table (info);
b49e97c9
TS
3689
3690 /* This function may be called more than once. */
f4416af6
AO
3691 s = mips_elf_got_section (abfd, TRUE);
3692 if (s)
3693 {
3694 if (! maybe_exclude)
3695 s->flags &= ~SEC_EXCLUDE;
3696 return TRUE;
3697 }
b49e97c9
TS
3698
3699 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3700 | SEC_LINKER_CREATED);
3701
f4416af6
AO
3702 if (maybe_exclude)
3703 flags |= SEC_EXCLUDE;
3704
72b4917c
TS
3705 /* We have to use an alignment of 2**4 here because this is hardcoded
3706 in the function stub generation and in the linker script. */
3496cb2a 3707 s = bfd_make_section_with_flags (abfd, ".got", flags);
b49e97c9 3708 if (s == NULL
72b4917c 3709 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 3710 return FALSE;
b49e97c9
TS
3711
3712 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
3713 linker script because we don't want to define the symbol if we
3714 are not creating a global offset table. */
14a793b2 3715 bh = NULL;
b49e97c9
TS
3716 if (! (_bfd_generic_link_add_one_symbol
3717 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 3718 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 3719 return FALSE;
14a793b2
AM
3720
3721 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
3722 h->non_elf = 0;
3723 h->def_regular = 1;
b49e97c9 3724 h->type = STT_OBJECT;
d329bcd1 3725 elf_hash_table (info)->hgot = h;
b49e97c9
TS
3726
3727 if (info->shared
c152c796 3728 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3729 return FALSE;
b49e97c9 3730
b49e97c9 3731 amt = sizeof (struct mips_got_info);
9719ad41 3732 g = bfd_alloc (abfd, amt);
b49e97c9 3733 if (g == NULL)
b34976b6 3734 return FALSE;
b49e97c9 3735 g->global_gotsym = NULL;
e3d54347 3736 g->global_gotno = 0;
0f20cc35 3737 g->tls_gotno = 0;
0a44bf69
RS
3738 g->local_gotno = MIPS_RESERVED_GOTNO (info);
3739 g->assigned_gotno = MIPS_RESERVED_GOTNO (info);
f4416af6
AO
3740 g->bfd2got = NULL;
3741 g->next = NULL;
0f20cc35 3742 g->tls_ldm_offset = MINUS_ONE;
b15e6682 3743 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
9719ad41 3744 mips_elf_got_entry_eq, NULL);
b15e6682
AO
3745 if (g->got_entries == NULL)
3746 return FALSE;
f0abc2a1
AM
3747 mips_elf_section_data (s)->u.got_info = g;
3748 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
3749 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3750
0a44bf69
RS
3751 /* VxWorks also needs a .got.plt section. */
3752 if (htab->is_vxworks)
3753 {
3754 s = bfd_make_section_with_flags (abfd, ".got.plt",
3755 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3756 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3757 if (s == NULL || !bfd_set_section_alignment (abfd, s, 4))
3758 return FALSE;
3759
3760 htab->sgotplt = s;
3761 }
b34976b6 3762 return TRUE;
b49e97c9 3763}
b49e97c9 3764\f
0a44bf69
RS
3765/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
3766 __GOTT_INDEX__ symbols. These symbols are only special for
3767 shared objects; they are not used in executables. */
3768
3769static bfd_boolean
3770is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
3771{
3772 return (mips_elf_hash_table (info)->is_vxworks
3773 && info->shared
3774 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
3775 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
3776}
3777\f
b49e97c9
TS
3778/* Calculate the value produced by the RELOCATION (which comes from
3779 the INPUT_BFD). The ADDEND is the addend to use for this
3780 RELOCATION; RELOCATION->R_ADDEND is ignored.
3781
3782 The result of the relocation calculation is stored in VALUEP.
3783 REQUIRE_JALXP indicates whether or not the opcode used with this
3784 relocation must be JALX.
3785
3786 This function returns bfd_reloc_continue if the caller need take no
3787 further action regarding this relocation, bfd_reloc_notsupported if
3788 something goes dramatically wrong, bfd_reloc_overflow if an
3789 overflow occurs, and bfd_reloc_ok to indicate success. */
3790
3791static bfd_reloc_status_type
9719ad41
RS
3792mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
3793 asection *input_section,
3794 struct bfd_link_info *info,
3795 const Elf_Internal_Rela *relocation,
3796 bfd_vma addend, reloc_howto_type *howto,
3797 Elf_Internal_Sym *local_syms,
3798 asection **local_sections, bfd_vma *valuep,
3799 const char **namep, bfd_boolean *require_jalxp,
3800 bfd_boolean save_addend)
b49e97c9
TS
3801{
3802 /* The eventual value we will return. */
3803 bfd_vma value;
3804 /* The address of the symbol against which the relocation is
3805 occurring. */
3806 bfd_vma symbol = 0;
3807 /* The final GP value to be used for the relocatable, executable, or
3808 shared object file being produced. */
3809 bfd_vma gp = MINUS_ONE;
3810 /* The place (section offset or address) of the storage unit being
3811 relocated. */
3812 bfd_vma p;
3813 /* The value of GP used to create the relocatable object. */
3814 bfd_vma gp0 = MINUS_ONE;
3815 /* The offset into the global offset table at which the address of
3816 the relocation entry symbol, adjusted by the addend, resides
3817 during execution. */
3818 bfd_vma g = MINUS_ONE;
3819 /* The section in which the symbol referenced by the relocation is
3820 located. */
3821 asection *sec = NULL;
3822 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 3823 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 3824 symbol. */
b34976b6
AM
3825 bfd_boolean local_p, was_local_p;
3826 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
3827 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
3828 /* TRUE if the symbol referred to by this relocation is
3829 "__gnu_local_gp". */
3830 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
3831 Elf_Internal_Shdr *symtab_hdr;
3832 size_t extsymoff;
3833 unsigned long r_symndx;
3834 int r_type;
b34976b6 3835 /* TRUE if overflow occurred during the calculation of the
b49e97c9 3836 relocation value. */
b34976b6
AM
3837 bfd_boolean overflowed_p;
3838 /* TRUE if this relocation refers to a MIPS16 function. */
3839 bfd_boolean target_is_16_bit_code_p = FALSE;
0a44bf69
RS
3840 struct mips_elf_link_hash_table *htab;
3841 bfd *dynobj;
3842
3843 dynobj = elf_hash_table (info)->dynobj;
3844 htab = mips_elf_hash_table (info);
b49e97c9
TS
3845
3846 /* Parse the relocation. */
3847 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
3848 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
3849 p = (input_section->output_section->vma
3850 + input_section->output_offset
3851 + relocation->r_offset);
3852
3853 /* Assume that there will be no overflow. */
b34976b6 3854 overflowed_p = FALSE;
b49e97c9
TS
3855
3856 /* Figure out whether or not the symbol is local, and get the offset
3857 used in the array of hash table entries. */
3858 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3859 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 3860 local_sections, FALSE);
bce03d3d 3861 was_local_p = local_p;
b49e97c9
TS
3862 if (! elf_bad_symtab (input_bfd))
3863 extsymoff = symtab_hdr->sh_info;
3864 else
3865 {
3866 /* The symbol table does not follow the rule that local symbols
3867 must come before globals. */
3868 extsymoff = 0;
3869 }
3870
3871 /* Figure out the value of the symbol. */
3872 if (local_p)
3873 {
3874 Elf_Internal_Sym *sym;
3875
3876 sym = local_syms + r_symndx;
3877 sec = local_sections[r_symndx];
3878
3879 symbol = sec->output_section->vma + sec->output_offset;
d4df96e6
L
3880 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
3881 || (sec->flags & SEC_MERGE))
b49e97c9 3882 symbol += sym->st_value;
d4df96e6
L
3883 if ((sec->flags & SEC_MERGE)
3884 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3885 {
3886 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
3887 addend -= symbol;
3888 addend += sec->output_section->vma + sec->output_offset;
3889 }
b49e97c9
TS
3890
3891 /* MIPS16 text labels should be treated as odd. */
3892 if (sym->st_other == STO_MIPS16)
3893 ++symbol;
3894
3895 /* Record the name of this symbol, for our caller. */
3896 *namep = bfd_elf_string_from_elf_section (input_bfd,
3897 symtab_hdr->sh_link,
3898 sym->st_name);
3899 if (*namep == '\0')
3900 *namep = bfd_section_name (input_bfd, sec);
3901
3902 target_is_16_bit_code_p = (sym->st_other == STO_MIPS16);
3903 }
3904 else
3905 {
560e09e9
NC
3906 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
3907
b49e97c9
TS
3908 /* For global symbols we look up the symbol in the hash-table. */
3909 h = ((struct mips_elf_link_hash_entry *)
3910 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
3911 /* Find the real hash-table entry for this symbol. */
3912 while (h->root.root.type == bfd_link_hash_indirect
3913 || h->root.root.type == bfd_link_hash_warning)
3914 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3915
3916 /* Record the name of this symbol, for our caller. */
3917 *namep = h->root.root.root.string;
3918
3919 /* See if this is the special _gp_disp symbol. Note that such a
3920 symbol must always be a global symbol. */
560e09e9 3921 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
3922 && ! NEWABI_P (input_bfd))
3923 {
3924 /* Relocations against _gp_disp are permitted only with
3925 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
d6f16593
MR
3926 if (r_type != R_MIPS_HI16 && r_type != R_MIPS_LO16
3927 && r_type != R_MIPS16_HI16 && r_type != R_MIPS16_LO16)
b49e97c9
TS
3928 return bfd_reloc_notsupported;
3929
b34976b6 3930 gp_disp_p = TRUE;
b49e97c9 3931 }
bbe506e8
TS
3932 /* See if this is the special _gp symbol. Note that such a
3933 symbol must always be a global symbol. */
3934 else if (strcmp (*namep, "__gnu_local_gp") == 0)
3935 gnu_local_gp_p = TRUE;
3936
3937
b49e97c9
TS
3938 /* If this symbol is defined, calculate its address. Note that
3939 _gp_disp is a magic symbol, always implicitly defined by the
3940 linker, so it's inappropriate to check to see whether or not
3941 its defined. */
3942 else if ((h->root.root.type == bfd_link_hash_defined
3943 || h->root.root.type == bfd_link_hash_defweak)
3944 && h->root.root.u.def.section)
3945 {
3946 sec = h->root.root.u.def.section;
3947 if (sec->output_section)
3948 symbol = (h->root.root.u.def.value
3949 + sec->output_section->vma
3950 + sec->output_offset);
3951 else
3952 symbol = h->root.root.u.def.value;
3953 }
3954 else if (h->root.root.type == bfd_link_hash_undefweak)
3955 /* We allow relocations against undefined weak symbols, giving
3956 it the value zero, so that you can undefined weak functions
3957 and check to see if they exist by looking at their
3958 addresses. */
3959 symbol = 0;
59c2e50f 3960 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
3961 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
3962 symbol = 0;
a4d0f181
TS
3963 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
3964 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
3965 {
3966 /* If this is a dynamic link, we should have created a
3967 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
3968 in in _bfd_mips_elf_create_dynamic_sections.
3969 Otherwise, we should define the symbol with a value of 0.
3970 FIXME: It should probably get into the symbol table
3971 somehow as well. */
3972 BFD_ASSERT (! info->shared);
3973 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
3974 symbol = 0;
3975 }
5e2b0d47
NC
3976 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
3977 {
3978 /* This is an optional symbol - an Irix specific extension to the
3979 ELF spec. Ignore it for now.
3980 XXX - FIXME - there is more to the spec for OPTIONAL symbols
3981 than simply ignoring them, but we do not handle this for now.
3982 For information see the "64-bit ELF Object File Specification"
3983 which is available from here:
3984 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
3985 symbol = 0;
3986 }
b49e97c9
TS
3987 else
3988 {
3989 if (! ((*info->callbacks->undefined_symbol)
3990 (info, h->root.root.root.string, input_bfd,
3991 input_section, relocation->r_offset,
59c2e50f
L
3992 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
3993 || ELF_ST_VISIBILITY (h->root.other))))
b49e97c9
TS
3994 return bfd_reloc_undefined;
3995 symbol = 0;
3996 }
3997
3998 target_is_16_bit_code_p = (h->root.other == STO_MIPS16);
3999 }
4000
4001 /* If this is a 32- or 64-bit call to a 16-bit function with a stub, we
4002 need to redirect the call to the stub, unless we're already *in*
4003 a stub. */
1049f94e 4004 if (r_type != R_MIPS16_26 && !info->relocatable
b49e97c9
TS
4005 && ((h != NULL && h->fn_stub != NULL)
4006 || (local_p && elf_tdata (input_bfd)->local_stubs != NULL
4007 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4008 && !mips_elf_stub_section_p (input_bfd, input_section))
4009 {
4010 /* This is a 32- or 64-bit call to a 16-bit function. We should
4011 have already noticed that we were going to need the
4012 stub. */
4013 if (local_p)
4014 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
4015 else
4016 {
4017 BFD_ASSERT (h->need_fn_stub);
4018 sec = h->fn_stub;
4019 }
4020
4021 symbol = sec->output_section->vma + sec->output_offset;
4022 }
4023 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
4024 need to redirect the call to the stub. */
1049f94e 4025 else if (r_type == R_MIPS16_26 && !info->relocatable
b49e97c9
TS
4026 && h != NULL
4027 && (h->call_stub != NULL || h->call_fp_stub != NULL)
4028 && !target_is_16_bit_code_p)
4029 {
4030 /* If both call_stub and call_fp_stub are defined, we can figure
4031 out which one to use by seeing which one appears in the input
4032 file. */
4033 if (h->call_stub != NULL && h->call_fp_stub != NULL)
4034 {
4035 asection *o;
4036
4037 sec = NULL;
4038 for (o = input_bfd->sections; o != NULL; o = o->next)
4039 {
4040 if (strncmp (bfd_get_section_name (input_bfd, o),
4041 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4042 {
4043 sec = h->call_fp_stub;
4044 break;
4045 }
4046 }
4047 if (sec == NULL)
4048 sec = h->call_stub;
4049 }
4050 else if (h->call_stub != NULL)
4051 sec = h->call_stub;
4052 else
4053 sec = h->call_fp_stub;
4054
eea6121a 4055 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
4056 symbol = sec->output_section->vma + sec->output_offset;
4057 }
4058
4059 /* Calls from 16-bit code to 32-bit code and vice versa require the
4060 special jalx instruction. */
1049f94e 4061 *require_jalxp = (!info->relocatable
b49e97c9
TS
4062 && (((r_type == R_MIPS16_26) && !target_is_16_bit_code_p)
4063 || ((r_type == R_MIPS_26) && target_is_16_bit_code_p)));
4064
4065 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 4066 local_sections, TRUE);
b49e97c9
TS
4067
4068 /* If we haven't already determined the GOT offset, or the GP value,
4069 and we're going to need it, get it now. */
4070 switch (r_type)
4071 {
0fdc1bf1 4072 case R_MIPS_GOT_PAGE:
93a2b7ae 4073 case R_MIPS_GOT_OFST:
d25aed71
RS
4074 /* We need to decay to GOT_DISP/addend if the symbol doesn't
4075 bind locally. */
4076 local_p = local_p || _bfd_elf_symbol_refs_local_p (&h->root, info, 1);
93a2b7ae 4077 if (local_p || r_type == R_MIPS_GOT_OFST)
0fdc1bf1
AO
4078 break;
4079 /* Fall through. */
4080
b49e97c9
TS
4081 case R_MIPS_CALL16:
4082 case R_MIPS_GOT16:
4083 case R_MIPS_GOT_DISP:
4084 case R_MIPS_GOT_HI16:
4085 case R_MIPS_CALL_HI16:
4086 case R_MIPS_GOT_LO16:
4087 case R_MIPS_CALL_LO16:
0f20cc35
DJ
4088 case R_MIPS_TLS_GD:
4089 case R_MIPS_TLS_GOTTPREL:
4090 case R_MIPS_TLS_LDM:
b49e97c9 4091 /* Find the index into the GOT where this value is located. */
0f20cc35
DJ
4092 if (r_type == R_MIPS_TLS_LDM)
4093 {
0a44bf69
RS
4094 g = mips_elf_local_got_index (abfd, input_bfd, info,
4095 sec, 0, 0, NULL, r_type);
0f20cc35
DJ
4096 if (g == MINUS_ONE)
4097 return bfd_reloc_outofrange;
4098 }
4099 else if (!local_p)
b49e97c9 4100 {
0a44bf69
RS
4101 /* On VxWorks, CALL relocations should refer to the .got.plt
4102 entry, which is initialized to point at the PLT stub. */
4103 if (htab->is_vxworks
4104 && (r_type == R_MIPS_CALL_HI16
4105 || r_type == R_MIPS_CALL_LO16
4106 || r_type == R_MIPS_CALL16))
4107 {
4108 BFD_ASSERT (addend == 0);
4109 BFD_ASSERT (h->root.needs_plt);
4110 g = mips_elf_gotplt_index (info, &h->root);
4111 }
4112 else
b49e97c9 4113 {
0a44bf69
RS
4114 /* GOT_PAGE may take a non-zero addend, that is ignored in a
4115 GOT_PAGE relocation that decays to GOT_DISP because the
4116 symbol turns out to be global. The addend is then added
4117 as GOT_OFST. */
4118 BFD_ASSERT (addend == 0 || r_type == R_MIPS_GOT_PAGE);
4119 g = mips_elf_global_got_index (dynobj, input_bfd,
4120 &h->root, r_type, info);
4121 if (h->tls_type == GOT_NORMAL
4122 && (! elf_hash_table(info)->dynamic_sections_created
4123 || (info->shared
4124 && (info->symbolic || h->root.forced_local)
4125 && h->root.def_regular)))
4126 {
4127 /* This is a static link or a -Bsymbolic link. The
4128 symbol is defined locally, or was forced to be local.
4129 We must initialize this entry in the GOT. */
4130 asection *sgot = mips_elf_got_section (dynobj, FALSE);
4131 MIPS_ELF_PUT_WORD (dynobj, symbol, sgot->contents + g);
4132 }
b49e97c9
TS
4133 }
4134 }
0a44bf69
RS
4135 else if (!htab->is_vxworks
4136 && (r_type == R_MIPS_CALL16 || (r_type == R_MIPS_GOT16)))
4137 /* The calculation below does not involve "g". */
b49e97c9
TS
4138 break;
4139 else
4140 {
0a44bf69
RS
4141 g = mips_elf_local_got_index (abfd, input_bfd, info, sec,
4142 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
4143 if (g == MINUS_ONE)
4144 return bfd_reloc_outofrange;
4145 }
4146
4147 /* Convert GOT indices to actual offsets. */
0a44bf69 4148 g = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, g);
b49e97c9
TS
4149 break;
4150
4151 case R_MIPS_HI16:
4152 case R_MIPS_LO16:
b49e97c9
TS
4153 case R_MIPS_GPREL16:
4154 case R_MIPS_GPREL32:
4155 case R_MIPS_LITERAL:
d6f16593
MR
4156 case R_MIPS16_HI16:
4157 case R_MIPS16_LO16:
4158 case R_MIPS16_GPREL:
b49e97c9
TS
4159 gp0 = _bfd_get_gp_value (input_bfd);
4160 gp = _bfd_get_gp_value (abfd);
0a44bf69
RS
4161 if (dynobj)
4162 gp += mips_elf_adjust_gp (abfd, mips_elf_got_info (dynobj, NULL),
f4416af6 4163 input_bfd);
b49e97c9
TS
4164 break;
4165
4166 default:
4167 break;
4168 }
4169
bbe506e8
TS
4170 if (gnu_local_gp_p)
4171 symbol = gp;
86324f90 4172
0a44bf69
RS
4173 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
4174 symbols are resolved by the loader. Add them to .rela.dyn. */
4175 if (h != NULL && is_gott_symbol (info, &h->root))
4176 {
4177 Elf_Internal_Rela outrel;
4178 bfd_byte *loc;
4179 asection *s;
4180
4181 s = mips_elf_rel_dyn_section (info, FALSE);
4182 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4183
4184 outrel.r_offset = (input_section->output_section->vma
4185 + input_section->output_offset
4186 + relocation->r_offset);
4187 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
4188 outrel.r_addend = addend;
4189 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
4190 *valuep = 0;
4191 return bfd_reloc_ok;
4192 }
4193
b49e97c9
TS
4194 /* Figure out what kind of relocation is being performed. */
4195 switch (r_type)
4196 {
4197 case R_MIPS_NONE:
4198 return bfd_reloc_continue;
4199
4200 case R_MIPS_16:
a7ebbfdf 4201 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
b49e97c9
TS
4202 overflowed_p = mips_elf_overflow_p (value, 16);
4203 break;
4204
4205 case R_MIPS_32:
4206 case R_MIPS_REL32:
4207 case R_MIPS_64:
4208 if ((info->shared
0a44bf69
RS
4209 || (!htab->is_vxworks
4210 && htab->root.dynamic_sections_created
b49e97c9 4211 && h != NULL
f5385ebf
AM
4212 && h->root.def_dynamic
4213 && !h->root.def_regular))
b49e97c9
TS
4214 && r_symndx != 0
4215 && (input_section->flags & SEC_ALLOC) != 0)
4216 {
4217 /* If we're creating a shared library, or this relocation is
4218 against a symbol in a shared library, then we can't know
4219 where the symbol will end up. So, we create a relocation
4220 record in the output, and leave the job up to the dynamic
0a44bf69
RS
4221 linker.
4222
4223 In VxWorks executables, references to external symbols
4224 are handled using copy relocs or PLT stubs, so there's
4225 no need to add a dynamic relocation here. */
b49e97c9
TS
4226 value = addend;
4227 if (!mips_elf_create_dynamic_relocation (abfd,
4228 info,
4229 relocation,
4230 h,
4231 sec,
4232 symbol,
4233 &value,
4234 input_section))
4235 return bfd_reloc_undefined;
4236 }
4237 else
4238 {
4239 if (r_type != R_MIPS_REL32)
4240 value = symbol + addend;
4241 else
4242 value = addend;
4243 }
4244 value &= howto->dst_mask;
092dcd75
CD
4245 break;
4246
4247 case R_MIPS_PC32:
4248 value = symbol + addend - p;
4249 value &= howto->dst_mask;
b49e97c9
TS
4250 break;
4251
b49e97c9
TS
4252 case R_MIPS16_26:
4253 /* The calculation for R_MIPS16_26 is just the same as for an
4254 R_MIPS_26. It's only the storage of the relocated field into
4255 the output file that's different. That's handled in
4256 mips_elf_perform_relocation. So, we just fall through to the
4257 R_MIPS_26 case here. */
4258 case R_MIPS_26:
4259 if (local_p)
30ac9238 4260 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
b49e97c9 4261 else
728b2f21
ILT
4262 {
4263 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
c314987d
RS
4264 if (h->root.root.type != bfd_link_hash_undefweak)
4265 overflowed_p = (value >> 26) != ((p + 4) >> 28);
728b2f21 4266 }
b49e97c9
TS
4267 value &= howto->dst_mask;
4268 break;
4269
0f20cc35
DJ
4270 case R_MIPS_TLS_DTPREL_HI16:
4271 value = (mips_elf_high (addend + symbol - dtprel_base (info))
4272 & howto->dst_mask);
4273 break;
4274
4275 case R_MIPS_TLS_DTPREL_LO16:
4276 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
4277 break;
4278
4279 case R_MIPS_TLS_TPREL_HI16:
4280 value = (mips_elf_high (addend + symbol - tprel_base (info))
4281 & howto->dst_mask);
4282 break;
4283
4284 case R_MIPS_TLS_TPREL_LO16:
4285 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
4286 break;
4287
b49e97c9 4288 case R_MIPS_HI16:
d6f16593 4289 case R_MIPS16_HI16:
b49e97c9
TS
4290 if (!gp_disp_p)
4291 {
4292 value = mips_elf_high (addend + symbol);
4293 value &= howto->dst_mask;
4294 }
4295 else
4296 {
d6f16593
MR
4297 /* For MIPS16 ABI code we generate this sequence
4298 0: li $v0,%hi(_gp_disp)
4299 4: addiupc $v1,%lo(_gp_disp)
4300 8: sll $v0,16
4301 12: addu $v0,$v1
4302 14: move $gp,$v0
4303 So the offsets of hi and lo relocs are the same, but the
4304 $pc is four higher than $t9 would be, so reduce
4305 both reloc addends by 4. */
4306 if (r_type == R_MIPS16_HI16)
4307 value = mips_elf_high (addend + gp - p - 4);
4308 else
4309 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
4310 overflowed_p = mips_elf_overflow_p (value, 16);
4311 }
4312 break;
4313
4314 case R_MIPS_LO16:
d6f16593 4315 case R_MIPS16_LO16:
b49e97c9
TS
4316 if (!gp_disp_p)
4317 value = (symbol + addend) & howto->dst_mask;
4318 else
4319 {
d6f16593
MR
4320 /* See the comment for R_MIPS16_HI16 above for the reason
4321 for this conditional. */
4322 if (r_type == R_MIPS16_LO16)
4323 value = addend + gp - p;
4324 else
4325 value = addend + gp - p + 4;
b49e97c9 4326 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 4327 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
4328 _gp_disp are normally generated from the .cpload
4329 pseudo-op. It generates code that normally looks like
4330 this:
4331
4332 lui $gp,%hi(_gp_disp)
4333 addiu $gp,$gp,%lo(_gp_disp)
4334 addu $gp,$gp,$t9
4335
4336 Here $t9 holds the address of the function being called,
4337 as required by the MIPS ELF ABI. The R_MIPS_LO16
4338 relocation can easily overflow in this situation, but the
4339 R_MIPS_HI16 relocation will handle the overflow.
4340 Therefore, we consider this a bug in the MIPS ABI, and do
4341 not check for overflow here. */
4342 }
4343 break;
4344
4345 case R_MIPS_LITERAL:
4346 /* Because we don't merge literal sections, we can handle this
4347 just like R_MIPS_GPREL16. In the long run, we should merge
4348 shared literals, and then we will need to additional work
4349 here. */
4350
4351 /* Fall through. */
4352
4353 case R_MIPS16_GPREL:
4354 /* The R_MIPS16_GPREL performs the same calculation as
4355 R_MIPS_GPREL16, but stores the relocated bits in a different
4356 order. We don't need to do anything special here; the
4357 differences are handled in mips_elf_perform_relocation. */
4358 case R_MIPS_GPREL16:
bce03d3d
AO
4359 /* Only sign-extend the addend if it was extracted from the
4360 instruction. If the addend was separate, leave it alone,
4361 otherwise we may lose significant bits. */
4362 if (howto->partial_inplace)
a7ebbfdf 4363 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
4364 value = symbol + addend - gp;
4365 /* If the symbol was local, any earlier relocatable links will
4366 have adjusted its addend with the gp offset, so compensate
4367 for that now. Don't do it for symbols forced local in this
4368 link, though, since they won't have had the gp offset applied
4369 to them before. */
4370 if (was_local_p)
4371 value += gp0;
b49e97c9
TS
4372 overflowed_p = mips_elf_overflow_p (value, 16);
4373 break;
4374
4375 case R_MIPS_GOT16:
4376 case R_MIPS_CALL16:
0a44bf69
RS
4377 /* VxWorks does not have separate local and global semantics for
4378 R_MIPS_GOT16; every relocation evaluates to "G". */
4379 if (!htab->is_vxworks && local_p)
b49e97c9 4380 {
b34976b6 4381 bfd_boolean forced;
b49e97c9 4382
b49e97c9 4383 forced = ! mips_elf_local_relocation_p (input_bfd, relocation,
b34976b6 4384 local_sections, FALSE);
0a44bf69 4385 value = mips_elf_got16_entry (abfd, input_bfd, info, sec,
f4416af6 4386 symbol + addend, forced);
b49e97c9
TS
4387 if (value == MINUS_ONE)
4388 return bfd_reloc_outofrange;
4389 value
0a44bf69 4390 = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
b49e97c9
TS
4391 overflowed_p = mips_elf_overflow_p (value, 16);
4392 break;
4393 }
4394
4395 /* Fall through. */
4396
0f20cc35
DJ
4397 case R_MIPS_TLS_GD:
4398 case R_MIPS_TLS_GOTTPREL:
4399 case R_MIPS_TLS_LDM:
b49e97c9 4400 case R_MIPS_GOT_DISP:
0fdc1bf1 4401 got_disp:
b49e97c9
TS
4402 value = g;
4403 overflowed_p = mips_elf_overflow_p (value, 16);
4404 break;
4405
4406 case R_MIPS_GPREL32:
bce03d3d
AO
4407 value = (addend + symbol + gp0 - gp);
4408 if (!save_addend)
4409 value &= howto->dst_mask;
b49e97c9
TS
4410 break;
4411
4412 case R_MIPS_PC16:
bad36eac
DJ
4413 case R_MIPS_GNU_REL16_S2:
4414 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
4415 overflowed_p = mips_elf_overflow_p (value, 18);
4416 value = (value >> 2) & howto->dst_mask;
b49e97c9
TS
4417 break;
4418
4419 case R_MIPS_GOT_HI16:
4420 case R_MIPS_CALL_HI16:
4421 /* We're allowed to handle these two relocations identically.
4422 The dynamic linker is allowed to handle the CALL relocations
4423 differently by creating a lazy evaluation stub. */
4424 value = g;
4425 value = mips_elf_high (value);
4426 value &= howto->dst_mask;
4427 break;
4428
4429 case R_MIPS_GOT_LO16:
4430 case R_MIPS_CALL_LO16:
4431 value = g & howto->dst_mask;
4432 break;
4433
4434 case R_MIPS_GOT_PAGE:
0fdc1bf1
AO
4435 /* GOT_PAGE relocations that reference non-local symbols decay
4436 to GOT_DISP. The corresponding GOT_OFST relocation decays to
4437 0. */
93a2b7ae 4438 if (! local_p)
0fdc1bf1 4439 goto got_disp;
0a44bf69
RS
4440 value = mips_elf_got_page (abfd, input_bfd, info, sec,
4441 symbol + addend, NULL);
b49e97c9
TS
4442 if (value == MINUS_ONE)
4443 return bfd_reloc_outofrange;
0a44bf69 4444 value = mips_elf_got_offset_from_index (dynobj, abfd, input_bfd, value);
b49e97c9
TS
4445 overflowed_p = mips_elf_overflow_p (value, 16);
4446 break;
4447
4448 case R_MIPS_GOT_OFST:
93a2b7ae 4449 if (local_p)
0a44bf69
RS
4450 mips_elf_got_page (abfd, input_bfd, info, sec,
4451 symbol + addend, &value);
0fdc1bf1
AO
4452 else
4453 value = addend;
b49e97c9
TS
4454 overflowed_p = mips_elf_overflow_p (value, 16);
4455 break;
4456
4457 case R_MIPS_SUB:
4458 value = symbol - addend;
4459 value &= howto->dst_mask;
4460 break;
4461
4462 case R_MIPS_HIGHER:
4463 value = mips_elf_higher (addend + symbol);
4464 value &= howto->dst_mask;
4465 break;
4466
4467 case R_MIPS_HIGHEST:
4468 value = mips_elf_highest (addend + symbol);
4469 value &= howto->dst_mask;
4470 break;
4471
4472 case R_MIPS_SCN_DISP:
4473 value = symbol + addend - sec->output_offset;
4474 value &= howto->dst_mask;
4475 break;
4476
b49e97c9 4477 case R_MIPS_JALR:
1367d393
ILT
4478 /* This relocation is only a hint. In some cases, we optimize
4479 it into a bal instruction. But we don't try to optimize
4480 branches to the PLT; that will wind up wasting time. */
4481 if (h != NULL && h->root.plt.offset != (bfd_vma) -1)
4482 return bfd_reloc_continue;
4483 value = symbol + addend;
4484 break;
b49e97c9 4485
1367d393 4486 case R_MIPS_PJUMP:
b49e97c9
TS
4487 case R_MIPS_GNU_VTINHERIT:
4488 case R_MIPS_GNU_VTENTRY:
4489 /* We don't do anything with these at present. */
4490 return bfd_reloc_continue;
4491
4492 default:
4493 /* An unrecognized relocation type. */
4494 return bfd_reloc_notsupported;
4495 }
4496
4497 /* Store the VALUE for our caller. */
4498 *valuep = value;
4499 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
4500}
4501
4502/* Obtain the field relocated by RELOCATION. */
4503
4504static bfd_vma
9719ad41
RS
4505mips_elf_obtain_contents (reloc_howto_type *howto,
4506 const Elf_Internal_Rela *relocation,
4507 bfd *input_bfd, bfd_byte *contents)
b49e97c9
TS
4508{
4509 bfd_vma x;
4510 bfd_byte *location = contents + relocation->r_offset;
4511
4512 /* Obtain the bytes. */
4513 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
4514
b49e97c9
TS
4515 return x;
4516}
4517
4518/* It has been determined that the result of the RELOCATION is the
4519 VALUE. Use HOWTO to place VALUE into the output file at the
4520 appropriate position. The SECTION is the section to which the
b34976b6 4521 relocation applies. If REQUIRE_JALX is TRUE, then the opcode used
b49e97c9
TS
4522 for the relocation must be either JAL or JALX, and it is
4523 unconditionally converted to JALX.
4524
b34976b6 4525 Returns FALSE if anything goes wrong. */
b49e97c9 4526
b34976b6 4527static bfd_boolean
9719ad41
RS
4528mips_elf_perform_relocation (struct bfd_link_info *info,
4529 reloc_howto_type *howto,
4530 const Elf_Internal_Rela *relocation,
4531 bfd_vma value, bfd *input_bfd,
4532 asection *input_section, bfd_byte *contents,
4533 bfd_boolean require_jalx)
b49e97c9
TS
4534{
4535 bfd_vma x;
4536 bfd_byte *location;
4537 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4538
4539 /* Figure out where the relocation is occurring. */
4540 location = contents + relocation->r_offset;
4541
d6f16593
MR
4542 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
4543
b49e97c9
TS
4544 /* Obtain the current value. */
4545 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
4546
4547 /* Clear the field we are setting. */
4548 x &= ~howto->dst_mask;
4549
b49e97c9
TS
4550 /* Set the field. */
4551 x |= (value & howto->dst_mask);
4552
4553 /* If required, turn JAL into JALX. */
4554 if (require_jalx)
4555 {
b34976b6 4556 bfd_boolean ok;
b49e97c9
TS
4557 bfd_vma opcode = x >> 26;
4558 bfd_vma jalx_opcode;
4559
4560 /* Check to see if the opcode is already JAL or JALX. */
4561 if (r_type == R_MIPS16_26)
4562 {
4563 ok = ((opcode == 0x6) || (opcode == 0x7));
4564 jalx_opcode = 0x7;
4565 }
4566 else
4567 {
4568 ok = ((opcode == 0x3) || (opcode == 0x1d));
4569 jalx_opcode = 0x1d;
4570 }
4571
4572 /* If the opcode is not JAL or JALX, there's a problem. */
4573 if (!ok)
4574 {
4575 (*_bfd_error_handler)
d003868e
AM
4576 (_("%B: %A+0x%lx: jump to stub routine which is not jal"),
4577 input_bfd,
4578 input_section,
b49e97c9
TS
4579 (unsigned long) relocation->r_offset);
4580 bfd_set_error (bfd_error_bad_value);
b34976b6 4581 return FALSE;
b49e97c9
TS
4582 }
4583
4584 /* Make this the JALX opcode. */
4585 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
4586 }
4587
1367d393
ILT
4588 /* On the RM9000, bal is faster than jal, because bal uses branch
4589 prediction hardware. If we are linking for the RM9000, and we
4590 see jal, and bal fits, use it instead. Note that this
4591 transformation should be safe for all architectures. */
4592 if (bfd_get_mach (input_bfd) == bfd_mach_mips9000
4593 && !info->relocatable
4594 && !require_jalx
4595 && ((r_type == R_MIPS_26 && (x >> 26) == 0x3) /* jal addr */
4596 || (r_type == R_MIPS_JALR && x == 0x0320f809))) /* jalr t9 */
4597 {
4598 bfd_vma addr;
4599 bfd_vma dest;
4600 bfd_signed_vma off;
4601
4602 addr = (input_section->output_section->vma
4603 + input_section->output_offset
4604 + relocation->r_offset
4605 + 4);
4606 if (r_type == R_MIPS_26)
4607 dest = (value << 2) | ((addr >> 28) << 28);
4608 else
4609 dest = value;
4610 off = dest - addr;
4611 if (off <= 0x1ffff && off >= -0x20000)
4612 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
4613 }
4614
b49e97c9
TS
4615 /* Put the value into the output. */
4616 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
d6f16593
MR
4617
4618 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
4619 location);
4620
b34976b6 4621 return TRUE;
b49e97c9
TS
4622}
4623
b34976b6 4624/* Returns TRUE if SECTION is a MIPS16 stub section. */
b49e97c9 4625
b34976b6 4626static bfd_boolean
9719ad41 4627mips_elf_stub_section_p (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
b49e97c9
TS
4628{
4629 const char *name = bfd_get_section_name (abfd, section);
4630
4631 return (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0
4632 || strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4633 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0);
4634}
4635\f
0a44bf69 4636/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
b49e97c9
TS
4637
4638static void
0a44bf69
RS
4639mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4640 unsigned int n)
b49e97c9
TS
4641{
4642 asection *s;
0a44bf69 4643 struct mips_elf_link_hash_table *htab;
b49e97c9 4644
0a44bf69
RS
4645 htab = mips_elf_hash_table (info);
4646 s = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
4647 BFD_ASSERT (s != NULL);
4648
0a44bf69
RS
4649 if (htab->is_vxworks)
4650 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4651 else
b49e97c9 4652 {
0a44bf69
RS
4653 if (s->size == 0)
4654 {
4655 /* Make room for a null element. */
4656 s->size += MIPS_ELF_REL_SIZE (abfd);
4657 ++s->reloc_count;
4658 }
4659 s->size += n * MIPS_ELF_REL_SIZE (abfd);
b49e97c9 4660 }
b49e97c9
TS
4661}
4662
4663/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
4664 is the original relocation, which is now being transformed into a
4665 dynamic relocation. The ADDENDP is adjusted if necessary; the
4666 caller should store the result in place of the original addend. */
4667
b34976b6 4668static bfd_boolean
9719ad41
RS
4669mips_elf_create_dynamic_relocation (bfd *output_bfd,
4670 struct bfd_link_info *info,
4671 const Elf_Internal_Rela *rel,
4672 struct mips_elf_link_hash_entry *h,
4673 asection *sec, bfd_vma symbol,
4674 bfd_vma *addendp, asection *input_section)
b49e97c9 4675{
947216bf 4676 Elf_Internal_Rela outrel[3];
b49e97c9
TS
4677 asection *sreloc;
4678 bfd *dynobj;
4679 int r_type;
5d41f0b6
RS
4680 long indx;
4681 bfd_boolean defined_p;
0a44bf69 4682 struct mips_elf_link_hash_table *htab;
b49e97c9 4683
0a44bf69 4684 htab = mips_elf_hash_table (info);
b49e97c9
TS
4685 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
4686 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 4687 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
4688 BFD_ASSERT (sreloc != NULL);
4689 BFD_ASSERT (sreloc->contents != NULL);
4690 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 4691 < sreloc->size);
b49e97c9 4692
b49e97c9
TS
4693 outrel[0].r_offset =
4694 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
4695 outrel[1].r_offset =
4696 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
4697 outrel[2].r_offset =
4698 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
4699
c5ae1840 4700 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 4701 /* The relocation field has been deleted. */
5d41f0b6
RS
4702 return TRUE;
4703
4704 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
4705 {
4706 /* The relocation field has been converted into a relative value of
4707 some sort. Functions like _bfd_elf_write_section_eh_frame expect
4708 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 4709 *addendp += symbol;
5d41f0b6 4710 return TRUE;
0d591ff7 4711 }
b49e97c9 4712
5d41f0b6
RS
4713 /* We must now calculate the dynamic symbol table index to use
4714 in the relocation. */
4715 if (h != NULL
6ece8836
TS
4716 && (!h->root.def_regular
4717 || (info->shared && !info->symbolic && !h->root.forced_local)))
5d41f0b6
RS
4718 {
4719 indx = h->root.dynindx;
4720 if (SGI_COMPAT (output_bfd))
4721 defined_p = h->root.def_regular;
4722 else
4723 /* ??? glibc's ld.so just adds the final GOT entry to the
4724 relocation field. It therefore treats relocs against
4725 defined symbols in the same way as relocs against
4726 undefined symbols. */
4727 defined_p = FALSE;
4728 }
b49e97c9
TS
4729 else
4730 {
5d41f0b6
RS
4731 if (sec != NULL && bfd_is_abs_section (sec))
4732 indx = 0;
4733 else if (sec == NULL || sec->owner == NULL)
fdd07405 4734 {
5d41f0b6
RS
4735 bfd_set_error (bfd_error_bad_value);
4736 return FALSE;
b49e97c9
TS
4737 }
4738 else
4739 {
5d41f0b6
RS
4740 indx = elf_section_data (sec->output_section)->dynindx;
4741 if (indx == 0)
4742 abort ();
b49e97c9
TS
4743 }
4744
5d41f0b6
RS
4745 /* Instead of generating a relocation using the section
4746 symbol, we may as well make it a fully relative
4747 relocation. We want to avoid generating relocations to
4748 local symbols because we used to generate them
4749 incorrectly, without adding the original symbol value,
4750 which is mandated by the ABI for section symbols. In
4751 order to give dynamic loaders and applications time to
4752 phase out the incorrect use, we refrain from emitting
4753 section-relative relocations. It's not like they're
4754 useful, after all. This should be a bit more efficient
4755 as well. */
4756 /* ??? Although this behavior is compatible with glibc's ld.so,
4757 the ABI says that relocations against STN_UNDEF should have
4758 a symbol value of 0. Irix rld honors this, so relocations
4759 against STN_UNDEF have no effect. */
4760 if (!SGI_COMPAT (output_bfd))
4761 indx = 0;
4762 defined_p = TRUE;
b49e97c9
TS
4763 }
4764
5d41f0b6
RS
4765 /* If the relocation was previously an absolute relocation and
4766 this symbol will not be referred to by the relocation, we must
4767 adjust it by the value we give it in the dynamic symbol table.
4768 Otherwise leave the job up to the dynamic linker. */
4769 if (defined_p && r_type != R_MIPS_REL32)
4770 *addendp += symbol;
4771
0a44bf69
RS
4772 if (htab->is_vxworks)
4773 /* VxWorks uses non-relative relocations for this. */
4774 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
4775 else
4776 /* The relocation is always an REL32 relocation because we don't
4777 know where the shared library will wind up at load-time. */
4778 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
4779 R_MIPS_REL32);
4780
5d41f0b6
RS
4781 /* For strict adherence to the ABI specification, we should
4782 generate a R_MIPS_64 relocation record by itself before the
4783 _REL32/_64 record as well, such that the addend is read in as
4784 a 64-bit value (REL32 is a 32-bit relocation, after all).
4785 However, since none of the existing ELF64 MIPS dynamic
4786 loaders seems to care, we don't waste space with these
4787 artificial relocations. If this turns out to not be true,
4788 mips_elf_allocate_dynamic_relocation() should be tweaked so
4789 as to make room for a pair of dynamic relocations per
4790 invocation if ABI_64_P, and here we should generate an
4791 additional relocation record with R_MIPS_64 by itself for a
4792 NULL symbol before this relocation record. */
4793 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
4794 ABI_64_P (output_bfd)
4795 ? R_MIPS_64
4796 : R_MIPS_NONE);
4797 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
4798
4799 /* Adjust the output offset of the relocation to reference the
4800 correct location in the output file. */
4801 outrel[0].r_offset += (input_section->output_section->vma
4802 + input_section->output_offset);
4803 outrel[1].r_offset += (input_section->output_section->vma
4804 + input_section->output_offset);
4805 outrel[2].r_offset += (input_section->output_section->vma
4806 + input_section->output_offset);
4807
b49e97c9
TS
4808 /* Put the relocation back out. We have to use the special
4809 relocation outputter in the 64-bit case since the 64-bit
4810 relocation format is non-standard. */
4811 if (ABI_64_P (output_bfd))
4812 {
4813 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
4814 (output_bfd, &outrel[0],
4815 (sreloc->contents
4816 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
4817 }
0a44bf69
RS
4818 else if (htab->is_vxworks)
4819 {
4820 /* VxWorks uses RELA rather than REL dynamic relocations. */
4821 outrel[0].r_addend = *addendp;
4822 bfd_elf32_swap_reloca_out
4823 (output_bfd, &outrel[0],
4824 (sreloc->contents
4825 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
4826 }
b49e97c9 4827 else
947216bf
AM
4828 bfd_elf32_swap_reloc_out
4829 (output_bfd, &outrel[0],
4830 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 4831
b49e97c9
TS
4832 /* We've now added another relocation. */
4833 ++sreloc->reloc_count;
4834
4835 /* Make sure the output section is writable. The dynamic linker
4836 will be writing to it. */
4837 elf_section_data (input_section->output_section)->this_hdr.sh_flags
4838 |= SHF_WRITE;
4839
4840 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 4841 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9
TS
4842 {
4843 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4844 bfd_byte *cr;
4845
4846 if (scpt)
4847 {
4848 Elf32_crinfo cptrel;
4849
4850 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4851 cptrel.vaddr = (rel->r_offset
4852 + input_section->output_section->vma
4853 + input_section->output_offset);
4854 if (r_type == R_MIPS_REL32)
4855 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4856 else
4857 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4858 mips_elf_set_cr_dist2to (cptrel, 0);
4859 cptrel.konst = *addendp;
4860
4861 cr = (scpt->contents
4862 + sizeof (Elf32_External_compact_rel));
abc0f8d0 4863 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
4864 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4865 ((Elf32_External_crinfo *) cr
4866 + scpt->reloc_count));
4867 ++scpt->reloc_count;
4868 }
4869 }
4870
b34976b6 4871 return TRUE;
b49e97c9
TS
4872}
4873\f
b49e97c9
TS
4874/* Return the MACH for a MIPS e_flags value. */
4875
4876unsigned long
9719ad41 4877_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
4878{
4879 switch (flags & EF_MIPS_MACH)
4880 {
4881 case E_MIPS_MACH_3900:
4882 return bfd_mach_mips3900;
4883
4884 case E_MIPS_MACH_4010:
4885 return bfd_mach_mips4010;
4886
4887 case E_MIPS_MACH_4100:
4888 return bfd_mach_mips4100;
4889
4890 case E_MIPS_MACH_4111:
4891 return bfd_mach_mips4111;
4892
00707a0e
RS
4893 case E_MIPS_MACH_4120:
4894 return bfd_mach_mips4120;
4895
b49e97c9
TS
4896 case E_MIPS_MACH_4650:
4897 return bfd_mach_mips4650;
4898
00707a0e
RS
4899 case E_MIPS_MACH_5400:
4900 return bfd_mach_mips5400;
4901
4902 case E_MIPS_MACH_5500:
4903 return bfd_mach_mips5500;
4904
0d2e43ed
ILT
4905 case E_MIPS_MACH_9000:
4906 return bfd_mach_mips9000;
4907
b49e97c9
TS
4908 case E_MIPS_MACH_SB1:
4909 return bfd_mach_mips_sb1;
4910
4911 default:
4912 switch (flags & EF_MIPS_ARCH)
4913 {
4914 default:
4915 case E_MIPS_ARCH_1:
4916 return bfd_mach_mips3000;
4917 break;
4918
4919 case E_MIPS_ARCH_2:
4920 return bfd_mach_mips6000;
4921 break;
4922
4923 case E_MIPS_ARCH_3:
4924 return bfd_mach_mips4000;
4925 break;
4926
4927 case E_MIPS_ARCH_4:
4928 return bfd_mach_mips8000;
4929 break;
4930
4931 case E_MIPS_ARCH_5:
4932 return bfd_mach_mips5;
4933 break;
4934
4935 case E_MIPS_ARCH_32:
4936 return bfd_mach_mipsisa32;
4937 break;
4938
4939 case E_MIPS_ARCH_64:
4940 return bfd_mach_mipsisa64;
4941 break;
af7ee8bf
CD
4942
4943 case E_MIPS_ARCH_32R2:
4944 return bfd_mach_mipsisa32r2;
4945 break;
5f74bc13
CD
4946
4947 case E_MIPS_ARCH_64R2:
4948 return bfd_mach_mipsisa64r2;
4949 break;
b49e97c9
TS
4950 }
4951 }
4952
4953 return 0;
4954}
4955
4956/* Return printable name for ABI. */
4957
4958static INLINE char *
9719ad41 4959elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
4960{
4961 flagword flags;
4962
4963 flags = elf_elfheader (abfd)->e_flags;
4964 switch (flags & EF_MIPS_ABI)
4965 {
4966 case 0:
4967 if (ABI_N32_P (abfd))
4968 return "N32";
4969 else if (ABI_64_P (abfd))
4970 return "64";
4971 else
4972 return "none";
4973 case E_MIPS_ABI_O32:
4974 return "O32";
4975 case E_MIPS_ABI_O64:
4976 return "O64";
4977 case E_MIPS_ABI_EABI32:
4978 return "EABI32";
4979 case E_MIPS_ABI_EABI64:
4980 return "EABI64";
4981 default:
4982 return "unknown abi";
4983 }
4984}
4985\f
4986/* MIPS ELF uses two common sections. One is the usual one, and the
4987 other is for small objects. All the small objects are kept
4988 together, and then referenced via the gp pointer, which yields
4989 faster assembler code. This is what we use for the small common
4990 section. This approach is copied from ecoff.c. */
4991static asection mips_elf_scom_section;
4992static asymbol mips_elf_scom_symbol;
4993static asymbol *mips_elf_scom_symbol_ptr;
4994
4995/* MIPS ELF also uses an acommon section, which represents an
4996 allocated common symbol which may be overridden by a
4997 definition in a shared library. */
4998static asection mips_elf_acom_section;
4999static asymbol mips_elf_acom_symbol;
5000static asymbol *mips_elf_acom_symbol_ptr;
5001
5002/* Handle the special MIPS section numbers that a symbol may use.
5003 This is used for both the 32-bit and the 64-bit ABI. */
5004
5005void
9719ad41 5006_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
5007{
5008 elf_symbol_type *elfsym;
5009
5010 elfsym = (elf_symbol_type *) asym;
5011 switch (elfsym->internal_elf_sym.st_shndx)
5012 {
5013 case SHN_MIPS_ACOMMON:
5014 /* This section is used in a dynamically linked executable file.
5015 It is an allocated common section. The dynamic linker can
5016 either resolve these symbols to something in a shared
5017 library, or it can just leave them here. For our purposes,
5018 we can consider these symbols to be in a new section. */
5019 if (mips_elf_acom_section.name == NULL)
5020 {
5021 /* Initialize the acommon section. */
5022 mips_elf_acom_section.name = ".acommon";
5023 mips_elf_acom_section.flags = SEC_ALLOC;
5024 mips_elf_acom_section.output_section = &mips_elf_acom_section;
5025 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
5026 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
5027 mips_elf_acom_symbol.name = ".acommon";
5028 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
5029 mips_elf_acom_symbol.section = &mips_elf_acom_section;
5030 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
5031 }
5032 asym->section = &mips_elf_acom_section;
5033 break;
5034
5035 case SHN_COMMON:
5036 /* Common symbols less than the GP size are automatically
5037 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
5038 if (asym->value > elf_gp_size (abfd)
5039 || IRIX_COMPAT (abfd) == ict_irix6)
5040 break;
5041 /* Fall through. */
5042 case SHN_MIPS_SCOMMON:
5043 if (mips_elf_scom_section.name == NULL)
5044 {
5045 /* Initialize the small common section. */
5046 mips_elf_scom_section.name = ".scommon";
5047 mips_elf_scom_section.flags = SEC_IS_COMMON;
5048 mips_elf_scom_section.output_section = &mips_elf_scom_section;
5049 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
5050 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
5051 mips_elf_scom_symbol.name = ".scommon";
5052 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
5053 mips_elf_scom_symbol.section = &mips_elf_scom_section;
5054 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
5055 }
5056 asym->section = &mips_elf_scom_section;
5057 asym->value = elfsym->internal_elf_sym.st_size;
5058 break;
5059
5060 case SHN_MIPS_SUNDEFINED:
5061 asym->section = bfd_und_section_ptr;
5062 break;
5063
b49e97c9 5064 case SHN_MIPS_TEXT:
00b4930b
TS
5065 {
5066 asection *section = bfd_get_section_by_name (abfd, ".text");
5067
5068 BFD_ASSERT (SGI_COMPAT (abfd));
5069 if (section != NULL)
5070 {
5071 asym->section = section;
5072 /* MIPS_TEXT is a bit special, the address is not an offset
5073 to the base of the .text section. So substract the section
5074 base address to make it an offset. */
5075 asym->value -= section->vma;
5076 }
5077 }
b49e97c9
TS
5078 break;
5079
5080 case SHN_MIPS_DATA:
00b4930b
TS
5081 {
5082 asection *section = bfd_get_section_by_name (abfd, ".data");
5083
5084 BFD_ASSERT (SGI_COMPAT (abfd));
5085 if (section != NULL)
5086 {
5087 asym->section = section;
5088 /* MIPS_DATA is a bit special, the address is not an offset
5089 to the base of the .data section. So substract the section
5090 base address to make it an offset. */
5091 asym->value -= section->vma;
5092 }
5093 }
b49e97c9 5094 break;
b49e97c9
TS
5095 }
5096}
5097\f
8c946ed5
RS
5098/* Implement elf_backend_eh_frame_address_size. This differs from
5099 the default in the way it handles EABI64.
5100
5101 EABI64 was originally specified as an LP64 ABI, and that is what
5102 -mabi=eabi normally gives on a 64-bit target. However, gcc has
5103 historically accepted the combination of -mabi=eabi and -mlong32,
5104 and this ILP32 variation has become semi-official over time.
5105 Both forms use elf32 and have pointer-sized FDE addresses.
5106
5107 If an EABI object was generated by GCC 4.0 or above, it will have
5108 an empty .gcc_compiled_longXX section, where XX is the size of longs
5109 in bits. Unfortunately, ILP32 objects generated by earlier compilers
5110 have no special marking to distinguish them from LP64 objects.
5111
5112 We don't want users of the official LP64 ABI to be punished for the
5113 existence of the ILP32 variant, but at the same time, we don't want
5114 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
5115 We therefore take the following approach:
5116
5117 - If ABFD contains a .gcc_compiled_longXX section, use it to
5118 determine the pointer size.
5119
5120 - Otherwise check the type of the first relocation. Assume that
5121 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
5122
5123 - Otherwise punt.
5124
5125 The second check is enough to detect LP64 objects generated by pre-4.0
5126 compilers because, in the kind of output generated by those compilers,
5127 the first relocation will be associated with either a CIE personality
5128 routine or an FDE start address. Furthermore, the compilers never
5129 used a special (non-pointer) encoding for this ABI.
5130
5131 Checking the relocation type should also be safe because there is no
5132 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
5133 did so. */
5134
5135unsigned int
5136_bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
5137{
5138 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5139 return 8;
5140 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
5141 {
5142 bfd_boolean long32_p, long64_p;
5143
5144 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
5145 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
5146 if (long32_p && long64_p)
5147 return 0;
5148 if (long32_p)
5149 return 4;
5150 if (long64_p)
5151 return 8;
5152
5153 if (sec->reloc_count > 0
5154 && elf_section_data (sec)->relocs != NULL
5155 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
5156 == R_MIPS_64))
5157 return 8;
5158
5159 return 0;
5160 }
5161 return 4;
5162}
5163\f
174fd7f9
RS
5164/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
5165 relocations against two unnamed section symbols to resolve to the
5166 same address. For example, if we have code like:
5167
5168 lw $4,%got_disp(.data)($gp)
5169 lw $25,%got_disp(.text)($gp)
5170 jalr $25
5171
5172 then the linker will resolve both relocations to .data and the program
5173 will jump there rather than to .text.
5174
5175 We can work around this problem by giving names to local section symbols.
5176 This is also what the MIPSpro tools do. */
5177
5178bfd_boolean
5179_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
5180{
5181 return SGI_COMPAT (abfd);
5182}
5183\f
b49e97c9
TS
5184/* Work over a section just before writing it out. This routine is
5185 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
5186 sections that need the SHF_MIPS_GPREL flag by name; there has to be
5187 a better way. */
5188
b34976b6 5189bfd_boolean
9719ad41 5190_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
5191{
5192 if (hdr->sh_type == SHT_MIPS_REGINFO
5193 && hdr->sh_size > 0)
5194 {
5195 bfd_byte buf[4];
5196
5197 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
5198 BFD_ASSERT (hdr->contents == NULL);
5199
5200 if (bfd_seek (abfd,
5201 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
5202 SEEK_SET) != 0)
b34976b6 5203 return FALSE;
b49e97c9 5204 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 5205 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 5206 return FALSE;
b49e97c9
TS
5207 }
5208
5209 if (hdr->sh_type == SHT_MIPS_OPTIONS
5210 && hdr->bfd_section != NULL
f0abc2a1
AM
5211 && mips_elf_section_data (hdr->bfd_section) != NULL
5212 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
5213 {
5214 bfd_byte *contents, *l, *lend;
5215
f0abc2a1
AM
5216 /* We stored the section contents in the tdata field in the
5217 set_section_contents routine. We save the section contents
5218 so that we don't have to read them again.
b49e97c9
TS
5219 At this point we know that elf_gp is set, so we can look
5220 through the section contents to see if there is an
5221 ODK_REGINFO structure. */
5222
f0abc2a1 5223 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
5224 l = contents;
5225 lend = contents + hdr->sh_size;
5226 while (l + sizeof (Elf_External_Options) <= lend)
5227 {
5228 Elf_Internal_Options intopt;
5229
5230 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5231 &intopt);
1bc8074d
MR
5232 if (intopt.size < sizeof (Elf_External_Options))
5233 {
5234 (*_bfd_error_handler)
5235 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5236 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5237 break;
5238 }
b49e97c9
TS
5239 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5240 {
5241 bfd_byte buf[8];
5242
5243 if (bfd_seek (abfd,
5244 (hdr->sh_offset
5245 + (l - contents)
5246 + sizeof (Elf_External_Options)
5247 + (sizeof (Elf64_External_RegInfo) - 8)),
5248 SEEK_SET) != 0)
b34976b6 5249 return FALSE;
b49e97c9 5250 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 5251 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 5252 return FALSE;
b49e97c9
TS
5253 }
5254 else if (intopt.kind == ODK_REGINFO)
5255 {
5256 bfd_byte buf[4];
5257
5258 if (bfd_seek (abfd,
5259 (hdr->sh_offset
5260 + (l - contents)
5261 + sizeof (Elf_External_Options)
5262 + (sizeof (Elf32_External_RegInfo) - 4)),
5263 SEEK_SET) != 0)
b34976b6 5264 return FALSE;
b49e97c9 5265 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 5266 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 5267 return FALSE;
b49e97c9
TS
5268 }
5269 l += intopt.size;
5270 }
5271 }
5272
5273 if (hdr->bfd_section != NULL)
5274 {
5275 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
5276
5277 if (strcmp (name, ".sdata") == 0
5278 || strcmp (name, ".lit8") == 0
5279 || strcmp (name, ".lit4") == 0)
5280 {
5281 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5282 hdr->sh_type = SHT_PROGBITS;
5283 }
5284 else if (strcmp (name, ".sbss") == 0)
5285 {
5286 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5287 hdr->sh_type = SHT_NOBITS;
5288 }
5289 else if (strcmp (name, ".srdata") == 0)
5290 {
5291 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
5292 hdr->sh_type = SHT_PROGBITS;
5293 }
5294 else if (strcmp (name, ".compact_rel") == 0)
5295 {
5296 hdr->sh_flags = 0;
5297 hdr->sh_type = SHT_PROGBITS;
5298 }
5299 else if (strcmp (name, ".rtproc") == 0)
5300 {
5301 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
5302 {
5303 unsigned int adjust;
5304
5305 adjust = hdr->sh_size % hdr->sh_addralign;
5306 if (adjust != 0)
5307 hdr->sh_size += hdr->sh_addralign - adjust;
5308 }
5309 }
5310 }
5311
b34976b6 5312 return TRUE;
b49e97c9
TS
5313}
5314
5315/* Handle a MIPS specific section when reading an object file. This
5316 is called when elfcode.h finds a section with an unknown type.
5317 This routine supports both the 32-bit and 64-bit ELF ABI.
5318
5319 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
5320 how to. */
5321
b34976b6 5322bfd_boolean
6dc132d9
L
5323_bfd_mips_elf_section_from_shdr (bfd *abfd,
5324 Elf_Internal_Shdr *hdr,
5325 const char *name,
5326 int shindex)
b49e97c9
TS
5327{
5328 flagword flags = 0;
5329
5330 /* There ought to be a place to keep ELF backend specific flags, but
5331 at the moment there isn't one. We just keep track of the
5332 sections by their name, instead. Fortunately, the ABI gives
5333 suggested names for all the MIPS specific sections, so we will
5334 probably get away with this. */
5335 switch (hdr->sh_type)
5336 {
5337 case SHT_MIPS_LIBLIST:
5338 if (strcmp (name, ".liblist") != 0)
b34976b6 5339 return FALSE;
b49e97c9
TS
5340 break;
5341 case SHT_MIPS_MSYM:
5342 if (strcmp (name, ".msym") != 0)
b34976b6 5343 return FALSE;
b49e97c9
TS
5344 break;
5345 case SHT_MIPS_CONFLICT:
5346 if (strcmp (name, ".conflict") != 0)
b34976b6 5347 return FALSE;
b49e97c9
TS
5348 break;
5349 case SHT_MIPS_GPTAB:
5350 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
b34976b6 5351 return FALSE;
b49e97c9
TS
5352 break;
5353 case SHT_MIPS_UCODE:
5354 if (strcmp (name, ".ucode") != 0)
b34976b6 5355 return FALSE;
b49e97c9
TS
5356 break;
5357 case SHT_MIPS_DEBUG:
5358 if (strcmp (name, ".mdebug") != 0)
b34976b6 5359 return FALSE;
b49e97c9
TS
5360 flags = SEC_DEBUGGING;
5361 break;
5362 case SHT_MIPS_REGINFO:
5363 if (strcmp (name, ".reginfo") != 0
5364 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 5365 return FALSE;
b49e97c9
TS
5366 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
5367 break;
5368 case SHT_MIPS_IFACE:
5369 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 5370 return FALSE;
b49e97c9
TS
5371 break;
5372 case SHT_MIPS_CONTENT:
5373 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
b34976b6 5374 return FALSE;
b49e97c9
TS
5375 break;
5376 case SHT_MIPS_OPTIONS:
cc2e31b9 5377 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 5378 return FALSE;
b49e97c9
TS
5379 break;
5380 case SHT_MIPS_DWARF:
5381 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
b34976b6 5382 return FALSE;
b49e97c9
TS
5383 break;
5384 case SHT_MIPS_SYMBOL_LIB:
5385 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 5386 return FALSE;
b49e97c9
TS
5387 break;
5388 case SHT_MIPS_EVENTS:
5389 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
5390 && strncmp (name, ".MIPS.post_rel",
5391 sizeof ".MIPS.post_rel" - 1) != 0)
b34976b6 5392 return FALSE;
b49e97c9
TS
5393 break;
5394 default:
cc2e31b9 5395 break;
b49e97c9
TS
5396 }
5397
6dc132d9 5398 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 5399 return FALSE;
b49e97c9
TS
5400
5401 if (flags)
5402 {
5403 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
5404 (bfd_get_section_flags (abfd,
5405 hdr->bfd_section)
5406 | flags)))
b34976b6 5407 return FALSE;
b49e97c9
TS
5408 }
5409
5410 /* FIXME: We should record sh_info for a .gptab section. */
5411
5412 /* For a .reginfo section, set the gp value in the tdata information
5413 from the contents of this section. We need the gp value while
5414 processing relocs, so we just get it now. The .reginfo section
5415 is not used in the 64-bit MIPS ELF ABI. */
5416 if (hdr->sh_type == SHT_MIPS_REGINFO)
5417 {
5418 Elf32_External_RegInfo ext;
5419 Elf32_RegInfo s;
5420
9719ad41
RS
5421 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
5422 &ext, 0, sizeof ext))
b34976b6 5423 return FALSE;
b49e97c9
TS
5424 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
5425 elf_gp (abfd) = s.ri_gp_value;
5426 }
5427
5428 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
5429 set the gp value based on what we find. We may see both
5430 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
5431 they should agree. */
5432 if (hdr->sh_type == SHT_MIPS_OPTIONS)
5433 {
5434 bfd_byte *contents, *l, *lend;
5435
9719ad41 5436 contents = bfd_malloc (hdr->sh_size);
b49e97c9 5437 if (contents == NULL)
b34976b6 5438 return FALSE;
b49e97c9 5439 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 5440 0, hdr->sh_size))
b49e97c9
TS
5441 {
5442 free (contents);
b34976b6 5443 return FALSE;
b49e97c9
TS
5444 }
5445 l = contents;
5446 lend = contents + hdr->sh_size;
5447 while (l + sizeof (Elf_External_Options) <= lend)
5448 {
5449 Elf_Internal_Options intopt;
5450
5451 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
5452 &intopt);
1bc8074d
MR
5453 if (intopt.size < sizeof (Elf_External_Options))
5454 {
5455 (*_bfd_error_handler)
5456 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
5457 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
5458 break;
5459 }
b49e97c9
TS
5460 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
5461 {
5462 Elf64_Internal_RegInfo intreg;
5463
5464 bfd_mips_elf64_swap_reginfo_in
5465 (abfd,
5466 ((Elf64_External_RegInfo *)
5467 (l + sizeof (Elf_External_Options))),
5468 &intreg);
5469 elf_gp (abfd) = intreg.ri_gp_value;
5470 }
5471 else if (intopt.kind == ODK_REGINFO)
5472 {
5473 Elf32_RegInfo intreg;
5474
5475 bfd_mips_elf32_swap_reginfo_in
5476 (abfd,
5477 ((Elf32_External_RegInfo *)
5478 (l + sizeof (Elf_External_Options))),
5479 &intreg);
5480 elf_gp (abfd) = intreg.ri_gp_value;
5481 }
5482 l += intopt.size;
5483 }
5484 free (contents);
5485 }
5486
b34976b6 5487 return TRUE;
b49e97c9
TS
5488}
5489
5490/* Set the correct type for a MIPS ELF section. We do this by the
5491 section name, which is a hack, but ought to work. This routine is
5492 used by both the 32-bit and the 64-bit ABI. */
5493
b34976b6 5494bfd_boolean
9719ad41 5495_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9
TS
5496{
5497 register const char *name;
1bc8074d 5498 unsigned int sh_type;
b49e97c9
TS
5499
5500 name = bfd_get_section_name (abfd, sec);
1bc8074d 5501 sh_type = hdr->sh_type;
b49e97c9
TS
5502
5503 if (strcmp (name, ".liblist") == 0)
5504 {
5505 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 5506 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
5507 /* The sh_link field is set in final_write_processing. */
5508 }
5509 else if (strcmp (name, ".conflict") == 0)
5510 hdr->sh_type = SHT_MIPS_CONFLICT;
5511 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
5512 {
5513 hdr->sh_type = SHT_MIPS_GPTAB;
5514 hdr->sh_entsize = sizeof (Elf32_External_gptab);
5515 /* The sh_info field is set in final_write_processing. */
5516 }
5517 else if (strcmp (name, ".ucode") == 0)
5518 hdr->sh_type = SHT_MIPS_UCODE;
5519 else if (strcmp (name, ".mdebug") == 0)
5520 {
5521 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 5522 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
5523 entsize of 0. FIXME: Does this matter? */
5524 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
5525 hdr->sh_entsize = 0;
5526 else
5527 hdr->sh_entsize = 1;
5528 }
5529 else if (strcmp (name, ".reginfo") == 0)
5530 {
5531 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 5532 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
5533 entsize of 0x18. FIXME: Does this matter? */
5534 if (SGI_COMPAT (abfd))
5535 {
5536 if ((abfd->flags & DYNAMIC) != 0)
5537 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5538 else
5539 hdr->sh_entsize = 1;
5540 }
5541 else
5542 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
5543 }
5544 else if (SGI_COMPAT (abfd)
5545 && (strcmp (name, ".hash") == 0
5546 || strcmp (name, ".dynamic") == 0
5547 || strcmp (name, ".dynstr") == 0))
5548 {
5549 if (SGI_COMPAT (abfd))
5550 hdr->sh_entsize = 0;
5551#if 0
8dc1a139 5552 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
5553 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
5554#endif
5555 }
5556 else if (strcmp (name, ".got") == 0
5557 || strcmp (name, ".srdata") == 0
5558 || strcmp (name, ".sdata") == 0
5559 || strcmp (name, ".sbss") == 0
5560 || strcmp (name, ".lit4") == 0
5561 || strcmp (name, ".lit8") == 0)
5562 hdr->sh_flags |= SHF_MIPS_GPREL;
5563 else if (strcmp (name, ".MIPS.interfaces") == 0)
5564 {
5565 hdr->sh_type = SHT_MIPS_IFACE;
5566 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5567 }
5568 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
5569 {
5570 hdr->sh_type = SHT_MIPS_CONTENT;
5571 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5572 /* The sh_info field is set in final_write_processing. */
5573 }
cc2e31b9 5574 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
5575 {
5576 hdr->sh_type = SHT_MIPS_OPTIONS;
5577 hdr->sh_entsize = 1;
5578 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5579 }
5580 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
5581 hdr->sh_type = SHT_MIPS_DWARF;
5582 else if (strcmp (name, ".MIPS.symlib") == 0)
5583 {
5584 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
5585 /* The sh_link and sh_info fields are set in
5586 final_write_processing. */
5587 }
5588 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
5589 || strncmp (name, ".MIPS.post_rel",
5590 sizeof ".MIPS.post_rel" - 1) == 0)
5591 {
5592 hdr->sh_type = SHT_MIPS_EVENTS;
5593 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
5594 /* The sh_link field is set in final_write_processing. */
5595 }
5596 else if (strcmp (name, ".msym") == 0)
5597 {
5598 hdr->sh_type = SHT_MIPS_MSYM;
5599 hdr->sh_flags |= SHF_ALLOC;
5600 hdr->sh_entsize = 8;
5601 }
5602
1bc8074d
MR
5603 /* In the unlikely event a special section is empty it has to lose its
5604 special meaning. This may happen e.g. when using `strip' with the
5605 "--only-keep-debug" option. */
5606 if (sec->size > 0 && !(sec->flags & SEC_HAS_CONTENTS))
5607 hdr->sh_type = sh_type;
5608
7a79a000
TS
5609 /* The generic elf_fake_sections will set up REL_HDR using the default
5610 kind of relocations. We used to set up a second header for the
5611 non-default kind of relocations here, but only NewABI would use
5612 these, and the IRIX ld doesn't like resulting empty RELA sections.
5613 Thus we create those header only on demand now. */
b49e97c9 5614
b34976b6 5615 return TRUE;
b49e97c9
TS
5616}
5617
5618/* Given a BFD section, try to locate the corresponding ELF section
5619 index. This is used by both the 32-bit and the 64-bit ABI.
5620 Actually, it's not clear to me that the 64-bit ABI supports these,
5621 but for non-PIC objects we will certainly want support for at least
5622 the .scommon section. */
5623
b34976b6 5624bfd_boolean
9719ad41
RS
5625_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5626 asection *sec, int *retval)
b49e97c9
TS
5627{
5628 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
5629 {
5630 *retval = SHN_MIPS_SCOMMON;
b34976b6 5631 return TRUE;
b49e97c9
TS
5632 }
5633 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
5634 {
5635 *retval = SHN_MIPS_ACOMMON;
b34976b6 5636 return TRUE;
b49e97c9 5637 }
b34976b6 5638 return FALSE;
b49e97c9
TS
5639}
5640\f
5641/* Hook called by the linker routine which adds symbols from an object
5642 file. We must handle the special MIPS section numbers here. */
5643
b34976b6 5644bfd_boolean
9719ad41 5645_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 5646 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
5647 flagword *flagsp ATTRIBUTE_UNUSED,
5648 asection **secp, bfd_vma *valp)
b49e97c9
TS
5649{
5650 if (SGI_COMPAT (abfd)
5651 && (abfd->flags & DYNAMIC) != 0
5652 && strcmp (*namep, "_rld_new_interface") == 0)
5653 {
8dc1a139 5654 /* Skip IRIX5 rld entry name. */
b49e97c9 5655 *namep = NULL;
b34976b6 5656 return TRUE;
b49e97c9
TS
5657 }
5658
eedecc07
DD
5659 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
5660 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
5661 by setting a DT_NEEDED for the shared object. Since _gp_disp is
5662 a magic symbol resolved by the linker, we ignore this bogus definition
5663 of _gp_disp. New ABI objects do not suffer from this problem so this
5664 is not done for them. */
5665 if (!NEWABI_P(abfd)
5666 && (sym->st_shndx == SHN_ABS)
5667 && (strcmp (*namep, "_gp_disp") == 0))
5668 {
5669 *namep = NULL;
5670 return TRUE;
5671 }
5672
b49e97c9
TS
5673 switch (sym->st_shndx)
5674 {
5675 case SHN_COMMON:
5676 /* Common symbols less than the GP size are automatically
5677 treated as SHN_MIPS_SCOMMON symbols. */
5678 if (sym->st_size > elf_gp_size (abfd)
5679 || IRIX_COMPAT (abfd) == ict_irix6)
5680 break;
5681 /* Fall through. */
5682 case SHN_MIPS_SCOMMON:
5683 *secp = bfd_make_section_old_way (abfd, ".scommon");
5684 (*secp)->flags |= SEC_IS_COMMON;
5685 *valp = sym->st_size;
5686 break;
5687
5688 case SHN_MIPS_TEXT:
5689 /* This section is used in a shared object. */
5690 if (elf_tdata (abfd)->elf_text_section == NULL)
5691 {
5692 asymbol *elf_text_symbol;
5693 asection *elf_text_section;
5694 bfd_size_type amt = sizeof (asection);
5695
5696 elf_text_section = bfd_zalloc (abfd, amt);
5697 if (elf_text_section == NULL)
b34976b6 5698 return FALSE;
b49e97c9
TS
5699
5700 amt = sizeof (asymbol);
5701 elf_text_symbol = bfd_zalloc (abfd, amt);
5702 if (elf_text_symbol == NULL)
b34976b6 5703 return FALSE;
b49e97c9
TS
5704
5705 /* Initialize the section. */
5706
5707 elf_tdata (abfd)->elf_text_section = elf_text_section;
5708 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
5709
5710 elf_text_section->symbol = elf_text_symbol;
5711 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
5712
5713 elf_text_section->name = ".text";
5714 elf_text_section->flags = SEC_NO_FLAGS;
5715 elf_text_section->output_section = NULL;
5716 elf_text_section->owner = abfd;
5717 elf_text_symbol->name = ".text";
5718 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5719 elf_text_symbol->section = elf_text_section;
5720 }
5721 /* This code used to do *secp = bfd_und_section_ptr if
5722 info->shared. I don't know why, and that doesn't make sense,
5723 so I took it out. */
5724 *secp = elf_tdata (abfd)->elf_text_section;
5725 break;
5726
5727 case SHN_MIPS_ACOMMON:
5728 /* Fall through. XXX Can we treat this as allocated data? */
5729 case SHN_MIPS_DATA:
5730 /* This section is used in a shared object. */
5731 if (elf_tdata (abfd)->elf_data_section == NULL)
5732 {
5733 asymbol *elf_data_symbol;
5734 asection *elf_data_section;
5735 bfd_size_type amt = sizeof (asection);
5736
5737 elf_data_section = bfd_zalloc (abfd, amt);
5738 if (elf_data_section == NULL)
b34976b6 5739 return FALSE;
b49e97c9
TS
5740
5741 amt = sizeof (asymbol);
5742 elf_data_symbol = bfd_zalloc (abfd, amt);
5743 if (elf_data_symbol == NULL)
b34976b6 5744 return FALSE;
b49e97c9
TS
5745
5746 /* Initialize the section. */
5747
5748 elf_tdata (abfd)->elf_data_section = elf_data_section;
5749 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
5750
5751 elf_data_section->symbol = elf_data_symbol;
5752 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
5753
5754 elf_data_section->name = ".data";
5755 elf_data_section->flags = SEC_NO_FLAGS;
5756 elf_data_section->output_section = NULL;
5757 elf_data_section->owner = abfd;
5758 elf_data_symbol->name = ".data";
5759 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
5760 elf_data_symbol->section = elf_data_section;
5761 }
5762 /* This code used to do *secp = bfd_und_section_ptr if
5763 info->shared. I don't know why, and that doesn't make sense,
5764 so I took it out. */
5765 *secp = elf_tdata (abfd)->elf_data_section;
5766 break;
5767
5768 case SHN_MIPS_SUNDEFINED:
5769 *secp = bfd_und_section_ptr;
5770 break;
5771 }
5772
5773 if (SGI_COMPAT (abfd)
5774 && ! info->shared
5775 && info->hash->creator == abfd->xvec
5776 && strcmp (*namep, "__rld_obj_head") == 0)
5777 {
5778 struct elf_link_hash_entry *h;
14a793b2 5779 struct bfd_link_hash_entry *bh;
b49e97c9
TS
5780
5781 /* Mark __rld_obj_head as dynamic. */
14a793b2 5782 bh = NULL;
b49e97c9 5783 if (! (_bfd_generic_link_add_one_symbol
9719ad41 5784 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 5785 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5786 return FALSE;
14a793b2
AM
5787
5788 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5789 h->non_elf = 0;
5790 h->def_regular = 1;
b49e97c9
TS
5791 h->type = STT_OBJECT;
5792
c152c796 5793 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5794 return FALSE;
b49e97c9 5795
b34976b6 5796 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b49e97c9
TS
5797 }
5798
5799 /* If this is a mips16 text symbol, add 1 to the value to make it
5800 odd. This will cause something like .word SYM to come up with
5801 the right value when it is loaded into the PC. */
5802 if (sym->st_other == STO_MIPS16)
5803 ++*valp;
5804
b34976b6 5805 return TRUE;
b49e97c9
TS
5806}
5807
5808/* This hook function is called before the linker writes out a global
5809 symbol. We mark symbols as small common if appropriate. This is
5810 also where we undo the increment of the value for a mips16 symbol. */
5811
b34976b6 5812bfd_boolean
9719ad41
RS
5813_bfd_mips_elf_link_output_symbol_hook
5814 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5815 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
5816 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
5817{
5818 /* If we see a common symbol, which implies a relocatable link, then
5819 if a symbol was small common in an input file, mark it as small
5820 common in the output file. */
5821 if (sym->st_shndx == SHN_COMMON
5822 && strcmp (input_sec->name, ".scommon") == 0)
5823 sym->st_shndx = SHN_MIPS_SCOMMON;
5824
79cda7cf
FF
5825 if (sym->st_other == STO_MIPS16)
5826 sym->st_value &= ~1;
b49e97c9 5827
b34976b6 5828 return TRUE;
b49e97c9
TS
5829}
5830\f
5831/* Functions for the dynamic linker. */
5832
5833/* Create dynamic sections when linking against a dynamic object. */
5834
b34976b6 5835bfd_boolean
9719ad41 5836_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5837{
5838 struct elf_link_hash_entry *h;
14a793b2 5839 struct bfd_link_hash_entry *bh;
b49e97c9
TS
5840 flagword flags;
5841 register asection *s;
5842 const char * const *namep;
0a44bf69 5843 struct mips_elf_link_hash_table *htab;
b49e97c9 5844
0a44bf69 5845 htab = mips_elf_hash_table (info);
b49e97c9
TS
5846 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5847 | SEC_LINKER_CREATED | SEC_READONLY);
5848
0a44bf69
RS
5849 /* The psABI requires a read-only .dynamic section, but the VxWorks
5850 EABI doesn't. */
5851 if (!htab->is_vxworks)
b49e97c9 5852 {
0a44bf69
RS
5853 s = bfd_get_section_by_name (abfd, ".dynamic");
5854 if (s != NULL)
5855 {
5856 if (! bfd_set_section_flags (abfd, s, flags))
5857 return FALSE;
5858 }
b49e97c9
TS
5859 }
5860
5861 /* We need to create .got section. */
f4416af6
AO
5862 if (! mips_elf_create_got_section (abfd, info, FALSE))
5863 return FALSE;
5864
0a44bf69 5865 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 5866 return FALSE;
b49e97c9 5867
b49e97c9
TS
5868 /* Create .stub section. */
5869 if (bfd_get_section_by_name (abfd,
5870 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
5871 {
3496cb2a
L
5872 s = bfd_make_section_with_flags (abfd,
5873 MIPS_ELF_STUB_SECTION_NAME (abfd),
5874 flags | SEC_CODE);
b49e97c9 5875 if (s == NULL
b49e97c9
TS
5876 || ! bfd_set_section_alignment (abfd, s,
5877 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 5878 return FALSE;
b49e97c9
TS
5879 }
5880
5881 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
5882 && !info->shared
5883 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5884 {
3496cb2a
L
5885 s = bfd_make_section_with_flags (abfd, ".rld_map",
5886 flags &~ (flagword) SEC_READONLY);
b49e97c9 5887 if (s == NULL
b49e97c9
TS
5888 || ! bfd_set_section_alignment (abfd, s,
5889 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 5890 return FALSE;
b49e97c9
TS
5891 }
5892
5893 /* On IRIX5, we adjust add some additional symbols and change the
5894 alignments of several sections. There is no ABI documentation
5895 indicating that this is necessary on IRIX6, nor any evidence that
5896 the linker takes such action. */
5897 if (IRIX_COMPAT (abfd) == ict_irix5)
5898 {
5899 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5900 {
14a793b2 5901 bh = NULL;
b49e97c9 5902 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
5903 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
5904 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5905 return FALSE;
14a793b2
AM
5906
5907 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5908 h->non_elf = 0;
5909 h->def_regular = 1;
b49e97c9
TS
5910 h->type = STT_SECTION;
5911
c152c796 5912 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5913 return FALSE;
b49e97c9
TS
5914 }
5915
5916 /* We need to create a .compact_rel section. */
5917 if (SGI_COMPAT (abfd))
5918 {
5919 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 5920 return FALSE;
b49e97c9
TS
5921 }
5922
44c410de 5923 /* Change alignments of some sections. */
b49e97c9
TS
5924 s = bfd_get_section_by_name (abfd, ".hash");
5925 if (s != NULL)
d80dcc6a 5926 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
5927 s = bfd_get_section_by_name (abfd, ".dynsym");
5928 if (s != NULL)
d80dcc6a 5929 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
5930 s = bfd_get_section_by_name (abfd, ".dynstr");
5931 if (s != NULL)
d80dcc6a 5932 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
5933 s = bfd_get_section_by_name (abfd, ".reginfo");
5934 if (s != NULL)
d80dcc6a 5935 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
5936 s = bfd_get_section_by_name (abfd, ".dynamic");
5937 if (s != NULL)
d80dcc6a 5938 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
5939 }
5940
5941 if (!info->shared)
5942 {
14a793b2
AM
5943 const char *name;
5944
5945 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
5946 bh = NULL;
5947 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
5948 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
5949 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5950 return FALSE;
14a793b2
AM
5951
5952 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5953 h->non_elf = 0;
5954 h->def_regular = 1;
b49e97c9
TS
5955 h->type = STT_SECTION;
5956
c152c796 5957 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5958 return FALSE;
b49e97c9
TS
5959
5960 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5961 {
5962 /* __rld_map is a four byte word located in the .data section
5963 and is filled in by the rtld to contain a pointer to
5964 the _r_debug structure. Its symbol value will be set in
5965 _bfd_mips_elf_finish_dynamic_symbol. */
5966 s = bfd_get_section_by_name (abfd, ".rld_map");
5967 BFD_ASSERT (s != NULL);
5968
14a793b2
AM
5969 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
5970 bh = NULL;
5971 if (!(_bfd_generic_link_add_one_symbol
9719ad41 5972 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
14a793b2 5973 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5974 return FALSE;
14a793b2
AM
5975
5976 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5977 h->non_elf = 0;
5978 h->def_regular = 1;
b49e97c9
TS
5979 h->type = STT_OBJECT;
5980
c152c796 5981 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5982 return FALSE;
b49e97c9
TS
5983 }
5984 }
5985
0a44bf69
RS
5986 if (htab->is_vxworks)
5987 {
5988 /* Create the .plt, .rela.plt, .dynbss and .rela.bss sections.
5989 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
5990 if (!_bfd_elf_create_dynamic_sections (abfd, info))
5991 return FALSE;
5992
5993 /* Cache the sections created above. */
5994 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
5995 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
5996 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
5997 htab->splt = bfd_get_section_by_name (abfd, ".plt");
5998 if (!htab->sdynbss
5999 || (!htab->srelbss && !info->shared)
6000 || !htab->srelplt
6001 || !htab->splt)
6002 abort ();
6003
6004 /* Do the usual VxWorks handling. */
6005 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
6006 return FALSE;
6007
6008 /* Work out the PLT sizes. */
6009 if (info->shared)
6010 {
6011 htab->plt_header_size
6012 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
6013 htab->plt_entry_size
6014 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
6015 }
6016 else
6017 {
6018 htab->plt_header_size
6019 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
6020 htab->plt_entry_size
6021 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
6022 }
6023 }
6024
b34976b6 6025 return TRUE;
b49e97c9
TS
6026}
6027\f
6028/* Look through the relocs for a section during the first phase, and
6029 allocate space in the global offset table. */
6030
b34976b6 6031bfd_boolean
9719ad41
RS
6032_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6033 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
6034{
6035 const char *name;
6036 bfd *dynobj;
6037 Elf_Internal_Shdr *symtab_hdr;
6038 struct elf_link_hash_entry **sym_hashes;
6039 struct mips_got_info *g;
6040 size_t extsymoff;
6041 const Elf_Internal_Rela *rel;
6042 const Elf_Internal_Rela *rel_end;
6043 asection *sgot;
6044 asection *sreloc;
9c5bfbb7 6045 const struct elf_backend_data *bed;
0a44bf69 6046 struct mips_elf_link_hash_table *htab;
b49e97c9 6047
1049f94e 6048 if (info->relocatable)
b34976b6 6049 return TRUE;
b49e97c9 6050
0a44bf69 6051 htab = mips_elf_hash_table (info);
b49e97c9
TS
6052 dynobj = elf_hash_table (info)->dynobj;
6053 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6054 sym_hashes = elf_sym_hashes (abfd);
6055 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6056
6057 /* Check for the mips16 stub sections. */
6058
6059 name = bfd_get_section_name (abfd, sec);
6060 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6061 {
6062 unsigned long r_symndx;
6063
6064 /* Look at the relocation information to figure out which symbol
6065 this is for. */
6066
6067 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
6068
6069 if (r_symndx < extsymoff
6070 || sym_hashes[r_symndx - extsymoff] == NULL)
6071 {
6072 asection *o;
6073
6074 /* This stub is for a local symbol. This stub will only be
6075 needed if there is some relocation in this BFD, other
6076 than a 16 bit function call, which refers to this symbol. */
6077 for (o = abfd->sections; o != NULL; o = o->next)
6078 {
6079 Elf_Internal_Rela *sec_relocs;
6080 const Elf_Internal_Rela *r, *rend;
6081
6082 /* We can ignore stub sections when looking for relocs. */
6083 if ((o->flags & SEC_RELOC) == 0
6084 || o->reloc_count == 0
6085 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6086 sizeof FN_STUB - 1) == 0
6087 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6088 sizeof CALL_STUB - 1) == 0
6089 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6090 sizeof CALL_FP_STUB - 1) == 0)
6091 continue;
6092
45d6a902 6093 sec_relocs
9719ad41 6094 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 6095 info->keep_memory);
b49e97c9 6096 if (sec_relocs == NULL)
b34976b6 6097 return FALSE;
b49e97c9
TS
6098
6099 rend = sec_relocs + o->reloc_count;
6100 for (r = sec_relocs; r < rend; r++)
6101 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
6102 && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
6103 break;
6104
6cdc0ccc 6105 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
6106 free (sec_relocs);
6107
6108 if (r < rend)
6109 break;
6110 }
6111
6112 if (o == NULL)
6113 {
6114 /* There is no non-call reloc for this stub, so we do
6115 not need it. Since this function is called before
6116 the linker maps input sections to output sections, we
6117 can easily discard it by setting the SEC_EXCLUDE
6118 flag. */
6119 sec->flags |= SEC_EXCLUDE;
b34976b6 6120 return TRUE;
b49e97c9
TS
6121 }
6122
6123 /* Record this stub in an array of local symbol stubs for
6124 this BFD. */
6125 if (elf_tdata (abfd)->local_stubs == NULL)
6126 {
6127 unsigned long symcount;
6128 asection **n;
6129 bfd_size_type amt;
6130
6131 if (elf_bad_symtab (abfd))
6132 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
6133 else
6134 symcount = symtab_hdr->sh_info;
6135 amt = symcount * sizeof (asection *);
9719ad41 6136 n = bfd_zalloc (abfd, amt);
b49e97c9 6137 if (n == NULL)
b34976b6 6138 return FALSE;
b49e97c9
TS
6139 elf_tdata (abfd)->local_stubs = n;
6140 }
6141
6142 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6143
6144 /* We don't need to set mips16_stubs_seen in this case.
6145 That flag is used to see whether we need to look through
6146 the global symbol table for stubs. We don't need to set
6147 it here, because we just have a local stub. */
6148 }
6149 else
6150 {
6151 struct mips_elf_link_hash_entry *h;
6152
6153 h = ((struct mips_elf_link_hash_entry *)
6154 sym_hashes[r_symndx - extsymoff]);
6155
973a3492
L
6156 while (h->root.root.type == bfd_link_hash_indirect
6157 || h->root.root.type == bfd_link_hash_warning)
6158 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
6159
b49e97c9
TS
6160 /* H is the symbol this stub is for. */
6161
6162 h->fn_stub = sec;
b34976b6 6163 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
6164 }
6165 }
6166 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6167 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6168 {
6169 unsigned long r_symndx;
6170 struct mips_elf_link_hash_entry *h;
6171 asection **loc;
6172
6173 /* Look at the relocation information to figure out which symbol
6174 this is for. */
6175
6176 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
6177
6178 if (r_symndx < extsymoff
6179 || sym_hashes[r_symndx - extsymoff] == NULL)
6180 {
6181 /* This stub was actually built for a static symbol defined
6182 in the same file. We assume that all static symbols in
6183 mips16 code are themselves mips16, so we can simply
6184 discard this stub. Since this function is called before
6185 the linker maps input sections to output sections, we can
6186 easily discard it by setting the SEC_EXCLUDE flag. */
6187 sec->flags |= SEC_EXCLUDE;
b34976b6 6188 return TRUE;
b49e97c9
TS
6189 }
6190
6191 h = ((struct mips_elf_link_hash_entry *)
6192 sym_hashes[r_symndx - extsymoff]);
6193
6194 /* H is the symbol this stub is for. */
6195
6196 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6197 loc = &h->call_fp_stub;
6198 else
6199 loc = &h->call_stub;
6200
6201 /* If we already have an appropriate stub for this function, we
6202 don't need another one, so we can discard this one. Since
6203 this function is called before the linker maps input sections
6204 to output sections, we can easily discard it by setting the
6205 SEC_EXCLUDE flag. We can also discard this section if we
6206 happen to already know that this is a mips16 function; it is
6207 not necessary to check this here, as it is checked later, but
6208 it is slightly faster to check now. */
6209 if (*loc != NULL || h->root.other == STO_MIPS16)
6210 {
6211 sec->flags |= SEC_EXCLUDE;
b34976b6 6212 return TRUE;
b49e97c9
TS
6213 }
6214
6215 *loc = sec;
b34976b6 6216 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
6217 }
6218
6219 if (dynobj == NULL)
6220 {
6221 sgot = NULL;
6222 g = NULL;
6223 }
6224 else
6225 {
f4416af6 6226 sgot = mips_elf_got_section (dynobj, FALSE);
b49e97c9
TS
6227 if (sgot == NULL)
6228 g = NULL;
6229 else
6230 {
f0abc2a1
AM
6231 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
6232 g = mips_elf_section_data (sgot)->u.got_info;
b49e97c9
TS
6233 BFD_ASSERT (g != NULL);
6234 }
6235 }
6236
6237 sreloc = NULL;
6238 bed = get_elf_backend_data (abfd);
6239 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
6240 for (rel = relocs; rel < rel_end; ++rel)
6241 {
6242 unsigned long r_symndx;
6243 unsigned int r_type;
6244 struct elf_link_hash_entry *h;
6245
6246 r_symndx = ELF_R_SYM (abfd, rel->r_info);
6247 r_type = ELF_R_TYPE (abfd, rel->r_info);
6248
6249 if (r_symndx < extsymoff)
6250 h = NULL;
6251 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
6252 {
6253 (*_bfd_error_handler)
d003868e
AM
6254 (_("%B: Malformed reloc detected for section %s"),
6255 abfd, name);
b49e97c9 6256 bfd_set_error (bfd_error_bad_value);
b34976b6 6257 return FALSE;
b49e97c9
TS
6258 }
6259 else
6260 {
6261 h = sym_hashes[r_symndx - extsymoff];
6262
6263 /* This may be an indirect symbol created because of a version. */
6264 if (h != NULL)
6265 {
6266 while (h->root.type == bfd_link_hash_indirect)
6267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6268 }
6269 }
6270
6271 /* Some relocs require a global offset table. */
6272 if (dynobj == NULL || sgot == NULL)
6273 {
6274 switch (r_type)
6275 {
6276 case R_MIPS_GOT16:
6277 case R_MIPS_CALL16:
6278 case R_MIPS_CALL_HI16:
6279 case R_MIPS_CALL_LO16:
6280 case R_MIPS_GOT_HI16:
6281 case R_MIPS_GOT_LO16:
6282 case R_MIPS_GOT_PAGE:
6283 case R_MIPS_GOT_OFST:
6284 case R_MIPS_GOT_DISP:
86324f90 6285 case R_MIPS_TLS_GOTTPREL:
0f20cc35
DJ
6286 case R_MIPS_TLS_GD:
6287 case R_MIPS_TLS_LDM:
b49e97c9
TS
6288 if (dynobj == NULL)
6289 elf_hash_table (info)->dynobj = dynobj = abfd;
f4416af6 6290 if (! mips_elf_create_got_section (dynobj, info, FALSE))
b34976b6 6291 return FALSE;
b49e97c9 6292 g = mips_elf_got_info (dynobj, &sgot);
0a44bf69
RS
6293 if (htab->is_vxworks && !info->shared)
6294 {
6295 (*_bfd_error_handler)
6296 (_("%B: GOT reloc at 0x%lx not expected in executables"),
6297 abfd, (unsigned long) rel->r_offset);
6298 bfd_set_error (bfd_error_bad_value);
6299 return FALSE;
6300 }
b49e97c9
TS
6301 break;
6302
6303 case R_MIPS_32:
6304 case R_MIPS_REL32:
6305 case R_MIPS_64:
0a44bf69
RS
6306 /* In VxWorks executables, references to external symbols
6307 are handled using copy relocs or PLT stubs, so there's
6308 no need to add a dynamic relocation here. */
b49e97c9 6309 if (dynobj == NULL
0a44bf69 6310 && (info->shared || (h != NULL && !htab->is_vxworks))
b49e97c9
TS
6311 && (sec->flags & SEC_ALLOC) != 0)
6312 elf_hash_table (info)->dynobj = dynobj = abfd;
6313 break;
6314
6315 default:
6316 break;
6317 }
6318 }
6319
0a44bf69
RS
6320 if (h)
6321 {
6322 ((struct mips_elf_link_hash_entry *) h)->is_relocation_target = TRUE;
6323
6324 /* Relocations against the special VxWorks __GOTT_BASE__ and
6325 __GOTT_INDEX__ symbols must be left to the loader. Allocate
6326 room for them in .rela.dyn. */
6327 if (is_gott_symbol (info, h))
6328 {
6329 if (sreloc == NULL)
6330 {
6331 sreloc = mips_elf_rel_dyn_section (info, TRUE);
6332 if (sreloc == NULL)
6333 return FALSE;
6334 }
6335 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
6336 }
6337 }
6338 else if (r_type == R_MIPS_CALL_LO16
6339 || r_type == R_MIPS_GOT_LO16
6340 || r_type == R_MIPS_GOT_DISP
6341 || (r_type == R_MIPS_GOT16 && htab->is_vxworks))
b49e97c9
TS
6342 {
6343 /* We may need a local GOT entry for this relocation. We
6344 don't count R_MIPS_GOT_PAGE because we can estimate the
6345 maximum number of pages needed by looking at the size of
6346 the segment. Similar comments apply to R_MIPS_GOT16 and
0a44bf69
RS
6347 R_MIPS_CALL16, except on VxWorks, where GOT relocations
6348 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 6349 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 6350 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
f4416af6 6351 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
0f20cc35 6352 rel->r_addend, g, 0))
f4416af6 6353 return FALSE;
b49e97c9
TS
6354 }
6355
6356 switch (r_type)
6357 {
6358 case R_MIPS_CALL16:
6359 if (h == NULL)
6360 {
6361 (*_bfd_error_handler)
d003868e
AM
6362 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
6363 abfd, (unsigned long) rel->r_offset);
b49e97c9 6364 bfd_set_error (bfd_error_bad_value);
b34976b6 6365 return FALSE;
b49e97c9
TS
6366 }
6367 /* Fall through. */
6368
6369 case R_MIPS_CALL_HI16:
6370 case R_MIPS_CALL_LO16:
6371 if (h != NULL)
6372 {
0a44bf69
RS
6373 /* VxWorks call relocations point the function's .got.plt
6374 entry, which will be allocated by adjust_dynamic_symbol.
6375 Otherwise, this symbol requires a global GOT entry. */
6376 if (!htab->is_vxworks
6377 && !mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
b34976b6 6378 return FALSE;
b49e97c9
TS
6379
6380 /* We need a stub, not a plt entry for the undefined
6381 function. But we record it as if it needs plt. See
c152c796 6382 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 6383 h->needs_plt = 1;
b49e97c9
TS
6384 h->type = STT_FUNC;
6385 }
6386 break;
6387
0fdc1bf1
AO
6388 case R_MIPS_GOT_PAGE:
6389 /* If this is a global, overridable symbol, GOT_PAGE will
6390 decay to GOT_DISP, so we'll need a GOT entry for it. */
6391 if (h == NULL)
6392 break;
6393 else
6394 {
6395 struct mips_elf_link_hash_entry *hmips =
6396 (struct mips_elf_link_hash_entry *) h;
143d77c5 6397
0fdc1bf1
AO
6398 while (hmips->root.root.type == bfd_link_hash_indirect
6399 || hmips->root.root.type == bfd_link_hash_warning)
6400 hmips = (struct mips_elf_link_hash_entry *)
6401 hmips->root.root.u.i.link;
143d77c5 6402
f5385ebf 6403 if (hmips->root.def_regular
0fdc1bf1 6404 && ! (info->shared && ! info->symbolic
f5385ebf 6405 && ! hmips->root.forced_local))
0fdc1bf1
AO
6406 break;
6407 }
6408 /* Fall through. */
6409
b49e97c9
TS
6410 case R_MIPS_GOT16:
6411 case R_MIPS_GOT_HI16:
6412 case R_MIPS_GOT_LO16:
6413 case R_MIPS_GOT_DISP:
0f20cc35 6414 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
b34976b6 6415 return FALSE;
b49e97c9
TS
6416 break;
6417
0f20cc35
DJ
6418 case R_MIPS_TLS_GOTTPREL:
6419 if (info->shared)
6420 info->flags |= DF_STATIC_TLS;
6421 /* Fall through */
6422
6423 case R_MIPS_TLS_LDM:
6424 if (r_type == R_MIPS_TLS_LDM)
6425 {
6426 r_symndx = 0;
6427 h = NULL;
6428 }
6429 /* Fall through */
6430
6431 case R_MIPS_TLS_GD:
6432 /* This symbol requires a global offset table entry, or two
6433 for TLS GD relocations. */
6434 {
6435 unsigned char flag = (r_type == R_MIPS_TLS_GD
6436 ? GOT_TLS_GD
6437 : r_type == R_MIPS_TLS_LDM
6438 ? GOT_TLS_LDM
6439 : GOT_TLS_IE);
6440 if (h != NULL)
6441 {
6442 struct mips_elf_link_hash_entry *hmips =
6443 (struct mips_elf_link_hash_entry *) h;
6444 hmips->tls_type |= flag;
6445
6446 if (h && ! mips_elf_record_global_got_symbol (h, abfd, info, g, flag))
6447 return FALSE;
6448 }
6449 else
6450 {
6451 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != 0);
6452
6453 if (! mips_elf_record_local_got_symbol (abfd, r_symndx,
6454 rel->r_addend, g, flag))
6455 return FALSE;
6456 }
6457 }
6458 break;
6459
b49e97c9
TS
6460 case R_MIPS_32:
6461 case R_MIPS_REL32:
6462 case R_MIPS_64:
0a44bf69
RS
6463 /* In VxWorks executables, references to external symbols
6464 are handled using copy relocs or PLT stubs, so there's
6465 no need to add a .rela.dyn entry for this relocation. */
6466 if ((info->shared || (h != NULL && !htab->is_vxworks))
b49e97c9
TS
6467 && (sec->flags & SEC_ALLOC) != 0)
6468 {
6469 if (sreloc == NULL)
6470 {
0a44bf69 6471 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 6472 if (sreloc == NULL)
f4416af6 6473 return FALSE;
b49e97c9 6474 }
82f0cfbd 6475#define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
b49e97c9 6476 if (info->shared)
82f0cfbd
EC
6477 {
6478 /* When creating a shared object, we must copy these
6479 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
6480 relocs. Make room for this reloc in .rel(a).dyn. */
6481 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
82f0cfbd
EC
6482 if ((sec->flags & MIPS_READONLY_SECTION)
6483 == MIPS_READONLY_SECTION)
6484 /* We tell the dynamic linker that there are
6485 relocations against the text segment. */
6486 info->flags |= DF_TEXTREL;
6487 }
b49e97c9
TS
6488 else
6489 {
6490 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 6491
b49e97c9
TS
6492 /* We only need to copy this reloc if the symbol is
6493 defined in a dynamic object. */
6494 hmips = (struct mips_elf_link_hash_entry *) h;
6495 ++hmips->possibly_dynamic_relocs;
82f0cfbd
EC
6496 if ((sec->flags & MIPS_READONLY_SECTION)
6497 == MIPS_READONLY_SECTION)
6498 /* We need it to tell the dynamic linker if there
6499 are relocations against the text segment. */
6500 hmips->readonly_reloc = TRUE;
b49e97c9
TS
6501 }
6502
6503 /* Even though we don't directly need a GOT entry for
6504 this symbol, a symbol must have a dynamic symbol
6505 table index greater that DT_MIPS_GOTSYM if there are
0a44bf69
RS
6506 dynamic relocations against it. This does not apply
6507 to VxWorks, which does not have the usual coupling
6508 between global GOT entries and .dynsym entries. */
6509 if (h != NULL && !htab->is_vxworks)
f4416af6
AO
6510 {
6511 if (dynobj == NULL)
6512 elf_hash_table (info)->dynobj = dynobj = abfd;
6513 if (! mips_elf_create_got_section (dynobj, info, TRUE))
6514 return FALSE;
6515 g = mips_elf_got_info (dynobj, &sgot);
0f20cc35 6516 if (! mips_elf_record_global_got_symbol (h, abfd, info, g, 0))
f4416af6
AO
6517 return FALSE;
6518 }
b49e97c9
TS
6519 }
6520
6521 if (SGI_COMPAT (abfd))
6522 mips_elf_hash_table (info)->compact_rel_size +=
6523 sizeof (Elf32_External_crinfo);
6524 break;
6525
0a44bf69
RS
6526 case R_MIPS_PC16:
6527 if (h)
6528 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
6529 break;
6530
b49e97c9 6531 case R_MIPS_26:
0a44bf69
RS
6532 if (h)
6533 ((struct mips_elf_link_hash_entry *) h)->is_branch_target = TRUE;
6534 /* Fall through. */
6535
b49e97c9
TS
6536 case R_MIPS_GPREL16:
6537 case R_MIPS_LITERAL:
6538 case R_MIPS_GPREL32:
6539 if (SGI_COMPAT (abfd))
6540 mips_elf_hash_table (info)->compact_rel_size +=
6541 sizeof (Elf32_External_crinfo);
6542 break;
6543
6544 /* This relocation describes the C++ object vtable hierarchy.
6545 Reconstruct it for later use during GC. */
6546 case R_MIPS_GNU_VTINHERIT:
c152c796 6547 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 6548 return FALSE;
b49e97c9
TS
6549 break;
6550
6551 /* This relocation describes which C++ vtable entries are actually
6552 used. Record for later use during GC. */
6553 case R_MIPS_GNU_VTENTRY:
c152c796 6554 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 6555 return FALSE;
b49e97c9
TS
6556 break;
6557
6558 default:
6559 break;
6560 }
6561
6562 /* We must not create a stub for a symbol that has relocations
0a44bf69
RS
6563 related to taking the function's address. This doesn't apply to
6564 VxWorks, where CALL relocs refer to a .got.plt entry instead of
6565 a normal .got entry. */
6566 if (!htab->is_vxworks && h != NULL)
6567 switch (r_type)
6568 {
6569 default:
6570 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
6571 break;
6572 case R_MIPS_CALL16:
6573 case R_MIPS_CALL_HI16:
6574 case R_MIPS_CALL_LO16:
6575 case R_MIPS_JALR:
6576 break;
6577 }
b49e97c9
TS
6578
6579 /* If this reloc is not a 16 bit call, and it has a global
6580 symbol, then we will need the fn_stub if there is one.
6581 References from a stub section do not count. */
6582 if (h != NULL
6583 && r_type != R_MIPS16_26
6584 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6585 sizeof FN_STUB - 1) != 0
6586 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6587 sizeof CALL_STUB - 1) != 0
6588 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6589 sizeof CALL_FP_STUB - 1) != 0)
6590 {
6591 struct mips_elf_link_hash_entry *mh;
6592
6593 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 6594 mh->need_fn_stub = TRUE;
b49e97c9
TS
6595 }
6596 }
6597
b34976b6 6598 return TRUE;
b49e97c9
TS
6599}
6600\f
d0647110 6601bfd_boolean
9719ad41
RS
6602_bfd_mips_relax_section (bfd *abfd, asection *sec,
6603 struct bfd_link_info *link_info,
6604 bfd_boolean *again)
d0647110
AO
6605{
6606 Elf_Internal_Rela *internal_relocs;
6607 Elf_Internal_Rela *irel, *irelend;
6608 Elf_Internal_Shdr *symtab_hdr;
6609 bfd_byte *contents = NULL;
d0647110
AO
6610 size_t extsymoff;
6611 bfd_boolean changed_contents = FALSE;
6612 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
6613 Elf_Internal_Sym *isymbuf = NULL;
6614
6615 /* We are not currently changing any sizes, so only one pass. */
6616 *again = FALSE;
6617
1049f94e 6618 if (link_info->relocatable)
d0647110
AO
6619 return TRUE;
6620
9719ad41 6621 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
45d6a902 6622 link_info->keep_memory);
d0647110
AO
6623 if (internal_relocs == NULL)
6624 return TRUE;
6625
6626 irelend = internal_relocs + sec->reloc_count
6627 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
6628 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6629 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6630
6631 for (irel = internal_relocs; irel < irelend; irel++)
6632 {
6633 bfd_vma symval;
6634 bfd_signed_vma sym_offset;
6635 unsigned int r_type;
6636 unsigned long r_symndx;
6637 asection *sym_sec;
6638 unsigned long instruction;
6639
6640 /* Turn jalr into bgezal, and jr into beq, if they're marked
6641 with a JALR relocation, that indicate where they jump to.
6642 This saves some pipeline bubbles. */
6643 r_type = ELF_R_TYPE (abfd, irel->r_info);
6644 if (r_type != R_MIPS_JALR)
6645 continue;
6646
6647 r_symndx = ELF_R_SYM (abfd, irel->r_info);
6648 /* Compute the address of the jump target. */
6649 if (r_symndx >= extsymoff)
6650 {
6651 struct mips_elf_link_hash_entry *h
6652 = ((struct mips_elf_link_hash_entry *)
6653 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
6654
6655 while (h->root.root.type == bfd_link_hash_indirect
6656 || h->root.root.type == bfd_link_hash_warning)
6657 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
143d77c5 6658
d0647110
AO
6659 /* If a symbol is undefined, or if it may be overridden,
6660 skip it. */
6661 if (! ((h->root.root.type == bfd_link_hash_defined
6662 || h->root.root.type == bfd_link_hash_defweak)
6663 && h->root.root.u.def.section)
6664 || (link_info->shared && ! link_info->symbolic
f5385ebf 6665 && !h->root.forced_local))
d0647110
AO
6666 continue;
6667
6668 sym_sec = h->root.root.u.def.section;
6669 if (sym_sec->output_section)
6670 symval = (h->root.root.u.def.value
6671 + sym_sec->output_section->vma
6672 + sym_sec->output_offset);
6673 else
6674 symval = h->root.root.u.def.value;
6675 }
6676 else
6677 {
6678 Elf_Internal_Sym *isym;
6679
6680 /* Read this BFD's symbols if we haven't done so already. */
6681 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
6682 {
6683 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6684 if (isymbuf == NULL)
6685 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6686 symtab_hdr->sh_info, 0,
6687 NULL, NULL, NULL);
6688 if (isymbuf == NULL)
6689 goto relax_return;
6690 }
6691
6692 isym = isymbuf + r_symndx;
6693 if (isym->st_shndx == SHN_UNDEF)
6694 continue;
6695 else if (isym->st_shndx == SHN_ABS)
6696 sym_sec = bfd_abs_section_ptr;
6697 else if (isym->st_shndx == SHN_COMMON)
6698 sym_sec = bfd_com_section_ptr;
6699 else
6700 sym_sec
6701 = bfd_section_from_elf_index (abfd, isym->st_shndx);
6702 symval = isym->st_value
6703 + sym_sec->output_section->vma
6704 + sym_sec->output_offset;
6705 }
6706
6707 /* Compute branch offset, from delay slot of the jump to the
6708 branch target. */
6709 sym_offset = (symval + irel->r_addend)
6710 - (sec_start + irel->r_offset + 4);
6711
6712 /* Branch offset must be properly aligned. */
6713 if ((sym_offset & 3) != 0)
6714 continue;
6715
6716 sym_offset >>= 2;
6717
6718 /* Check that it's in range. */
6719 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
6720 continue;
143d77c5 6721
d0647110
AO
6722 /* Get the section contents if we haven't done so already. */
6723 if (contents == NULL)
6724 {
6725 /* Get cached copy if it exists. */
6726 if (elf_section_data (sec)->this_hdr.contents != NULL)
6727 contents = elf_section_data (sec)->this_hdr.contents;
6728 else
6729 {
eea6121a 6730 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
d0647110
AO
6731 goto relax_return;
6732 }
6733 }
6734
6735 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
6736
6737 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
6738 if ((instruction & 0xfc1fffff) == 0x0000f809)
6739 instruction = 0x04110000;
6740 /* If it was jr <reg>, turn it into b <target>. */
6741 else if ((instruction & 0xfc1fffff) == 0x00000008)
6742 instruction = 0x10000000;
6743 else
6744 continue;
6745
6746 instruction |= (sym_offset & 0xffff);
6747 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
6748 changed_contents = TRUE;
6749 }
6750
6751 if (contents != NULL
6752 && elf_section_data (sec)->this_hdr.contents != contents)
6753 {
6754 if (!changed_contents && !link_info->keep_memory)
6755 free (contents);
6756 else
6757 {
6758 /* Cache the section contents for elf_link_input_bfd. */
6759 elf_section_data (sec)->this_hdr.contents = contents;
6760 }
6761 }
6762 return TRUE;
6763
143d77c5 6764 relax_return:
eea6121a
AM
6765 if (contents != NULL
6766 && elf_section_data (sec)->this_hdr.contents != contents)
6767 free (contents);
d0647110
AO
6768 return FALSE;
6769}
6770\f
b49e97c9
TS
6771/* Adjust a symbol defined by a dynamic object and referenced by a
6772 regular object. The current definition is in some section of the
6773 dynamic object, but we're not including those sections. We have to
6774 change the definition to something the rest of the link can
6775 understand. */
6776
b34976b6 6777bfd_boolean
9719ad41
RS
6778_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6779 struct elf_link_hash_entry *h)
b49e97c9
TS
6780{
6781 bfd *dynobj;
6782 struct mips_elf_link_hash_entry *hmips;
6783 asection *s;
6784
6785 dynobj = elf_hash_table (info)->dynobj;
6786
6787 /* Make sure we know what is going on here. */
6788 BFD_ASSERT (dynobj != NULL
f5385ebf 6789 && (h->needs_plt
f6e332e6 6790 || h->u.weakdef != NULL
f5385ebf
AM
6791 || (h->def_dynamic
6792 && h->ref_regular
6793 && !h->def_regular)));
b49e97c9
TS
6794
6795 /* If this symbol is defined in a dynamic object, we need to copy
6796 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6797 file. */
6798 hmips = (struct mips_elf_link_hash_entry *) h;
1049f94e 6799 if (! info->relocatable
b49e97c9
TS
6800 && hmips->possibly_dynamic_relocs != 0
6801 && (h->root.type == bfd_link_hash_defweak
f5385ebf 6802 || !h->def_regular))
b49e97c9 6803 {
0a44bf69
RS
6804 mips_elf_allocate_dynamic_relocations
6805 (dynobj, info, hmips->possibly_dynamic_relocs);
82f0cfbd 6806 if (hmips->readonly_reloc)
b49e97c9
TS
6807 /* We tell the dynamic linker that there are relocations
6808 against the text segment. */
6809 info->flags |= DF_TEXTREL;
6810 }
6811
6812 /* For a function, create a stub, if allowed. */
6813 if (! hmips->no_fn_stub
f5385ebf 6814 && h->needs_plt)
b49e97c9
TS
6815 {
6816 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 6817 return TRUE;
b49e97c9
TS
6818
6819 /* If this symbol is not defined in a regular file, then set
6820 the symbol to the stub location. This is required to make
6821 function pointers compare as equal between the normal
6822 executable and the shared library. */
f5385ebf 6823 if (!h->def_regular)
b49e97c9
TS
6824 {
6825 /* We need .stub section. */
6826 s = bfd_get_section_by_name (dynobj,
6827 MIPS_ELF_STUB_SECTION_NAME (dynobj));
6828 BFD_ASSERT (s != NULL);
6829
6830 h->root.u.def.section = s;
eea6121a 6831 h->root.u.def.value = s->size;
b49e97c9
TS
6832
6833 /* XXX Write this stub address somewhere. */
eea6121a 6834 h->plt.offset = s->size;
b49e97c9
TS
6835
6836 /* Make room for this stub code. */
eea6121a 6837 s->size += MIPS_FUNCTION_STUB_SIZE;
b49e97c9
TS
6838
6839 /* The last half word of the stub will be filled with the index
6840 of this symbol in .dynsym section. */
b34976b6 6841 return TRUE;
b49e97c9
TS
6842 }
6843 }
6844 else if ((h->type == STT_FUNC)
f5385ebf 6845 && !h->needs_plt)
b49e97c9
TS
6846 {
6847 /* This will set the entry for this symbol in the GOT to 0, and
6848 the dynamic linker will take care of this. */
6849 h->root.u.def.value = 0;
b34976b6 6850 return TRUE;
b49e97c9
TS
6851 }
6852
6853 /* If this is a weak symbol, and there is a real definition, the
6854 processor independent code will have arranged for us to see the
6855 real definition first, and we can just use the same value. */
f6e332e6 6856 if (h->u.weakdef != NULL)
b49e97c9 6857 {
f6e332e6
AM
6858 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6859 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6860 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6861 h->root.u.def.value = h->u.weakdef->root.u.def.value;
b34976b6 6862 return TRUE;
b49e97c9
TS
6863 }
6864
6865 /* This is a reference to a symbol defined by a dynamic object which
6866 is not a function. */
6867
b34976b6 6868 return TRUE;
b49e97c9 6869}
0a44bf69
RS
6870
6871/* Likewise, for VxWorks. */
6872
6873bfd_boolean
6874_bfd_mips_vxworks_adjust_dynamic_symbol (struct bfd_link_info *info,
6875 struct elf_link_hash_entry *h)
6876{
6877 bfd *dynobj;
6878 struct mips_elf_link_hash_entry *hmips;
6879 struct mips_elf_link_hash_table *htab;
6880 unsigned int power_of_two;
6881
6882 htab = mips_elf_hash_table (info);
6883 dynobj = elf_hash_table (info)->dynobj;
6884 hmips = (struct mips_elf_link_hash_entry *) h;
6885
6886 /* Make sure we know what is going on here. */
6887 BFD_ASSERT (dynobj != NULL
6888 && (h->needs_plt
6889 || h->needs_copy
6890 || h->u.weakdef != NULL
6891 || (h->def_dynamic
6892 && h->ref_regular
6893 && !h->def_regular)));
6894
6895 /* If the symbol is defined by a dynamic object, we need a PLT stub if
6896 either (a) we want to branch to the symbol or (b) we're linking an
6897 executable that needs a canonical function address. In the latter
6898 case, the canonical address will be the address of the executable's
6899 load stub. */
6900 if ((hmips->is_branch_target
6901 || (!info->shared
6902 && h->type == STT_FUNC
6903 && hmips->is_relocation_target))
6904 && h->def_dynamic
6905 && h->ref_regular
6906 && !h->def_regular
6907 && !h->forced_local)
6908 h->needs_plt = 1;
6909
6910 /* Locally-binding symbols do not need a PLT stub; we can refer to
6911 the functions directly. */
6912 else if (h->needs_plt
6913 && (SYMBOL_CALLS_LOCAL (info, h)
6914 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6915 && h->root.type == bfd_link_hash_undefweak)))
6916 {
6917 h->needs_plt = 0;
6918 return TRUE;
6919 }
6920
6921 if (h->needs_plt)
6922 {
6923 /* If this is the first symbol to need a PLT entry, allocate room
6924 for the header, and for the header's .rela.plt.unloaded entries. */
6925 if (htab->splt->size == 0)
6926 {
6927 htab->splt->size += htab->plt_header_size;
6928 if (!info->shared)
6929 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
6930 }
6931
6932 /* Assign the next .plt entry to this symbol. */
6933 h->plt.offset = htab->splt->size;
6934 htab->splt->size += htab->plt_entry_size;
6935
6936 /* If the output file has no definition of the symbol, set the
6937 symbol's value to the address of the stub. For executables,
6938 point at the PLT load stub rather than the lazy resolution stub;
6939 this stub will become the canonical function address. */
6940 if (!h->def_regular)
6941 {
6942 h->root.u.def.section = htab->splt;
6943 h->root.u.def.value = h->plt.offset;
6944 if (!info->shared)
6945 h->root.u.def.value += 8;
6946 }
6947
6948 /* Make room for the .got.plt entry and the R_JUMP_SLOT relocation. */
6949 htab->sgotplt->size += 4;
6950 htab->srelplt->size += sizeof (Elf32_External_Rela);
6951
6952 /* Make room for the .rela.plt.unloaded relocations. */
6953 if (!info->shared)
6954 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
6955
6956 return TRUE;
6957 }
6958
6959 /* If a function symbol is defined by a dynamic object, and we do not
6960 need a PLT stub for it, the symbol's value should be zero. */
6961 if (h->type == STT_FUNC
6962 && h->def_dynamic
6963 && h->ref_regular
6964 && !h->def_regular)
6965 {
6966 h->root.u.def.value = 0;
6967 return TRUE;
6968 }
6969
6970 /* If this is a weak symbol, and there is a real definition, the
6971 processor independent code will have arranged for us to see the
6972 real definition first, and we can just use the same value. */
6973 if (h->u.weakdef != NULL)
6974 {
6975 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6976 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6977 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6978 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6979 return TRUE;
6980 }
6981
6982 /* This is a reference to a symbol defined by a dynamic object which
6983 is not a function. */
6984 if (info->shared)
6985 return TRUE;
6986
6987 /* We must allocate the symbol in our .dynbss section, which will
6988 become part of the .bss section of the executable. There will be
6989 an entry for this symbol in the .dynsym section. The dynamic
6990 object will contain position independent code, so all references
6991 from the dynamic object to this symbol will go through the global
6992 offset table. The dynamic linker will use the .dynsym entry to
6993 determine the address it must put in the global offset table, so
6994 both the dynamic object and the regular object will refer to the
6995 same memory location for the variable. */
6996
6997 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6998 {
6999 htab->srelbss->size += sizeof (Elf32_External_Rela);
7000 h->needs_copy = 1;
7001 }
7002
7003 /* We need to figure out the alignment required for this symbol. */
7004 power_of_two = bfd_log2 (h->size);
7005 if (power_of_two > 4)
7006 power_of_two = 4;
7007
7008 /* Apply the required alignment. */
7009 htab->sdynbss->size = BFD_ALIGN (htab->sdynbss->size,
7010 (bfd_size_type) 1 << power_of_two);
7011 if (power_of_two > bfd_get_section_alignment (dynobj, htab->sdynbss)
7012 && !bfd_set_section_alignment (dynobj, htab->sdynbss, power_of_two))
7013 return FALSE;
7014
7015 /* Define the symbol as being at this point in the section. */
7016 h->root.u.def.section = htab->sdynbss;
7017 h->root.u.def.value = htab->sdynbss->size;
7018
7019 /* Increment the section size to make room for the symbol. */
7020 htab->sdynbss->size += h->size;
7021
7022 return TRUE;
7023}
b49e97c9
TS
7024\f
7025/* This function is called after all the input files have been read,
7026 and the input sections have been assigned to output sections. We
7027 check for any mips16 stub sections that we can discard. */
7028
b34976b6 7029bfd_boolean
9719ad41
RS
7030_bfd_mips_elf_always_size_sections (bfd *output_bfd,
7031 struct bfd_link_info *info)
b49e97c9
TS
7032{
7033 asection *ri;
7034
f4416af6
AO
7035 bfd *dynobj;
7036 asection *s;
7037 struct mips_got_info *g;
7038 int i;
7039 bfd_size_type loadable_size = 0;
7040 bfd_size_type local_gotno;
7041 bfd *sub;
0f20cc35 7042 struct mips_elf_count_tls_arg count_tls_arg;
0a44bf69
RS
7043 struct mips_elf_link_hash_table *htab;
7044
7045 htab = mips_elf_hash_table (info);
f4416af6 7046
b49e97c9
TS
7047 /* The .reginfo section has a fixed size. */
7048 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
7049 if (ri != NULL)
9719ad41 7050 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
b49e97c9 7051
1049f94e 7052 if (! (info->relocatable
f4416af6
AO
7053 || ! mips_elf_hash_table (info)->mips16_stubs_seen))
7054 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 7055 mips_elf_check_mips16_stubs, NULL);
f4416af6
AO
7056
7057 dynobj = elf_hash_table (info)->dynobj;
7058 if (dynobj == NULL)
7059 /* Relocatable links don't have it. */
7060 return TRUE;
143d77c5 7061
f4416af6
AO
7062 g = mips_elf_got_info (dynobj, &s);
7063 if (s == NULL)
b34976b6 7064 return TRUE;
b49e97c9 7065
f4416af6
AO
7066 /* Calculate the total loadable size of the output. That
7067 will give us the maximum number of GOT_PAGE entries
7068 required. */
7069 for (sub = info->input_bfds; sub; sub = sub->link_next)
7070 {
7071 asection *subsection;
7072
7073 for (subsection = sub->sections;
7074 subsection;
7075 subsection = subsection->next)
7076 {
7077 if ((subsection->flags & SEC_ALLOC) == 0)
7078 continue;
eea6121a 7079 loadable_size += ((subsection->size + 0xf)
f4416af6
AO
7080 &~ (bfd_size_type) 0xf);
7081 }
7082 }
7083
7084 /* There has to be a global GOT entry for every symbol with
7085 a dynamic symbol table index of DT_MIPS_GOTSYM or
7086 higher. Therefore, it make sense to put those symbols
7087 that need GOT entries at the end of the symbol table. We
7088 do that here. */
7089 if (! mips_elf_sort_hash_table (info, 1))
7090 return FALSE;
7091
7092 if (g->global_gotsym != NULL)
7093 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
7094 else
7095 /* If there are no global symbols, or none requiring
7096 relocations, then GLOBAL_GOTSYM will be NULL. */
7097 i = 0;
7098
7099 /* In the worst case, we'll get one stub per dynamic symbol, plus
7100 one to account for the dummy entry at the end required by IRIX
7101 rld. */
7102 loadable_size += MIPS_FUNCTION_STUB_SIZE * (i + 1);
7103
0a44bf69
RS
7104 if (htab->is_vxworks)
7105 /* There's no need to allocate page entries for VxWorks; R_MIPS_GOT16
7106 relocations against local symbols evaluate to "G", and the EABI does
7107 not include R_MIPS_GOT_PAGE. */
7108 local_gotno = 0;
7109 else
7110 /* Assume there are two loadable segments consisting of contiguous
7111 sections. Is 5 enough? */
7112 local_gotno = (loadable_size >> 16) + 5;
f4416af6
AO
7113
7114 g->local_gotno += local_gotno;
eea6121a 7115 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
7116
7117 g->global_gotno = i;
eea6121a 7118 s->size += i * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6 7119
0f20cc35
DJ
7120 /* We need to calculate tls_gotno for global symbols at this point
7121 instead of building it up earlier, to avoid doublecounting
7122 entries for one global symbol from multiple input files. */
7123 count_tls_arg.info = info;
7124 count_tls_arg.needed = 0;
7125 elf_link_hash_traverse (elf_hash_table (info),
7126 mips_elf_count_global_tls_entries,
7127 &count_tls_arg);
7128 g->tls_gotno += count_tls_arg.needed;
7129 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
7130
7131 mips_elf_resolve_final_got_entries (g);
7132
0a44bf69
RS
7133 /* VxWorks does not support multiple GOTs. It initializes $gp to
7134 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
7135 dynamic loader. */
7136 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35
DJ
7137 {
7138 if (! mips_elf_multi_got (output_bfd, info, g, s, local_gotno))
7139 return FALSE;
7140 }
7141 else
7142 {
7143 /* Set up TLS entries for the first GOT. */
7144 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
7145 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
7146 }
b49e97c9 7147
b34976b6 7148 return TRUE;
b49e97c9
TS
7149}
7150
7151/* Set the sizes of the dynamic sections. */
7152
b34976b6 7153bfd_boolean
9719ad41
RS
7154_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
7155 struct bfd_link_info *info)
b49e97c9
TS
7156{
7157 bfd *dynobj;
0a44bf69 7158 asection *s, *sreldyn;
b34976b6 7159 bfd_boolean reltext;
0a44bf69 7160 struct mips_elf_link_hash_table *htab;
b49e97c9 7161
0a44bf69 7162 htab = mips_elf_hash_table (info);
b49e97c9
TS
7163 dynobj = elf_hash_table (info)->dynobj;
7164 BFD_ASSERT (dynobj != NULL);
7165
7166 if (elf_hash_table (info)->dynamic_sections_created)
7167 {
7168 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 7169 if (info->executable)
b49e97c9
TS
7170 {
7171 s = bfd_get_section_by_name (dynobj, ".interp");
7172 BFD_ASSERT (s != NULL);
eea6121a 7173 s->size
b49e97c9
TS
7174 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
7175 s->contents
7176 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
7177 }
7178 }
7179
7180 /* The check_relocs and adjust_dynamic_symbol entry points have
7181 determined the sizes of the various dynamic sections. Allocate
7182 memory for them. */
b34976b6 7183 reltext = FALSE;
0a44bf69 7184 sreldyn = NULL;
b49e97c9
TS
7185 for (s = dynobj->sections; s != NULL; s = s->next)
7186 {
7187 const char *name;
b49e97c9
TS
7188
7189 /* It's OK to base decisions on the section name, because none
7190 of the dynobj section names depend upon the input files. */
7191 name = bfd_get_section_name (dynobj, s);
7192
7193 if ((s->flags & SEC_LINKER_CREATED) == 0)
7194 continue;
7195
b49e97c9
TS
7196 if (strncmp (name, ".rel", 4) == 0)
7197 {
c456f082 7198 if (s->size != 0)
b49e97c9
TS
7199 {
7200 const char *outname;
7201 asection *target;
7202
7203 /* If this relocation section applies to a read only
7204 section, then we probably need a DT_TEXTREL entry.
0a44bf69 7205 If the relocation section is .rel(a).dyn, we always
b49e97c9
TS
7206 assert a DT_TEXTREL entry rather than testing whether
7207 there exists a relocation to a read only section or
7208 not. */
7209 outname = bfd_get_section_name (output_bfd,
7210 s->output_section);
7211 target = bfd_get_section_by_name (output_bfd, outname + 4);
7212 if ((target != NULL
7213 && (target->flags & SEC_READONLY) != 0
7214 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 7215 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 7216 reltext = TRUE;
b49e97c9
TS
7217
7218 /* We use the reloc_count field as a counter if we need
7219 to copy relocs into the output file. */
0a44bf69 7220 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 7221 s->reloc_count = 0;
f4416af6
AO
7222
7223 /* If combreloc is enabled, elf_link_sort_relocs() will
7224 sort relocations, but in a different way than we do,
7225 and before we're done creating relocations. Also, it
7226 will move them around between input sections'
7227 relocation's contents, so our sorting would be
7228 broken, so don't let it run. */
7229 info->combreloc = 0;
b49e97c9
TS
7230 }
7231 }
0a44bf69
RS
7232 else if (htab->is_vxworks && strcmp (name, ".got") == 0)
7233 {
7234 /* Executables do not need a GOT. */
7235 if (info->shared)
7236 {
7237 /* Allocate relocations for all but the reserved entries. */
7238 struct mips_got_info *g;
7239 unsigned int count;
7240
7241 g = mips_elf_got_info (dynobj, NULL);
7242 count = (g->global_gotno
7243 + g->local_gotno
7244 - MIPS_RESERVED_GOTNO (info));
7245 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
7246 }
7247 }
7248 else if (!htab->is_vxworks && strncmp (name, ".got", 4) == 0)
b49e97c9 7249 {
f4416af6
AO
7250 /* _bfd_mips_elf_always_size_sections() has already done
7251 most of the work, but some symbols may have been mapped
7252 to versions that we must now resolve in the got_entries
7253 hash tables. */
7254 struct mips_got_info *gg = mips_elf_got_info (dynobj, NULL);
7255 struct mips_got_info *g = gg;
7256 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
7257 unsigned int needed_relocs = 0;
143d77c5 7258
f4416af6 7259 if (gg->next)
b49e97c9 7260 {
f4416af6
AO
7261 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (output_bfd);
7262 set_got_offset_arg.info = info;
b49e97c9 7263
0f20cc35
DJ
7264 /* NOTE 2005-02-03: How can this call, or the next, ever
7265 find any indirect entries to resolve? They were all
7266 resolved in mips_elf_multi_got. */
f4416af6
AO
7267 mips_elf_resolve_final_got_entries (gg);
7268 for (g = gg->next; g && g->next != gg; g = g->next)
b49e97c9 7269 {
f4416af6
AO
7270 unsigned int save_assign;
7271
7272 mips_elf_resolve_final_got_entries (g);
7273
7274 /* Assign offsets to global GOT entries. */
7275 save_assign = g->assigned_gotno;
7276 g->assigned_gotno = g->local_gotno;
7277 set_got_offset_arg.g = g;
7278 set_got_offset_arg.needed_relocs = 0;
7279 htab_traverse (g->got_entries,
7280 mips_elf_set_global_got_offset,
7281 &set_got_offset_arg);
7282 needed_relocs += set_got_offset_arg.needed_relocs;
7283 BFD_ASSERT (g->assigned_gotno - g->local_gotno
7284 <= g->global_gotno);
7285
7286 g->assigned_gotno = save_assign;
7287 if (info->shared)
7288 {
7289 needed_relocs += g->local_gotno - g->assigned_gotno;
7290 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
7291 + g->next->global_gotno
0f20cc35 7292 + g->next->tls_gotno
0a44bf69 7293 + MIPS_RESERVED_GOTNO (info));
f4416af6 7294 }
b49e97c9 7295 }
0f20cc35
DJ
7296 }
7297 else
7298 {
7299 struct mips_elf_count_tls_arg arg;
7300 arg.info = info;
7301 arg.needed = 0;
b49e97c9 7302
0f20cc35
DJ
7303 htab_traverse (gg->got_entries, mips_elf_count_local_tls_relocs,
7304 &arg);
7305 elf_link_hash_traverse (elf_hash_table (info),
7306 mips_elf_count_global_tls_relocs,
7307 &arg);
7308
7309 needed_relocs += arg.needed;
f4416af6 7310 }
0f20cc35
DJ
7311
7312 if (needed_relocs)
0a44bf69
RS
7313 mips_elf_allocate_dynamic_relocations (dynobj, info,
7314 needed_relocs);
b49e97c9
TS
7315 }
7316 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
7317 {
8dc1a139 7318 /* IRIX rld assumes that the function stub isn't at the end
b49e97c9 7319 of .text section. So put a dummy. XXX */
eea6121a 7320 s->size += MIPS_FUNCTION_STUB_SIZE;
b49e97c9
TS
7321 }
7322 else if (! info->shared
7323 && ! mips_elf_hash_table (info)->use_rld_obj_head
7324 && strncmp (name, ".rld_map", 8) == 0)
7325 {
7326 /* We add a room for __rld_map. It will be filled in by the
7327 rtld to contain a pointer to the _r_debug structure. */
eea6121a 7328 s->size += 4;
b49e97c9
TS
7329 }
7330 else if (SGI_COMPAT (output_bfd)
7331 && strncmp (name, ".compact_rel", 12) == 0)
eea6121a 7332 s->size += mips_elf_hash_table (info)->compact_rel_size;
0a44bf69
RS
7333 else if (strncmp (name, ".init", 5) != 0
7334 && s != htab->sgotplt
7335 && s != htab->splt)
b49e97c9
TS
7336 {
7337 /* It's not one of our sections, so don't allocate space. */
7338 continue;
7339 }
7340
c456f082 7341 if (s->size == 0)
b49e97c9 7342 {
8423293d 7343 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
7344 continue;
7345 }
7346
c456f082
AM
7347 if ((s->flags & SEC_HAS_CONTENTS) == 0)
7348 continue;
7349
0a44bf69
RS
7350 /* Allocate memory for this section last, since we may increase its
7351 size above. */
7352 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) == 0)
7353 {
7354 sreldyn = s;
7355 continue;
7356 }
7357
b49e97c9 7358 /* Allocate memory for the section contents. */
eea6121a 7359 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 7360 if (s->contents == NULL)
b49e97c9
TS
7361 {
7362 bfd_set_error (bfd_error_no_memory);
b34976b6 7363 return FALSE;
b49e97c9
TS
7364 }
7365 }
7366
0a44bf69
RS
7367 /* Allocate memory for the .rel(a).dyn section. */
7368 if (sreldyn != NULL)
7369 {
7370 sreldyn->contents = bfd_zalloc (dynobj, sreldyn->size);
7371 if (sreldyn->contents == NULL)
7372 {
7373 bfd_set_error (bfd_error_no_memory);
7374 return FALSE;
7375 }
7376 }
7377
b49e97c9
TS
7378 if (elf_hash_table (info)->dynamic_sections_created)
7379 {
7380 /* Add some entries to the .dynamic section. We fill in the
7381 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
7382 must add the entries now so that we get the correct size for
7383 the .dynamic section. The DT_DEBUG entry is filled in by the
7384 dynamic linker and used by the debugger. */
7385 if (! info->shared)
7386 {
7387 /* SGI object has the equivalence of DT_DEBUG in the
7388 DT_MIPS_RLD_MAP entry. */
7389 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
b34976b6 7390 return FALSE;
b49e97c9
TS
7391 if (!SGI_COMPAT (output_bfd))
7392 {
7393 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
b34976b6 7394 return FALSE;
b49e97c9
TS
7395 }
7396 }
7397 else
7398 {
7399 /* Shared libraries on traditional mips have DT_DEBUG. */
7400 if (!SGI_COMPAT (output_bfd))
7401 {
7402 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
b34976b6 7403 return FALSE;
b49e97c9
TS
7404 }
7405 }
7406
0a44bf69 7407 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
7408 info->flags |= DF_TEXTREL;
7409
7410 if ((info->flags & DF_TEXTREL) != 0)
7411 {
7412 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 7413 return FALSE;
b49e97c9
TS
7414 }
7415
7416 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 7417 return FALSE;
b49e97c9 7418
0a44bf69 7419 if (htab->is_vxworks)
b49e97c9 7420 {
0a44bf69
RS
7421 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
7422 use any of the DT_MIPS_* tags. */
7423 if (mips_elf_rel_dyn_section (info, FALSE))
7424 {
7425 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
7426 return FALSE;
b49e97c9 7427
0a44bf69
RS
7428 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
7429 return FALSE;
b49e97c9 7430
0a44bf69
RS
7431 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
7432 return FALSE;
7433 }
7434 if (htab->splt->size > 0)
7435 {
7436 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
7437 return FALSE;
7438
7439 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
7440 return FALSE;
7441
7442 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
7443 return FALSE;
7444 }
b49e97c9 7445 }
0a44bf69
RS
7446 else
7447 {
7448 if (mips_elf_rel_dyn_section (info, FALSE))
7449 {
7450 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
7451 return FALSE;
b49e97c9 7452
0a44bf69
RS
7453 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
7454 return FALSE;
b49e97c9 7455
0a44bf69
RS
7456 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
7457 return FALSE;
7458 }
b49e97c9 7459
0a44bf69
RS
7460 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
7461 return FALSE;
b49e97c9 7462
0a44bf69
RS
7463 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
7464 return FALSE;
b49e97c9 7465
0a44bf69
RS
7466 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
7467 return FALSE;
b49e97c9 7468
0a44bf69
RS
7469 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
7470 return FALSE;
b49e97c9 7471
0a44bf69
RS
7472 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
7473 return FALSE;
b49e97c9 7474
0a44bf69
RS
7475 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
7476 return FALSE;
b49e97c9 7477
0a44bf69
RS
7478 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
7479 return FALSE;
7480
7481 if (IRIX_COMPAT (dynobj) == ict_irix5
7482 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
7483 return FALSE;
7484
7485 if (IRIX_COMPAT (dynobj) == ict_irix6
7486 && (bfd_get_section_by_name
7487 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
7488 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
7489 return FALSE;
7490 }
b49e97c9
TS
7491 }
7492
b34976b6 7493 return TRUE;
b49e97c9
TS
7494}
7495\f
81d43bff
RS
7496/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
7497 Adjust its R_ADDEND field so that it is correct for the output file.
7498 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
7499 and sections respectively; both use symbol indexes. */
7500
7501static void
7502mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
7503 bfd *input_bfd, Elf_Internal_Sym *local_syms,
7504 asection **local_sections, Elf_Internal_Rela *rel)
7505{
7506 unsigned int r_type, r_symndx;
7507 Elf_Internal_Sym *sym;
7508 asection *sec;
7509
7510 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections, FALSE))
7511 {
7512 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
7513 if (r_type == R_MIPS16_GPREL
7514 || r_type == R_MIPS_GPREL16
7515 || r_type == R_MIPS_GPREL32
7516 || r_type == R_MIPS_LITERAL)
7517 {
7518 rel->r_addend += _bfd_get_gp_value (input_bfd);
7519 rel->r_addend -= _bfd_get_gp_value (output_bfd);
7520 }
7521
7522 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
7523 sym = local_syms + r_symndx;
7524
7525 /* Adjust REL's addend to account for section merging. */
7526 if (!info->relocatable)
7527 {
7528 sec = local_sections[r_symndx];
7529 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
7530 }
7531
7532 /* This would normally be done by the rela_normal code in elflink.c. */
7533 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7534 rel->r_addend += local_sections[r_symndx]->output_offset;
7535 }
7536}
7537
b49e97c9
TS
7538/* Relocate a MIPS ELF section. */
7539
b34976b6 7540bfd_boolean
9719ad41
RS
7541_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
7542 bfd *input_bfd, asection *input_section,
7543 bfd_byte *contents, Elf_Internal_Rela *relocs,
7544 Elf_Internal_Sym *local_syms,
7545 asection **local_sections)
b49e97c9
TS
7546{
7547 Elf_Internal_Rela *rel;
7548 const Elf_Internal_Rela *relend;
7549 bfd_vma addend = 0;
b34976b6 7550 bfd_boolean use_saved_addend_p = FALSE;
9c5bfbb7 7551 const struct elf_backend_data *bed;
b49e97c9
TS
7552
7553 bed = get_elf_backend_data (output_bfd);
7554 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
7555 for (rel = relocs; rel < relend; ++rel)
7556 {
7557 const char *name;
c9adbffe 7558 bfd_vma value = 0;
b49e97c9 7559 reloc_howto_type *howto;
b34976b6
AM
7560 bfd_boolean require_jalx;
7561 /* TRUE if the relocation is a RELA relocation, rather than a
b49e97c9 7562 REL relocation. */
b34976b6 7563 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 7564 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 7565 const char *msg;
b49e97c9
TS
7566
7567 /* Find the relocation howto for this relocation. */
4a14403c 7568 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
7569 {
7570 /* Some 32-bit code uses R_MIPS_64. In particular, people use
7571 64-bit code, but make sure all their addresses are in the
7572 lowermost or uppermost 32-bit section of the 64-bit address
7573 space. Thus, when they use an R_MIPS_64 they mean what is
7574 usually meant by R_MIPS_32, with the exception that the
7575 stored value is sign-extended to 64 bits. */
b34976b6 7576 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
7577
7578 /* On big-endian systems, we need to lie about the position
7579 of the reloc. */
7580 if (bfd_big_endian (input_bfd))
7581 rel->r_offset += 4;
7582 }
7583 else
7584 /* NewABI defaults to RELA relocations. */
7585 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
4ffba85c
AO
7586 NEWABI_P (input_bfd)
7587 && (MIPS_RELOC_RELA_P
7588 (input_bfd, input_section,
7589 rel - relocs)));
b49e97c9
TS
7590
7591 if (!use_saved_addend_p)
7592 {
7593 Elf_Internal_Shdr *rel_hdr;
7594
7595 /* If these relocations were originally of the REL variety,
7596 we must pull the addend out of the field that will be
7597 relocated. Otherwise, we simply use the contents of the
7598 RELA relocation. To determine which flavor or relocation
7599 this is, we depend on the fact that the INPUT_SECTION's
7600 REL_HDR is read before its REL_HDR2. */
7601 rel_hdr = &elf_section_data (input_section)->rel_hdr;
7602 if ((size_t) (rel - relocs)
7603 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
7604 rel_hdr = elf_section_data (input_section)->rel_hdr2;
7605 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
7606 {
d6f16593
MR
7607 bfd_byte *location = contents + rel->r_offset;
7608
b49e97c9 7609 /* Note that this is a REL relocation. */
b34976b6 7610 rela_relocation_p = FALSE;
b49e97c9
TS
7611
7612 /* Get the addend, which is stored in the input file. */
d6f16593
MR
7613 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE,
7614 location);
b49e97c9
TS
7615 addend = mips_elf_obtain_contents (howto, rel, input_bfd,
7616 contents);
d6f16593
MR
7617 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, FALSE,
7618 location);
7619
b49e97c9
TS
7620 addend &= howto->src_mask;
7621
7622 /* For some kinds of relocations, the ADDEND is a
7623 combination of the addend stored in two different
7624 relocations. */
d6f16593 7625 if (r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16
b49e97c9
TS
7626 || (r_type == R_MIPS_GOT16
7627 && mips_elf_local_relocation_p (input_bfd, rel,
b34976b6 7628 local_sections, FALSE)))
b49e97c9
TS
7629 {
7630 bfd_vma l;
7631 const Elf_Internal_Rela *lo16_relocation;
7632 reloc_howto_type *lo16_howto;
d6f16593
MR
7633 bfd_byte *lo16_location;
7634 int lo16_type;
7635
7636 if (r_type == R_MIPS16_HI16)
7637 lo16_type = R_MIPS16_LO16;
7638 else
7639 lo16_type = R_MIPS_LO16;
b49e97c9
TS
7640
7641 /* The combined value is the sum of the HI16 addend,
7642 left-shifted by sixteen bits, and the LO16
7643 addend, sign extended. (Usually, the code does
7644 a `lui' of the HI16 value, and then an `addiu' of
7645 the LO16 value.)
7646
4030e8f6
CD
7647 Scan ahead to find a matching LO16 relocation.
7648
7649 According to the MIPS ELF ABI, the R_MIPS_LO16
7650 relocation must be immediately following.
7651 However, for the IRIX6 ABI, the next relocation
7652 may be a composed relocation consisting of
7653 several relocations for the same address. In
7654 that case, the R_MIPS_LO16 relocation may occur
7655 as one of these. We permit a similar extension
7656 in general, as that is useful for GCC. */
7657 lo16_relocation = mips_elf_next_relocation (input_bfd,
d6f16593 7658 lo16_type,
b49e97c9
TS
7659 rel, relend);
7660 if (lo16_relocation == NULL)
b34976b6 7661 return FALSE;
b49e97c9 7662
d6f16593
MR
7663 lo16_location = contents + lo16_relocation->r_offset;
7664
b49e97c9 7665 /* Obtain the addend kept there. */
4030e8f6 7666 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd,
d6f16593
MR
7667 lo16_type, FALSE);
7668 _bfd_mips16_elf_reloc_unshuffle (input_bfd, lo16_type, FALSE,
7669 lo16_location);
b49e97c9
TS
7670 l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
7671 input_bfd, contents);
d6f16593
MR
7672 _bfd_mips16_elf_reloc_shuffle (input_bfd, lo16_type, FALSE,
7673 lo16_location);
b49e97c9 7674 l &= lo16_howto->src_mask;
5a659663 7675 l <<= lo16_howto->rightshift;
a7ebbfdf 7676 l = _bfd_mips_elf_sign_extend (l, 16);
b49e97c9
TS
7677
7678 addend <<= 16;
7679
7680 /* Compute the combined addend. */
7681 addend += l;
b49e97c9 7682 }
30ac9238
RS
7683 else
7684 addend <<= howto->rightshift;
b49e97c9
TS
7685 }
7686 else
7687 addend = rel->r_addend;
81d43bff
RS
7688 mips_elf_adjust_addend (output_bfd, info, input_bfd,
7689 local_syms, local_sections, rel);
b49e97c9
TS
7690 }
7691
1049f94e 7692 if (info->relocatable)
b49e97c9 7693 {
4a14403c 7694 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
7695 && bfd_big_endian (input_bfd))
7696 rel->r_offset -= 4;
7697
81d43bff 7698 if (!rela_relocation_p && rel->r_addend)
5a659663 7699 {
81d43bff 7700 addend += rel->r_addend;
30ac9238 7701 if (r_type == R_MIPS_HI16
4030e8f6 7702 || r_type == R_MIPS_GOT16)
5a659663
TS
7703 addend = mips_elf_high (addend);
7704 else if (r_type == R_MIPS_HIGHER)
7705 addend = mips_elf_higher (addend);
7706 else if (r_type == R_MIPS_HIGHEST)
7707 addend = mips_elf_highest (addend);
30ac9238
RS
7708 else
7709 addend >>= howto->rightshift;
b49e97c9 7710
30ac9238
RS
7711 /* We use the source mask, rather than the destination
7712 mask because the place to which we are writing will be
7713 source of the addend in the final link. */
b49e97c9
TS
7714 addend &= howto->src_mask;
7715
5a659663 7716 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
7717 /* See the comment above about using R_MIPS_64 in the 32-bit
7718 ABI. Here, we need to update the addend. It would be
7719 possible to get away with just using the R_MIPS_32 reloc
7720 but for endianness. */
7721 {
7722 bfd_vma sign_bits;
7723 bfd_vma low_bits;
7724 bfd_vma high_bits;
7725
7726 if (addend & ((bfd_vma) 1 << 31))
7727#ifdef BFD64
7728 sign_bits = ((bfd_vma) 1 << 32) - 1;
7729#else
7730 sign_bits = -1;
7731#endif
7732 else
7733 sign_bits = 0;
7734
7735 /* If we don't know that we have a 64-bit type,
7736 do two separate stores. */
7737 if (bfd_big_endian (input_bfd))
7738 {
7739 /* Store the sign-bits (which are most significant)
7740 first. */
7741 low_bits = sign_bits;
7742 high_bits = addend;
7743 }
7744 else
7745 {
7746 low_bits = addend;
7747 high_bits = sign_bits;
7748 }
7749 bfd_put_32 (input_bfd, low_bits,
7750 contents + rel->r_offset);
7751 bfd_put_32 (input_bfd, high_bits,
7752 contents + rel->r_offset + 4);
7753 continue;
7754 }
7755
7756 if (! mips_elf_perform_relocation (info, howto, rel, addend,
7757 input_bfd, input_section,
b34976b6
AM
7758 contents, FALSE))
7759 return FALSE;
b49e97c9
TS
7760 }
7761
7762 /* Go on to the next relocation. */
7763 continue;
7764 }
7765
7766 /* In the N32 and 64-bit ABIs there may be multiple consecutive
7767 relocations for the same offset. In that case we are
7768 supposed to treat the output of each relocation as the addend
7769 for the next. */
7770 if (rel + 1 < relend
7771 && rel->r_offset == rel[1].r_offset
7772 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 7773 use_saved_addend_p = TRUE;
b49e97c9 7774 else
b34976b6 7775 use_saved_addend_p = FALSE;
b49e97c9
TS
7776
7777 /* Figure out what value we are supposed to relocate. */
7778 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
7779 input_section, info, rel,
7780 addend, howto, local_syms,
7781 local_sections, &value,
bce03d3d
AO
7782 &name, &require_jalx,
7783 use_saved_addend_p))
b49e97c9
TS
7784 {
7785 case bfd_reloc_continue:
7786 /* There's nothing to do. */
7787 continue;
7788
7789 case bfd_reloc_undefined:
7790 /* mips_elf_calculate_relocation already called the
7791 undefined_symbol callback. There's no real point in
7792 trying to perform the relocation at this point, so we
7793 just skip ahead to the next relocation. */
7794 continue;
7795
7796 case bfd_reloc_notsupported:
7797 msg = _("internal error: unsupported relocation error");
7798 info->callbacks->warning
7799 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 7800 return FALSE;
b49e97c9
TS
7801
7802 case bfd_reloc_overflow:
7803 if (use_saved_addend_p)
7804 /* Ignore overflow until we reach the last relocation for
7805 a given location. */
7806 ;
7807 else
7808 {
7809 BFD_ASSERT (name != NULL);
7810 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f 7811 (info, NULL, name, howto->name, (bfd_vma) 0,
b49e97c9 7812 input_bfd, input_section, rel->r_offset)))
b34976b6 7813 return FALSE;
b49e97c9
TS
7814 }
7815 break;
7816
7817 case bfd_reloc_ok:
7818 break;
7819
7820 default:
7821 abort ();
7822 break;
7823 }
7824
7825 /* If we've got another relocation for the address, keep going
7826 until we reach the last one. */
7827 if (use_saved_addend_p)
7828 {
7829 addend = value;
7830 continue;
7831 }
7832
4a14403c 7833 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
7834 /* See the comment above about using R_MIPS_64 in the 32-bit
7835 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
7836 that calculated the right value. Now, however, we
7837 sign-extend the 32-bit result to 64-bits, and store it as a
7838 64-bit value. We are especially generous here in that we
7839 go to extreme lengths to support this usage on systems with
7840 only a 32-bit VMA. */
7841 {
7842 bfd_vma sign_bits;
7843 bfd_vma low_bits;
7844 bfd_vma high_bits;
7845
7846 if (value & ((bfd_vma) 1 << 31))
7847#ifdef BFD64
7848 sign_bits = ((bfd_vma) 1 << 32) - 1;
7849#else
7850 sign_bits = -1;
7851#endif
7852 else
7853 sign_bits = 0;
7854
7855 /* If we don't know that we have a 64-bit type,
7856 do two separate stores. */
7857 if (bfd_big_endian (input_bfd))
7858 {
7859 /* Undo what we did above. */
7860 rel->r_offset -= 4;
7861 /* Store the sign-bits (which are most significant)
7862 first. */
7863 low_bits = sign_bits;
7864 high_bits = value;
7865 }
7866 else
7867 {
7868 low_bits = value;
7869 high_bits = sign_bits;
7870 }
7871 bfd_put_32 (input_bfd, low_bits,
7872 contents + rel->r_offset);
7873 bfd_put_32 (input_bfd, high_bits,
7874 contents + rel->r_offset + 4);
7875 continue;
7876 }
7877
7878 /* Actually perform the relocation. */
7879 if (! mips_elf_perform_relocation (info, howto, rel, value,
7880 input_bfd, input_section,
7881 contents, require_jalx))
b34976b6 7882 return FALSE;
b49e97c9
TS
7883 }
7884
b34976b6 7885 return TRUE;
b49e97c9
TS
7886}
7887\f
7888/* If NAME is one of the special IRIX6 symbols defined by the linker,
7889 adjust it appropriately now. */
7890
7891static void
9719ad41
RS
7892mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
7893 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
7894{
7895 /* The linker script takes care of providing names and values for
7896 these, but we must place them into the right sections. */
7897 static const char* const text_section_symbols[] = {
7898 "_ftext",
7899 "_etext",
7900 "__dso_displacement",
7901 "__elf_header",
7902 "__program_header_table",
7903 NULL
7904 };
7905
7906 static const char* const data_section_symbols[] = {
7907 "_fdata",
7908 "_edata",
7909 "_end",
7910 "_fbss",
7911 NULL
7912 };
7913
7914 const char* const *p;
7915 int i;
7916
7917 for (i = 0; i < 2; ++i)
7918 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
7919 *p;
7920 ++p)
7921 if (strcmp (*p, name) == 0)
7922 {
7923 /* All of these symbols are given type STT_SECTION by the
7924 IRIX6 linker. */
7925 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 7926 sym->st_other = STO_PROTECTED;
b49e97c9
TS
7927
7928 /* The IRIX linker puts these symbols in special sections. */
7929 if (i == 0)
7930 sym->st_shndx = SHN_MIPS_TEXT;
7931 else
7932 sym->st_shndx = SHN_MIPS_DATA;
7933
7934 break;
7935 }
7936}
7937
7938/* Finish up dynamic symbol handling. We set the contents of various
7939 dynamic sections here. */
7940
b34976b6 7941bfd_boolean
9719ad41
RS
7942_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
7943 struct bfd_link_info *info,
7944 struct elf_link_hash_entry *h,
7945 Elf_Internal_Sym *sym)
b49e97c9
TS
7946{
7947 bfd *dynobj;
b49e97c9 7948 asection *sgot;
f4416af6 7949 struct mips_got_info *g, *gg;
b49e97c9 7950 const char *name;
b49e97c9
TS
7951
7952 dynobj = elf_hash_table (info)->dynobj;
b49e97c9 7953
c5ae1840 7954 if (h->plt.offset != MINUS_ONE)
b49e97c9
TS
7955 {
7956 asection *s;
7957 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7958
7959 /* This symbol has a stub. Set it up. */
7960
7961 BFD_ASSERT (h->dynindx != -1);
7962
7963 s = bfd_get_section_by_name (dynobj,
7964 MIPS_ELF_STUB_SECTION_NAME (dynobj));
7965 BFD_ASSERT (s != NULL);
7966
6ece8836 7967 /* FIXME: Can h->dynindx be more than 64K? */
b49e97c9 7968 if (h->dynindx & 0xffff0000)
b34976b6 7969 return FALSE;
b49e97c9
TS
7970
7971 /* Fill the stub. */
7972 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
7973 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
7974 bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
7975 bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
7976
eea6121a 7977 BFD_ASSERT (h->plt.offset <= s->size);
b49e97c9
TS
7978 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7979
7980 /* Mark the symbol as undefined. plt.offset != -1 occurs
7981 only for the referenced symbol. */
7982 sym->st_shndx = SHN_UNDEF;
7983
7984 /* The run-time linker uses the st_value field of the symbol
7985 to reset the global offset table entry for this external
7986 to its stub address when unlinking a shared object. */
c5ae1840
TS
7987 sym->st_value = (s->output_section->vma + s->output_offset
7988 + h->plt.offset);
b49e97c9
TS
7989 }
7990
7991 BFD_ASSERT (h->dynindx != -1
f5385ebf 7992 || h->forced_local);
b49e97c9 7993
f4416af6 7994 sgot = mips_elf_got_section (dynobj, FALSE);
b49e97c9 7995 BFD_ASSERT (sgot != NULL);
f4416af6 7996 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
f0abc2a1 7997 g = mips_elf_section_data (sgot)->u.got_info;
b49e97c9
TS
7998 BFD_ASSERT (g != NULL);
7999
8000 /* Run through the global symbol table, creating GOT entries for all
8001 the symbols that need them. */
8002 if (g->global_gotsym != NULL
8003 && h->dynindx >= g->global_gotsym->dynindx)
8004 {
8005 bfd_vma offset;
8006 bfd_vma value;
8007
6eaa6adc 8008 value = sym->st_value;
0f20cc35 8009 offset = mips_elf_global_got_index (dynobj, output_bfd, h, R_MIPS_GOT16, info);
b49e97c9
TS
8010 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
8011 }
8012
0f20cc35 8013 if (g->next && h->dynindx != -1 && h->type != STT_TLS)
f4416af6
AO
8014 {
8015 struct mips_got_entry e, *p;
0626d451 8016 bfd_vma entry;
f4416af6 8017 bfd_vma offset;
f4416af6
AO
8018
8019 gg = g;
8020
8021 e.abfd = output_bfd;
8022 e.symndx = -1;
8023 e.d.h = (struct mips_elf_link_hash_entry *)h;
0f20cc35 8024 e.tls_type = 0;
143d77c5 8025
f4416af6
AO
8026 for (g = g->next; g->next != gg; g = g->next)
8027 {
8028 if (g->got_entries
8029 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
8030 &e)))
8031 {
8032 offset = p->gotidx;
0626d451
RS
8033 if (info->shared
8034 || (elf_hash_table (info)->dynamic_sections_created
8035 && p->d.h != NULL
f5385ebf
AM
8036 && p->d.h->root.def_dynamic
8037 && !p->d.h->root.def_regular))
0626d451
RS
8038 {
8039 /* Create an R_MIPS_REL32 relocation for this entry. Due to
8040 the various compatibility problems, it's easier to mock
8041 up an R_MIPS_32 or R_MIPS_64 relocation and leave
8042 mips_elf_create_dynamic_relocation to calculate the
8043 appropriate addend. */
8044 Elf_Internal_Rela rel[3];
8045
8046 memset (rel, 0, sizeof (rel));
8047 if (ABI_64_P (output_bfd))
8048 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
8049 else
8050 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
8051 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
8052
8053 entry = 0;
8054 if (! (mips_elf_create_dynamic_relocation
8055 (output_bfd, info, rel,
8056 e.d.h, NULL, sym->st_value, &entry, sgot)))
8057 return FALSE;
8058 }
8059 else
8060 entry = sym->st_value;
8061 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
8062 }
8063 }
8064 }
8065
b49e97c9
TS
8066 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
8067 name = h->root.root.string;
8068 if (strcmp (name, "_DYNAMIC") == 0
22edb2f1 8069 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
8070 sym->st_shndx = SHN_ABS;
8071 else if (strcmp (name, "_DYNAMIC_LINK") == 0
8072 || strcmp (name, "_DYNAMIC_LINKING") == 0)
8073 {
8074 sym->st_shndx = SHN_ABS;
8075 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8076 sym->st_value = 1;
8077 }
4a14403c 8078 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
8079 {
8080 sym->st_shndx = SHN_ABS;
8081 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8082 sym->st_value = elf_gp (output_bfd);
8083 }
8084 else if (SGI_COMPAT (output_bfd))
8085 {
8086 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
8087 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
8088 {
8089 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8090 sym->st_other = STO_PROTECTED;
8091 sym->st_value = 0;
8092 sym->st_shndx = SHN_MIPS_DATA;
8093 }
8094 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
8095 {
8096 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8097 sym->st_other = STO_PROTECTED;
8098 sym->st_value = mips_elf_hash_table (info)->procedure_count;
8099 sym->st_shndx = SHN_ABS;
8100 }
8101 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
8102 {
8103 if (h->type == STT_FUNC)
8104 sym->st_shndx = SHN_MIPS_TEXT;
8105 else if (h->type == STT_OBJECT)
8106 sym->st_shndx = SHN_MIPS_DATA;
8107 }
8108 }
8109
8110 /* Handle the IRIX6-specific symbols. */
8111 if (IRIX_COMPAT (output_bfd) == ict_irix6)
8112 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
8113
8114 if (! info->shared)
8115 {
8116 if (! mips_elf_hash_table (info)->use_rld_obj_head
8117 && (strcmp (name, "__rld_map") == 0
8118 || strcmp (name, "__RLD_MAP") == 0))
8119 {
8120 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
8121 BFD_ASSERT (s != NULL);
8122 sym->st_value = s->output_section->vma + s->output_offset;
9719ad41 8123 bfd_put_32 (output_bfd, 0, s->contents);
b49e97c9
TS
8124 if (mips_elf_hash_table (info)->rld_value == 0)
8125 mips_elf_hash_table (info)->rld_value = sym->st_value;
8126 }
8127 else if (mips_elf_hash_table (info)->use_rld_obj_head
8128 && strcmp (name, "__rld_obj_head") == 0)
8129 {
8130 /* IRIX6 does not use a .rld_map section. */
8131 if (IRIX_COMPAT (output_bfd) == ict_irix5
8132 || IRIX_COMPAT (output_bfd) == ict_none)
8133 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
8134 != NULL);
8135 mips_elf_hash_table (info)->rld_value = sym->st_value;
8136 }
8137 }
8138
8139 /* If this is a mips16 symbol, force the value to be even. */
79cda7cf
FF
8140 if (sym->st_other == STO_MIPS16)
8141 sym->st_value &= ~1;
b49e97c9 8142
b34976b6 8143 return TRUE;
b49e97c9
TS
8144}
8145
0a44bf69
RS
8146/* Likewise, for VxWorks. */
8147
8148bfd_boolean
8149_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
8150 struct bfd_link_info *info,
8151 struct elf_link_hash_entry *h,
8152 Elf_Internal_Sym *sym)
8153{
8154 bfd *dynobj;
8155 asection *sgot;
8156 struct mips_got_info *g;
8157 struct mips_elf_link_hash_table *htab;
8158
8159 htab = mips_elf_hash_table (info);
8160 dynobj = elf_hash_table (info)->dynobj;
8161
8162 if (h->plt.offset != (bfd_vma) -1)
8163 {
6d79d2ed 8164 bfd_byte *loc;
0a44bf69
RS
8165 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
8166 Elf_Internal_Rela rel;
8167 static const bfd_vma *plt_entry;
8168
8169 BFD_ASSERT (h->dynindx != -1);
8170 BFD_ASSERT (htab->splt != NULL);
8171 BFD_ASSERT (h->plt.offset <= htab->splt->size);
8172
8173 /* Calculate the address of the .plt entry. */
8174 plt_address = (htab->splt->output_section->vma
8175 + htab->splt->output_offset
8176 + h->plt.offset);
8177
8178 /* Calculate the index of the entry. */
8179 plt_index = ((h->plt.offset - htab->plt_header_size)
8180 / htab->plt_entry_size);
8181
8182 /* Calculate the address of the .got.plt entry. */
8183 got_address = (htab->sgotplt->output_section->vma
8184 + htab->sgotplt->output_offset
8185 + plt_index * 4);
8186
8187 /* Calculate the offset of the .got.plt entry from
8188 _GLOBAL_OFFSET_TABLE_. */
8189 got_offset = mips_elf_gotplt_index (info, h);
8190
8191 /* Calculate the offset for the branch at the start of the PLT
8192 entry. The branch jumps to the beginning of .plt. */
8193 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
8194
8195 /* Fill in the initial value of the .got.plt entry. */
8196 bfd_put_32 (output_bfd, plt_address,
8197 htab->sgotplt->contents + plt_index * 4);
8198
8199 /* Find out where the .plt entry should go. */
8200 loc = htab->splt->contents + h->plt.offset;
8201
8202 if (info->shared)
8203 {
8204 plt_entry = mips_vxworks_shared_plt_entry;
8205 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
8206 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
8207 }
8208 else
8209 {
8210 bfd_vma got_address_high, got_address_low;
8211
8212 plt_entry = mips_vxworks_exec_plt_entry;
8213 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
8214 got_address_low = got_address & 0xffff;
8215
8216 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
8217 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
8218 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
8219 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
8220 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
8221 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
8222 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
8223 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
8224
8225 loc = (htab->srelplt2->contents
8226 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
8227
8228 /* Emit a relocation for the .got.plt entry. */
8229 rel.r_offset = got_address;
8230 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
8231 rel.r_addend = h->plt.offset;
8232 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8233
8234 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
8235 loc += sizeof (Elf32_External_Rela);
8236 rel.r_offset = plt_address + 8;
8237 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
8238 rel.r_addend = got_offset;
8239 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8240
8241 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
8242 loc += sizeof (Elf32_External_Rela);
8243 rel.r_offset += 4;
8244 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
8245 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8246 }
8247
8248 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
8249 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
8250 rel.r_offset = got_address;
8251 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
8252 rel.r_addend = 0;
8253 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8254
8255 if (!h->def_regular)
8256 sym->st_shndx = SHN_UNDEF;
8257 }
8258
8259 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
8260
8261 sgot = mips_elf_got_section (dynobj, FALSE);
8262 BFD_ASSERT (sgot != NULL);
8263 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
8264 g = mips_elf_section_data (sgot)->u.got_info;
8265 BFD_ASSERT (g != NULL);
8266
8267 /* See if this symbol has an entry in the GOT. */
8268 if (g->global_gotsym != NULL
8269 && h->dynindx >= g->global_gotsym->dynindx)
8270 {
8271 bfd_vma offset;
8272 Elf_Internal_Rela outrel;
8273 bfd_byte *loc;
8274 asection *s;
8275
8276 /* Install the symbol value in the GOT. */
8277 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
8278 R_MIPS_GOT16, info);
8279 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
8280
8281 /* Add a dynamic relocation for it. */
8282 s = mips_elf_rel_dyn_section (info, FALSE);
8283 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
8284 outrel.r_offset = (sgot->output_section->vma
8285 + sgot->output_offset
8286 + offset);
8287 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
8288 outrel.r_addend = 0;
8289 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
8290 }
8291
8292 /* Emit a copy reloc, if needed. */
8293 if (h->needs_copy)
8294 {
8295 Elf_Internal_Rela rel;
8296
8297 BFD_ASSERT (h->dynindx != -1);
8298
8299 rel.r_offset = (h->root.u.def.section->output_section->vma
8300 + h->root.u.def.section->output_offset
8301 + h->root.u.def.value);
8302 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
8303 rel.r_addend = 0;
8304 bfd_elf32_swap_reloca_out (output_bfd, &rel,
8305 htab->srelbss->contents
8306 + (htab->srelbss->reloc_count
8307 * sizeof (Elf32_External_Rela)));
8308 ++htab->srelbss->reloc_count;
8309 }
8310
8311 /* If this is a mips16 symbol, force the value to be even. */
8312 if (sym->st_other == STO_MIPS16)
8313 sym->st_value &= ~1;
8314
8315 return TRUE;
8316}
8317
8318/* Install the PLT header for a VxWorks executable and finalize the
8319 contents of .rela.plt.unloaded. */
8320
8321static void
8322mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
8323{
8324 Elf_Internal_Rela rela;
8325 bfd_byte *loc;
8326 bfd_vma got_value, got_value_high, got_value_low, plt_address;
8327 static const bfd_vma *plt_entry;
8328 struct mips_elf_link_hash_table *htab;
8329
8330 htab = mips_elf_hash_table (info);
8331 plt_entry = mips_vxworks_exec_plt0_entry;
8332
8333 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
8334 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
8335 + htab->root.hgot->root.u.def.section->output_offset
8336 + htab->root.hgot->root.u.def.value);
8337
8338 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
8339 got_value_low = got_value & 0xffff;
8340
8341 /* Calculate the address of the PLT header. */
8342 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
8343
8344 /* Install the PLT header. */
8345 loc = htab->splt->contents;
8346 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
8347 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
8348 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
8349 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
8350 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
8351 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
8352
8353 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
8354 loc = htab->srelplt2->contents;
8355 rela.r_offset = plt_address;
8356 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
8357 rela.r_addend = 0;
8358 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8359 loc += sizeof (Elf32_External_Rela);
8360
8361 /* Output the relocation for the following addiu of
8362 %lo(_GLOBAL_OFFSET_TABLE_). */
8363 rela.r_offset += 4;
8364 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
8365 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8366 loc += sizeof (Elf32_External_Rela);
8367
8368 /* Fix up the remaining relocations. They may have the wrong
8369 symbol index for _G_O_T_ or _P_L_T_ depending on the order
8370 in which symbols were output. */
8371 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
8372 {
8373 Elf_Internal_Rela rel;
8374
8375 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
8376 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
8377 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8378 loc += sizeof (Elf32_External_Rela);
8379
8380 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
8381 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
8382 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8383 loc += sizeof (Elf32_External_Rela);
8384
8385 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
8386 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
8387 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
8388 loc += sizeof (Elf32_External_Rela);
8389 }
8390}
8391
8392/* Install the PLT header for a VxWorks shared library. */
8393
8394static void
8395mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
8396{
8397 unsigned int i;
8398 struct mips_elf_link_hash_table *htab;
8399
8400 htab = mips_elf_hash_table (info);
8401
8402 /* We just need to copy the entry byte-by-byte. */
8403 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
8404 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
8405 htab->splt->contents + i * 4);
8406}
8407
b49e97c9
TS
8408/* Finish up the dynamic sections. */
8409
b34976b6 8410bfd_boolean
9719ad41
RS
8411_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
8412 struct bfd_link_info *info)
b49e97c9
TS
8413{
8414 bfd *dynobj;
8415 asection *sdyn;
8416 asection *sgot;
f4416af6 8417 struct mips_got_info *gg, *g;
0a44bf69 8418 struct mips_elf_link_hash_table *htab;
b49e97c9 8419
0a44bf69 8420 htab = mips_elf_hash_table (info);
b49e97c9
TS
8421 dynobj = elf_hash_table (info)->dynobj;
8422
8423 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8424
f4416af6 8425 sgot = mips_elf_got_section (dynobj, FALSE);
b49e97c9 8426 if (sgot == NULL)
f4416af6 8427 gg = g = NULL;
b49e97c9
TS
8428 else
8429 {
f4416af6
AO
8430 BFD_ASSERT (mips_elf_section_data (sgot) != NULL);
8431 gg = mips_elf_section_data (sgot)->u.got_info;
8432 BFD_ASSERT (gg != NULL);
8433 g = mips_elf_got_for_ibfd (gg, output_bfd);
b49e97c9
TS
8434 BFD_ASSERT (g != NULL);
8435 }
8436
8437 if (elf_hash_table (info)->dynamic_sections_created)
8438 {
8439 bfd_byte *b;
8440
8441 BFD_ASSERT (sdyn != NULL);
8442 BFD_ASSERT (g != NULL);
8443
8444 for (b = sdyn->contents;
eea6121a 8445 b < sdyn->contents + sdyn->size;
b49e97c9
TS
8446 b += MIPS_ELF_DYN_SIZE (dynobj))
8447 {
8448 Elf_Internal_Dyn dyn;
8449 const char *name;
8450 size_t elemsize;
8451 asection *s;
b34976b6 8452 bfd_boolean swap_out_p;
b49e97c9
TS
8453
8454 /* Read in the current dynamic entry. */
8455 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8456
8457 /* Assume that we're going to modify it and write it out. */
b34976b6 8458 swap_out_p = TRUE;
b49e97c9
TS
8459
8460 switch (dyn.d_tag)
8461 {
8462 case DT_RELENT:
b49e97c9
TS
8463 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
8464 break;
8465
0a44bf69
RS
8466 case DT_RELAENT:
8467 BFD_ASSERT (htab->is_vxworks);
8468 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
8469 break;
8470
b49e97c9
TS
8471 case DT_STRSZ:
8472 /* Rewrite DT_STRSZ. */
8473 dyn.d_un.d_val =
8474 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
8475 break;
8476
8477 case DT_PLTGOT:
8478 name = ".got";
0a44bf69
RS
8479 if (htab->is_vxworks)
8480 {
8481 /* _GLOBAL_OFFSET_TABLE_ is defined to be the beginning
8482 of the ".got" section in DYNOBJ. */
8483 s = bfd_get_section_by_name (dynobj, name);
8484 BFD_ASSERT (s != NULL);
8485 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8486 }
8487 else
8488 {
8489 s = bfd_get_section_by_name (output_bfd, name);
8490 BFD_ASSERT (s != NULL);
8491 dyn.d_un.d_ptr = s->vma;
8492 }
b49e97c9
TS
8493 break;
8494
8495 case DT_MIPS_RLD_VERSION:
8496 dyn.d_un.d_val = 1; /* XXX */
8497 break;
8498
8499 case DT_MIPS_FLAGS:
8500 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
8501 break;
8502
b49e97c9 8503 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
8504 {
8505 time_t t;
8506 time (&t);
8507 dyn.d_un.d_val = t;
8508 }
b49e97c9
TS
8509 break;
8510
8511 case DT_MIPS_ICHECKSUM:
8512 /* XXX FIXME: */
b34976b6 8513 swap_out_p = FALSE;
b49e97c9
TS
8514 break;
8515
8516 case DT_MIPS_IVERSION:
8517 /* XXX FIXME: */
b34976b6 8518 swap_out_p = FALSE;
b49e97c9
TS
8519 break;
8520
8521 case DT_MIPS_BASE_ADDRESS:
8522 s = output_bfd->sections;
8523 BFD_ASSERT (s != NULL);
8524 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
8525 break;
8526
8527 case DT_MIPS_LOCAL_GOTNO:
8528 dyn.d_un.d_val = g->local_gotno;
8529 break;
8530
8531 case DT_MIPS_UNREFEXTNO:
8532 /* The index into the dynamic symbol table which is the
8533 entry of the first external symbol that is not
8534 referenced within the same object. */
8535 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
8536 break;
8537
8538 case DT_MIPS_GOTSYM:
f4416af6 8539 if (gg->global_gotsym)
b49e97c9 8540 {
f4416af6 8541 dyn.d_un.d_val = gg->global_gotsym->dynindx;
b49e97c9
TS
8542 break;
8543 }
8544 /* In case if we don't have global got symbols we default
8545 to setting DT_MIPS_GOTSYM to the same value as
8546 DT_MIPS_SYMTABNO, so we just fall through. */
8547
8548 case DT_MIPS_SYMTABNO:
8549 name = ".dynsym";
8550 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
8551 s = bfd_get_section_by_name (output_bfd, name);
8552 BFD_ASSERT (s != NULL);
8553
eea6121a 8554 dyn.d_un.d_val = s->size / elemsize;
b49e97c9
TS
8555 break;
8556
8557 case DT_MIPS_HIPAGENO:
0a44bf69 8558 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO (info);
b49e97c9
TS
8559 break;
8560
8561 case DT_MIPS_RLD_MAP:
8562 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
8563 break;
8564
8565 case DT_MIPS_OPTIONS:
8566 s = (bfd_get_section_by_name
8567 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
8568 dyn.d_un.d_ptr = s->vma;
8569 break;
8570
0a44bf69
RS
8571 case DT_RELASZ:
8572 BFD_ASSERT (htab->is_vxworks);
8573 /* The count does not include the JUMP_SLOT relocations. */
8574 if (htab->srelplt)
8575 dyn.d_un.d_val -= htab->srelplt->size;
8576 break;
8577
8578 case DT_PLTREL:
8579 BFD_ASSERT (htab->is_vxworks);
8580 dyn.d_un.d_val = DT_RELA;
8581 break;
8582
8583 case DT_PLTRELSZ:
8584 BFD_ASSERT (htab->is_vxworks);
8585 dyn.d_un.d_val = htab->srelplt->size;
8586 break;
8587
8588 case DT_JMPREL:
8589 BFD_ASSERT (htab->is_vxworks);
8590 dyn.d_un.d_val = (htab->srelplt->output_section->vma
8591 + htab->srelplt->output_offset);
8592 break;
8593
b49e97c9 8594 default:
b34976b6 8595 swap_out_p = FALSE;
b49e97c9
TS
8596 break;
8597 }
8598
8599 if (swap_out_p)
8600 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8601 (dynobj, &dyn, b);
8602 }
8603 }
8604
eea6121a 8605 if (sgot != NULL && sgot->size > 0)
b49e97c9 8606 {
0a44bf69
RS
8607 if (htab->is_vxworks)
8608 {
8609 /* The first entry of the global offset table points to the
8610 ".dynamic" section. The second is initialized by the
8611 loader and contains the shared library identifier.
8612 The third is also initialized by the loader and points
8613 to the lazy resolution stub. */
8614 MIPS_ELF_PUT_WORD (output_bfd,
8615 sdyn->output_offset + sdyn->output_section->vma,
8616 sgot->contents);
8617 MIPS_ELF_PUT_WORD (output_bfd, 0,
8618 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8619 MIPS_ELF_PUT_WORD (output_bfd, 0,
8620 sgot->contents
8621 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
8622 }
8623 else
8624 {
8625 /* The first entry of the global offset table will be filled at
8626 runtime. The second entry will be used by some runtime loaders.
8627 This isn't the case of IRIX rld. */
8628 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
8629 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
8630 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
8631 }
b49e97c9
TS
8632 }
8633
8634 if (sgot != NULL)
8635 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
8636 = MIPS_ELF_GOT_SIZE (output_bfd);
8637
f4416af6
AO
8638 /* Generate dynamic relocations for the non-primary gots. */
8639 if (gg != NULL && gg->next)
8640 {
8641 Elf_Internal_Rela rel[3];
8642 bfd_vma addend = 0;
8643
8644 memset (rel, 0, sizeof (rel));
8645 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
8646
8647 for (g = gg->next; g->next != gg; g = g->next)
8648 {
0f20cc35
DJ
8649 bfd_vma index = g->next->local_gotno + g->next->global_gotno
8650 + g->next->tls_gotno;
f4416af6 8651
9719ad41 8652 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
f4416af6 8653 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
9719ad41 8654 MIPS_ELF_PUT_WORD (output_bfd, 0x80000000, sgot->contents
f4416af6
AO
8655 + index++ * MIPS_ELF_GOT_SIZE (output_bfd));
8656
8657 if (! info->shared)
8658 continue;
8659
8660 while (index < g->assigned_gotno)
8661 {
8662 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
8663 = index++ * MIPS_ELF_GOT_SIZE (output_bfd);
8664 if (!(mips_elf_create_dynamic_relocation
8665 (output_bfd, info, rel, NULL,
8666 bfd_abs_section_ptr,
8667 0, &addend, sgot)))
8668 return FALSE;
8669 BFD_ASSERT (addend == 0);
8670 }
8671 }
8672 }
8673
3133ddbf
DJ
8674 /* The generation of dynamic relocations for the non-primary gots
8675 adds more dynamic relocations. We cannot count them until
8676 here. */
8677
8678 if (elf_hash_table (info)->dynamic_sections_created)
8679 {
8680 bfd_byte *b;
8681 bfd_boolean swap_out_p;
8682
8683 BFD_ASSERT (sdyn != NULL);
8684
8685 for (b = sdyn->contents;
8686 b < sdyn->contents + sdyn->size;
8687 b += MIPS_ELF_DYN_SIZE (dynobj))
8688 {
8689 Elf_Internal_Dyn dyn;
8690 asection *s;
8691
8692 /* Read in the current dynamic entry. */
8693 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
8694
8695 /* Assume that we're going to modify it and write it out. */
8696 swap_out_p = TRUE;
8697
8698 switch (dyn.d_tag)
8699 {
8700 case DT_RELSZ:
8701 /* Reduce DT_RELSZ to account for any relocations we
8702 decided not to make. This is for the n64 irix rld,
8703 which doesn't seem to apply any relocations if there
8704 are trailing null entries. */
0a44bf69 8705 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
8706 dyn.d_un.d_val = (s->reloc_count
8707 * (ABI_64_P (output_bfd)
8708 ? sizeof (Elf64_Mips_External_Rel)
8709 : sizeof (Elf32_External_Rel)));
8710 break;
8711
8712 default:
8713 swap_out_p = FALSE;
8714 break;
8715 }
8716
8717 if (swap_out_p)
8718 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
8719 (dynobj, &dyn, b);
8720 }
8721 }
8722
b49e97c9 8723 {
b49e97c9
TS
8724 asection *s;
8725 Elf32_compact_rel cpt;
8726
b49e97c9
TS
8727 if (SGI_COMPAT (output_bfd))
8728 {
8729 /* Write .compact_rel section out. */
8730 s = bfd_get_section_by_name (dynobj, ".compact_rel");
8731 if (s != NULL)
8732 {
8733 cpt.id1 = 1;
8734 cpt.num = s->reloc_count;
8735 cpt.id2 = 2;
8736 cpt.offset = (s->output_section->filepos
8737 + sizeof (Elf32_External_compact_rel));
8738 cpt.reserved0 = 0;
8739 cpt.reserved1 = 0;
8740 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
8741 ((Elf32_External_compact_rel *)
8742 s->contents));
8743
8744 /* Clean up a dummy stub function entry in .text. */
8745 s = bfd_get_section_by_name (dynobj,
8746 MIPS_ELF_STUB_SECTION_NAME (dynobj));
8747 if (s != NULL)
8748 {
8749 file_ptr dummy_offset;
8750
eea6121a
AM
8751 BFD_ASSERT (s->size >= MIPS_FUNCTION_STUB_SIZE);
8752 dummy_offset = s->size - MIPS_FUNCTION_STUB_SIZE;
b49e97c9
TS
8753 memset (s->contents + dummy_offset, 0,
8754 MIPS_FUNCTION_STUB_SIZE);
8755 }
8756 }
8757 }
8758
0a44bf69
RS
8759 /* The psABI says that the dynamic relocations must be sorted in
8760 increasing order of r_symndx. The VxWorks EABI doesn't require
8761 this, and because the code below handles REL rather than RELA
8762 relocations, using it for VxWorks would be outright harmful. */
8763 if (!htab->is_vxworks)
b49e97c9 8764 {
0a44bf69
RS
8765 s = mips_elf_rel_dyn_section (info, FALSE);
8766 if (s != NULL
8767 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
8768 {
8769 reldyn_sorting_bfd = output_bfd;
b49e97c9 8770
0a44bf69
RS
8771 if (ABI_64_P (output_bfd))
8772 qsort ((Elf64_External_Rel *) s->contents + 1,
8773 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
8774 sort_dynamic_relocs_64);
8775 else
8776 qsort ((Elf32_External_Rel *) s->contents + 1,
8777 s->reloc_count - 1, sizeof (Elf32_External_Rel),
8778 sort_dynamic_relocs);
8779 }
b49e97c9 8780 }
b49e97c9
TS
8781 }
8782
0a44bf69
RS
8783 if (htab->is_vxworks && htab->splt->size > 0)
8784 {
8785 if (info->shared)
8786 mips_vxworks_finish_shared_plt (output_bfd, info);
8787 else
8788 mips_vxworks_finish_exec_plt (output_bfd, info);
8789 }
b34976b6 8790 return TRUE;
b49e97c9
TS
8791}
8792
b49e97c9 8793
64543e1a
RS
8794/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
8795
8796static void
9719ad41 8797mips_set_isa_flags (bfd *abfd)
b49e97c9 8798{
64543e1a 8799 flagword val;
b49e97c9
TS
8800
8801 switch (bfd_get_mach (abfd))
8802 {
8803 default:
8804 case bfd_mach_mips3000:
8805 val = E_MIPS_ARCH_1;
8806 break;
8807
8808 case bfd_mach_mips3900:
8809 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
8810 break;
8811
8812 case bfd_mach_mips6000:
8813 val = E_MIPS_ARCH_2;
8814 break;
8815
8816 case bfd_mach_mips4000:
8817 case bfd_mach_mips4300:
8818 case bfd_mach_mips4400:
8819 case bfd_mach_mips4600:
8820 val = E_MIPS_ARCH_3;
8821 break;
8822
8823 case bfd_mach_mips4010:
8824 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
8825 break;
8826
8827 case bfd_mach_mips4100:
8828 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
8829 break;
8830
8831 case bfd_mach_mips4111:
8832 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
8833 break;
8834
00707a0e
RS
8835 case bfd_mach_mips4120:
8836 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
8837 break;
8838
b49e97c9
TS
8839 case bfd_mach_mips4650:
8840 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
8841 break;
8842
00707a0e
RS
8843 case bfd_mach_mips5400:
8844 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
8845 break;
8846
8847 case bfd_mach_mips5500:
8848 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
8849 break;
8850
0d2e43ed
ILT
8851 case bfd_mach_mips9000:
8852 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
8853 break;
8854
b49e97c9 8855 case bfd_mach_mips5000:
5a7ea749 8856 case bfd_mach_mips7000:
b49e97c9
TS
8857 case bfd_mach_mips8000:
8858 case bfd_mach_mips10000:
8859 case bfd_mach_mips12000:
8860 val = E_MIPS_ARCH_4;
8861 break;
8862
8863 case bfd_mach_mips5:
8864 val = E_MIPS_ARCH_5;
8865 break;
8866
8867 case bfd_mach_mips_sb1:
8868 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
8869 break;
8870
8871 case bfd_mach_mipsisa32:
8872 val = E_MIPS_ARCH_32;
8873 break;
8874
8875 case bfd_mach_mipsisa64:
8876 val = E_MIPS_ARCH_64;
af7ee8bf
CD
8877 break;
8878
8879 case bfd_mach_mipsisa32r2:
8880 val = E_MIPS_ARCH_32R2;
8881 break;
5f74bc13
CD
8882
8883 case bfd_mach_mipsisa64r2:
8884 val = E_MIPS_ARCH_64R2;
8885 break;
b49e97c9 8886 }
b49e97c9
TS
8887 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
8888 elf_elfheader (abfd)->e_flags |= val;
8889
64543e1a
RS
8890}
8891
8892
8893/* The final processing done just before writing out a MIPS ELF object
8894 file. This gets the MIPS architecture right based on the machine
8895 number. This is used by both the 32-bit and the 64-bit ABI. */
8896
8897void
9719ad41
RS
8898_bfd_mips_elf_final_write_processing (bfd *abfd,
8899 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
8900{
8901 unsigned int i;
8902 Elf_Internal_Shdr **hdrpp;
8903 const char *name;
8904 asection *sec;
8905
8906 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
8907 is nonzero. This is for compatibility with old objects, which used
8908 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
8909 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
8910 mips_set_isa_flags (abfd);
8911
b49e97c9
TS
8912 /* Set the sh_info field for .gptab sections and other appropriate
8913 info for each special section. */
8914 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
8915 i < elf_numsections (abfd);
8916 i++, hdrpp++)
8917 {
8918 switch ((*hdrpp)->sh_type)
8919 {
8920 case SHT_MIPS_MSYM:
8921 case SHT_MIPS_LIBLIST:
8922 sec = bfd_get_section_by_name (abfd, ".dynstr");
8923 if (sec != NULL)
8924 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8925 break;
8926
8927 case SHT_MIPS_GPTAB:
8928 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
8929 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
8930 BFD_ASSERT (name != NULL
8931 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
8932 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
8933 BFD_ASSERT (sec != NULL);
8934 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
8935 break;
8936
8937 case SHT_MIPS_CONTENT:
8938 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
8939 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
8940 BFD_ASSERT (name != NULL
8941 && strncmp (name, ".MIPS.content",
8942 sizeof ".MIPS.content" - 1) == 0);
8943 sec = bfd_get_section_by_name (abfd,
8944 name + sizeof ".MIPS.content" - 1);
8945 BFD_ASSERT (sec != NULL);
8946 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8947 break;
8948
8949 case SHT_MIPS_SYMBOL_LIB:
8950 sec = bfd_get_section_by_name (abfd, ".dynsym");
8951 if (sec != NULL)
8952 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8953 sec = bfd_get_section_by_name (abfd, ".liblist");
8954 if (sec != NULL)
8955 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
8956 break;
8957
8958 case SHT_MIPS_EVENTS:
8959 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
8960 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
8961 BFD_ASSERT (name != NULL);
8962 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
8963 sec = bfd_get_section_by_name (abfd,
8964 name + sizeof ".MIPS.events" - 1);
8965 else
8966 {
8967 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
8968 sizeof ".MIPS.post_rel" - 1) == 0);
8969 sec = bfd_get_section_by_name (abfd,
8970 (name
8971 + sizeof ".MIPS.post_rel" - 1));
8972 }
8973 BFD_ASSERT (sec != NULL);
8974 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
8975 break;
8976
8977 }
8978 }
8979}
8980\f
8dc1a139 8981/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
8982 segments. */
8983
8984int
9719ad41 8985_bfd_mips_elf_additional_program_headers (bfd *abfd)
b49e97c9
TS
8986{
8987 asection *s;
8988 int ret = 0;
8989
8990 /* See if we need a PT_MIPS_REGINFO segment. */
8991 s = bfd_get_section_by_name (abfd, ".reginfo");
8992 if (s && (s->flags & SEC_LOAD))
8993 ++ret;
8994
8995 /* See if we need a PT_MIPS_OPTIONS segment. */
8996 if (IRIX_COMPAT (abfd) == ict_irix6
8997 && bfd_get_section_by_name (abfd,
8998 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
8999 ++ret;
9000
9001 /* See if we need a PT_MIPS_RTPROC segment. */
9002 if (IRIX_COMPAT (abfd) == ict_irix5
9003 && bfd_get_section_by_name (abfd, ".dynamic")
9004 && bfd_get_section_by_name (abfd, ".mdebug"))
9005 ++ret;
9006
9007 return ret;
9008}
9009
8dc1a139 9010/* Modify the segment map for an IRIX5 executable. */
b49e97c9 9011
b34976b6 9012bfd_boolean
9719ad41
RS
9013_bfd_mips_elf_modify_segment_map (bfd *abfd,
9014 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
9015{
9016 asection *s;
9017 struct elf_segment_map *m, **pm;
9018 bfd_size_type amt;
9019
9020 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
9021 segment. */
9022 s = bfd_get_section_by_name (abfd, ".reginfo");
9023 if (s != NULL && (s->flags & SEC_LOAD) != 0)
9024 {
9025 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9026 if (m->p_type == PT_MIPS_REGINFO)
9027 break;
9028 if (m == NULL)
9029 {
9030 amt = sizeof *m;
9719ad41 9031 m = bfd_zalloc (abfd, amt);
b49e97c9 9032 if (m == NULL)
b34976b6 9033 return FALSE;
b49e97c9
TS
9034
9035 m->p_type = PT_MIPS_REGINFO;
9036 m->count = 1;
9037 m->sections[0] = s;
9038
9039 /* We want to put it after the PHDR and INTERP segments. */
9040 pm = &elf_tdata (abfd)->segment_map;
9041 while (*pm != NULL
9042 && ((*pm)->p_type == PT_PHDR
9043 || (*pm)->p_type == PT_INTERP))
9044 pm = &(*pm)->next;
9045
9046 m->next = *pm;
9047 *pm = m;
9048 }
9049 }
9050
9051 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
9052 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 9053 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 9054 table. */
c1fd6598
AO
9055 if (NEWABI_P (abfd)
9056 /* On non-IRIX6 new abi, we'll have already created a segment
9057 for this section, so don't create another. I'm not sure this
9058 is not also the case for IRIX 6, but I can't test it right
9059 now. */
9060 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
9061 {
9062 for (s = abfd->sections; s; s = s->next)
9063 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
9064 break;
9065
9066 if (s)
9067 {
9068 struct elf_segment_map *options_segment;
9069
98a8deaf
RS
9070 pm = &elf_tdata (abfd)->segment_map;
9071 while (*pm != NULL
9072 && ((*pm)->p_type == PT_PHDR
9073 || (*pm)->p_type == PT_INTERP))
9074 pm = &(*pm)->next;
b49e97c9
TS
9075
9076 amt = sizeof (struct elf_segment_map);
9077 options_segment = bfd_zalloc (abfd, amt);
9078 options_segment->next = *pm;
9079 options_segment->p_type = PT_MIPS_OPTIONS;
9080 options_segment->p_flags = PF_R;
b34976b6 9081 options_segment->p_flags_valid = TRUE;
b49e97c9
TS
9082 options_segment->count = 1;
9083 options_segment->sections[0] = s;
9084 *pm = options_segment;
9085 }
9086 }
9087 else
9088 {
9089 if (IRIX_COMPAT (abfd) == ict_irix5)
9090 {
9091 /* If there are .dynamic and .mdebug sections, we make a room
9092 for the RTPROC header. FIXME: Rewrite without section names. */
9093 if (bfd_get_section_by_name (abfd, ".interp") == NULL
9094 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
9095 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
9096 {
9097 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
9098 if (m->p_type == PT_MIPS_RTPROC)
9099 break;
9100 if (m == NULL)
9101 {
9102 amt = sizeof *m;
9719ad41 9103 m = bfd_zalloc (abfd, amt);
b49e97c9 9104 if (m == NULL)
b34976b6 9105 return FALSE;
b49e97c9
TS
9106
9107 m->p_type = PT_MIPS_RTPROC;
9108
9109 s = bfd_get_section_by_name (abfd, ".rtproc");
9110 if (s == NULL)
9111 {
9112 m->count = 0;
9113 m->p_flags = 0;
9114 m->p_flags_valid = 1;
9115 }
9116 else
9117 {
9118 m->count = 1;
9119 m->sections[0] = s;
9120 }
9121
9122 /* We want to put it after the DYNAMIC segment. */
9123 pm = &elf_tdata (abfd)->segment_map;
9124 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
9125 pm = &(*pm)->next;
9126 if (*pm != NULL)
9127 pm = &(*pm)->next;
9128
9129 m->next = *pm;
9130 *pm = m;
9131 }
9132 }
9133 }
8dc1a139 9134 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
9135 .dynstr, .dynsym, and .hash sections, and everything in
9136 between. */
9137 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
9138 pm = &(*pm)->next)
9139 if ((*pm)->p_type == PT_DYNAMIC)
9140 break;
9141 m = *pm;
9142 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
9143 {
9144 /* For a normal mips executable the permissions for the PT_DYNAMIC
9145 segment are read, write and execute. We do that here since
9146 the code in elf.c sets only the read permission. This matters
9147 sometimes for the dynamic linker. */
9148 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
9149 {
9150 m->p_flags = PF_R | PF_W | PF_X;
9151 m->p_flags_valid = 1;
9152 }
9153 }
9154 if (m != NULL
9155 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
9156 {
9157 static const char *sec_names[] =
9158 {
9159 ".dynamic", ".dynstr", ".dynsym", ".hash"
9160 };
9161 bfd_vma low, high;
9162 unsigned int i, c;
9163 struct elf_segment_map *n;
9164
792b4a53 9165 low = ~(bfd_vma) 0;
b49e97c9
TS
9166 high = 0;
9167 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
9168 {
9169 s = bfd_get_section_by_name (abfd, sec_names[i]);
9170 if (s != NULL && (s->flags & SEC_LOAD) != 0)
9171 {
9172 bfd_size_type sz;
9173
9174 if (low > s->vma)
9175 low = s->vma;
eea6121a 9176 sz = s->size;
b49e97c9
TS
9177 if (high < s->vma + sz)
9178 high = s->vma + sz;
9179 }
9180 }
9181
9182 c = 0;
9183 for (s = abfd->sections; s != NULL; s = s->next)
9184 if ((s->flags & SEC_LOAD) != 0
9185 && s->vma >= low
eea6121a 9186 && s->vma + s->size <= high)
b49e97c9
TS
9187 ++c;
9188
9189 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 9190 n = bfd_zalloc (abfd, amt);
b49e97c9 9191 if (n == NULL)
b34976b6 9192 return FALSE;
b49e97c9
TS
9193 *n = *m;
9194 n->count = c;
9195
9196 i = 0;
9197 for (s = abfd->sections; s != NULL; s = s->next)
9198 {
9199 if ((s->flags & SEC_LOAD) != 0
9200 && s->vma >= low
eea6121a 9201 && s->vma + s->size <= high)
b49e97c9
TS
9202 {
9203 n->sections[i] = s;
9204 ++i;
9205 }
9206 }
9207
9208 *pm = n;
9209 }
9210 }
9211
b34976b6 9212 return TRUE;
b49e97c9
TS
9213}
9214\f
9215/* Return the section that should be marked against GC for a given
9216 relocation. */
9217
9218asection *
9719ad41
RS
9219_bfd_mips_elf_gc_mark_hook (asection *sec,
9220 struct bfd_link_info *info ATTRIBUTE_UNUSED,
9221 Elf_Internal_Rela *rel,
9222 struct elf_link_hash_entry *h,
9223 Elf_Internal_Sym *sym)
b49e97c9
TS
9224{
9225 /* ??? Do mips16 stub sections need to be handled special? */
9226
9227 if (h != NULL)
9228 {
1e2f5b6e 9229 switch (ELF_R_TYPE (sec->owner, rel->r_info))
b49e97c9
TS
9230 {
9231 case R_MIPS_GNU_VTINHERIT:
9232 case R_MIPS_GNU_VTENTRY:
9233 break;
9234
9235 default:
9236 switch (h->root.type)
9237 {
9238 case bfd_link_hash_defined:
9239 case bfd_link_hash_defweak:
9240 return h->root.u.def.section;
9241
9242 case bfd_link_hash_common:
9243 return h->root.u.c.p->section;
9244
9245 default:
9246 break;
9247 }
9248 }
9249 }
9250 else
1e2f5b6e 9251 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
b49e97c9
TS
9252
9253 return NULL;
9254}
9255
9256/* Update the got entry reference counts for the section being removed. */
9257
b34976b6 9258bfd_boolean
9719ad41
RS
9259_bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
9260 struct bfd_link_info *info ATTRIBUTE_UNUSED,
9261 asection *sec ATTRIBUTE_UNUSED,
9262 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
b49e97c9
TS
9263{
9264#if 0
9265 Elf_Internal_Shdr *symtab_hdr;
9266 struct elf_link_hash_entry **sym_hashes;
9267 bfd_signed_vma *local_got_refcounts;
9268 const Elf_Internal_Rela *rel, *relend;
9269 unsigned long r_symndx;
9270 struct elf_link_hash_entry *h;
9271
9272 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9273 sym_hashes = elf_sym_hashes (abfd);
9274 local_got_refcounts = elf_local_got_refcounts (abfd);
9275
9276 relend = relocs + sec->reloc_count;
9277 for (rel = relocs; rel < relend; rel++)
9278 switch (ELF_R_TYPE (abfd, rel->r_info))
9279 {
9280 case R_MIPS_GOT16:
9281 case R_MIPS_CALL16:
9282 case R_MIPS_CALL_HI16:
9283 case R_MIPS_CALL_LO16:
9284 case R_MIPS_GOT_HI16:
9285 case R_MIPS_GOT_LO16:
4a14403c
TS
9286 case R_MIPS_GOT_DISP:
9287 case R_MIPS_GOT_PAGE:
9288 case R_MIPS_GOT_OFST:
b49e97c9
TS
9289 /* ??? It would seem that the existing MIPS code does no sort
9290 of reference counting or whatnot on its GOT and PLT entries,
9291 so it is not possible to garbage collect them at this time. */
9292 break;
9293
9294 default:
9295 break;
9296 }
9297#endif
9298
b34976b6 9299 return TRUE;
b49e97c9
TS
9300}
9301\f
9302/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
9303 hiding the old indirect symbol. Process additional relocation
9304 information. Also called for weakdefs, in which case we just let
9305 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
9306
9307void
fcfa13d2 9308_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
9309 struct elf_link_hash_entry *dir,
9310 struct elf_link_hash_entry *ind)
b49e97c9
TS
9311{
9312 struct mips_elf_link_hash_entry *dirmips, *indmips;
9313
fcfa13d2 9314 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9
TS
9315
9316 if (ind->root.type != bfd_link_hash_indirect)
9317 return;
9318
9319 dirmips = (struct mips_elf_link_hash_entry *) dir;
9320 indmips = (struct mips_elf_link_hash_entry *) ind;
9321 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
9322 if (indmips->readonly_reloc)
b34976b6 9323 dirmips->readonly_reloc = TRUE;
b49e97c9 9324 if (indmips->no_fn_stub)
b34976b6 9325 dirmips->no_fn_stub = TRUE;
0f20cc35
DJ
9326
9327 if (dirmips->tls_type == 0)
9328 dirmips->tls_type = indmips->tls_type;
b49e97c9
TS
9329}
9330
9331void
9719ad41
RS
9332_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
9333 struct elf_link_hash_entry *entry,
9334 bfd_boolean force_local)
b49e97c9
TS
9335{
9336 bfd *dynobj;
9337 asection *got;
9338 struct mips_got_info *g;
9339 struct mips_elf_link_hash_entry *h;
7c5fcef7 9340
b49e97c9 9341 h = (struct mips_elf_link_hash_entry *) entry;
7c5fcef7
L
9342 if (h->forced_local)
9343 return;
4b555070 9344 h->forced_local = force_local;
7c5fcef7 9345
b49e97c9 9346 dynobj = elf_hash_table (info)->dynobj;
8d1d654f
AM
9347 if (dynobj != NULL && force_local && h->root.type != STT_TLS
9348 && (got = mips_elf_got_section (dynobj, FALSE)) != NULL
9349 && (g = mips_elf_section_data (got)->u.got_info) != NULL)
f4416af6 9350 {
c45a316a
AM
9351 if (g->next)
9352 {
9353 struct mips_got_entry e;
9354 struct mips_got_info *gg = g;
9355
9356 /* Since we're turning what used to be a global symbol into a
9357 local one, bump up the number of local entries of each GOT
9358 that had an entry for it. This will automatically decrease
9359 the number of global entries, since global_gotno is actually
9360 the upper limit of global entries. */
9361 e.abfd = dynobj;
9362 e.symndx = -1;
9363 e.d.h = h;
0f20cc35 9364 e.tls_type = 0;
c45a316a
AM
9365
9366 for (g = g->next; g != gg; g = g->next)
9367 if (htab_find (g->got_entries, &e))
9368 {
9369 BFD_ASSERT (g->global_gotno > 0);
9370 g->local_gotno++;
9371 g->global_gotno--;
9372 }
b49e97c9 9373
c45a316a
AM
9374 /* If this was a global symbol forced into the primary GOT, we
9375 no longer need an entry for it. We can't release the entry
9376 at this point, but we must at least stop counting it as one
9377 of the symbols that required a forced got entry. */
9378 if (h->root.got.offset == 2)
9379 {
9380 BFD_ASSERT (gg->assigned_gotno > 0);
9381 gg->assigned_gotno--;
9382 }
9383 }
9384 else if (g->global_gotno == 0 && g->global_gotsym == NULL)
9385 /* If we haven't got through GOT allocation yet, just bump up the
9386 number of local entries, as this symbol won't be counted as
9387 global. */
9388 g->local_gotno++;
9389 else if (h->root.got.offset == 1)
f4416af6 9390 {
c45a316a
AM
9391 /* If we're past non-multi-GOT allocation and this symbol had
9392 been marked for a global got entry, give it a local entry
9393 instead. */
9394 BFD_ASSERT (g->global_gotno > 0);
9395 g->local_gotno++;
9396 g->global_gotno--;
f4416af6
AO
9397 }
9398 }
f4416af6
AO
9399
9400 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
b49e97c9
TS
9401}
9402\f
d01414a5
TS
9403#define PDR_SIZE 32
9404
b34976b6 9405bfd_boolean
9719ad41
RS
9406_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
9407 struct bfd_link_info *info)
d01414a5
TS
9408{
9409 asection *o;
b34976b6 9410 bfd_boolean ret = FALSE;
d01414a5
TS
9411 unsigned char *tdata;
9412 size_t i, skip;
9413
9414 o = bfd_get_section_by_name (abfd, ".pdr");
9415 if (! o)
b34976b6 9416 return FALSE;
eea6121a 9417 if (o->size == 0)
b34976b6 9418 return FALSE;
eea6121a 9419 if (o->size % PDR_SIZE != 0)
b34976b6 9420 return FALSE;
d01414a5
TS
9421 if (o->output_section != NULL
9422 && bfd_is_abs_section (o->output_section))
b34976b6 9423 return FALSE;
d01414a5 9424
eea6121a 9425 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 9426 if (! tdata)
b34976b6 9427 return FALSE;
d01414a5 9428
9719ad41 9429 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 9430 info->keep_memory);
d01414a5
TS
9431 if (!cookie->rels)
9432 {
9433 free (tdata);
b34976b6 9434 return FALSE;
d01414a5
TS
9435 }
9436
9437 cookie->rel = cookie->rels;
9438 cookie->relend = cookie->rels + o->reloc_count;
9439
eea6121a 9440 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 9441 {
c152c796 9442 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
9443 {
9444 tdata[i] = 1;
9445 skip ++;
9446 }
9447 }
9448
9449 if (skip != 0)
9450 {
f0abc2a1 9451 mips_elf_section_data (o)->u.tdata = tdata;
eea6121a 9452 o->size -= skip * PDR_SIZE;
b34976b6 9453 ret = TRUE;
d01414a5
TS
9454 }
9455 else
9456 free (tdata);
9457
9458 if (! info->keep_memory)
9459 free (cookie->rels);
9460
9461 return ret;
9462}
9463
b34976b6 9464bfd_boolean
9719ad41 9465_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
9466{
9467 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
9468 return TRUE;
9469 return FALSE;
53bfd6b4 9470}
d01414a5 9471
b34976b6 9472bfd_boolean
9719ad41
RS
9473_bfd_mips_elf_write_section (bfd *output_bfd, asection *sec,
9474 bfd_byte *contents)
d01414a5
TS
9475{
9476 bfd_byte *to, *from, *end;
9477 int i;
9478
9479 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 9480 return FALSE;
d01414a5 9481
f0abc2a1 9482 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 9483 return FALSE;
d01414a5
TS
9484
9485 to = contents;
eea6121a 9486 end = contents + sec->size;
d01414a5
TS
9487 for (from = contents, i = 0;
9488 from < end;
9489 from += PDR_SIZE, i++)
9490 {
f0abc2a1 9491 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
9492 continue;
9493 if (to != from)
9494 memcpy (to, from, PDR_SIZE);
9495 to += PDR_SIZE;
9496 }
9497 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 9498 sec->output_offset, sec->size);
b34976b6 9499 return TRUE;
d01414a5 9500}
53bfd6b4 9501\f
b49e97c9
TS
9502/* MIPS ELF uses a special find_nearest_line routine in order the
9503 handle the ECOFF debugging information. */
9504
9505struct mips_elf_find_line
9506{
9507 struct ecoff_debug_info d;
9508 struct ecoff_find_line i;
9509};
9510
b34976b6 9511bfd_boolean
9719ad41
RS
9512_bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
9513 asymbol **symbols, bfd_vma offset,
9514 const char **filename_ptr,
9515 const char **functionname_ptr,
9516 unsigned int *line_ptr)
b49e97c9
TS
9517{
9518 asection *msec;
9519
9520 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
9521 filename_ptr, functionname_ptr,
9522 line_ptr))
b34976b6 9523 return TRUE;
b49e97c9
TS
9524
9525 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
9526 filename_ptr, functionname_ptr,
9719ad41 9527 line_ptr, ABI_64_P (abfd) ? 8 : 0,
b49e97c9 9528 &elf_tdata (abfd)->dwarf2_find_line_info))
b34976b6 9529 return TRUE;
b49e97c9
TS
9530
9531 msec = bfd_get_section_by_name (abfd, ".mdebug");
9532 if (msec != NULL)
9533 {
9534 flagword origflags;
9535 struct mips_elf_find_line *fi;
9536 const struct ecoff_debug_swap * const swap =
9537 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
9538
9539 /* If we are called during a link, mips_elf_final_link may have
9540 cleared the SEC_HAS_CONTENTS field. We force it back on here
9541 if appropriate (which it normally will be). */
9542 origflags = msec->flags;
9543 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
9544 msec->flags |= SEC_HAS_CONTENTS;
9545
9546 fi = elf_tdata (abfd)->find_line_info;
9547 if (fi == NULL)
9548 {
9549 bfd_size_type external_fdr_size;
9550 char *fraw_src;
9551 char *fraw_end;
9552 struct fdr *fdr_ptr;
9553 bfd_size_type amt = sizeof (struct mips_elf_find_line);
9554
9719ad41 9555 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
9556 if (fi == NULL)
9557 {
9558 msec->flags = origflags;
b34976b6 9559 return FALSE;
b49e97c9
TS
9560 }
9561
9562 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
9563 {
9564 msec->flags = origflags;
b34976b6 9565 return FALSE;
b49e97c9
TS
9566 }
9567
9568 /* Swap in the FDR information. */
9569 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 9570 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
9571 if (fi->d.fdr == NULL)
9572 {
9573 msec->flags = origflags;
b34976b6 9574 return FALSE;
b49e97c9
TS
9575 }
9576 external_fdr_size = swap->external_fdr_size;
9577 fdr_ptr = fi->d.fdr;
9578 fraw_src = (char *) fi->d.external_fdr;
9579 fraw_end = (fraw_src
9580 + fi->d.symbolic_header.ifdMax * external_fdr_size);
9581 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 9582 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9
TS
9583
9584 elf_tdata (abfd)->find_line_info = fi;
9585
9586 /* Note that we don't bother to ever free this information.
9587 find_nearest_line is either called all the time, as in
9588 objdump -l, so the information should be saved, or it is
9589 rarely called, as in ld error messages, so the memory
9590 wasted is unimportant. Still, it would probably be a
9591 good idea for free_cached_info to throw it away. */
9592 }
9593
9594 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
9595 &fi->i, filename_ptr, functionname_ptr,
9596 line_ptr))
9597 {
9598 msec->flags = origflags;
b34976b6 9599 return TRUE;
b49e97c9
TS
9600 }
9601
9602 msec->flags = origflags;
9603 }
9604
9605 /* Fall back on the generic ELF find_nearest_line routine. */
9606
9607 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
9608 filename_ptr, functionname_ptr,
9609 line_ptr);
9610}
4ab527b0
FF
9611
9612bfd_boolean
9613_bfd_mips_elf_find_inliner_info (bfd *abfd,
9614 const char **filename_ptr,
9615 const char **functionname_ptr,
9616 unsigned int *line_ptr)
9617{
9618 bfd_boolean found;
9619 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9620 functionname_ptr, line_ptr,
9621 & elf_tdata (abfd)->dwarf2_find_line_info);
9622 return found;
9623}
9624
b49e97c9
TS
9625\f
9626/* When are writing out the .options or .MIPS.options section,
9627 remember the bytes we are writing out, so that we can install the
9628 GP value in the section_processing routine. */
9629
b34976b6 9630bfd_boolean
9719ad41
RS
9631_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
9632 const void *location,
9633 file_ptr offset, bfd_size_type count)
b49e97c9 9634{
cc2e31b9 9635 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
9636 {
9637 bfd_byte *c;
9638
9639 if (elf_section_data (section) == NULL)
9640 {
9641 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 9642 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 9643 if (elf_section_data (section) == NULL)
b34976b6 9644 return FALSE;
b49e97c9 9645 }
f0abc2a1 9646 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
9647 if (c == NULL)
9648 {
eea6121a 9649 c = bfd_zalloc (abfd, section->size);
b49e97c9 9650 if (c == NULL)
b34976b6 9651 return FALSE;
f0abc2a1 9652 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
9653 }
9654
9719ad41 9655 memcpy (c + offset, location, count);
b49e97c9
TS
9656 }
9657
9658 return _bfd_elf_set_section_contents (abfd, section, location, offset,
9659 count);
9660}
9661
9662/* This is almost identical to bfd_generic_get_... except that some
9663 MIPS relocations need to be handled specially. Sigh. */
9664
9665bfd_byte *
9719ad41
RS
9666_bfd_elf_mips_get_relocated_section_contents
9667 (bfd *abfd,
9668 struct bfd_link_info *link_info,
9669 struct bfd_link_order *link_order,
9670 bfd_byte *data,
9671 bfd_boolean relocatable,
9672 asymbol **symbols)
b49e97c9
TS
9673{
9674 /* Get enough memory to hold the stuff */
9675 bfd *input_bfd = link_order->u.indirect.section->owner;
9676 asection *input_section = link_order->u.indirect.section;
eea6121a 9677 bfd_size_type sz;
b49e97c9
TS
9678
9679 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
9680 arelent **reloc_vector = NULL;
9681 long reloc_count;
9682
9683 if (reloc_size < 0)
9684 goto error_return;
9685
9719ad41 9686 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
9687 if (reloc_vector == NULL && reloc_size != 0)
9688 goto error_return;
9689
9690 /* read in the section */
eea6121a
AM
9691 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
9692 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
9693 goto error_return;
9694
b49e97c9
TS
9695 reloc_count = bfd_canonicalize_reloc (input_bfd,
9696 input_section,
9697 reloc_vector,
9698 symbols);
9699 if (reloc_count < 0)
9700 goto error_return;
9701
9702 if (reloc_count > 0)
9703 {
9704 arelent **parent;
9705 /* for mips */
9706 int gp_found;
9707 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
9708
9709 {
9710 struct bfd_hash_entry *h;
9711 struct bfd_link_hash_entry *lh;
9712 /* Skip all this stuff if we aren't mixing formats. */
9713 if (abfd && input_bfd
9714 && abfd->xvec == input_bfd->xvec)
9715 lh = 0;
9716 else
9717 {
b34976b6 9718 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
9719 lh = (struct bfd_link_hash_entry *) h;
9720 }
9721 lookup:
9722 if (lh)
9723 {
9724 switch (lh->type)
9725 {
9726 case bfd_link_hash_undefined:
9727 case bfd_link_hash_undefweak:
9728 case bfd_link_hash_common:
9729 gp_found = 0;
9730 break;
9731 case bfd_link_hash_defined:
9732 case bfd_link_hash_defweak:
9733 gp_found = 1;
9734 gp = lh->u.def.value;
9735 break;
9736 case bfd_link_hash_indirect:
9737 case bfd_link_hash_warning:
9738 lh = lh->u.i.link;
9739 /* @@FIXME ignoring warning for now */
9740 goto lookup;
9741 case bfd_link_hash_new:
9742 default:
9743 abort ();
9744 }
9745 }
9746 else
9747 gp_found = 0;
9748 }
9749 /* end mips */
9719ad41 9750 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 9751 {
9719ad41 9752 char *error_message = NULL;
b49e97c9
TS
9753 bfd_reloc_status_type r;
9754
9755 /* Specific to MIPS: Deal with relocation types that require
9756 knowing the gp of the output bfd. */
9757 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 9758
8236346f
EC
9759 /* If we've managed to find the gp and have a special
9760 function for the relocation then go ahead, else default
9761 to the generic handling. */
9762 if (gp_found
9763 && (*parent)->howto->special_function
9764 == _bfd_mips_elf32_gprel16_reloc)
9765 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
9766 input_section, relocatable,
9767 data, gp);
9768 else
86324f90 9769 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
9770 input_section,
9771 relocatable ? abfd : NULL,
9772 &error_message);
b49e97c9 9773
1049f94e 9774 if (relocatable)
b49e97c9
TS
9775 {
9776 asection *os = input_section->output_section;
9777
9778 /* A partial link, so keep the relocs */
9779 os->orelocation[os->reloc_count] = *parent;
9780 os->reloc_count++;
9781 }
9782
9783 if (r != bfd_reloc_ok)
9784 {
9785 switch (r)
9786 {
9787 case bfd_reloc_undefined:
9788 if (!((*link_info->callbacks->undefined_symbol)
9789 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
5e2b0d47 9790 input_bfd, input_section, (*parent)->address, TRUE)))
b49e97c9
TS
9791 goto error_return;
9792 break;
9793 case bfd_reloc_dangerous:
9719ad41 9794 BFD_ASSERT (error_message != NULL);
b49e97c9
TS
9795 if (!((*link_info->callbacks->reloc_dangerous)
9796 (link_info, error_message, input_bfd, input_section,
9797 (*parent)->address)))
9798 goto error_return;
9799 break;
9800 case bfd_reloc_overflow:
9801 if (!((*link_info->callbacks->reloc_overflow)
dfeffb9f
L
9802 (link_info, NULL,
9803 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
b49e97c9
TS
9804 (*parent)->howto->name, (*parent)->addend,
9805 input_bfd, input_section, (*parent)->address)))
9806 goto error_return;
9807 break;
9808 case bfd_reloc_outofrange:
9809 default:
9810 abort ();
9811 break;
9812 }
9813
9814 }
9815 }
9816 }
9817 if (reloc_vector != NULL)
9818 free (reloc_vector);
9819 return data;
9820
9821error_return:
9822 if (reloc_vector != NULL)
9823 free (reloc_vector);
9824 return NULL;
9825}
9826\f
9827/* Create a MIPS ELF linker hash table. */
9828
9829struct bfd_link_hash_table *
9719ad41 9830_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
9831{
9832 struct mips_elf_link_hash_table *ret;
9833 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
9834
9719ad41
RS
9835 ret = bfd_malloc (amt);
9836 if (ret == NULL)
b49e97c9
TS
9837 return NULL;
9838
66eb6687
AM
9839 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
9840 mips_elf_link_hash_newfunc,
9841 sizeof (struct mips_elf_link_hash_entry)))
b49e97c9 9842 {
e2d34d7d 9843 free (ret);
b49e97c9
TS
9844 return NULL;
9845 }
9846
9847#if 0
9848 /* We no longer use this. */
9849 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
9850 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
9851#endif
9852 ret->procedure_count = 0;
9853 ret->compact_rel_size = 0;
b34976b6 9854 ret->use_rld_obj_head = FALSE;
b49e97c9 9855 ret->rld_value = 0;
b34976b6 9856 ret->mips16_stubs_seen = FALSE;
0a44bf69
RS
9857 ret->is_vxworks = FALSE;
9858 ret->srelbss = NULL;
9859 ret->sdynbss = NULL;
9860 ret->srelplt = NULL;
9861 ret->srelplt2 = NULL;
9862 ret->sgotplt = NULL;
9863 ret->splt = NULL;
9864 ret->plt_header_size = 0;
9865 ret->plt_entry_size = 0;
b49e97c9
TS
9866
9867 return &ret->root.root;
9868}
0a44bf69
RS
9869
9870/* Likewise, but indicate that the target is VxWorks. */
9871
9872struct bfd_link_hash_table *
9873_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
9874{
9875 struct bfd_link_hash_table *ret;
9876
9877 ret = _bfd_mips_elf_link_hash_table_create (abfd);
9878 if (ret)
9879 {
9880 struct mips_elf_link_hash_table *htab;
9881
9882 htab = (struct mips_elf_link_hash_table *) ret;
9883 htab->is_vxworks = 1;
9884 }
9885 return ret;
9886}
b49e97c9
TS
9887\f
9888/* We need to use a special link routine to handle the .reginfo and
9889 the .mdebug sections. We need to merge all instances of these
9890 sections together, not write them all out sequentially. */
9891
b34976b6 9892bfd_boolean
9719ad41 9893_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 9894{
b49e97c9
TS
9895 asection *o;
9896 struct bfd_link_order *p;
9897 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
9898 asection *rtproc_sec;
9899 Elf32_RegInfo reginfo;
9900 struct ecoff_debug_info debug;
7a2a6943
NC
9901 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9902 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 9903 HDRR *symhdr = &debug.symbolic_header;
9719ad41 9904 void *mdebug_handle = NULL;
b49e97c9
TS
9905 asection *s;
9906 EXTR esym;
9907 unsigned int i;
9908 bfd_size_type amt;
0a44bf69 9909 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
9910
9911 static const char * const secname[] =
9912 {
9913 ".text", ".init", ".fini", ".data",
9914 ".rodata", ".sdata", ".sbss", ".bss"
9915 };
9916 static const int sc[] =
9917 {
9918 scText, scInit, scFini, scData,
9919 scRData, scSData, scSBss, scBss
9920 };
9921
b49e97c9
TS
9922 /* We'd carefully arranged the dynamic symbol indices, and then the
9923 generic size_dynamic_sections renumbered them out from under us.
9924 Rather than trying somehow to prevent the renumbering, just do
9925 the sort again. */
0a44bf69 9926 htab = mips_elf_hash_table (info);
b49e97c9
TS
9927 if (elf_hash_table (info)->dynamic_sections_created)
9928 {
9929 bfd *dynobj;
9930 asection *got;
9931 struct mips_got_info *g;
7a2a6943 9932 bfd_size_type dynsecsymcount;
b49e97c9
TS
9933
9934 /* When we resort, we must tell mips_elf_sort_hash_table what
9935 the lowest index it may use is. That's the number of section
9936 symbols we're going to add. The generic ELF linker only
9937 adds these symbols when building a shared object. Note that
9938 we count the sections after (possibly) removing the .options
9939 section above. */
7a2a6943
NC
9940
9941 dynsecsymcount = 0;
9942 if (info->shared)
9943 {
9944 asection * p;
9945
9946 for (p = abfd->sections; p ; p = p->next)
9947 if ((p->flags & SEC_EXCLUDE) == 0
9948 && (p->flags & SEC_ALLOC) != 0
9949 && !(*bed->elf_backend_omit_section_dynsym) (abfd, info, p))
9950 ++ dynsecsymcount;
9951 }
86324f90 9952
7a2a6943 9953 if (! mips_elf_sort_hash_table (info, dynsecsymcount + 1))
b34976b6 9954 return FALSE;
b49e97c9
TS
9955
9956 /* Make sure we didn't grow the global .got region. */
9957 dynobj = elf_hash_table (info)->dynobj;
f4416af6 9958 got = mips_elf_got_section (dynobj, FALSE);
f0abc2a1 9959 g = mips_elf_section_data (got)->u.got_info;
b49e97c9
TS
9960
9961 if (g->global_gotsym != NULL)
9962 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
9963 - g->global_gotsym->dynindx)
9964 <= g->global_gotno);
9965 }
9966
b49e97c9
TS
9967 /* Get a value for the GP register. */
9968 if (elf_gp (abfd) == 0)
9969 {
9970 struct bfd_link_hash_entry *h;
9971
b34976b6 9972 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 9973 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
9974 elf_gp (abfd) = (h->u.def.value
9975 + h->u.def.section->output_section->vma
9976 + h->u.def.section->output_offset);
0a44bf69
RS
9977 else if (htab->is_vxworks
9978 && (h = bfd_link_hash_lookup (info->hash,
9979 "_GLOBAL_OFFSET_TABLE_",
9980 FALSE, FALSE, TRUE))
9981 && h->type == bfd_link_hash_defined)
9982 elf_gp (abfd) = (h->u.def.section->output_section->vma
9983 + h->u.def.section->output_offset
9984 + h->u.def.value);
1049f94e 9985 else if (info->relocatable)
b49e97c9
TS
9986 {
9987 bfd_vma lo = MINUS_ONE;
9988
9989 /* Find the GP-relative section with the lowest offset. */
9719ad41 9990 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
9991 if (o->vma < lo
9992 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
9993 lo = o->vma;
9994
9995 /* And calculate GP relative to that. */
0a44bf69 9996 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
9997 }
9998 else
9999 {
10000 /* If the relocate_section function needs to do a reloc
10001 involving the GP value, it should make a reloc_dangerous
10002 callback to warn that GP is not defined. */
10003 }
10004 }
10005
10006 /* Go through the sections and collect the .reginfo and .mdebug
10007 information. */
10008 reginfo_sec = NULL;
10009 mdebug_sec = NULL;
10010 gptab_data_sec = NULL;
10011 gptab_bss_sec = NULL;
9719ad41 10012 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
10013 {
10014 if (strcmp (o->name, ".reginfo") == 0)
10015 {
10016 memset (&reginfo, 0, sizeof reginfo);
10017
10018 /* We have found the .reginfo section in the output file.
10019 Look through all the link_orders comprising it and merge
10020 the information together. */
8423293d 10021 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
10022 {
10023 asection *input_section;
10024 bfd *input_bfd;
10025 Elf32_External_RegInfo ext;
10026 Elf32_RegInfo sub;
10027
10028 if (p->type != bfd_indirect_link_order)
10029 {
10030 if (p->type == bfd_data_link_order)
10031 continue;
10032 abort ();
10033 }
10034
10035 input_section = p->u.indirect.section;
10036 input_bfd = input_section->owner;
10037
b49e97c9 10038 if (! bfd_get_section_contents (input_bfd, input_section,
9719ad41 10039 &ext, 0, sizeof ext))
b34976b6 10040 return FALSE;
b49e97c9
TS
10041
10042 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
10043
10044 reginfo.ri_gprmask |= sub.ri_gprmask;
10045 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
10046 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
10047 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
10048 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
10049
10050 /* ri_gp_value is set by the function
10051 mips_elf32_section_processing when the section is
10052 finally written out. */
10053
10054 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10055 elf_link_input_bfd ignores this section. */
10056 input_section->flags &= ~SEC_HAS_CONTENTS;
10057 }
10058
10059 /* Size has been set in _bfd_mips_elf_always_size_sections. */
eea6121a 10060 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
10061
10062 /* Skip this section later on (I don't think this currently
10063 matters, but someday it might). */
8423293d 10064 o->map_head.link_order = NULL;
b49e97c9
TS
10065
10066 reginfo_sec = o;
10067 }
10068
10069 if (strcmp (o->name, ".mdebug") == 0)
10070 {
10071 struct extsym_info einfo;
10072 bfd_vma last;
10073
10074 /* We have found the .mdebug section in the output file.
10075 Look through all the link_orders comprising it and merge
10076 the information together. */
10077 symhdr->magic = swap->sym_magic;
10078 /* FIXME: What should the version stamp be? */
10079 symhdr->vstamp = 0;
10080 symhdr->ilineMax = 0;
10081 symhdr->cbLine = 0;
10082 symhdr->idnMax = 0;
10083 symhdr->ipdMax = 0;
10084 symhdr->isymMax = 0;
10085 symhdr->ioptMax = 0;
10086 symhdr->iauxMax = 0;
10087 symhdr->issMax = 0;
10088 symhdr->issExtMax = 0;
10089 symhdr->ifdMax = 0;
10090 symhdr->crfd = 0;
10091 symhdr->iextMax = 0;
10092
10093 /* We accumulate the debugging information itself in the
10094 debug_info structure. */
10095 debug.line = NULL;
10096 debug.external_dnr = NULL;
10097 debug.external_pdr = NULL;
10098 debug.external_sym = NULL;
10099 debug.external_opt = NULL;
10100 debug.external_aux = NULL;
10101 debug.ss = NULL;
10102 debug.ssext = debug.ssext_end = NULL;
10103 debug.external_fdr = NULL;
10104 debug.external_rfd = NULL;
10105 debug.external_ext = debug.external_ext_end = NULL;
10106
10107 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 10108 if (mdebug_handle == NULL)
b34976b6 10109 return FALSE;
b49e97c9
TS
10110
10111 esym.jmptbl = 0;
10112 esym.cobol_main = 0;
10113 esym.weakext = 0;
10114 esym.reserved = 0;
10115 esym.ifd = ifdNil;
10116 esym.asym.iss = issNil;
10117 esym.asym.st = stLocal;
10118 esym.asym.reserved = 0;
10119 esym.asym.index = indexNil;
10120 last = 0;
10121 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
10122 {
10123 esym.asym.sc = sc[i];
10124 s = bfd_get_section_by_name (abfd, secname[i]);
10125 if (s != NULL)
10126 {
10127 esym.asym.value = s->vma;
eea6121a 10128 last = s->vma + s->size;
b49e97c9
TS
10129 }
10130 else
10131 esym.asym.value = last;
10132 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
10133 secname[i], &esym))
b34976b6 10134 return FALSE;
b49e97c9
TS
10135 }
10136
8423293d 10137 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
10138 {
10139 asection *input_section;
10140 bfd *input_bfd;
10141 const struct ecoff_debug_swap *input_swap;
10142 struct ecoff_debug_info input_debug;
10143 char *eraw_src;
10144 char *eraw_end;
10145
10146 if (p->type != bfd_indirect_link_order)
10147 {
10148 if (p->type == bfd_data_link_order)
10149 continue;
10150 abort ();
10151 }
10152
10153 input_section = p->u.indirect.section;
10154 input_bfd = input_section->owner;
10155
10156 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
10157 || (get_elf_backend_data (input_bfd)
10158 ->elf_backend_ecoff_debug_swap) == NULL)
10159 {
10160 /* I don't know what a non MIPS ELF bfd would be
10161 doing with a .mdebug section, but I don't really
10162 want to deal with it. */
10163 continue;
10164 }
10165
10166 input_swap = (get_elf_backend_data (input_bfd)
10167 ->elf_backend_ecoff_debug_swap);
10168
eea6121a 10169 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
10170
10171 /* The ECOFF linking code expects that we have already
10172 read in the debugging information and set up an
10173 ecoff_debug_info structure, so we do that now. */
10174 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
10175 &input_debug))
b34976b6 10176 return FALSE;
b49e97c9
TS
10177
10178 if (! (bfd_ecoff_debug_accumulate
10179 (mdebug_handle, abfd, &debug, swap, input_bfd,
10180 &input_debug, input_swap, info)))
b34976b6 10181 return FALSE;
b49e97c9
TS
10182
10183 /* Loop through the external symbols. For each one with
10184 interesting information, try to find the symbol in
10185 the linker global hash table and save the information
10186 for the output external symbols. */
10187 eraw_src = input_debug.external_ext;
10188 eraw_end = (eraw_src
10189 + (input_debug.symbolic_header.iextMax
10190 * input_swap->external_ext_size));
10191 for (;
10192 eraw_src < eraw_end;
10193 eraw_src += input_swap->external_ext_size)
10194 {
10195 EXTR ext;
10196 const char *name;
10197 struct mips_elf_link_hash_entry *h;
10198
9719ad41 10199 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
10200 if (ext.asym.sc == scNil
10201 || ext.asym.sc == scUndefined
10202 || ext.asym.sc == scSUndefined)
10203 continue;
10204
10205 name = input_debug.ssext + ext.asym.iss;
10206 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 10207 name, FALSE, FALSE, TRUE);
b49e97c9
TS
10208 if (h == NULL || h->esym.ifd != -2)
10209 continue;
10210
10211 if (ext.ifd != -1)
10212 {
10213 BFD_ASSERT (ext.ifd
10214 < input_debug.symbolic_header.ifdMax);
10215 ext.ifd = input_debug.ifdmap[ext.ifd];
10216 }
10217
10218 h->esym = ext;
10219 }
10220
10221 /* Free up the information we just read. */
10222 free (input_debug.line);
10223 free (input_debug.external_dnr);
10224 free (input_debug.external_pdr);
10225 free (input_debug.external_sym);
10226 free (input_debug.external_opt);
10227 free (input_debug.external_aux);
10228 free (input_debug.ss);
10229 free (input_debug.ssext);
10230 free (input_debug.external_fdr);
10231 free (input_debug.external_rfd);
10232 free (input_debug.external_ext);
10233
10234 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10235 elf_link_input_bfd ignores this section. */
10236 input_section->flags &= ~SEC_HAS_CONTENTS;
10237 }
10238
10239 if (SGI_COMPAT (abfd) && info->shared)
10240 {
10241 /* Create .rtproc section. */
10242 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
10243 if (rtproc_sec == NULL)
10244 {
10245 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
10246 | SEC_LINKER_CREATED | SEC_READONLY);
10247
3496cb2a
L
10248 rtproc_sec = bfd_make_section_with_flags (abfd,
10249 ".rtproc",
10250 flags);
b49e97c9 10251 if (rtproc_sec == NULL
b49e97c9 10252 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 10253 return FALSE;
b49e97c9
TS
10254 }
10255
10256 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
10257 info, rtproc_sec,
10258 &debug))
b34976b6 10259 return FALSE;
b49e97c9
TS
10260 }
10261
10262 /* Build the external symbol information. */
10263 einfo.abfd = abfd;
10264 einfo.info = info;
10265 einfo.debug = &debug;
10266 einfo.swap = swap;
b34976b6 10267 einfo.failed = FALSE;
b49e97c9 10268 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 10269 mips_elf_output_extsym, &einfo);
b49e97c9 10270 if (einfo.failed)
b34976b6 10271 return FALSE;
b49e97c9
TS
10272
10273 /* Set the size of the .mdebug section. */
eea6121a 10274 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
10275
10276 /* Skip this section later on (I don't think this currently
10277 matters, but someday it might). */
8423293d 10278 o->map_head.link_order = NULL;
b49e97c9
TS
10279
10280 mdebug_sec = o;
10281 }
10282
10283 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
10284 {
10285 const char *subname;
10286 unsigned int c;
10287 Elf32_gptab *tab;
10288 Elf32_External_gptab *ext_tab;
10289 unsigned int j;
10290
10291 /* The .gptab.sdata and .gptab.sbss sections hold
10292 information describing how the small data area would
10293 change depending upon the -G switch. These sections
10294 not used in executables files. */
1049f94e 10295 if (! info->relocatable)
b49e97c9 10296 {
8423293d 10297 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
10298 {
10299 asection *input_section;
10300
10301 if (p->type != bfd_indirect_link_order)
10302 {
10303 if (p->type == bfd_data_link_order)
10304 continue;
10305 abort ();
10306 }
10307
10308 input_section = p->u.indirect.section;
10309
10310 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10311 elf_link_input_bfd ignores this section. */
10312 input_section->flags &= ~SEC_HAS_CONTENTS;
10313 }
10314
10315 /* Skip this section later on (I don't think this
10316 currently matters, but someday it might). */
8423293d 10317 o->map_head.link_order = NULL;
b49e97c9
TS
10318
10319 /* Really remove the section. */
5daa8fe7 10320 bfd_section_list_remove (abfd, o);
b49e97c9
TS
10321 --abfd->section_count;
10322
10323 continue;
10324 }
10325
10326 /* There is one gptab for initialized data, and one for
10327 uninitialized data. */
10328 if (strcmp (o->name, ".gptab.sdata") == 0)
10329 gptab_data_sec = o;
10330 else if (strcmp (o->name, ".gptab.sbss") == 0)
10331 gptab_bss_sec = o;
10332 else
10333 {
10334 (*_bfd_error_handler)
10335 (_("%s: illegal section name `%s'"),
10336 bfd_get_filename (abfd), o->name);
10337 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 10338 return FALSE;
b49e97c9
TS
10339 }
10340
10341 /* The linker script always combines .gptab.data and
10342 .gptab.sdata into .gptab.sdata, and likewise for
10343 .gptab.bss and .gptab.sbss. It is possible that there is
10344 no .sdata or .sbss section in the output file, in which
10345 case we must change the name of the output section. */
10346 subname = o->name + sizeof ".gptab" - 1;
10347 if (bfd_get_section_by_name (abfd, subname) == NULL)
10348 {
10349 if (o == gptab_data_sec)
10350 o->name = ".gptab.data";
10351 else
10352 o->name = ".gptab.bss";
10353 subname = o->name + sizeof ".gptab" - 1;
10354 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
10355 }
10356
10357 /* Set up the first entry. */
10358 c = 1;
10359 amt = c * sizeof (Elf32_gptab);
9719ad41 10360 tab = bfd_malloc (amt);
b49e97c9 10361 if (tab == NULL)
b34976b6 10362 return FALSE;
b49e97c9
TS
10363 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
10364 tab[0].gt_header.gt_unused = 0;
10365
10366 /* Combine the input sections. */
8423293d 10367 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
10368 {
10369 asection *input_section;
10370 bfd *input_bfd;
10371 bfd_size_type size;
10372 unsigned long last;
10373 bfd_size_type gpentry;
10374
10375 if (p->type != bfd_indirect_link_order)
10376 {
10377 if (p->type == bfd_data_link_order)
10378 continue;
10379 abort ();
10380 }
10381
10382 input_section = p->u.indirect.section;
10383 input_bfd = input_section->owner;
10384
10385 /* Combine the gptab entries for this input section one
10386 by one. We know that the input gptab entries are
10387 sorted by ascending -G value. */
eea6121a 10388 size = input_section->size;
b49e97c9
TS
10389 last = 0;
10390 for (gpentry = sizeof (Elf32_External_gptab);
10391 gpentry < size;
10392 gpentry += sizeof (Elf32_External_gptab))
10393 {
10394 Elf32_External_gptab ext_gptab;
10395 Elf32_gptab int_gptab;
10396 unsigned long val;
10397 unsigned long add;
b34976b6 10398 bfd_boolean exact;
b49e97c9
TS
10399 unsigned int look;
10400
10401 if (! (bfd_get_section_contents
9719ad41
RS
10402 (input_bfd, input_section, &ext_gptab, gpentry,
10403 sizeof (Elf32_External_gptab))))
b49e97c9
TS
10404 {
10405 free (tab);
b34976b6 10406 return FALSE;
b49e97c9
TS
10407 }
10408
10409 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
10410 &int_gptab);
10411 val = int_gptab.gt_entry.gt_g_value;
10412 add = int_gptab.gt_entry.gt_bytes - last;
10413
b34976b6 10414 exact = FALSE;
b49e97c9
TS
10415 for (look = 1; look < c; look++)
10416 {
10417 if (tab[look].gt_entry.gt_g_value >= val)
10418 tab[look].gt_entry.gt_bytes += add;
10419
10420 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 10421 exact = TRUE;
b49e97c9
TS
10422 }
10423
10424 if (! exact)
10425 {
10426 Elf32_gptab *new_tab;
10427 unsigned int max;
10428
10429 /* We need a new table entry. */
10430 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 10431 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
10432 if (new_tab == NULL)
10433 {
10434 free (tab);
b34976b6 10435 return FALSE;
b49e97c9
TS
10436 }
10437 tab = new_tab;
10438 tab[c].gt_entry.gt_g_value = val;
10439 tab[c].gt_entry.gt_bytes = add;
10440
10441 /* Merge in the size for the next smallest -G
10442 value, since that will be implied by this new
10443 value. */
10444 max = 0;
10445 for (look = 1; look < c; look++)
10446 {
10447 if (tab[look].gt_entry.gt_g_value < val
10448 && (max == 0
10449 || (tab[look].gt_entry.gt_g_value
10450 > tab[max].gt_entry.gt_g_value)))
10451 max = look;
10452 }
10453 if (max != 0)
10454 tab[c].gt_entry.gt_bytes +=
10455 tab[max].gt_entry.gt_bytes;
10456
10457 ++c;
10458 }
10459
10460 last = int_gptab.gt_entry.gt_bytes;
10461 }
10462
10463 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10464 elf_link_input_bfd ignores this section. */
10465 input_section->flags &= ~SEC_HAS_CONTENTS;
10466 }
10467
10468 /* The table must be sorted by -G value. */
10469 if (c > 2)
10470 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
10471
10472 /* Swap out the table. */
10473 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 10474 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
10475 if (ext_tab == NULL)
10476 {
10477 free (tab);
b34976b6 10478 return FALSE;
b49e97c9
TS
10479 }
10480
10481 for (j = 0; j < c; j++)
10482 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
10483 free (tab);
10484
eea6121a 10485 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
10486 o->contents = (bfd_byte *) ext_tab;
10487
10488 /* Skip this section later on (I don't think this currently
10489 matters, but someday it might). */
8423293d 10490 o->map_head.link_order = NULL;
b49e97c9
TS
10491 }
10492 }
10493
10494 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 10495 if (!bfd_elf_final_link (abfd, info))
b34976b6 10496 return FALSE;
b49e97c9
TS
10497
10498 /* Now write out the computed sections. */
10499
9719ad41 10500 if (reginfo_sec != NULL)
b49e97c9
TS
10501 {
10502 Elf32_External_RegInfo ext;
10503
10504 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 10505 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 10506 return FALSE;
b49e97c9
TS
10507 }
10508
9719ad41 10509 if (mdebug_sec != NULL)
b49e97c9
TS
10510 {
10511 BFD_ASSERT (abfd->output_has_begun);
10512 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
10513 swap, info,
10514 mdebug_sec->filepos))
b34976b6 10515 return FALSE;
b49e97c9
TS
10516
10517 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
10518 }
10519
9719ad41 10520 if (gptab_data_sec != NULL)
b49e97c9
TS
10521 {
10522 if (! bfd_set_section_contents (abfd, gptab_data_sec,
10523 gptab_data_sec->contents,
eea6121a 10524 0, gptab_data_sec->size))
b34976b6 10525 return FALSE;
b49e97c9
TS
10526 }
10527
9719ad41 10528 if (gptab_bss_sec != NULL)
b49e97c9
TS
10529 {
10530 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
10531 gptab_bss_sec->contents,
eea6121a 10532 0, gptab_bss_sec->size))
b34976b6 10533 return FALSE;
b49e97c9
TS
10534 }
10535
10536 if (SGI_COMPAT (abfd))
10537 {
10538 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
10539 if (rtproc_sec != NULL)
10540 {
10541 if (! bfd_set_section_contents (abfd, rtproc_sec,
10542 rtproc_sec->contents,
eea6121a 10543 0, rtproc_sec->size))
b34976b6 10544 return FALSE;
b49e97c9
TS
10545 }
10546 }
10547
b34976b6 10548 return TRUE;
b49e97c9
TS
10549}
10550\f
64543e1a
RS
10551/* Structure for saying that BFD machine EXTENSION extends BASE. */
10552
10553struct mips_mach_extension {
10554 unsigned long extension, base;
10555};
10556
10557
10558/* An array describing how BFD machines relate to one another. The entries
10559 are ordered topologically with MIPS I extensions listed last. */
10560
10561static const struct mips_mach_extension mips_mach_extensions[] = {
10562 /* MIPS64 extensions. */
5f74bc13 10563 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
64543e1a
RS
10564 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
10565
10566 /* MIPS V extensions. */
10567 { bfd_mach_mipsisa64, bfd_mach_mips5 },
10568
10569 /* R10000 extensions. */
10570 { bfd_mach_mips12000, bfd_mach_mips10000 },
10571
10572 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
10573 vr5400 ISA, but doesn't include the multimedia stuff. It seems
10574 better to allow vr5400 and vr5500 code to be merged anyway, since
10575 many libraries will just use the core ISA. Perhaps we could add
10576 some sort of ASE flag if this ever proves a problem. */
10577 { bfd_mach_mips5500, bfd_mach_mips5400 },
10578 { bfd_mach_mips5400, bfd_mach_mips5000 },
10579
10580 /* MIPS IV extensions. */
10581 { bfd_mach_mips5, bfd_mach_mips8000 },
10582 { bfd_mach_mips10000, bfd_mach_mips8000 },
10583 { bfd_mach_mips5000, bfd_mach_mips8000 },
5a7ea749 10584 { bfd_mach_mips7000, bfd_mach_mips8000 },
0d2e43ed 10585 { bfd_mach_mips9000, bfd_mach_mips8000 },
64543e1a
RS
10586
10587 /* VR4100 extensions. */
10588 { bfd_mach_mips4120, bfd_mach_mips4100 },
10589 { bfd_mach_mips4111, bfd_mach_mips4100 },
10590
10591 /* MIPS III extensions. */
10592 { bfd_mach_mips8000, bfd_mach_mips4000 },
10593 { bfd_mach_mips4650, bfd_mach_mips4000 },
10594 { bfd_mach_mips4600, bfd_mach_mips4000 },
10595 { bfd_mach_mips4400, bfd_mach_mips4000 },
10596 { bfd_mach_mips4300, bfd_mach_mips4000 },
10597 { bfd_mach_mips4100, bfd_mach_mips4000 },
10598 { bfd_mach_mips4010, bfd_mach_mips4000 },
10599
10600 /* MIPS32 extensions. */
10601 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
10602
10603 /* MIPS II extensions. */
10604 { bfd_mach_mips4000, bfd_mach_mips6000 },
10605 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
10606
10607 /* MIPS I extensions. */
10608 { bfd_mach_mips6000, bfd_mach_mips3000 },
10609 { bfd_mach_mips3900, bfd_mach_mips3000 }
10610};
10611
10612
10613/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
10614
10615static bfd_boolean
9719ad41 10616mips_mach_extends_p (unsigned long base, unsigned long extension)
64543e1a
RS
10617{
10618 size_t i;
10619
c5211a54
RS
10620 if (extension == base)
10621 return TRUE;
10622
10623 if (base == bfd_mach_mipsisa32
10624 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
10625 return TRUE;
10626
10627 if (base == bfd_mach_mipsisa32r2
10628 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
10629 return TRUE;
10630
10631 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
64543e1a 10632 if (extension == mips_mach_extensions[i].extension)
c5211a54
RS
10633 {
10634 extension = mips_mach_extensions[i].base;
10635 if (extension == base)
10636 return TRUE;
10637 }
64543e1a 10638
c5211a54 10639 return FALSE;
64543e1a
RS
10640}
10641
10642
10643/* Return true if the given ELF header flags describe a 32-bit binary. */
00707a0e 10644
b34976b6 10645static bfd_boolean
9719ad41 10646mips_32bit_flags_p (flagword flags)
00707a0e 10647{
64543e1a
RS
10648 return ((flags & EF_MIPS_32BITMODE) != 0
10649 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
10650 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
10651 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
10652 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
10653 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
10654 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
00707a0e
RS
10655}
10656
64543e1a 10657
b49e97c9
TS
10658/* Merge backend specific data from an object file to the output
10659 object file when linking. */
10660
b34976b6 10661bfd_boolean
9719ad41 10662_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
b49e97c9
TS
10663{
10664 flagword old_flags;
10665 flagword new_flags;
b34976b6
AM
10666 bfd_boolean ok;
10667 bfd_boolean null_input_bfd = TRUE;
b49e97c9
TS
10668 asection *sec;
10669
10670 /* Check if we have the same endianess */
82e51918 10671 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
aa701218
AO
10672 {
10673 (*_bfd_error_handler)
d003868e
AM
10674 (_("%B: endianness incompatible with that of the selected emulation"),
10675 ibfd);
aa701218
AO
10676 return FALSE;
10677 }
b49e97c9
TS
10678
10679 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
10680 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 10681 return TRUE;
b49e97c9 10682
aa701218
AO
10683 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
10684 {
10685 (*_bfd_error_handler)
d003868e
AM
10686 (_("%B: ABI is incompatible with that of the selected emulation"),
10687 ibfd);
aa701218
AO
10688 return FALSE;
10689 }
10690
b49e97c9
TS
10691 new_flags = elf_elfheader (ibfd)->e_flags;
10692 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
10693 old_flags = elf_elfheader (obfd)->e_flags;
10694
10695 if (! elf_flags_init (obfd))
10696 {
b34976b6 10697 elf_flags_init (obfd) = TRUE;
b49e97c9
TS
10698 elf_elfheader (obfd)->e_flags = new_flags;
10699 elf_elfheader (obfd)->e_ident[EI_CLASS]
10700 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
10701
10702 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
10703 && bfd_get_arch_info (obfd)->the_default)
10704 {
10705 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
10706 bfd_get_mach (ibfd)))
b34976b6 10707 return FALSE;
b49e97c9
TS
10708 }
10709
b34976b6 10710 return TRUE;
b49e97c9
TS
10711 }
10712
10713 /* Check flag compatibility. */
10714
10715 new_flags &= ~EF_MIPS_NOREORDER;
10716 old_flags &= ~EF_MIPS_NOREORDER;
10717
f4416af6
AO
10718 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
10719 doesn't seem to matter. */
10720 new_flags &= ~EF_MIPS_XGOT;
10721 old_flags &= ~EF_MIPS_XGOT;
10722
98a8deaf
RS
10723 /* MIPSpro generates ucode info in n64 objects. Again, we should
10724 just be able to ignore this. */
10725 new_flags &= ~EF_MIPS_UCODE;
10726 old_flags &= ~EF_MIPS_UCODE;
10727
0a44bf69
RS
10728 /* Don't care about the PIC flags from dynamic objects; they are
10729 PIC by design. */
10730 if ((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0
10731 && (ibfd->flags & DYNAMIC) != 0)
10732 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
10733
b49e97c9 10734 if (new_flags == old_flags)
b34976b6 10735 return TRUE;
b49e97c9
TS
10736
10737 /* Check to see if the input BFD actually contains any sections.
10738 If not, its flags may not have been initialised either, but it cannot
10739 actually cause any incompatibility. */
10740 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
10741 {
10742 /* Ignore synthetic sections and empty .text, .data and .bss sections
10743 which are automatically generated by gas. */
10744 if (strcmp (sec->name, ".reginfo")
10745 && strcmp (sec->name, ".mdebug")
eea6121a 10746 && (sec->size != 0
d13d89fa
NS
10747 || (strcmp (sec->name, ".text")
10748 && strcmp (sec->name, ".data")
10749 && strcmp (sec->name, ".bss"))))
b49e97c9 10750 {
b34976b6 10751 null_input_bfd = FALSE;
b49e97c9
TS
10752 break;
10753 }
10754 }
10755 if (null_input_bfd)
b34976b6 10756 return TRUE;
b49e97c9 10757
b34976b6 10758 ok = TRUE;
b49e97c9 10759
143d77c5
EC
10760 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
10761 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
b49e97c9 10762 {
b49e97c9 10763 (*_bfd_error_handler)
d003868e
AM
10764 (_("%B: warning: linking PIC files with non-PIC files"),
10765 ibfd);
143d77c5 10766 ok = TRUE;
b49e97c9
TS
10767 }
10768
143d77c5
EC
10769 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
10770 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
10771 if (! (new_flags & EF_MIPS_PIC))
10772 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
10773
10774 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
10775 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
b49e97c9 10776
64543e1a
RS
10777 /* Compare the ISAs. */
10778 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
b49e97c9 10779 {
64543e1a 10780 (*_bfd_error_handler)
d003868e
AM
10781 (_("%B: linking 32-bit code with 64-bit code"),
10782 ibfd);
64543e1a
RS
10783 ok = FALSE;
10784 }
10785 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
10786 {
10787 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
10788 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
b49e97c9 10789 {
64543e1a
RS
10790 /* Copy the architecture info from IBFD to OBFD. Also copy
10791 the 32-bit flag (if set) so that we continue to recognise
10792 OBFD as a 32-bit binary. */
10793 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
10794 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10795 elf_elfheader (obfd)->e_flags
10796 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
10797
10798 /* Copy across the ABI flags if OBFD doesn't use them
10799 and if that was what caused us to treat IBFD as 32-bit. */
10800 if ((old_flags & EF_MIPS_ABI) == 0
10801 && mips_32bit_flags_p (new_flags)
10802 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
10803 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
b49e97c9
TS
10804 }
10805 else
10806 {
64543e1a 10807 /* The ISAs aren't compatible. */
b49e97c9 10808 (*_bfd_error_handler)
d003868e
AM
10809 (_("%B: linking %s module with previous %s modules"),
10810 ibfd,
64543e1a
RS
10811 bfd_printable_name (ibfd),
10812 bfd_printable_name (obfd));
b34976b6 10813 ok = FALSE;
b49e97c9 10814 }
b49e97c9
TS
10815 }
10816
64543e1a
RS
10817 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
10818 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
10819
10820 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
b49e97c9
TS
10821 does set EI_CLASS differently from any 32-bit ABI. */
10822 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
10823 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
10824 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
10825 {
10826 /* Only error if both are set (to different values). */
10827 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
10828 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
10829 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
10830 {
10831 (*_bfd_error_handler)
d003868e
AM
10832 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
10833 ibfd,
b49e97c9
TS
10834 elf_mips_abi_name (ibfd),
10835 elf_mips_abi_name (obfd));
b34976b6 10836 ok = FALSE;
b49e97c9
TS
10837 }
10838 new_flags &= ~EF_MIPS_ABI;
10839 old_flags &= ~EF_MIPS_ABI;
10840 }
10841
fb39dac1
RS
10842 /* For now, allow arbitrary mixing of ASEs (retain the union). */
10843 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
10844 {
10845 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
10846
10847 new_flags &= ~ EF_MIPS_ARCH_ASE;
10848 old_flags &= ~ EF_MIPS_ARCH_ASE;
10849 }
10850
b49e97c9
TS
10851 /* Warn about any other mismatches */
10852 if (new_flags != old_flags)
10853 {
10854 (*_bfd_error_handler)
d003868e
AM
10855 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
10856 ibfd, (unsigned long) new_flags,
b49e97c9 10857 (unsigned long) old_flags);
b34976b6 10858 ok = FALSE;
b49e97c9
TS
10859 }
10860
10861 if (! ok)
10862 {
10863 bfd_set_error (bfd_error_bad_value);
b34976b6 10864 return FALSE;
b49e97c9
TS
10865 }
10866
b34976b6 10867 return TRUE;
b49e97c9
TS
10868}
10869
10870/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
10871
b34976b6 10872bfd_boolean
9719ad41 10873_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
10874{
10875 BFD_ASSERT (!elf_flags_init (abfd)
10876 || elf_elfheader (abfd)->e_flags == flags);
10877
10878 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
10879 elf_flags_init (abfd) = TRUE;
10880 return TRUE;
b49e97c9
TS
10881}
10882
b34976b6 10883bfd_boolean
9719ad41 10884_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 10885{
9719ad41 10886 FILE *file = ptr;
b49e97c9
TS
10887
10888 BFD_ASSERT (abfd != NULL && ptr != NULL);
10889
10890 /* Print normal ELF private data. */
10891 _bfd_elf_print_private_bfd_data (abfd, ptr);
10892
10893 /* xgettext:c-format */
10894 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
10895
10896 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
10897 fprintf (file, _(" [abi=O32]"));
10898 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
10899 fprintf (file, _(" [abi=O64]"));
10900 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
10901 fprintf (file, _(" [abi=EABI32]"));
10902 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
10903 fprintf (file, _(" [abi=EABI64]"));
10904 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
10905 fprintf (file, _(" [abi unknown]"));
10906 else if (ABI_N32_P (abfd))
10907 fprintf (file, _(" [abi=N32]"));
10908 else if (ABI_64_P (abfd))
10909 fprintf (file, _(" [abi=64]"));
10910 else
10911 fprintf (file, _(" [no abi set]"));
10912
10913 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
10914 fprintf (file, _(" [mips1]"));
10915 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
10916 fprintf (file, _(" [mips2]"));
10917 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
10918 fprintf (file, _(" [mips3]"));
10919 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
10920 fprintf (file, _(" [mips4]"));
10921 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
10922 fprintf (file, _(" [mips5]"));
10923 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
10924 fprintf (file, _(" [mips32]"));
10925 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
10926 fprintf (file, _(" [mips64]"));
af7ee8bf
CD
10927 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
10928 fprintf (file, _(" [mips32r2]"));
5f74bc13
CD
10929 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
10930 fprintf (file, _(" [mips64r2]"));
b49e97c9
TS
10931 else
10932 fprintf (file, _(" [unknown ISA]"));
10933
40d32fc6
CD
10934 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
10935 fprintf (file, _(" [mdmx]"));
10936
10937 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
10938 fprintf (file, _(" [mips16]"));
10939
b49e97c9
TS
10940 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
10941 fprintf (file, _(" [32bitmode]"));
10942 else
10943 fprintf (file, _(" [not 32bitmode]"));
10944
10945 fputc ('\n', file);
10946
b34976b6 10947 return TRUE;
b49e97c9 10948}
2f89ff8d 10949
b35d266b 10950const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 10951{
7dcb9820
AM
10952 { ".lit4", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
10953 { ".lit8", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
7dcb9820 10954 { ".mdebug", 7, 0, SHT_MIPS_DEBUG, 0 },
551b43fd
AM
10955 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
10956 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
10957 { ".ucode", 6, 0, SHT_MIPS_UCODE, 0 },
7dcb9820 10958 { NULL, 0, 0, 0, 0 }
2f89ff8d 10959};
5e2b0d47
NC
10960
10961/* Ensure that the STO_OPTIONAL flag is copied into h->other,
10962 even if this is not a defintion of the symbol. */
10963void
10964_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
10965 const Elf_Internal_Sym *isym,
10966 bfd_boolean definition,
10967 bfd_boolean dynamic ATTRIBUTE_UNUSED)
10968{
10969 if (! definition
10970 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
10971 h->other |= STO_OPTIONAL;
10972}
12ac1cf5
NC
10973
10974/* Decide whether an undefined symbol is special and can be ignored.
10975 This is the case for OPTIONAL symbols on IRIX. */
10976bfd_boolean
10977_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
10978{
10979 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
10980}
This page took 0.89943 seconds and 4 git commands to generate.