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