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