* elf32-i386.c (elf_i386_size_dynamic_sections): When checking for
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23 /* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "bfdlink.h"
31 #include "genlink.h"
32 #include "elf-bfd.h"
33 #include "elf/mips.h"
34
35 /* Get the ECOFF swapping routines. */
36 #include "coff/sym.h"
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
41 #define ECOFF_32
42 #include "ecoffswap.h"
43
44 static bfd_reloc_status_type mips32_64bit_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48 static void mips_info_to_howto_rel
49 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
50 static void bfd_mips_elf32_swap_gptab_in
51 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
52 static void bfd_mips_elf32_swap_gptab_out
53 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
54 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
55 static boolean mips_elf32_object_p PARAMS ((bfd *));
56 static boolean mips_elf_create_procedure_table
57 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
58 struct ecoff_debug_info *));
59 static int mips_elf_additional_program_headers PARAMS ((bfd *));
60 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
61 static INLINE int elf_mips_isa PARAMS ((flagword));
62 static boolean mips_elf32_section_from_shdr
63 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
64 static boolean mips_elf32_section_processing
65 PARAMS ((bfd *, Elf32_Internal_Shdr *));
66 static boolean mips_elf_is_local_label
67 PARAMS ((bfd *, asymbol *));
68 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
69 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
70 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
71 PARAMS ((bfd *));
72 static int gptab_compare PARAMS ((const void *, const void *));
73 static boolean mips_elf_final_link
74 PARAMS ((bfd *, struct bfd_link_info *));
75 static void mips_elf_relocate_hi16
76 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
77 bfd_vma));
78 static void mips_elf_relocate_got_local
79 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
80 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
81 static void mips_elf_relocate_global_got
82 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
83 static bfd_reloc_status_type mips16_jump_reloc
84 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
85 static boolean mips_elf_adjust_dynindx
86 PARAMS ((struct elf_link_hash_entry *, PTR));
87 static boolean mips_elf_relocate_section
88 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
89 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
90 static boolean mips_elf_link_output_symbol_hook
91 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
92 asection *));
93 static boolean mips_elf_create_dynamic_sections
94 PARAMS ((bfd *, struct bfd_link_info *));
95 static boolean mips_elf_create_compact_rel_section
96 PARAMS ((bfd *, struct bfd_link_info *));
97 static boolean mips_elf_create_got_section
98 PARAMS ((bfd *, struct bfd_link_info *));
99 static boolean mips_elf_check_relocs
100 PARAMS ((bfd *, struct bfd_link_info *, asection *,
101 const Elf_Internal_Rela *));
102 static boolean mips_elf_adjust_dynamic_symbol
103 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
104 static boolean mips_elf_size_dynamic_sections
105 PARAMS ((bfd *, struct bfd_link_info *));
106 static boolean mips_elf_finish_dynamic_symbol
107 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
108 Elf_Internal_Sym *));
109 static boolean mips_elf_finish_dynamic_sections
110 PARAMS ((bfd *, struct bfd_link_info *));
111 static boolean mips_elf_add_symbol_hook
112 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
113 const char **, flagword *, asection **, bfd_vma *));
114 static bfd_reloc_status_type mips_elf_final_gp
115 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
116 static bfd_byte *elf32_mips_get_relocated_section_contents
117 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
118 bfd_byte *, boolean, asymbol **));
119
120 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
121 executables. FIXME: At the moment, we default to always generating
122 Irix 5 executables. */
123
124 #define SGI_COMPAT(abfd) (1)
125
126 /* This structure is used to hold .got information when linking. It
127 is stored in the tdata field of the bfd_elf_section_data structure. */
128
129 struct mips_got_info
130 {
131 /* The symbol index of the first global .got symbol. */
132 unsigned long global_gotsym;
133 /* The number of local .got entries. */
134 unsigned int local_gotno;
135 };
136
137 /* The number of local .got entries we reserve. */
138 #define MIPS_RESERVED_GOTNO (2)
139
140 /* Instructions which appear in a stub. For some reason the stub is
141 slightly different on an SGI system. */
142 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
143 #define STUB_LW(abfd) \
144 (SGI_COMPAT (abfd) \
145 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
146 : 0x8f998000) /* lw t9,0x8000(gp) */
147 #define STUB_MOVE 0x03e07825 /* move t7,ra */
148 #define STUB_JALR 0x0320f809 /* jal t9 */
149 #define STUB_LI16 0x34180000 /* ori t8,zero,0 */
150 #define MIPS_FUNCTION_STUB_SIZE (16)
151
152 /* Names of sections which appear in the .dynsym section in an Irix 5
153 executable. */
154
155 static const char * const mips_elf_dynsym_sec_names[] =
156 {
157 ".text",
158 ".init",
159 ".fini",
160 ".data",
161 ".rodata",
162 ".sdata",
163 ".sbss",
164 ".bss",
165 NULL
166 };
167
168 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
169 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
170
171 /* The number of entries in mips_elf_dynsym_sec_names which go in the
172 text segment. */
173
174 #define MIPS_TEXT_DYNSYM_SECNO (3)
175
176 /* The names of the runtime procedure table symbols used on Irix 5. */
177
178 static const char * const mips_elf_dynsym_rtproc_names[] =
179 {
180 "_procedure_table",
181 "_procedure_string_table",
182 "_procedure_table_size",
183 NULL
184 };
185
186 /* These structures are used to generate the .compact_rel section on
187 Irix 5. */
188
189 typedef struct
190 {
191 unsigned long id1; /* Always one? */
192 unsigned long num; /* Number of compact relocation entries. */
193 unsigned long id2; /* Always two? */
194 unsigned long offset; /* The file offset of the first relocation. */
195 unsigned long reserved0; /* Zero? */
196 unsigned long reserved1; /* Zero? */
197 } Elf32_compact_rel;
198
199 typedef struct
200 {
201 bfd_byte id1[4];
202 bfd_byte num[4];
203 bfd_byte id2[4];
204 bfd_byte offset[4];
205 bfd_byte reserved0[4];
206 bfd_byte reserved1[4];
207 } Elf32_External_compact_rel;
208
209 typedef struct
210 {
211 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
212 unsigned int rtype : 4; /* Relocation types. See below. */
213 unsigned int dist2to : 8;
214 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
215 unsigned long konst; /* KONST field. See below. */
216 unsigned long vaddr; /* VADDR to be relocated. */
217 } Elf32_crinfo;
218
219 typedef struct
220 {
221 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
222 unsigned int rtype : 4; /* Relocation types. See below. */
223 unsigned int dist2to : 8;
224 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
225 unsigned long konst; /* KONST field. See below. */
226 } Elf32_crinfo2;
227
228 typedef struct
229 {
230 bfd_byte info[4];
231 bfd_byte konst[4];
232 bfd_byte vaddr[4];
233 } Elf32_External_crinfo;
234
235 typedef struct
236 {
237 bfd_byte info[4];
238 bfd_byte konst[4];
239 } Elf32_External_crinfo2;
240
241 /* These are the constants used to swap the bitfields in a crinfo. */
242
243 #define CRINFO_CTYPE (0x1)
244 #define CRINFO_CTYPE_SH (31)
245 #define CRINFO_RTYPE (0xf)
246 #define CRINFO_RTYPE_SH (27)
247 #define CRINFO_DIST2TO (0xff)
248 #define CRINFO_DIST2TO_SH (19)
249 #define CRINFO_RELVADDR (0x7ffff)
250 #define CRINFO_RELVADDR_SH (0)
251
252 /* A compact relocation info has long (3 words) or short (2 words)
253 formats. A short format doesn't have VADDR field and relvaddr
254 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
255 #define CRF_MIPS_LONG 1
256 #define CRF_MIPS_SHORT 0
257
258 /* There are 4 types of compact relocation at least. The value KONST
259 has different meaning for each type:
260
261 (type) (konst)
262 CT_MIPS_REL32 Address in data
263 CT_MIPS_WORD Address in word (XXX)
264 CT_MIPS_GPHI_LO GP - vaddr
265 CT_MIPS_JMPAD Address to jump
266 */
267
268 #define CRT_MIPS_REL32 0xa
269 #define CRT_MIPS_WORD 0xb
270 #define CRT_MIPS_GPHI_LO 0xc
271 #define CRT_MIPS_JMPAD 0xd
272
273 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
274 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
275 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
276 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
277
278 static void bfd_elf32_swap_compact_rel_out
279 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
280 static void bfd_elf32_swap_crinfo_out
281 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
282
283 #define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
284
285 enum reloc_type
286 {
287 R_MIPS_NONE = 0,
288 R_MIPS_16, R_MIPS_32,
289 R_MIPS_REL32, R_MIPS_26,
290 R_MIPS_HI16, R_MIPS_LO16,
291 R_MIPS_GPREL16, R_MIPS_LITERAL,
292 R_MIPS_GOT16, R_MIPS_PC16,
293 R_MIPS_CALL16, R_MIPS_GPREL32,
294 /* The remaining relocs are defined on Irix, although they are not
295 in the MIPS ELF ABI. */
296 R_MIPS_UNUSED1, R_MIPS_UNUSED2,
297 R_MIPS_UNUSED3,
298 R_MIPS_SHIFT5, R_MIPS_SHIFT6,
299 R_MIPS_64, R_MIPS_GOT_DISP,
300 R_MIPS_GOT_PAGE, R_MIPS_GOT_OFST,
301 R_MIPS_GOT_HI16, R_MIPS_GOT_LO16,
302 R_MIPS_SUB, R_MIPS_INSERT_A,
303 R_MIPS_INSERT_B, R_MIPS_DELETE,
304 R_MIPS_HIGHER, R_MIPS_HIGHEST,
305 R_MIPS_CALL_HI16, R_MIPS_CALL_LO16,
306 R_MIPS_max,
307 /* This reloc is used for the mips16. */
308 R_MIPS16_26 = 100
309 };
310
311 static reloc_howto_type elf_mips_howto_table[] =
312 {
313 /* No relocation. */
314 HOWTO (R_MIPS_NONE, /* type */
315 0, /* rightshift */
316 0, /* size (0 = byte, 1 = short, 2 = long) */
317 0, /* bitsize */
318 false, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* special_function */
322 "R_MIPS_NONE", /* name */
323 false, /* partial_inplace */
324 0, /* src_mask */
325 0, /* dst_mask */
326 false), /* pcrel_offset */
327
328 /* 16 bit relocation. */
329 HOWTO (R_MIPS_16, /* type */
330 0, /* rightshift */
331 1, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
333 false, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
336 bfd_elf_generic_reloc, /* special_function */
337 "R_MIPS_16", /* name */
338 true, /* partial_inplace */
339 0xffff, /* src_mask */
340 0xffff, /* dst_mask */
341 false), /* pcrel_offset */
342
343 /* 32 bit relocation. */
344 HOWTO (R_MIPS_32, /* type */
345 0, /* rightshift */
346 2, /* size (0 = byte, 1 = short, 2 = long) */
347 32, /* bitsize */
348 false, /* pc_relative */
349 0, /* bitpos */
350 complain_overflow_bitfield, /* complain_on_overflow */
351 bfd_elf_generic_reloc, /* special_function */
352 "R_MIPS_32", /* name */
353 true, /* partial_inplace */
354 0xffffffff, /* src_mask */
355 0xffffffff, /* dst_mask */
356 false), /* pcrel_offset */
357
358 /* 32 bit symbol relative relocation. */
359 HOWTO (R_MIPS_REL32, /* type */
360 0, /* rightshift */
361 2, /* size (0 = byte, 1 = short, 2 = long) */
362 32, /* bitsize */
363 false, /* pc_relative */
364 0, /* bitpos */
365 complain_overflow_bitfield, /* complain_on_overflow */
366 bfd_elf_generic_reloc, /* special_function */
367 "R_MIPS_REL32", /* name */
368 true, /* partial_inplace */
369 0xffffffff, /* src_mask */
370 0xffffffff, /* dst_mask */
371 false), /* pcrel_offset */
372
373 /* 26 bit branch address. */
374 HOWTO (R_MIPS_26, /* type */
375 2, /* rightshift */
376 2, /* size (0 = byte, 1 = short, 2 = long) */
377 26, /* bitsize */
378 false, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_dont, /* complain_on_overflow */
381 /* This needs complex overflow
382 detection, because the upper four
383 bits must match the PC. */
384 bfd_elf_generic_reloc, /* special_function */
385 "R_MIPS_26", /* name */
386 true, /* partial_inplace */
387 0x3ffffff, /* src_mask */
388 0x3ffffff, /* dst_mask */
389 false), /* pcrel_offset */
390
391 /* High 16 bits of symbol value. */
392 HOWTO (R_MIPS_HI16, /* type */
393 0, /* rightshift */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
395 16, /* bitsize */
396 false, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_dont, /* complain_on_overflow */
399 _bfd_mips_elf_hi16_reloc, /* special_function */
400 "R_MIPS_HI16", /* name */
401 true, /* partial_inplace */
402 0xffff, /* src_mask */
403 0xffff, /* dst_mask */
404 false), /* pcrel_offset */
405
406 /* Low 16 bits of symbol value. */
407 HOWTO (R_MIPS_LO16, /* type */
408 0, /* rightshift */
409 2, /* size (0 = byte, 1 = short, 2 = long) */
410 16, /* bitsize */
411 false, /* pc_relative */
412 0, /* bitpos */
413 complain_overflow_dont, /* complain_on_overflow */
414 _bfd_mips_elf_lo16_reloc, /* special_function */
415 "R_MIPS_LO16", /* name */
416 true, /* partial_inplace */
417 0xffff, /* src_mask */
418 0xffff, /* dst_mask */
419 false), /* pcrel_offset */
420
421 /* GP relative reference. */
422 HOWTO (R_MIPS_GPREL16, /* type */
423 0, /* rightshift */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
425 16, /* bitsize */
426 false, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_signed, /* complain_on_overflow */
429 _bfd_mips_elf_gprel16_reloc, /* special_function */
430 "R_MIPS_GPREL16", /* name */
431 true, /* partial_inplace */
432 0xffff, /* src_mask */
433 0xffff, /* dst_mask */
434 false), /* pcrel_offset */
435
436 /* Reference to literal section. */
437 HOWTO (R_MIPS_LITERAL, /* type */
438 0, /* rightshift */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
440 16, /* bitsize */
441 false, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_signed, /* complain_on_overflow */
444 _bfd_mips_elf_gprel16_reloc, /* special_function */
445 "R_MIPS_LITERAL", /* name */
446 true, /* partial_inplace */
447 0xffff, /* src_mask */
448 0xffff, /* dst_mask */
449 false), /* pcrel_offset */
450
451 /* Reference to global offset table. */
452 HOWTO (R_MIPS_GOT16, /* type */
453 0, /* rightshift */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
455 16, /* bitsize */
456 false, /* pc_relative */
457 0, /* bitpos */
458 complain_overflow_signed, /* complain_on_overflow */
459 _bfd_mips_elf_got16_reloc, /* special_function */
460 "R_MIPS_GOT16", /* name */
461 false, /* partial_inplace */
462 0, /* src_mask */
463 0xffff, /* dst_mask */
464 false), /* pcrel_offset */
465
466 /* 16 bit PC relative reference. */
467 HOWTO (R_MIPS_PC16, /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
471 true, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_signed, /* complain_on_overflow */
474 bfd_elf_generic_reloc, /* special_function */
475 "R_MIPS_PC16", /* name */
476 true, /* partial_inplace */
477 0xffff, /* src_mask */
478 0xffff, /* dst_mask */
479 false), /* pcrel_offset */
480
481 /* 16 bit call through global offset table. */
482 /* FIXME: This is not handled correctly. */
483 HOWTO (R_MIPS_CALL16, /* type */
484 0, /* rightshift */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
486 16, /* bitsize */
487 false, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_signed, /* complain_on_overflow */
490 bfd_elf_generic_reloc, /* special_function */
491 "R_MIPS_CALL16", /* name */
492 false, /* partial_inplace */
493 0, /* src_mask */
494 0xffff, /* dst_mask */
495 false), /* pcrel_offset */
496
497 /* 32 bit GP relative reference. */
498 HOWTO (R_MIPS_GPREL32, /* type */
499 0, /* rightshift */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
501 32, /* bitsize */
502 false, /* pc_relative */
503 0, /* bitpos */
504 complain_overflow_bitfield, /* complain_on_overflow */
505 _bfd_mips_elf_gprel32_reloc, /* special_function */
506 "R_MIPS_GPREL32", /* name */
507 true, /* partial_inplace */
508 0xffffffff, /* src_mask */
509 0xffffffff, /* dst_mask */
510 false), /* pcrel_offset */
511
512 /* The remaining relocs are defined on Irix 5, although they are
513 not defined by the ABI. */
514 { 13 },
515 { 14 },
516 { 15 },
517
518 /* A 5 bit shift field. */
519 HOWTO (R_MIPS_SHIFT5, /* type */
520 0, /* rightshift */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
522 5, /* bitsize */
523 false, /* pc_relative */
524 6, /* bitpos */
525 complain_overflow_bitfield, /* complain_on_overflow */
526 bfd_elf_generic_reloc, /* special_function */
527 "R_MIPS_SHIFT5", /* name */
528 true, /* partial_inplace */
529 0x000007c0, /* src_mask */
530 0x000007c0, /* dst_mask */
531 false), /* pcrel_offset */
532
533 /* A 6 bit shift field. */
534 /* FIXME: This is not handled correctly; a special function is
535 needed to put the most significant bit in the right place. */
536 HOWTO (R_MIPS_SHIFT6, /* type */
537 0, /* rightshift */
538 2, /* size (0 = byte, 1 = short, 2 = long) */
539 6, /* bitsize */
540 false, /* pc_relative */
541 6, /* bitpos */
542 complain_overflow_bitfield, /* complain_on_overflow */
543 bfd_elf_generic_reloc, /* special_function */
544 "R_MIPS_SHIFT6", /* name */
545 true, /* partial_inplace */
546 0x000007c4, /* src_mask */
547 0x000007c4, /* dst_mask */
548 false), /* pcrel_offset */
549
550 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
551 are 64 bits long; the upper 32 bits are simply a sign extension.
552 The fields of the howto should be the same as for R_MIPS_32,
553 other than the type, name, and special_function. */
554 HOWTO (R_MIPS_64, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 32, /* bitsize */
558 false, /* pc_relative */
559 0, /* bitpos */
560 complain_overflow_bitfield, /* complain_on_overflow */
561 mips32_64bit_reloc, /* special_function */
562 "R_MIPS_64", /* name */
563 true, /* partial_inplace */
564 0xffffffff, /* src_mask */
565 0xffffffff, /* dst_mask */
566 false), /* pcrel_offset */
567
568 /* Displacement in the global offset table. */
569 /* FIXME: Not handled correctly. */
570 HOWTO (R_MIPS_GOT_DISP, /* type */
571 0, /* rightshift */
572 2, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 false, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_bitfield, /* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_MIPS_GOT_DISP", /* name */
579 true, /* partial_inplace */
580 0x0000ffff, /* src_mask */
581 0x0000ffff, /* dst_mask */
582 false), /* pcrel_offset */
583
584 /* Displacement to page pointer in the global offset table. */
585 /* FIXME: Not handled correctly. */
586 HOWTO (R_MIPS_GOT_PAGE, /* type */
587 0, /* rightshift */
588 2, /* size (0 = byte, 1 = short, 2 = long) */
589 16, /* bitsize */
590 false, /* pc_relative */
591 0, /* bitpos */
592 complain_overflow_bitfield, /* complain_on_overflow */
593 bfd_elf_generic_reloc, /* special_function */
594 "R_MIPS_GOT_PAGE", /* name */
595 true, /* partial_inplace */
596 0x0000ffff, /* src_mask */
597 0x0000ffff, /* dst_mask */
598 false), /* pcrel_offset */
599
600 /* Offset from page pointer in the global offset table. */
601 /* FIXME: Not handled correctly. */
602 HOWTO (R_MIPS_GOT_OFST, /* type */
603 0, /* rightshift */
604 2, /* size (0 = byte, 1 = short, 2 = long) */
605 16, /* bitsize */
606 false, /* pc_relative */
607 0, /* bitpos */
608 complain_overflow_bitfield, /* complain_on_overflow */
609 bfd_elf_generic_reloc, /* special_function */
610 "R_MIPS_GOT_OFST", /* name */
611 true, /* partial_inplace */
612 0x0000ffff, /* src_mask */
613 0x0000ffff, /* dst_mask */
614 false), /* pcrel_offset */
615
616 /* High 16 bits of displacement in global offset table. */
617 /* FIXME: Not handled correctly. */
618 HOWTO (R_MIPS_GOT_HI16, /* type */
619 0, /* rightshift */
620 2, /* size (0 = byte, 1 = short, 2 = long) */
621 16, /* bitsize */
622 false, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_dont, /* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_MIPS_GOT_HI16", /* name */
627 true, /* partial_inplace */
628 0x0000ffff, /* src_mask */
629 0x0000ffff, /* dst_mask */
630 false), /* pcrel_offset */
631
632 /* Low 16 bits of displacement in global offset table. */
633 /* FIXME: Not handled correctly. */
634 HOWTO (R_MIPS_GOT_LO16, /* type */
635 0, /* rightshift */
636 2, /* size (0 = byte, 1 = short, 2 = long) */
637 16, /* bitsize */
638 false, /* pc_relative */
639 0, /* bitpos */
640 complain_overflow_dont, /* complain_on_overflow */
641 bfd_elf_generic_reloc, /* special_function */
642 "R_MIPS_GOT_LO16", /* name */
643 true, /* partial_inplace */
644 0x0000ffff, /* src_mask */
645 0x0000ffff, /* dst_mask */
646 false), /* pcrel_offset */
647
648 /* 64 bit subtraction. Presumably not used in 32 bit ELF. */
649 { R_MIPS_SUB },
650
651 /* Used to cause the linker to insert and delete instructions? */
652 { R_MIPS_INSERT_A },
653 { R_MIPS_INSERT_B },
654 { R_MIPS_DELETE },
655
656 /* Get the higher values of a 64 bit addend. Presumably not used in
657 32 bit ELF. */
658 { R_MIPS_HIGHER },
659 { R_MIPS_HIGHEST },
660
661 /* High 16 bits of displacement in global offset table. */
662 /* FIXME: Not handled correctly. */
663 HOWTO (R_MIPS_CALL_HI16, /* type */
664 0, /* rightshift */
665 2, /* size (0 = byte, 1 = short, 2 = long) */
666 16, /* bitsize */
667 false, /* pc_relative */
668 0, /* bitpos */
669 complain_overflow_dont, /* complain_on_overflow */
670 bfd_elf_generic_reloc, /* special_function */
671 "R_MIPS_CALL_HI16", /* name */
672 true, /* partial_inplace */
673 0x0000ffff, /* src_mask */
674 0x0000ffff, /* dst_mask */
675 false), /* pcrel_offset */
676
677 /* Low 16 bits of displacement in global offset table. */
678 /* FIXME: Not handled correctly. */
679 HOWTO (R_MIPS_CALL_LO16, /* type */
680 0, /* rightshift */
681 2, /* size (0 = byte, 1 = short, 2 = long) */
682 16, /* bitsize */
683 false, /* pc_relative */
684 0, /* bitpos */
685 complain_overflow_dont, /* complain_on_overflow */
686 bfd_elf_generic_reloc, /* special_function */
687 "R_MIPS_CALL_LO16", /* name */
688 true, /* partial_inplace */
689 0x0000ffff, /* src_mask */
690 0x0000ffff, /* dst_mask */
691 false) /* pcrel_offset */
692 };
693
694 /* The reloc used for the mips16 jump instruction. */
695 static reloc_howto_type elf_mips16_jump_howto =
696 HOWTO (R_MIPS16_26, /* type */
697 2, /* rightshift */
698 2, /* size (0 = byte, 1 = short, 2 = long) */
699 26, /* bitsize */
700 false, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_dont, /* complain_on_overflow */
703 /* This needs complex overflow
704 detection, because the upper four
705 bits must match the PC. */
706 mips16_jump_reloc, /* special_function */
707 "R_MIPS16_26", /* name */
708 true, /* partial_inplace */
709 0x3ffffff, /* src_mask */
710 0x3ffffff, /* dst_mask */
711 false); /* pcrel_offset */
712
713 /* Do a R_MIPS_HI16 relocation. This has to be done in combination
714 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
715 the HI16. Here we just save the information we need; we do the
716 actual relocation when we see the LO16. MIPS ELF requires that the
717 LO16 immediately follow the HI16. As a GNU extension, we permit an
718 arbitrary number of HI16 relocs to be associated with a single LO16
719 reloc. This extension permits gcc to output the HI and LO relocs
720 itself. */
721
722 struct mips_hi16
723 {
724 struct mips_hi16 *next;
725 bfd_byte *addr;
726 bfd_vma addend;
727 };
728
729 /* FIXME: This should not be a static variable. */
730
731 static struct mips_hi16 *mips_hi16_list;
732
733 bfd_reloc_status_type
734 _bfd_mips_elf_hi16_reloc (abfd,
735 reloc_entry,
736 symbol,
737 data,
738 input_section,
739 output_bfd,
740 error_message)
741 bfd *abfd;
742 arelent *reloc_entry;
743 asymbol *symbol;
744 PTR data;
745 asection *input_section;
746 bfd *output_bfd;
747 char **error_message;
748 {
749 bfd_reloc_status_type ret;
750 bfd_vma relocation;
751 struct mips_hi16 *n;
752
753 /* If we're relocating, and this an external symbol, we don't want
754 to change anything. */
755 if (output_bfd != (bfd *) NULL
756 && (symbol->flags & BSF_SECTION_SYM) == 0
757 && reloc_entry->addend == 0)
758 {
759 reloc_entry->address += input_section->output_offset;
760 return bfd_reloc_ok;
761 }
762
763 ret = bfd_reloc_ok;
764
765 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
766 {
767 boolean relocateable;
768 bfd_vma gp;
769
770 if (ret == bfd_reloc_undefined)
771 abort ();
772
773 if (output_bfd != NULL)
774 relocateable = true;
775 else
776 {
777 relocateable = false;
778 output_bfd = symbol->section->output_section->owner;
779 }
780
781 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
782 error_message, &gp);
783 if (ret != bfd_reloc_ok)
784 return ret;
785
786 relocation = gp - reloc_entry->address;
787 }
788 else
789 {
790 if (bfd_is_und_section (symbol->section)
791 && output_bfd == (bfd *) NULL)
792 ret = bfd_reloc_undefined;
793
794 if (bfd_is_com_section (symbol->section))
795 relocation = 0;
796 else
797 relocation = symbol->value;
798 }
799
800 relocation += symbol->section->output_section->vma;
801 relocation += symbol->section->output_offset;
802 relocation += reloc_entry->addend;
803
804 if (reloc_entry->address > input_section->_cooked_size)
805 return bfd_reloc_outofrange;
806
807 /* Save the information, and let LO16 do the actual relocation. */
808 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
809 if (n == NULL)
810 return bfd_reloc_outofrange;
811 n->addr = (bfd_byte *) data + reloc_entry->address;
812 n->addend = relocation;
813 n->next = mips_hi16_list;
814 mips_hi16_list = n;
815
816 if (output_bfd != (bfd *) NULL)
817 reloc_entry->address += input_section->output_offset;
818
819 return ret;
820 }
821
822 /* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
823 inplace relocation; this function exists in order to do the
824 R_MIPS_HI16 relocation described above. */
825
826 bfd_reloc_status_type
827 _bfd_mips_elf_lo16_reloc (abfd,
828 reloc_entry,
829 symbol,
830 data,
831 input_section,
832 output_bfd,
833 error_message)
834 bfd *abfd;
835 arelent *reloc_entry;
836 asymbol *symbol;
837 PTR data;
838 asection *input_section;
839 bfd *output_bfd;
840 char **error_message;
841 {
842 arelent gp_disp_relent;
843
844 if (mips_hi16_list != NULL)
845 {
846 struct mips_hi16 *l;
847
848 l = mips_hi16_list;
849 while (l != NULL)
850 {
851 unsigned long insn;
852 unsigned long val;
853 unsigned long vallo;
854 struct mips_hi16 *next;
855
856 /* Do the HI16 relocation. Note that we actually don't need
857 to know anything about the LO16 itself, except where to
858 find the low 16 bits of the addend needed by the LO16. */
859 insn = bfd_get_32 (abfd, l->addr);
860 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
861 & 0xffff);
862 val = ((insn & 0xffff) << 16) + vallo;
863 val += l->addend;
864
865 /* The low order 16 bits are always treated as a signed
866 value. Therefore, a negative value in the low order bits
867 requires an adjustment in the high order bits. We need
868 to make this adjustment in two ways: once for the bits we
869 took from the data, and once for the bits we are putting
870 back in to the data. */
871 if ((vallo & 0x8000) != 0)
872 val -= 0x10000;
873 if ((val & 0x8000) != 0)
874 val += 0x10000;
875
876 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
877 bfd_put_32 (abfd, insn, l->addr);
878
879 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
880 {
881 gp_disp_relent = *reloc_entry;
882 reloc_entry = &gp_disp_relent;
883 reloc_entry->addend = l->addend;
884 }
885
886 next = l->next;
887 free (l);
888 l = next;
889 }
890
891 mips_hi16_list = NULL;
892 }
893 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
894 {
895 bfd_reloc_status_type ret;
896 bfd_vma gp, relocation;
897
898 /* FIXME: Does this case ever occur? */
899
900 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
901 if (ret != bfd_reloc_ok)
902 return ret;
903
904 relocation = gp - reloc_entry->address;
905 relocation += symbol->section->output_section->vma;
906 relocation += symbol->section->output_offset;
907 relocation += reloc_entry->addend;
908
909 if (reloc_entry->address > input_section->_cooked_size)
910 return bfd_reloc_outofrange;
911
912 gp_disp_relent = *reloc_entry;
913 reloc_entry = &gp_disp_relent;
914 reloc_entry->addend = relocation - 4;
915 }
916
917 /* Now do the LO16 reloc in the usual way. */
918 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
919 input_section, output_bfd, error_message);
920 }
921
922 /* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
923 table used for PIC code. If the symbol is an external symbol, the
924 instruction is modified to contain the offset of the appropriate
925 entry in the global offset table. If the symbol is a section
926 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
927 addends are combined to form the real addend against the section
928 symbol; the GOT16 is modified to contain the offset of an entry in
929 the global offset table, and the LO16 is modified to offset it
930 appropriately. Thus an offset larger than 16 bits requires a
931 modified value in the global offset table.
932
933 This implementation suffices for the assembler, but the linker does
934 not yet know how to create global offset tables. */
935
936 bfd_reloc_status_type
937 _bfd_mips_elf_got16_reloc (abfd,
938 reloc_entry,
939 symbol,
940 data,
941 input_section,
942 output_bfd,
943 error_message)
944 bfd *abfd;
945 arelent *reloc_entry;
946 asymbol *symbol;
947 PTR data;
948 asection *input_section;
949 bfd *output_bfd;
950 char **error_message;
951 {
952 /* If we're relocating, and this an external symbol, we don't want
953 to change anything. */
954 if (output_bfd != (bfd *) NULL
955 && (symbol->flags & BSF_SECTION_SYM) == 0
956 && reloc_entry->addend == 0)
957 {
958 reloc_entry->address += input_section->output_offset;
959 return bfd_reloc_ok;
960 }
961
962 /* If we're relocating, and this is a local symbol, we can handle it
963 just like HI16. */
964 if (output_bfd != (bfd *) NULL
965 && (symbol->flags & BSF_SECTION_SYM) != 0)
966 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
967 input_section, output_bfd, error_message);
968
969 abort ();
970 }
971
972 /* We have to figure out the gp value, so that we can adjust the
973 symbol value correctly. We look up the symbol _gp in the output
974 BFD. If we can't find it, we're stuck. We cache it in the ELF
975 target data. We don't need to adjust the symbol value for an
976 external symbol if we are producing relocateable output. */
977
978 static bfd_reloc_status_type
979 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
980 bfd *output_bfd;
981 asymbol *symbol;
982 boolean relocateable;
983 char **error_message;
984 bfd_vma *pgp;
985 {
986 if (bfd_is_und_section (symbol->section)
987 && ! relocateable)
988 {
989 *pgp = 0;
990 return bfd_reloc_undefined;
991 }
992
993 *pgp = _bfd_get_gp_value (output_bfd);
994 if (*pgp == 0
995 && (! relocateable
996 || (symbol->flags & BSF_SECTION_SYM) != 0))
997 {
998 if (relocateable)
999 {
1000 /* Make up a value. */
1001 *pgp = symbol->section->output_section->vma + 0x4000;
1002 _bfd_set_gp_value (output_bfd, *pgp);
1003 }
1004 else
1005 {
1006 unsigned int count;
1007 asymbol **sym;
1008 unsigned int i;
1009
1010 count = bfd_get_symcount (output_bfd);
1011 sym = bfd_get_outsymbols (output_bfd);
1012
1013 if (sym == (asymbol **) NULL)
1014 i = count;
1015 else
1016 {
1017 for (i = 0; i < count; i++, sym++)
1018 {
1019 register CONST char *name;
1020
1021 name = bfd_asymbol_name (*sym);
1022 if (*name == '_' && strcmp (name, "_gp") == 0)
1023 {
1024 *pgp = bfd_asymbol_value (*sym);
1025 _bfd_set_gp_value (output_bfd, *pgp);
1026 break;
1027 }
1028 }
1029 }
1030
1031 if (i >= count)
1032 {
1033 /* Only get the error once. */
1034 *pgp = 4;
1035 _bfd_set_gp_value (output_bfd, *pgp);
1036 *error_message =
1037 (char *) "GP relative relocation when _gp not defined";
1038 return bfd_reloc_dangerous;
1039 }
1040 }
1041 }
1042
1043 return bfd_reloc_ok;
1044 }
1045
1046 /* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1047 become the offset from the gp register. This function also handles
1048 R_MIPS_LITERAL relocations, although those can be handled more
1049 cleverly because the entries in the .lit8 and .lit4 sections can be
1050 merged. */
1051
1052 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1053 arelent *, asection *,
1054 boolean, PTR, bfd_vma));
1055
1056 bfd_reloc_status_type
1057 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1058 output_bfd, error_message)
1059 bfd *abfd;
1060 arelent *reloc_entry;
1061 asymbol *symbol;
1062 PTR data;
1063 asection *input_section;
1064 bfd *output_bfd;
1065 char **error_message;
1066 {
1067 boolean relocateable;
1068 bfd_reloc_status_type ret;
1069 bfd_vma gp;
1070
1071 /* If we're relocating, and this is an external symbol with no
1072 addend, we don't want to change anything. We will only have an
1073 addend if this is a newly created reloc, not read from an ELF
1074 file. */
1075 if (output_bfd != (bfd *) NULL
1076 && (symbol->flags & BSF_SECTION_SYM) == 0
1077 && reloc_entry->addend == 0)
1078 {
1079 reloc_entry->address += input_section->output_offset;
1080 return bfd_reloc_ok;
1081 }
1082
1083 if (output_bfd != (bfd *) NULL)
1084 relocateable = true;
1085 else
1086 {
1087 relocateable = false;
1088 output_bfd = symbol->section->output_section->owner;
1089 }
1090
1091 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1092 &gp);
1093 if (ret != bfd_reloc_ok)
1094 return ret;
1095
1096 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1097 relocateable, data, gp);
1098 }
1099
1100 static bfd_reloc_status_type
1101 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1102 gp)
1103 bfd *abfd;
1104 asymbol *symbol;
1105 arelent *reloc_entry;
1106 asection *input_section;
1107 boolean relocateable;
1108 PTR data;
1109 bfd_vma gp;
1110 {
1111 bfd_vma relocation;
1112 unsigned long insn;
1113 unsigned long val;
1114
1115 if (bfd_is_com_section (symbol->section))
1116 relocation = 0;
1117 else
1118 relocation = symbol->value;
1119
1120 relocation += symbol->section->output_section->vma;
1121 relocation += symbol->section->output_offset;
1122
1123 if (reloc_entry->address > input_section->_cooked_size)
1124 return bfd_reloc_outofrange;
1125
1126 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1127
1128 /* Set val to the offset into the section or symbol. */
1129 if (reloc_entry->howto->src_mask == 0)
1130 {
1131 /* This case occurs with the 64-bit MIPS ELF ABI. */
1132 val = reloc_entry->addend;
1133 }
1134 else
1135 {
1136 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1137 if (val & 0x8000)
1138 val -= 0x10000;
1139 }
1140
1141 /* Adjust val for the final section location and GP value. If we
1142 are producing relocateable output, we don't want to do this for
1143 an external symbol. */
1144 if (! relocateable
1145 || (symbol->flags & BSF_SECTION_SYM) != 0)
1146 val += relocation - gp;
1147
1148 insn = (insn &~ 0xffff) | (val & 0xffff);
1149 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1150
1151 if (relocateable)
1152 reloc_entry->address += input_section->output_offset;
1153
1154 /* Make sure it fit in 16 bits. */
1155 if (val >= 0x8000 && val < 0xffff8000)
1156 return bfd_reloc_overflow;
1157
1158 return bfd_reloc_ok;
1159 }
1160
1161 /* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1162 from the gp register? XXX */
1163
1164 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1165 arelent *, asection *,
1166 boolean, PTR, bfd_vma));
1167
1168 bfd_reloc_status_type
1169 _bfd_mips_elf_gprel32_reloc (abfd,
1170 reloc_entry,
1171 symbol,
1172 data,
1173 input_section,
1174 output_bfd,
1175 error_message)
1176 bfd *abfd;
1177 arelent *reloc_entry;
1178 asymbol *symbol;
1179 PTR data;
1180 asection *input_section;
1181 bfd *output_bfd;
1182 char **error_message;
1183 {
1184 boolean relocateable;
1185 bfd_reloc_status_type ret;
1186 bfd_vma gp;
1187
1188 /* If we're relocating, and this is an external symbol with no
1189 addend, we don't want to change anything. We will only have an
1190 addend if this is a newly created reloc, not read from an ELF
1191 file. */
1192 if (output_bfd != (bfd *) NULL
1193 && (symbol->flags & BSF_SECTION_SYM) == 0
1194 && reloc_entry->addend == 0)
1195 {
1196 *error_message = (char *)
1197 "32bits gp relative relocation occurs for an external symbol";
1198 return bfd_reloc_outofrange;
1199 }
1200
1201 if (output_bfd != (bfd *) NULL)
1202 {
1203 relocateable = true;
1204 gp = _bfd_get_gp_value (output_bfd);
1205 }
1206 else
1207 {
1208 relocateable = false;
1209 output_bfd = symbol->section->output_section->owner;
1210
1211 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1212 error_message, &gp);
1213 if (ret != bfd_reloc_ok)
1214 return ret;
1215 }
1216
1217 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1218 relocateable, data, gp);
1219 }
1220
1221 static bfd_reloc_status_type
1222 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1223 gp)
1224 bfd *abfd;
1225 asymbol *symbol;
1226 arelent *reloc_entry;
1227 asection *input_section;
1228 boolean relocateable;
1229 PTR data;
1230 bfd_vma gp;
1231 {
1232 bfd_vma relocation;
1233 unsigned long val;
1234
1235 if (bfd_is_com_section (symbol->section))
1236 relocation = 0;
1237 else
1238 relocation = symbol->value;
1239
1240 relocation += symbol->section->output_section->vma;
1241 relocation += symbol->section->output_offset;
1242
1243 if (reloc_entry->address > input_section->_cooked_size)
1244 return bfd_reloc_outofrange;
1245
1246 if (reloc_entry->howto->src_mask == 0)
1247 {
1248 /* This case arises with the 64-bit MIPS ELF ABI. */
1249 val = 0;
1250 }
1251 else
1252 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1253
1254 /* Set val to the offset into the section or symbol. */
1255 val += reloc_entry->addend;
1256
1257 /* Adjust val for the final section location and GP value. If we
1258 are producing relocateable output, we don't want to do this for
1259 an external symbol. */
1260 if (! relocateable
1261 || (symbol->flags & BSF_SECTION_SYM) != 0)
1262 val += relocation - gp;
1263
1264 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1265
1266 if (relocateable)
1267 reloc_entry->address += input_section->output_offset;
1268
1269 return bfd_reloc_ok;
1270 }
1271
1272 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1273 generated when addreses are 64 bits. The upper 32 bits are a simle
1274 sign extension. */
1275
1276 static bfd_reloc_status_type
1277 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1278 output_bfd, error_message)
1279 bfd *abfd;
1280 arelent *reloc_entry;
1281 asymbol *symbol;
1282 PTR data;
1283 asection *input_section;
1284 bfd *output_bfd;
1285 char **error_message;
1286 {
1287 bfd_reloc_status_type r;
1288 arelent reloc32;
1289 unsigned long val;
1290 bfd_size_type addr;
1291
1292 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1293 input_section, output_bfd, error_message);
1294 if (r != bfd_reloc_continue)
1295 return r;
1296
1297 /* Do a normal 32 bit relocation on the lower 32 bits. */
1298 reloc32 = *reloc_entry;
1299 if (bfd_big_endian (abfd))
1300 reloc32.address += 4;
1301 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1302 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1303 output_bfd, error_message);
1304
1305 /* Sign extend into the upper 32 bits. */
1306 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1307 if ((val & 0x80000000) != 0)
1308 val = 0xffffffff;
1309 else
1310 val = 0;
1311 addr = reloc_entry->address;
1312 if (bfd_little_endian (abfd))
1313 addr += 4;
1314 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1315
1316 return r;
1317 }
1318
1319 /* Handle a mips16 jump. */
1320
1321 static bfd_reloc_status_type
1322 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1323 output_bfd, error_message)
1324 bfd *abfd;
1325 arelent *reloc_entry;
1326 asymbol *symbol;
1327 PTR data;
1328 asection *input_section;
1329 bfd *output_bfd;
1330 char **error_message;
1331 {
1332 if (output_bfd != (bfd *) NULL
1333 && (symbol->flags & BSF_SECTION_SYM) == 0
1334 && reloc_entry->addend == 0)
1335 {
1336 reloc_entry->address += input_section->output_offset;
1337 return bfd_reloc_ok;
1338 }
1339
1340 /* FIXME. */
1341 abort ();
1342 }
1343
1344 /* A mapping from BFD reloc types to MIPS ELF reloc types. */
1345
1346 struct elf_reloc_map {
1347 bfd_reloc_code_real_type bfd_reloc_val;
1348 enum reloc_type elf_reloc_val;
1349 };
1350
1351 static CONST struct elf_reloc_map mips_reloc_map[] =
1352 {
1353 { BFD_RELOC_NONE, R_MIPS_NONE, },
1354 { BFD_RELOC_16, R_MIPS_16 },
1355 { BFD_RELOC_32, R_MIPS_32 },
1356 { BFD_RELOC_CTOR, R_MIPS_32 },
1357 { BFD_RELOC_64, R_MIPS_64 },
1358 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1359 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1360 { BFD_RELOC_LO16, R_MIPS_LO16 },
1361 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1362 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1363 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1364 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1365 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1366 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1367 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1368 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1369 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1370 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }
1371 };
1372
1373 /* Given a BFD reloc type, return a howto structure. */
1374
1375 static reloc_howto_type *
1376 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1377 bfd *abfd;
1378 bfd_reloc_code_real_type code;
1379 {
1380 unsigned int i;
1381
1382 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1383 {
1384 if (mips_reloc_map[i].bfd_reloc_val == code)
1385 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1386 }
1387
1388 /* Special handling for the MIPS16 jump, since it is a made up reloc
1389 type with a large value. */
1390 if (code == BFD_RELOC_MIPS16_JMP)
1391 return &elf_mips16_jump_howto;
1392
1393 return NULL;
1394 }
1395
1396 /* Given a MIPS reloc type, fill in an arelent structure. */
1397
1398 static void
1399 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1400 bfd *abfd;
1401 arelent *cache_ptr;
1402 Elf32_Internal_Rel *dst;
1403 {
1404 unsigned int r_type;
1405
1406 r_type = ELF32_R_TYPE (dst->r_info);
1407 if (r_type == R_MIPS16_26)
1408 cache_ptr->howto = &elf_mips16_jump_howto;
1409 else
1410 {
1411 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1412 cache_ptr->howto = &elf_mips_howto_table[r_type];
1413 }
1414
1415 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1416 value for the object file. We get the addend now, rather than
1417 when we do the relocation, because the symbol manipulations done
1418 by the linker may cause us to lose track of the input BFD. */
1419 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1420 && (r_type == (unsigned int) R_MIPS_GPREL16
1421 || r_type == (unsigned int) R_MIPS_LITERAL))
1422 cache_ptr->addend = elf_gp (abfd);
1423 }
1424 \f
1425 /* A .reginfo section holds a single Elf32_RegInfo structure. These
1426 routines swap this structure in and out. They are used outside of
1427 BFD, so they are globally visible. */
1428
1429 void
1430 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1431 bfd *abfd;
1432 const Elf32_External_RegInfo *ex;
1433 Elf32_RegInfo *in;
1434 {
1435 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1436 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1437 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1438 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1439 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1440 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1441 }
1442
1443 void
1444 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1445 bfd *abfd;
1446 const Elf32_RegInfo *in;
1447 Elf32_External_RegInfo *ex;
1448 {
1449 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1450 (bfd_byte *) ex->ri_gprmask);
1451 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1452 (bfd_byte *) ex->ri_cprmask[0]);
1453 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1454 (bfd_byte *) ex->ri_cprmask[1]);
1455 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1456 (bfd_byte *) ex->ri_cprmask[2]);
1457 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1458 (bfd_byte *) ex->ri_cprmask[3]);
1459 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1460 (bfd_byte *) ex->ri_gp_value);
1461 }
1462
1463 /* In the 64 bit ABI, the .MIPS.options section holds register
1464 information in an Elf64_Reginfo structure. These routines swap
1465 them in and out. They are globally visible because they are used
1466 outside of BFD. These routines are here so that gas can call them
1467 without worrying about whether the 64 bit ABI has been included. */
1468
1469 void
1470 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1471 bfd *abfd;
1472 const Elf64_External_RegInfo *ex;
1473 Elf64_Internal_RegInfo *in;
1474 {
1475 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1476 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1477 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1478 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1479 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1480 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1481 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1482 }
1483
1484 void
1485 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1486 bfd *abfd;
1487 const Elf64_Internal_RegInfo *in;
1488 Elf64_External_RegInfo *ex;
1489 {
1490 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1491 (bfd_byte *) ex->ri_gprmask);
1492 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1493 (bfd_byte *) ex->ri_pad);
1494 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1495 (bfd_byte *) ex->ri_cprmask[0]);
1496 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1497 (bfd_byte *) ex->ri_cprmask[1]);
1498 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1499 (bfd_byte *) ex->ri_cprmask[2]);
1500 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1501 (bfd_byte *) ex->ri_cprmask[3]);
1502 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1503 (bfd_byte *) ex->ri_gp_value);
1504 }
1505
1506 /* Swap an entry in a .gptab section. Note that these routines rely
1507 on the equivalence of the two elements of the union. */
1508
1509 static void
1510 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1511 bfd *abfd;
1512 const Elf32_External_gptab *ex;
1513 Elf32_gptab *in;
1514 {
1515 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1516 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1517 }
1518
1519 static void
1520 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1521 bfd *abfd;
1522 const Elf32_gptab *in;
1523 Elf32_External_gptab *ex;
1524 {
1525 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1526 ex->gt_entry.gt_g_value);
1527 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1528 ex->gt_entry.gt_bytes);
1529 }
1530
1531 static void
1532 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1533 bfd *abfd;
1534 const Elf32_compact_rel *in;
1535 Elf32_External_compact_rel *ex;
1536 {
1537 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1538 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1539 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1540 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1541 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1542 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1543 }
1544
1545 static void
1546 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1547 bfd *abfd;
1548 const Elf32_crinfo *in;
1549 Elf32_External_crinfo *ex;
1550 {
1551 unsigned long l;
1552
1553 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1554 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1555 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1556 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1557 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1558 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1559 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1560 }
1561
1562 /* Swap in an options header. */
1563
1564 void
1565 bfd_mips_elf_swap_options_in (abfd, ex, in)
1566 bfd *abfd;
1567 const Elf_External_Options *ex;
1568 Elf_Internal_Options *in;
1569 {
1570 in->kind = bfd_h_get_8 (abfd, ex->kind);
1571 in->size = bfd_h_get_8 (abfd, ex->size);
1572 in->section = bfd_h_get_16 (abfd, ex->section);
1573 in->info = bfd_h_get_32 (abfd, ex->info);
1574 }
1575
1576 /* Swap out an options header. */
1577
1578 void
1579 bfd_mips_elf_swap_options_out (abfd, in, ex)
1580 bfd *abfd;
1581 const Elf_Internal_Options *in;
1582 Elf_External_Options *ex;
1583 {
1584 bfd_h_put_8 (abfd, in->kind, ex->kind);
1585 bfd_h_put_8 (abfd, in->size, ex->size);
1586 bfd_h_put_16 (abfd, in->section, ex->section);
1587 bfd_h_put_32 (abfd, in->info, ex->info);
1588 }
1589 \f
1590 /* Determine whether a symbol is global for the purposes of splitting
1591 the symbol table into global symbols and local symbols. At least
1592 on Irix 5, this split must be between section symbols and all other
1593 symbols. On most ELF targets the split is between static symbols
1594 and externally visible symbols. */
1595
1596 /*ARGSUSED*/
1597 static boolean
1598 mips_elf_sym_is_global (abfd, sym)
1599 bfd *abfd;
1600 asymbol *sym;
1601 {
1602 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1603 }
1604 \f
1605 /* Set the right machine number for a MIPS ELF file. This is used for
1606 both the 32-bit and the 64-bit ABI. */
1607
1608 boolean
1609 _bfd_mips_elf_object_p (abfd)
1610 bfd *abfd;
1611 {
1612 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
1613 {
1614 default:
1615 case E_MIPS_ARCH_1:
1616 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 3000);
1617 break;
1618
1619 case E_MIPS_ARCH_2:
1620 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
1621 break;
1622
1623 case E_MIPS_ARCH_3:
1624 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
1625 break;
1626
1627 case E_MIPS_ARCH_4:
1628 (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 8000);
1629 break;
1630 }
1631
1632 return true;
1633 }
1634
1635 /* Set the right machine number for a 32-bit MIPS ELF file. */
1636
1637 static boolean
1638 mips_elf32_object_p (abfd)
1639 bfd *abfd;
1640 {
1641 /* Irix 5 is broken. Object file symbol tables are not always
1642 sorted correctly such that local symbols precede global symbols,
1643 and the sh_info field in the symbol table is not always right. */
1644 elf_bad_symtab (abfd) = true;
1645
1646 return _bfd_mips_elf_object_p (abfd);
1647 }
1648
1649 /* The final processing done just before writing out a MIPS ELF object
1650 file. This gets the MIPS architecture right based on the machine
1651 number. This is used by both the 32-bit and the 64-bit ABI. */
1652
1653 /*ARGSUSED*/
1654 void
1655 _bfd_mips_elf_final_write_processing (abfd, linker)
1656 bfd *abfd;
1657 boolean linker;
1658 {
1659 unsigned long val;
1660 unsigned int i;
1661 Elf_Internal_Shdr **hdrpp;
1662 const char *name;
1663 asection *sec;
1664
1665 switch (bfd_get_mach (abfd))
1666 {
1667 case 3000:
1668 val = E_MIPS_ARCH_1;
1669 break;
1670
1671 case 6000:
1672 val = E_MIPS_ARCH_2;
1673 break;
1674
1675 case 4000:
1676 val = E_MIPS_ARCH_3;
1677 break;
1678
1679 case 8000:
1680 val = E_MIPS_ARCH_4;
1681 break;
1682
1683 default:
1684 val = 0;
1685 break;
1686 }
1687
1688 elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1689 elf_elfheader (abfd)->e_flags |= val;
1690
1691 /* Set the sh_info field for .gptab sections. */
1692 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1693 i < elf_elfheader (abfd)->e_shnum;
1694 i++, hdrpp++)
1695 {
1696 switch ((*hdrpp)->sh_type)
1697 {
1698 case SHT_MIPS_LIBLIST:
1699 sec = bfd_get_section_by_name (abfd, ".dynstr");
1700 if (sec != NULL)
1701 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1702 break;
1703
1704 case SHT_MIPS_GPTAB:
1705 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1706 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1707 BFD_ASSERT (name != NULL
1708 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1709 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1710 BFD_ASSERT (sec != NULL);
1711 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1712 break;
1713
1714 case SHT_MIPS_CONTENT:
1715 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1716 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1717 BFD_ASSERT (name != NULL
1718 && strncmp (name, ".MIPS.content",
1719 sizeof ".MIPS.content" - 1) == 0);
1720 sec = bfd_get_section_by_name (abfd,
1721 name + sizeof ".MIPS.content" - 1);
1722 BFD_ASSERT (sec != NULL);
1723 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1724 break;
1725
1726 case SHT_MIPS_SYMBOL_LIB:
1727 sec = bfd_get_section_by_name (abfd, ".dynsym");
1728 if (sec != NULL)
1729 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1730 sec = bfd_get_section_by_name (abfd, ".liblist");
1731 if (sec != NULL)
1732 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1733 break;
1734
1735 case SHT_MIPS_EVENTS:
1736 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1737 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1738 BFD_ASSERT (name != NULL);
1739 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
1740 sec = bfd_get_section_by_name (abfd,
1741 name + sizeof ".MIPS.events" - 1);
1742 else
1743 {
1744 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
1745 sizeof ".MIPS.post_rel" - 1) == 0);
1746 sec = bfd_get_section_by_name (abfd,
1747 (name
1748 + sizeof ".MIPS.post_rel" - 1));
1749 }
1750 BFD_ASSERT (sec != NULL);
1751 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1752 break;
1753 }
1754 }
1755 }
1756 \f
1757 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1758
1759 boolean
1760 _bfd_mips_elf_set_private_flags (abfd, flags)
1761 bfd *abfd;
1762 flagword flags;
1763 {
1764 BFD_ASSERT (!elf_flags_init (abfd)
1765 || elf_elfheader (abfd)->e_flags == flags);
1766
1767 elf_elfheader (abfd)->e_flags = flags;
1768 elf_flags_init (abfd) = true;
1769 return true;
1770 }
1771
1772 /* Copy backend specific data from one object module to another */
1773
1774 boolean
1775 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
1776 bfd *ibfd;
1777 bfd *obfd;
1778 {
1779 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1780 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1781 return true;
1782
1783 BFD_ASSERT (!elf_flags_init (obfd)
1784 || (elf_elfheader (obfd)->e_flags
1785 == elf_elfheader (ibfd)->e_flags));
1786
1787 elf_gp (obfd) = elf_gp (ibfd);
1788 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1789 elf_flags_init (obfd) = true;
1790 return true;
1791 }
1792
1793 /* Return the ISA for a MIPS e_flags value. */
1794
1795 static INLINE int
1796 elf_mips_isa (flags)
1797 flagword flags;
1798 {
1799 switch (flags & EF_MIPS_ARCH)
1800 {
1801 case E_MIPS_ARCH_1:
1802 return 1;
1803 case E_MIPS_ARCH_2:
1804 return 2;
1805 case E_MIPS_ARCH_3:
1806 return 3;
1807 case E_MIPS_ARCH_4:
1808 return 4;
1809 }
1810 return 4;
1811 }
1812
1813 /* Merge backend specific data from an object file to the output
1814 object file when linking. */
1815
1816 boolean
1817 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
1818 bfd *ibfd;
1819 bfd *obfd;
1820 {
1821 flagword old_flags;
1822 flagword new_flags;
1823 boolean ok;
1824
1825 /* Check if we have the same endianess */
1826 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1827 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1828 {
1829 (*_bfd_error_handler)
1830 ("%s: compiled for a %s endian system and target is %s endian",
1831 bfd_get_filename (ibfd),
1832 bfd_big_endian (ibfd) ? "big" : "little",
1833 bfd_big_endian (obfd) ? "big" : "little");
1834
1835 bfd_set_error (bfd_error_wrong_format);
1836 return false;
1837 }
1838
1839 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1840 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1841 return true;
1842
1843 new_flags = elf_elfheader (ibfd)->e_flags;
1844 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
1845 old_flags = elf_elfheader (obfd)->e_flags;
1846
1847 if (! elf_flags_init (obfd))
1848 {
1849 elf_flags_init (obfd) = true;
1850 elf_elfheader (obfd)->e_flags = new_flags;
1851
1852 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1853 && bfd_get_arch_info (obfd)->the_default)
1854 {
1855 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1856 bfd_get_mach (ibfd)))
1857 return false;
1858 }
1859
1860 return true;
1861 }
1862
1863 /* Check flag compatibility. */
1864
1865 new_flags &= ~EF_MIPS_NOREORDER;
1866 old_flags &= ~EF_MIPS_NOREORDER;
1867
1868 if (new_flags == old_flags)
1869 return true;
1870
1871 ok = true;
1872
1873 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
1874 {
1875 new_flags &= ~EF_MIPS_PIC;
1876 old_flags &= ~EF_MIPS_PIC;
1877 (*_bfd_error_handler)
1878 ("%s: linking PIC files with non-PIC files",
1879 bfd_get_filename (ibfd));
1880 ok = false;
1881 }
1882
1883 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
1884 {
1885 new_flags &= ~EF_MIPS_CPIC;
1886 old_flags &= ~EF_MIPS_CPIC;
1887 (*_bfd_error_handler)
1888 ("%s: linking abicalls files with non-abicalls files",
1889 bfd_get_filename (ibfd));
1890 ok = false;
1891 }
1892
1893 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
1894 and -mips4 code. They will normally use the same data sizes and
1895 calling conventions. */
1896 if ((new_flags & EF_MIPS_ARCH) != (old_flags & EF_MIPS_ARCH))
1897 {
1898 int new_isa, old_isa;
1899
1900 new_isa = elf_mips_isa (new_flags);
1901 old_isa = elf_mips_isa (old_flags);
1902 if ((new_isa == 1 || new_isa == 2)
1903 ? (old_isa != 1 && old_isa != 2)
1904 : (old_isa == 1 || old_isa == 2))
1905 {
1906 (*_bfd_error_handler)
1907 ("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)",
1908 bfd_get_filename (ibfd), new_isa, old_isa);
1909 ok = false;
1910 }
1911
1912 new_flags &= ~ EF_MIPS_ARCH;
1913 old_flags &= ~ EF_MIPS_ARCH;
1914 }
1915
1916 /* Warn about any other mismatches */
1917 if (new_flags != old_flags)
1918 {
1919 (*_bfd_error_handler)
1920 ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
1921 bfd_get_filename (ibfd), (unsigned long) new_flags,
1922 (unsigned long) old_flags);
1923 ok = false;
1924 }
1925
1926 if (! ok)
1927 {
1928 bfd_set_error (bfd_error_bad_value);
1929 return false;
1930 }
1931
1932 return true;
1933 }
1934 \f
1935 /* Handle a MIPS specific section when reading an object file. This
1936 is called when elfcode.h finds a section with an unknown type.
1937 This routine supports both the 32-bit and 64-bit ELF ABI.
1938
1939 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
1940 how to. */
1941
1942 boolean
1943 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
1944 bfd *abfd;
1945 Elf_Internal_Shdr *hdr;
1946 const char *name;
1947 {
1948 /* There ought to be a place to keep ELF backend specific flags, but
1949 at the moment there isn't one. We just keep track of the
1950 sections by their name, instead. Fortunately, the ABI gives
1951 suggested names for all the MIPS specific sections, so we will
1952 probably get away with this. */
1953 switch (hdr->sh_type)
1954 {
1955 case SHT_MIPS_LIBLIST:
1956 if (strcmp (name, ".liblist") != 0)
1957 return false;
1958 break;
1959 case SHT_MIPS_MSYM:
1960 if (strcmp (name, ".msym") != 0)
1961 return false;
1962 break;
1963 case SHT_MIPS_CONFLICT:
1964 if (strcmp (name, ".conflict") != 0)
1965 return false;
1966 break;
1967 case SHT_MIPS_GPTAB:
1968 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
1969 return false;
1970 break;
1971 case SHT_MIPS_UCODE:
1972 if (strcmp (name, ".ucode") != 0)
1973 return false;
1974 break;
1975 case SHT_MIPS_DEBUG:
1976 if (strcmp (name, ".mdebug") != 0)
1977 return false;
1978 break;
1979 case SHT_MIPS_REGINFO:
1980 if (strcmp (name, ".reginfo") != 0
1981 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
1982 return false;
1983 break;
1984 case SHT_MIPS_IFACE:
1985 if (strcmp (name, ".MIPS.interfaces") != 0)
1986 return false;
1987 break;
1988 case SHT_MIPS_CONTENT:
1989 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
1990 return false;
1991 break;
1992 case SHT_MIPS_OPTIONS:
1993 if (strcmp (name, ".options") != 0
1994 && strcmp (name, ".MIPS.options") != 0)
1995 return false;
1996 break;
1997 case SHT_MIPS_DWARF:
1998 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
1999 return false;
2000 break;
2001 case SHT_MIPS_SYMBOL_LIB:
2002 if (strcmp (name, ".MIPS.symlib") != 0)
2003 return false;
2004 break;
2005 case SHT_MIPS_EVENTS:
2006 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2007 && strncmp (name, ".MIPS.post_rel",
2008 sizeof ".MIPS.post_rel" - 1) != 0)
2009 return false;
2010 break;
2011 default:
2012 return false;
2013 }
2014
2015 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2016 return false;
2017
2018 if (hdr->sh_type == SHT_MIPS_DEBUG)
2019 {
2020 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2021 (bfd_get_section_flags (abfd,
2022 hdr->bfd_section)
2023 | SEC_DEBUGGING)))
2024 return false;
2025 }
2026
2027 return true;
2028 }
2029
2030 /* Handle a 32-bit MIPS ELF specific section. */
2031
2032 static boolean
2033 mips_elf32_section_from_shdr (abfd, hdr, name)
2034 bfd *abfd;
2035 Elf_Internal_Shdr *hdr;
2036 char *name;
2037 {
2038 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2039 return false;
2040
2041 /* FIXME: We should record sh_info for a .gptab section. */
2042
2043 /* For a .reginfo section, set the gp value in the tdata information
2044 from the contents of this section. We need the gp value while
2045 processing relocs, so we just get it now. The .reginfo section
2046 is not used in the 64-bit MIPS ELF ABI. */
2047 if (hdr->sh_type == SHT_MIPS_REGINFO)
2048 {
2049 Elf32_External_RegInfo ext;
2050 Elf32_RegInfo s;
2051
2052 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2053 (file_ptr) 0, sizeof ext))
2054 return false;
2055 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2056 elf_gp (abfd) = s.ri_gp_value;
2057 }
2058
2059 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2060 set the gp value based on what we find. We may see both
2061 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2062 they should agree. */
2063 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2064 {
2065 bfd_byte *contents, *l, *lend;
2066
2067 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2068 if (contents == NULL)
2069 return false;
2070 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2071 (file_ptr) 0, hdr->sh_size))
2072 {
2073 free (contents);
2074 return false;
2075 }
2076 l = contents;
2077 lend = contents + hdr->sh_size;
2078 while (l + sizeof (Elf_External_Options) <= lend)
2079 {
2080 Elf_Internal_Options intopt;
2081
2082 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2083 &intopt);
2084 if (intopt.kind == ODK_REGINFO)
2085 {
2086 Elf32_RegInfo intreg;
2087
2088 bfd_mips_elf32_swap_reginfo_in
2089 (abfd,
2090 ((Elf32_External_RegInfo *)
2091 (l + sizeof (Elf_External_Options))),
2092 &intreg);
2093 elf_gp (abfd) = intreg.ri_gp_value;
2094 }
2095 l += intopt.size;
2096 }
2097 free (contents);
2098 }
2099
2100 return true;
2101 }
2102
2103 /* Set the correct type for a MIPS ELF section. We do this by the
2104 section name, which is a hack, but ought to work. This routine is
2105 used by both the 32-bit and the 64-bit ABI. */
2106
2107 boolean
2108 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2109 bfd *abfd;
2110 Elf32_Internal_Shdr *hdr;
2111 asection *sec;
2112 {
2113 register const char *name;
2114
2115 name = bfd_get_section_name (abfd, sec);
2116
2117 if (strcmp (name, ".liblist") == 0)
2118 {
2119 hdr->sh_type = SHT_MIPS_LIBLIST;
2120 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2121 /* The sh_link field is set in final_write_processing. */
2122 }
2123 else if (strcmp (name, ".msym") == 0)
2124 {
2125 hdr->sh_type = SHT_MIPS_MSYM;
2126 hdr->sh_entsize = 8;
2127 /* FIXME: Set the sh_info field. */
2128 }
2129 else if (strcmp (name, ".conflict") == 0)
2130 hdr->sh_type = SHT_MIPS_CONFLICT;
2131 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2132 {
2133 hdr->sh_type = SHT_MIPS_GPTAB;
2134 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2135 /* The sh_info field is set in final_write_processing. */
2136 }
2137 else if (strcmp (name, ".ucode") == 0)
2138 hdr->sh_type = SHT_MIPS_UCODE;
2139 else if (strcmp (name, ".mdebug") == 0)
2140 {
2141 hdr->sh_type = SHT_MIPS_DEBUG;
2142 /* In a shared object on Irix 5.3, the .mdebug section has an
2143 entsize of 0. FIXME: Does this matter? */
2144 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2145 hdr->sh_entsize = 0;
2146 else
2147 hdr->sh_entsize = 1;
2148 }
2149 else if (strcmp (name, ".reginfo") == 0)
2150 {
2151 hdr->sh_type = SHT_MIPS_REGINFO;
2152 /* In a shared object on Irix 5.3, the .reginfo section has an
2153 entsize of 0x18. FIXME: Does this matter? */
2154 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2155 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2156 else
2157 hdr->sh_entsize = 1;
2158
2159 /* Force the section size to the correct value, even if the
2160 linker thinks it is larger. The link routine below will only
2161 write out this much data for .reginfo. */
2162 hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
2163 }
2164 else if (SGI_COMPAT (abfd)
2165 && (strcmp (name, ".hash") == 0
2166 || strcmp (name, ".dynamic") == 0
2167 || strcmp (name, ".dynstr") == 0))
2168 {
2169 hdr->sh_entsize = 0;
2170 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2171 }
2172 else if (strcmp (name, ".got") == 0
2173 || strcmp (name, ".sdata") == 0
2174 || strcmp (name, ".sbss") == 0
2175 || strcmp (name, ".lit4") == 0
2176 || strcmp (name, ".lit8") == 0)
2177 hdr->sh_flags |= SHF_MIPS_GPREL;
2178 else if (strcmp (name, ".MIPS.interfaces") == 0)
2179 {
2180 hdr->sh_type = SHT_MIPS_IFACE;
2181 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2182 }
2183 else if (strcmp (name, ".MIPS.content") == 0)
2184 {
2185 hdr->sh_type = SHT_MIPS_CONTENT;
2186 /* The sh_info field is set in final_write_processing. */
2187 }
2188 else if (strcmp (name, ".options") == 0
2189 || strcmp (name, ".MIPS.options") == 0)
2190 {
2191 hdr->sh_type = SHT_MIPS_OPTIONS;
2192 hdr->sh_entsize = 1;
2193 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2194 }
2195 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2196 hdr->sh_type = SHT_MIPS_DWARF;
2197 else if (strcmp (name, ".MIPS.symlib") == 0)
2198 {
2199 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2200 /* The sh_link and sh_info fields are set in
2201 final_write_processing. */
2202 }
2203 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2204 || strncmp (name, ".MIPS.post_rel",
2205 sizeof ".MIPS.post_rel" - 1) == 0)
2206 {
2207 hdr->sh_type = SHT_MIPS_EVENTS;
2208 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2209 /* The sh_link field is set in final_write_processing. */
2210 }
2211
2212 return true;
2213 }
2214
2215 /* Given a BFD section, try to locate the corresponding ELF section
2216 index. This is used by both the 32-bit and the 64-bit ABI.
2217 Actually, it's not clear to me that the 64-bit ABI supports these,
2218 but for non-PIC objects we will certainly want support for at least
2219 the .scommon section. */
2220
2221 boolean
2222 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2223 bfd *abfd;
2224 Elf32_Internal_Shdr *hdr;
2225 asection *sec;
2226 int *retval;
2227 {
2228 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2229 {
2230 *retval = SHN_MIPS_SCOMMON;
2231 return true;
2232 }
2233 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2234 {
2235 *retval = SHN_MIPS_ACOMMON;
2236 return true;
2237 }
2238 return false;
2239 }
2240
2241 /* When are writing out the .options or .MIPS.options section,
2242 remember the bytes we are writing out, so that we can install the
2243 GP value in the section_processing routine. */
2244
2245 boolean
2246 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2247 bfd *abfd;
2248 sec_ptr section;
2249 PTR location;
2250 file_ptr offset;
2251 bfd_size_type count;
2252 {
2253 if (strcmp (section->name, ".options") == 0
2254 || strcmp (section->name, ".MIPS.options") == 0)
2255 {
2256 bfd_byte *c;
2257
2258 if (elf_section_data (section) == NULL)
2259 {
2260 section->used_by_bfd =
2261 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2262 if (elf_section_data (section) == NULL)
2263 return false;
2264 }
2265 c = (bfd_byte *) elf_section_data (section)->tdata;
2266 if (c == NULL)
2267 {
2268 bfd_size_type size;
2269
2270 if (section->_cooked_size != 0)
2271 size = section->_cooked_size;
2272 else
2273 size = section->_raw_size;
2274 c = (PTR) bfd_zalloc (abfd, size);
2275 if (c == NULL)
2276 return false;
2277 elf_section_data (section)->tdata = (PTR) c;
2278 }
2279
2280 memcpy (c + offset, location, count);
2281 }
2282
2283 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2284 count);
2285 }
2286
2287 /* Work over a section just before writing it out. This routine is
2288 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2289 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2290 a better way. */
2291
2292 boolean
2293 _bfd_mips_elf_section_processing (abfd, hdr)
2294 bfd *abfd;
2295 Elf_Internal_Shdr *hdr;
2296 {
2297 if (hdr->bfd_section != NULL)
2298 {
2299 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2300
2301 if (strcmp (name, ".sdata") == 0)
2302 {
2303 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2304 hdr->sh_type = SHT_PROGBITS;
2305 }
2306 else if (strcmp (name, ".sbss") == 0)
2307 {
2308 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2309 hdr->sh_type = SHT_NOBITS;
2310 }
2311 else if (strcmp (name, ".lit8") == 0
2312 || strcmp (name, ".lit4") == 0)
2313 {
2314 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2315 hdr->sh_type = SHT_PROGBITS;
2316 }
2317 else if (strcmp (name, ".compact_rel") == 0)
2318 {
2319 hdr->sh_flags = 0;
2320 hdr->sh_type = SHT_PROGBITS;
2321 }
2322 else if (strcmp (name, ".rtproc") == 0)
2323 {
2324 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2325 {
2326 unsigned int adjust;
2327
2328 adjust = hdr->sh_size % hdr->sh_addralign;
2329 if (adjust != 0)
2330 hdr->sh_size += hdr->sh_addralign - adjust;
2331 }
2332 }
2333 }
2334
2335 return true;
2336 }
2337
2338 /* Work over a section just before writing it out. We update the GP
2339 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2340 on the value we are using. */
2341
2342 static boolean
2343 mips_elf32_section_processing (abfd, hdr)
2344 bfd *abfd;
2345 Elf32_Internal_Shdr *hdr;
2346 {
2347 if (hdr->sh_type == SHT_MIPS_REGINFO)
2348 {
2349 bfd_byte buf[4];
2350
2351 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2352 BFD_ASSERT (hdr->contents == NULL);
2353
2354 if (bfd_seek (abfd,
2355 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2356 SEEK_SET) == -1)
2357 return false;
2358 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2359 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2360 return false;
2361 }
2362
2363 if (hdr->sh_type == SHT_MIPS_OPTIONS
2364 && hdr->bfd_section != NULL
2365 && elf_section_data (hdr->bfd_section) != NULL
2366 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2367 {
2368 bfd_byte *contents, *l, *lend;
2369
2370 /* We stored the section contents in the elf_section_data tdata
2371 field in the set_section_contents routine. We save the
2372 section contents so that we don't have to read them again.
2373 At this point we know that elf_gp is set, so we can look
2374 through the section contents to see if there is an
2375 ODK_REGINFO structure. */
2376
2377 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2378 l = contents;
2379 lend = contents + hdr->sh_size;
2380 while (l + sizeof (Elf_External_Options) <= lend)
2381 {
2382 Elf_Internal_Options intopt;
2383
2384 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2385 &intopt);
2386 if (intopt.kind == ODK_REGINFO)
2387 {
2388 bfd_byte buf[4];
2389
2390 if (bfd_seek (abfd,
2391 (hdr->sh_offset
2392 + (l - contents)
2393 + sizeof (Elf_External_Options)
2394 + (sizeof (Elf32_External_RegInfo) - 4)),
2395 SEEK_SET) == -1)
2396 return false;
2397 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2398 if (bfd_write (buf, 1, 4, abfd) != 4)
2399 return false;
2400 }
2401 l += intopt.size;
2402 }
2403 }
2404
2405 return _bfd_mips_elf_section_processing (abfd, hdr);
2406 }
2407 \f
2408 /* MIPS ELF uses two common sections. One is the usual one, and the
2409 other is for small objects. All the small objects are kept
2410 together, and then referenced via the gp pointer, which yields
2411 faster assembler code. This is what we use for the small common
2412 section. This approach is copied from ecoff.c. */
2413 static asection mips_elf_scom_section;
2414 static asymbol mips_elf_scom_symbol;
2415 static asymbol *mips_elf_scom_symbol_ptr;
2416
2417 /* MIPS ELF also uses an acommon section, which represents an
2418 allocated common symbol which may be overridden by a
2419 definition in a shared library. */
2420 static asection mips_elf_acom_section;
2421 static asymbol mips_elf_acom_symbol;
2422 static asymbol *mips_elf_acom_symbol_ptr;
2423
2424 /* The Irix 5 support uses two virtual sections, which represent
2425 text/data symbols defined in dynamic objects. */
2426 static asection mips_elf_text_section;
2427 static asection *mips_elf_text_section_ptr;
2428 static asymbol mips_elf_text_symbol;
2429 static asymbol *mips_elf_text_symbol_ptr;
2430
2431 static asection mips_elf_data_section;
2432 static asection *mips_elf_data_section_ptr;
2433 static asymbol mips_elf_data_symbol;
2434 static asymbol *mips_elf_data_symbol_ptr;
2435
2436 /* Handle the special MIPS section numbers that a symbol may use.
2437 This is used for both the 32-bit and the 64-bit ABI. */
2438
2439 void
2440 _bfd_mips_elf_symbol_processing (abfd, asym)
2441 bfd *abfd;
2442 asymbol *asym;
2443 {
2444 elf_symbol_type *elfsym;
2445
2446 elfsym = (elf_symbol_type *) asym;
2447 switch (elfsym->internal_elf_sym.st_shndx)
2448 {
2449 case SHN_MIPS_ACOMMON:
2450 /* This section is used in a dynamically linked executable file.
2451 It is an allocated common section. The dynamic linker can
2452 either resolve these symbols to something in a shared
2453 library, or it can just leave them here. For our purposes,
2454 we can consider these symbols to be in a new section. */
2455 if (mips_elf_acom_section.name == NULL)
2456 {
2457 /* Initialize the acommon section. */
2458 mips_elf_acom_section.name = ".acommon";
2459 mips_elf_acom_section.flags = SEC_ALLOC;
2460 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2461 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2462 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2463 mips_elf_acom_symbol.name = ".acommon";
2464 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2465 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2466 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2467 }
2468 asym->section = &mips_elf_acom_section;
2469 break;
2470
2471 case SHN_COMMON:
2472 /* Common symbols less than the GP size are automatically
2473 treated as SHN_MIPS_SCOMMON symbols. */
2474 if (asym->value > elf_gp_size (abfd))
2475 break;
2476 /* Fall through. */
2477 case SHN_MIPS_SCOMMON:
2478 if (mips_elf_scom_section.name == NULL)
2479 {
2480 /* Initialize the small common section. */
2481 mips_elf_scom_section.name = ".scommon";
2482 mips_elf_scom_section.flags = SEC_IS_COMMON;
2483 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2484 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2485 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2486 mips_elf_scom_symbol.name = ".scommon";
2487 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2488 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2489 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2490 }
2491 asym->section = &mips_elf_scom_section;
2492 asym->value = elfsym->internal_elf_sym.st_size;
2493 break;
2494
2495 case SHN_MIPS_SUNDEFINED:
2496 asym->section = bfd_und_section_ptr;
2497 break;
2498
2499 #if 0 /* for SGI_COMPAT */
2500 case SHN_MIPS_TEXT:
2501 asym->section = mips_elf_text_section_ptr;
2502 break;
2503
2504 case SHN_MIPS_DATA:
2505 asym->section = mips_elf_data_section_ptr;
2506 break;
2507 #endif
2508 }
2509 }
2510 \f
2511 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2512 segments. */
2513
2514 static int
2515 mips_elf_additional_program_headers (abfd)
2516 bfd *abfd;
2517 {
2518 asection *s;
2519 int ret;
2520
2521 ret = 0;
2522
2523 if (! SGI_COMPAT (abfd))
2524 return ret;
2525
2526 s = bfd_get_section_by_name (abfd, ".reginfo");
2527 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2528 {
2529 /* We need a PT_MIPS_REGINFO segment. */
2530 ++ret;
2531 }
2532
2533 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2534 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2535 {
2536 /* We need a PT_MIPS_RTPROC segment. */
2537 ++ret;
2538 }
2539
2540 return ret;
2541 }
2542
2543 /* Modify the segment map for an Irix 5 executable. */
2544
2545 static boolean
2546 mips_elf_modify_segment_map (abfd)
2547 bfd *abfd;
2548 {
2549 asection *s;
2550 struct elf_segment_map *m, **pm;
2551
2552 if (! SGI_COMPAT (abfd))
2553 return true;
2554
2555 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2556 segment. */
2557 s = bfd_get_section_by_name (abfd, ".reginfo");
2558 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2559 {
2560 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2561 if (m->p_type == PT_MIPS_REGINFO)
2562 break;
2563 if (m == NULL)
2564 {
2565 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2566 if (m == NULL)
2567 return false;
2568
2569 m->p_type = PT_MIPS_REGINFO;
2570 m->count = 1;
2571 m->sections[0] = s;
2572
2573 /* We want to put it after the PHDR and INTERP segments. */
2574 pm = &elf_tdata (abfd)->segment_map;
2575 while (*pm != NULL
2576 && ((*pm)->p_type == PT_PHDR
2577 || (*pm)->p_type == PT_INTERP))
2578 pm = &(*pm)->next;
2579
2580 m->next = *pm;
2581 *pm = m;
2582 }
2583 }
2584
2585 /* If there are .dynamic and .mdebug sections, we make a room for
2586 the RTPROC header. FIXME: Rewrite without section names. */
2587 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2588 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2589 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2590 {
2591 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2592 if (m->p_type == PT_MIPS_RTPROC)
2593 break;
2594 if (m == NULL)
2595 {
2596 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2597 if (m == NULL)
2598 return false;
2599
2600 m->p_type = PT_MIPS_RTPROC;
2601
2602 s = bfd_get_section_by_name (abfd, ".rtproc");
2603 if (s == NULL)
2604 {
2605 m->count = 0;
2606 m->p_flags = 0;
2607 m->p_flags_valid = 1;
2608 }
2609 else
2610 {
2611 m->count = 1;
2612 m->sections[0] = s;
2613 }
2614
2615 /* We want to put it after the DYNAMIC segment. */
2616 pm = &elf_tdata (abfd)->segment_map;
2617 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2618 pm = &(*pm)->next;
2619 if (*pm != NULL)
2620 pm = &(*pm)->next;
2621
2622 m->next = *pm;
2623 *pm = m;
2624 }
2625 }
2626
2627 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2628 .dynsym, and .hash sections, and everything in between. */
2629 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2630 if ((*pm)->p_type == PT_DYNAMIC)
2631 break;
2632 m = *pm;
2633 if (m != NULL
2634 && m->count == 1
2635 && strcmp (m->sections[0]->name, ".dynamic") == 0)
2636 {
2637 static const char *sec_names[] =
2638 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2639 bfd_vma low, high;
2640 unsigned int i, c;
2641 struct elf_segment_map *n;
2642
2643 low = 0xffffffff;
2644 high = 0;
2645 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
2646 {
2647 s = bfd_get_section_by_name (abfd, sec_names[i]);
2648 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2649 {
2650 bfd_size_type sz;
2651
2652 if (low > s->vma)
2653 low = s->vma;
2654 sz = s->_cooked_size;
2655 if (sz == 0)
2656 sz = s->_raw_size;
2657 if (high < s->vma + sz)
2658 high = s->vma + sz;
2659 }
2660 }
2661
2662 c = 0;
2663 for (s = abfd->sections; s != NULL; s = s->next)
2664 if ((s->flags & SEC_LOAD) != 0
2665 && s->vma >= low
2666 && ((s->vma
2667 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2668 <= high))
2669 ++c;
2670
2671 n = ((struct elf_segment_map *)
2672 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
2673 if (n == NULL)
2674 return false;
2675 *n = *m;
2676 n->count = c;
2677
2678 i = 0;
2679 for (s = abfd->sections; s != NULL; s = s->next)
2680 {
2681 if ((s->flags & SEC_LOAD) != 0
2682 && s->vma >= low
2683 && ((s->vma
2684 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2685 <= high))
2686 {
2687 n->sections[i] = s;
2688 ++i;
2689 }
2690 }
2691
2692 *pm = n;
2693 }
2694
2695 return true;
2696 }
2697 \f
2698 /* The structure of the runtime procedure descriptor created by the
2699 loader for use by the static exception system. */
2700
2701 typedef struct runtime_pdr {
2702 bfd_vma adr; /* memory address of start of procedure */
2703 long regmask; /* save register mask */
2704 long regoffset; /* save register offset */
2705 long fregmask; /* save floating point register mask */
2706 long fregoffset; /* save floating point register offset */
2707 long frameoffset; /* frame size */
2708 short framereg; /* frame pointer register */
2709 short pcreg; /* offset or reg of return pc */
2710 long irpss; /* index into the runtime string table */
2711 long reserved;
2712 struct exception_info *exception_info;/* pointer to exception array */
2713 } RPDR, *pRPDR;
2714 #define cbRPDR sizeof(RPDR)
2715 #define rpdNil ((pRPDR) 0)
2716
2717 /* Swap RPDR (runtime procedure table entry) for output. */
2718
2719 static void ecoff_swap_rpdr_out
2720 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
2721
2722 static void
2723 ecoff_swap_rpdr_out (abfd, in, ex)
2724 bfd *abfd;
2725 const RPDR *in;
2726 struct rpdr_ext *ex;
2727 {
2728 /* ecoff_put_off was defined in ecoffswap.h. */
2729 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
2730 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
2731 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
2732 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
2733 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
2734 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
2735
2736 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
2737 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
2738
2739 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
2740 #if 0 /* FIXME */
2741 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
2742 #endif
2743 }
2744 \f
2745 /* Read ECOFF debugging information from a .mdebug section into a
2746 ecoff_debug_info structure. */
2747
2748 boolean
2749 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
2750 bfd *abfd;
2751 asection *section;
2752 struct ecoff_debug_info *debug;
2753 {
2754 HDRR *symhdr;
2755 const struct ecoff_debug_swap *swap;
2756 char *ext_hdr = NULL;
2757
2758 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2759
2760 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
2761 if (ext_hdr == NULL && swap->external_hdr_size != 0)
2762 goto error_return;
2763
2764 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2765 swap->external_hdr_size)
2766 == false)
2767 goto error_return;
2768
2769 symhdr = &debug->symbolic_header;
2770 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2771
2772 /* The symbolic header contains absolute file offsets and sizes to
2773 read. */
2774 #define READ(ptr, offset, count, size, type) \
2775 if (symhdr->count == 0) \
2776 debug->ptr = NULL; \
2777 else \
2778 { \
2779 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2780 if (debug->ptr == NULL) \
2781 goto error_return; \
2782 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
2783 || (bfd_read (debug->ptr, size, symhdr->count, \
2784 abfd) != size * symhdr->count)) \
2785 goto error_return; \
2786 }
2787
2788 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2789 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2790 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2791 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2792 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2793 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2794 union aux_ext *);
2795 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2796 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2797 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2798 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2799 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2800 #undef READ
2801
2802 debug->fdr = NULL;
2803 debug->adjust = NULL;
2804
2805 return true;
2806
2807 error_return:
2808 if (ext_hdr != NULL)
2809 free (ext_hdr);
2810 if (debug->line != NULL)
2811 free (debug->line);
2812 if (debug->external_dnr != NULL)
2813 free (debug->external_dnr);
2814 if (debug->external_pdr != NULL)
2815 free (debug->external_pdr);
2816 if (debug->external_sym != NULL)
2817 free (debug->external_sym);
2818 if (debug->external_opt != NULL)
2819 free (debug->external_opt);
2820 if (debug->external_aux != NULL)
2821 free (debug->external_aux);
2822 if (debug->ss != NULL)
2823 free (debug->ss);
2824 if (debug->ssext != NULL)
2825 free (debug->ssext);
2826 if (debug->external_fdr != NULL)
2827 free (debug->external_fdr);
2828 if (debug->external_rfd != NULL)
2829 free (debug->external_rfd);
2830 if (debug->external_ext != NULL)
2831 free (debug->external_ext);
2832 return false;
2833 }
2834 \f
2835 /* MIPS ELF local labels start with '$', not 'L'. */
2836
2837 /*ARGSUSED*/
2838 static boolean
2839 mips_elf_is_local_label (abfd, symbol)
2840 bfd *abfd;
2841 asymbol *symbol;
2842 {
2843 return symbol->name[0] == '$';
2844 }
2845
2846 /* MIPS ELF uses a special find_nearest_line routine in order the
2847 handle the ECOFF debugging information. */
2848
2849 struct mips_elf_find_line
2850 {
2851 struct ecoff_debug_info d;
2852 struct ecoff_find_line i;
2853 };
2854
2855 boolean
2856 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
2857 functionname_ptr, line_ptr)
2858 bfd *abfd;
2859 asection *section;
2860 asymbol **symbols;
2861 bfd_vma offset;
2862 const char **filename_ptr;
2863 const char **functionname_ptr;
2864 unsigned int *line_ptr;
2865 {
2866 asection *msec;
2867
2868 msec = bfd_get_section_by_name (abfd, ".mdebug");
2869 if (msec != NULL)
2870 {
2871 flagword origflags;
2872 struct mips_elf_find_line *fi;
2873 const struct ecoff_debug_swap * const swap =
2874 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2875
2876 /* If we are called during a link, mips_elf_final_link may have
2877 cleared the SEC_HAS_CONTENTS field. We force it back on here
2878 if appropriate (which it normally will be). */
2879 origflags = msec->flags;
2880 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
2881 msec->flags |= SEC_HAS_CONTENTS;
2882
2883 fi = elf_tdata (abfd)->find_line_info;
2884 if (fi == NULL)
2885 {
2886 bfd_size_type external_fdr_size;
2887 char *fraw_src;
2888 char *fraw_end;
2889 struct fdr *fdr_ptr;
2890
2891 fi = ((struct mips_elf_find_line *)
2892 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
2893 if (fi == NULL)
2894 {
2895 msec->flags = origflags;
2896 return false;
2897 }
2898
2899 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
2900 {
2901 msec->flags = origflags;
2902 return false;
2903 }
2904
2905 /* Swap in the FDR information. */
2906 fi->d.fdr = ((struct fdr *)
2907 bfd_alloc (abfd,
2908 (fi->d.symbolic_header.ifdMax *
2909 sizeof (struct fdr))));
2910 if (fi->d.fdr == NULL)
2911 {
2912 msec->flags = origflags;
2913 return false;
2914 }
2915 external_fdr_size = swap->external_fdr_size;
2916 fdr_ptr = fi->d.fdr;
2917 fraw_src = (char *) fi->d.external_fdr;
2918 fraw_end = (fraw_src
2919 + fi->d.symbolic_header.ifdMax * external_fdr_size);
2920 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
2921 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
2922
2923 elf_tdata (abfd)->find_line_info = fi;
2924
2925 /* Note that we don't bother to ever free this information.
2926 find_nearest_line is either called all the time, as in
2927 objdump -l, so the information should be saved, or it is
2928 rarely called, as in ld error messages, so the memory
2929 wasted is unimportant. Still, it would probably be a
2930 good idea for free_cached_info to throw it away. */
2931 }
2932
2933 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
2934 &fi->i, filename_ptr, functionname_ptr,
2935 line_ptr))
2936 {
2937 msec->flags = origflags;
2938 return true;
2939 }
2940
2941 msec->flags = origflags;
2942 }
2943
2944 /* Fall back on the generic ELF find_nearest_line routine. */
2945
2946 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
2947 filename_ptr, functionname_ptr,
2948 line_ptr);
2949 }
2950 \f
2951 /* The MIPS ELF linker needs additional information for each symbol in
2952 the global hash table. */
2953
2954 struct mips_elf_link_hash_entry
2955 {
2956 struct elf_link_hash_entry root;
2957
2958 /* External symbol information. */
2959 EXTR esym;
2960
2961 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
2962 unsigned int mips_32_relocs;
2963 };
2964
2965 /* MIPS ELF linker hash table. */
2966
2967 struct mips_elf_link_hash_table
2968 {
2969 struct elf_link_hash_table root;
2970 /* String section indices for the dynamic section symbols. */
2971 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
2972 /* The number of .rtproc entries. */
2973 bfd_size_type procedure_count;
2974 /* The size of the .compact_rel section (if SGI_COMPAT). */
2975 bfd_size_type compact_rel_size;
2976 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
2977 entry is set to the address of __rld_obj_head as in Irix 5. */
2978 boolean use_rld_obj_head;
2979 /* This is the value of the __rld_map or __rld_obj_head symbol. */
2980 bfd_vma rld_value;
2981 };
2982
2983 /* Look up an entry in a MIPS ELF linker hash table. */
2984
2985 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
2986 ((struct mips_elf_link_hash_entry *) \
2987 elf_link_hash_lookup (&(table)->root, (string), (create), \
2988 (copy), (follow)))
2989
2990 /* Traverse a MIPS ELF linker hash table. */
2991
2992 #define mips_elf_link_hash_traverse(table, func, info) \
2993 (elf_link_hash_traverse \
2994 (&(table)->root, \
2995 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
2996 (info)))
2997
2998 /* Get the MIPS ELF linker hash table from a link_info structure. */
2999
3000 #define mips_elf_hash_table(p) \
3001 ((struct mips_elf_link_hash_table *) ((p)->hash))
3002
3003 static boolean mips_elf_output_extsym
3004 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3005
3006 /* Create an entry in a MIPS ELF linker hash table. */
3007
3008 static struct bfd_hash_entry *
3009 mips_elf_link_hash_newfunc (entry, table, string)
3010 struct bfd_hash_entry *entry;
3011 struct bfd_hash_table *table;
3012 const char *string;
3013 {
3014 struct mips_elf_link_hash_entry *ret =
3015 (struct mips_elf_link_hash_entry *) entry;
3016
3017 /* Allocate the structure if it has not already been allocated by a
3018 subclass. */
3019 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3020 ret = ((struct mips_elf_link_hash_entry *)
3021 bfd_hash_allocate (table,
3022 sizeof (struct mips_elf_link_hash_entry)));
3023 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3024 return (struct bfd_hash_entry *) ret;
3025
3026 /* Call the allocation method of the superclass. */
3027 ret = ((struct mips_elf_link_hash_entry *)
3028 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3029 table, string));
3030 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3031 {
3032 /* Set local fields. */
3033 memset (&ret->esym, 0, sizeof (EXTR));
3034 /* We use -2 as a marker to indicate that the information has
3035 not been set. -1 means there is no associated ifd. */
3036 ret->esym.ifd = -2;
3037 ret->mips_32_relocs = 0;
3038 }
3039
3040 return (struct bfd_hash_entry *) ret;
3041 }
3042
3043 /* Create a MIPS ELF linker hash table. */
3044
3045 static struct bfd_link_hash_table *
3046 mips_elf_link_hash_table_create (abfd)
3047 bfd *abfd;
3048 {
3049 struct mips_elf_link_hash_table *ret;
3050 unsigned int i;
3051
3052 ret = ((struct mips_elf_link_hash_table *)
3053 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3054 if (ret == (struct mips_elf_link_hash_table *) NULL)
3055 return NULL;
3056
3057 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3058 mips_elf_link_hash_newfunc))
3059 {
3060 bfd_release (abfd, ret);
3061 return NULL;
3062 }
3063
3064 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3065 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3066 ret->procedure_count = 0;
3067 ret->compact_rel_size = 0;
3068 ret->use_rld_obj_head = false;
3069 ret->rld_value = 0;
3070
3071 return &ret->root.root;
3072 }
3073
3074 /* Hook called by the linker routine which adds symbols from an object
3075 file. We must handle the special MIPS section numbers here. */
3076
3077 /*ARGSUSED*/
3078 static boolean
3079 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3080 bfd *abfd;
3081 struct bfd_link_info *info;
3082 const Elf_Internal_Sym *sym;
3083 const char **namep;
3084 flagword *flagsp;
3085 asection **secp;
3086 bfd_vma *valp;
3087 {
3088 if (SGI_COMPAT (abfd)
3089 && (abfd->flags & DYNAMIC) != 0
3090 && strcmp (*namep, "_rld_new_interface") == 0)
3091 {
3092 /* Skip Irix 5 rld entry name. */
3093 *namep = NULL;
3094 return true;
3095 }
3096
3097 switch (sym->st_shndx)
3098 {
3099 case SHN_COMMON:
3100 /* Common symbols less than the GP size are automatically
3101 treated as SHN_MIPS_SCOMMON symbols. */
3102 if (sym->st_size > elf_gp_size (abfd))
3103 break;
3104 /* Fall through. */
3105 case SHN_MIPS_SCOMMON:
3106 *secp = bfd_make_section_old_way (abfd, ".scommon");
3107 (*secp)->flags |= SEC_IS_COMMON;
3108 *valp = sym->st_size;
3109 break;
3110
3111 case SHN_MIPS_TEXT:
3112 /* This section is used in a shared object. */
3113 if (mips_elf_text_section_ptr == NULL)
3114 {
3115 /* Initialize the section. */
3116 mips_elf_text_section.name = ".text";
3117 mips_elf_text_section.flags = SEC_NO_FLAGS;
3118 mips_elf_text_section.output_section = NULL;
3119 mips_elf_text_section.owner = abfd;
3120 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3121 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3122 mips_elf_text_symbol.name = ".text";
3123 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3124 mips_elf_text_symbol.section = &mips_elf_text_section;
3125 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3126 mips_elf_text_section_ptr = &mips_elf_text_section;
3127 }
3128 if (info->shared)
3129 *secp = bfd_und_section_ptr;
3130 else
3131 *secp = mips_elf_text_section_ptr;
3132 break;
3133
3134 case SHN_MIPS_ACOMMON:
3135 /* Fall through. XXX Can we treat this as allocated data? */
3136 case SHN_MIPS_DATA:
3137 /* This section is used in a shared object. */
3138 if (mips_elf_data_section_ptr == NULL)
3139 {
3140 /* Initialize the section. */
3141 mips_elf_data_section.name = ".data";
3142 mips_elf_data_section.flags = SEC_NO_FLAGS;
3143 mips_elf_data_section.output_section = NULL;
3144 mips_elf_data_section.owner = abfd;
3145 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3146 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3147 mips_elf_data_symbol.name = ".data";
3148 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3149 mips_elf_data_symbol.section = &mips_elf_data_section;
3150 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3151 mips_elf_data_section_ptr = &mips_elf_data_section;
3152 }
3153 if (info->shared)
3154 *secp = bfd_und_section_ptr;
3155 else
3156 *secp = mips_elf_data_section_ptr;
3157 break;
3158
3159 case SHN_MIPS_SUNDEFINED:
3160 *secp = bfd_und_section_ptr;
3161 break;
3162 }
3163
3164 if (SGI_COMPAT (abfd)
3165 && ! info->shared
3166 && info->hash->creator == abfd->xvec
3167 && strcmp (*namep, "__rld_obj_head") == 0)
3168 {
3169 struct elf_link_hash_entry *h;
3170
3171 /* Mark __rld_obj_head as dynamic. */
3172 h = NULL;
3173 if (! (_bfd_generic_link_add_one_symbol
3174 (info, abfd, *namep, BSF_GLOBAL, *secp,
3175 (bfd_vma) *valp, (const char *) NULL, false,
3176 get_elf_backend_data (abfd)->collect,
3177 (struct bfd_link_hash_entry **) &h)))
3178 return false;
3179 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3180 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3181 h->type = STT_OBJECT;
3182
3183 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3184 return false;
3185
3186 mips_elf_hash_table (info)->use_rld_obj_head = true;
3187 }
3188
3189 /* If this is a mips16 text symbol, add 1 to the value to make it
3190 odd. This will cause something like .word SYM to come up with
3191 the right value when it is loaded into the PC. */
3192 if (sym->st_other == STO_MIPS16)
3193 ++*valp;
3194
3195 return true;
3196 }
3197
3198 /* Structure used to pass information to mips_elf_output_extsym. */
3199
3200 struct extsym_info
3201 {
3202 bfd *abfd;
3203 struct bfd_link_info *info;
3204 struct ecoff_debug_info *debug;
3205 const struct ecoff_debug_swap *swap;
3206 boolean failed;
3207 };
3208
3209 /* This routine is used to write out ECOFF debugging external symbol
3210 information. It is called via mips_elf_link_hash_traverse. The
3211 ECOFF external symbol information must match the ELF external
3212 symbol information. Unfortunately, at this point we don't know
3213 whether a symbol is required by reloc information, so the two
3214 tables may wind up being different. We must sort out the external
3215 symbol information before we can set the final size of the .mdebug
3216 section, and we must set the size of the .mdebug section before we
3217 can relocate any sections, and we can't know which symbols are
3218 required by relocation until we relocate the sections.
3219 Fortunately, it is relatively unlikely that any symbol will be
3220 stripped but required by a reloc. In particular, it can not happen
3221 when generating a final executable. */
3222
3223 static boolean
3224 mips_elf_output_extsym (h, data)
3225 struct mips_elf_link_hash_entry *h;
3226 PTR data;
3227 {
3228 struct extsym_info *einfo = (struct extsym_info *) data;
3229 boolean strip;
3230 asection *sec, *output_section;
3231
3232 if (h->root.indx == -2)
3233 strip = false;
3234 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3235 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3236 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3237 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3238 strip = true;
3239 else if (einfo->info->strip == strip_all
3240 || (einfo->info->strip == strip_some
3241 && bfd_hash_lookup (einfo->info->keep_hash,
3242 h->root.root.root.string,
3243 false, false) == NULL))
3244 strip = true;
3245 else
3246 strip = false;
3247
3248 if (strip)
3249 return true;
3250
3251 if (h->esym.ifd == -2)
3252 {
3253 h->esym.jmptbl = 0;
3254 h->esym.cobol_main = 0;
3255 h->esym.weakext = 0;
3256 h->esym.reserved = 0;
3257 h->esym.ifd = ifdNil;
3258 h->esym.asym.value = 0;
3259 h->esym.asym.st = stGlobal;
3260
3261 if (SGI_COMPAT (einfo->abfd)
3262 && (h->root.root.type == bfd_link_hash_undefined
3263 || h->root.root.type == bfd_link_hash_undefweak))
3264 {
3265 const char *name;
3266
3267 /* Use undefined class. Also, set class and type for some
3268 special symbols. */
3269 name = h->root.root.root.string;
3270 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3271 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3272 {
3273 h->esym.asym.sc = scData;
3274 h->esym.asym.st = stLabel;
3275 h->esym.asym.value = 0;
3276 }
3277 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3278 {
3279 h->esym.asym.sc = scAbs;
3280 h->esym.asym.st = stLabel;
3281 h->esym.asym.value =
3282 mips_elf_hash_table (einfo->info)->procedure_count;
3283 }
3284 else if (strcmp (name, "_gp_disp") == 0)
3285 {
3286 h->esym.asym.sc = scAbs;
3287 h->esym.asym.st = stLabel;
3288 h->esym.asym.value = elf_gp (einfo->abfd);
3289 }
3290 else
3291 h->esym.asym.sc = scUndefined;
3292 }
3293 else if (h->root.root.type != bfd_link_hash_defined
3294 && h->root.root.type != bfd_link_hash_defweak)
3295 h->esym.asym.sc = scAbs;
3296 else
3297 {
3298 const char *name;
3299
3300 sec = h->root.root.u.def.section;
3301 output_section = sec->output_section;
3302
3303 /* When making a shared library and symbol h is the one from
3304 the another shared library, OUTPUT_SECTION may be null. */
3305 if (output_section == NULL)
3306 h->esym.asym.sc = scUndefined;
3307 else
3308 {
3309 name = bfd_section_name (output_section->owner, output_section);
3310
3311 if (strcmp (name, ".text") == 0)
3312 h->esym.asym.sc = scText;
3313 else if (strcmp (name, ".data") == 0)
3314 h->esym.asym.sc = scData;
3315 else if (strcmp (name, ".sdata") == 0)
3316 h->esym.asym.sc = scSData;
3317 else if (strcmp (name, ".rodata") == 0
3318 || strcmp (name, ".rdata") == 0)
3319 h->esym.asym.sc = scRData;
3320 else if (strcmp (name, ".bss") == 0)
3321 h->esym.asym.sc = scBss;
3322 else if (strcmp (name, ".sbss") == 0)
3323 h->esym.asym.sc = scSBss;
3324 else if (strcmp (name, ".init") == 0)
3325 h->esym.asym.sc = scInit;
3326 else if (strcmp (name, ".fini") == 0)
3327 h->esym.asym.sc = scFini;
3328 else
3329 h->esym.asym.sc = scAbs;
3330 }
3331 }
3332
3333 h->esym.asym.reserved = 0;
3334 h->esym.asym.index = indexNil;
3335 }
3336
3337 if (h->root.root.type == bfd_link_hash_common)
3338 h->esym.asym.value = h->root.root.u.c.size;
3339 else if (h->root.root.type == bfd_link_hash_defined
3340 || h->root.root.type == bfd_link_hash_defweak)
3341 {
3342 if (h->esym.asym.sc == scCommon)
3343 h->esym.asym.sc = scBss;
3344 else if (h->esym.asym.sc == scSCommon)
3345 h->esym.asym.sc = scSBss;
3346
3347 sec = h->root.root.u.def.section;
3348 output_section = sec->output_section;
3349 if (output_section != NULL)
3350 h->esym.asym.value = (h->root.root.u.def.value
3351 + sec->output_offset
3352 + output_section->vma);
3353 else
3354 h->esym.asym.value = 0;
3355 }
3356 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3357 {
3358 /* Set type and value for a symbol with a function stub. */
3359 h->esym.asym.st = stProc;
3360 sec = h->root.root.u.def.section;
3361 if (sec == NULL)
3362 h->esym.asym.value = 0;
3363 else
3364 {
3365 output_section = sec->output_section;
3366 if (output_section != NULL)
3367 h->esym.asym.value = (h->root.plt_offset
3368 + sec->output_offset
3369 + output_section->vma);
3370 else
3371 h->esym.asym.value = 0;
3372 }
3373 #if 0 /* FIXME? */
3374 h->esym.ifd = 0;
3375 #endif
3376 }
3377
3378 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3379 h->root.root.root.string,
3380 &h->esym))
3381 {
3382 einfo->failed = true;
3383 return false;
3384 }
3385
3386 return true;
3387 }
3388
3389 /* Create a runtime procedure table from the .mdebug section. */
3390
3391 static boolean
3392 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3393 PTR handle;
3394 bfd *abfd;
3395 struct bfd_link_info *info;
3396 asection *s;
3397 struct ecoff_debug_info *debug;
3398 {
3399 const struct ecoff_debug_swap *swap;
3400 HDRR *hdr = &debug->symbolic_header;
3401 RPDR *rpdr, *rp;
3402 struct rpdr_ext *erp;
3403 PTR rtproc;
3404 struct pdr_ext *epdr;
3405 struct sym_ext *esym;
3406 char *ss, **sv;
3407 char *str;
3408 unsigned long size, count;
3409 unsigned long sindex;
3410 unsigned long i;
3411 PDR pdr;
3412 SYMR sym;
3413 const char *no_name_func = "static procedure (no name)";
3414
3415 epdr = NULL;
3416 rpdr = NULL;
3417 esym = NULL;
3418 ss = NULL;
3419 sv = NULL;
3420
3421 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3422
3423 sindex = strlen (no_name_func) + 1;
3424 count = hdr->ipdMax;
3425 if (count > 0)
3426 {
3427 size = swap->external_pdr_size;
3428
3429 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3430 if (epdr == NULL)
3431 goto error_return;
3432
3433 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3434 goto error_return;
3435
3436 size = sizeof (RPDR);
3437 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3438 if (rpdr == NULL)
3439 goto error_return;
3440
3441 sv = (char **) bfd_malloc (sizeof (char *) * count);
3442 if (sv == NULL)
3443 goto error_return;
3444
3445 count = hdr->isymMax;
3446 size = swap->external_sym_size;
3447 esym = (struct sym_ext *) bfd_malloc (size * count);
3448 if (esym == NULL)
3449 goto error_return;
3450
3451 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3452 goto error_return;
3453
3454 count = hdr->issMax;
3455 ss = (char *) bfd_malloc (count);
3456 if (ss == NULL)
3457 goto error_return;
3458 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3459 goto error_return;
3460
3461 count = hdr->ipdMax;
3462 for (i = 0; i < count; i++, rp++)
3463 {
3464 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3465 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3466 rp->adr = sym.value;
3467 rp->regmask = pdr.regmask;
3468 rp->regoffset = pdr.regoffset;
3469 rp->fregmask = pdr.fregmask;
3470 rp->fregoffset = pdr.fregoffset;
3471 rp->frameoffset = pdr.frameoffset;
3472 rp->framereg = pdr.framereg;
3473 rp->pcreg = pdr.pcreg;
3474 rp->irpss = sindex;
3475 sv[i] = ss + sym.iss;
3476 sindex += strlen (sv[i]) + 1;
3477 }
3478 }
3479
3480 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3481 size = BFD_ALIGN (size, 16);
3482 rtproc = (PTR) bfd_alloc (abfd, size);
3483 if (rtproc == NULL)
3484 {
3485 mips_elf_hash_table (info)->procedure_count = 0;
3486 goto error_return;
3487 }
3488
3489 mips_elf_hash_table (info)->procedure_count = count + 2;
3490
3491 erp = (struct rpdr_ext *) rtproc;
3492 memset (erp, 0, sizeof (struct rpdr_ext));
3493 erp++;
3494 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3495 strcpy (str, no_name_func);
3496 str += strlen (no_name_func) + 1;
3497 for (i = 0; i < count; i++)
3498 {
3499 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3500 strcpy (str, sv[i]);
3501 str += strlen (sv[i]) + 1;
3502 }
3503 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3504
3505 /* Set the size and contents of .rtproc section. */
3506 s->_raw_size = size;
3507 s->contents = rtproc;
3508
3509 /* Skip this section later on (I don't think this currently
3510 matters, but someday it might). */
3511 s->link_order_head = (struct bfd_link_order *) NULL;
3512
3513 if (epdr != NULL)
3514 free (epdr);
3515 if (rpdr != NULL)
3516 free (rpdr);
3517 if (esym != NULL)
3518 free (esym);
3519 if (ss != NULL)
3520 free (ss);
3521 if (sv != NULL)
3522 free (sv);
3523
3524 return true;
3525
3526 error_return:
3527 if (epdr != NULL)
3528 free (epdr);
3529 if (rpdr != NULL)
3530 free (rpdr);
3531 if (esym != NULL)
3532 free (esym);
3533 if (ss != NULL)
3534 free (ss);
3535 if (sv != NULL)
3536 free (sv);
3537 return false;
3538 }
3539
3540 /* A comparison routine used to sort .gptab entries. */
3541
3542 static int
3543 gptab_compare (p1, p2)
3544 const PTR p1;
3545 const PTR p2;
3546 {
3547 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
3548 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
3549
3550 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3551 }
3552
3553 /* We need to use a special link routine to handle the .reginfo and
3554 the .mdebug sections. We need to merge all instances of these
3555 sections together, not write them all out sequentially. */
3556
3557 static boolean
3558 mips_elf_final_link (abfd, info)
3559 bfd *abfd;
3560 struct bfd_link_info *info;
3561 {
3562 asection **secpp;
3563 asection *o;
3564 struct bfd_link_order *p;
3565 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3566 asection *rtproc_sec;
3567 Elf32_RegInfo reginfo;
3568 struct ecoff_debug_info debug;
3569 const struct ecoff_debug_swap *swap
3570 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3571 HDRR *symhdr = &debug.symbolic_header;
3572 PTR mdebug_handle = NULL;
3573
3574 /* Drop the .options section, since it has special semantics which I
3575 haven't bothered to figure out. */
3576 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
3577 {
3578 if (strcmp ((*secpp)->name, ".options") == 0)
3579 {
3580 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
3581 if (p->type == bfd_indirect_link_order)
3582 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
3583 (*secpp)->link_order_head = NULL;
3584 *secpp = (*secpp)->next;
3585 --abfd->section_count;
3586 break;
3587 }
3588 }
3589
3590 /* Get a value for the GP register. */
3591 if (elf_gp (abfd) == 0)
3592 {
3593 struct bfd_link_hash_entry *h;
3594
3595 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
3596 if (h != (struct bfd_link_hash_entry *) NULL
3597 && h->type == bfd_link_hash_defined)
3598 elf_gp (abfd) = (h->u.def.value
3599 + h->u.def.section->output_section->vma
3600 + h->u.def.section->output_offset);
3601 else if (info->relocateable)
3602 {
3603 bfd_vma lo;
3604
3605 /* Make up a value. */
3606 lo = (bfd_vma) -1;
3607 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3608 {
3609 if (o->vma < lo
3610 && (strcmp (o->name, ".sbss") == 0
3611 || strcmp (o->name, ".sdata") == 0
3612 || strcmp (o->name, ".lit4") == 0
3613 || strcmp (o->name, ".lit8") == 0))
3614 lo = o->vma;
3615 }
3616 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
3617 }
3618 else
3619 {
3620 /* If the relocate_section function needs to do a reloc
3621 involving the GP value, it should make a reloc_dangerous
3622 callback to warn that GP is not defined. */
3623 }
3624 }
3625
3626 /* Go through the sections and collect the .reginfo and .mdebug
3627 information. */
3628 reginfo_sec = NULL;
3629 mdebug_sec = NULL;
3630 gptab_data_sec = NULL;
3631 gptab_bss_sec = NULL;
3632 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3633 {
3634 if (strcmp (o->name, ".reginfo") == 0)
3635 {
3636 memset (&reginfo, 0, sizeof reginfo);
3637
3638 /* We have found the .reginfo section in the output file.
3639 Look through all the link_orders comprising it and merge
3640 the information together. */
3641 for (p = o->link_order_head;
3642 p != (struct bfd_link_order *) NULL;
3643 p = p->next)
3644 {
3645 asection *input_section;
3646 bfd *input_bfd;
3647 Elf32_External_RegInfo ext;
3648 Elf32_RegInfo sub;
3649
3650 if (p->type != bfd_indirect_link_order)
3651 {
3652 if (p->type == bfd_fill_link_order)
3653 continue;
3654 abort ();
3655 }
3656
3657 input_section = p->u.indirect.section;
3658 input_bfd = input_section->owner;
3659
3660 /* The linker emulation code has probably clobbered the
3661 size to be zero bytes. */
3662 if (input_section->_raw_size == 0)
3663 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
3664
3665 if (! bfd_get_section_contents (input_bfd, input_section,
3666 (PTR) &ext,
3667 (file_ptr) 0,
3668 sizeof ext))
3669 return false;
3670
3671 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
3672
3673 reginfo.ri_gprmask |= sub.ri_gprmask;
3674 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3675 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3676 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3677 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3678
3679 /* ri_gp_value is set by the function
3680 mips_elf32_section_processing when the section is
3681 finally written out. */
3682
3683 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3684 elf_link_input_bfd ignores this section. */
3685 input_section->flags &=~ SEC_HAS_CONTENTS;
3686 }
3687
3688 /* Force the section size to the value we want. */
3689 o->_raw_size = sizeof (Elf32_External_RegInfo);
3690
3691 /* Skip this section later on (I don't think this currently
3692 matters, but someday it might). */
3693 o->link_order_head = (struct bfd_link_order *) NULL;
3694
3695 reginfo_sec = o;
3696 }
3697
3698 if (strcmp (o->name, ".mdebug") == 0)
3699 {
3700 struct extsym_info einfo;
3701
3702 /* We have found the .mdebug section in the output file.
3703 Look through all the link_orders comprising it and merge
3704 the information together. */
3705 symhdr->magic = swap->sym_magic;
3706 /* FIXME: What should the version stamp be? */
3707 symhdr->vstamp = 0;
3708 symhdr->ilineMax = 0;
3709 symhdr->cbLine = 0;
3710 symhdr->idnMax = 0;
3711 symhdr->ipdMax = 0;
3712 symhdr->isymMax = 0;
3713 symhdr->ioptMax = 0;
3714 symhdr->iauxMax = 0;
3715 symhdr->issMax = 0;
3716 symhdr->issExtMax = 0;
3717 symhdr->ifdMax = 0;
3718 symhdr->crfd = 0;
3719 symhdr->iextMax = 0;
3720
3721 /* We accumulate the debugging information itself in the
3722 debug_info structure. */
3723 debug.line = NULL;
3724 debug.external_dnr = NULL;
3725 debug.external_pdr = NULL;
3726 debug.external_sym = NULL;
3727 debug.external_opt = NULL;
3728 debug.external_aux = NULL;
3729 debug.ss = NULL;
3730 debug.ssext = debug.ssext_end = NULL;
3731 debug.external_fdr = NULL;
3732 debug.external_rfd = NULL;
3733 debug.external_ext = debug.external_ext_end = NULL;
3734
3735 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3736 if (mdebug_handle == (PTR) NULL)
3737 return false;
3738
3739 if (SGI_COMPAT (abfd))
3740 {
3741 asection *s;
3742 EXTR esym;
3743 bfd_vma last;
3744 unsigned int i;
3745 static const char * const name[] =
3746 { ".text", ".init", ".fini", ".data",
3747 ".rodata", ".sdata", ".sbss", ".bss" };
3748 static const int sc[] = { scText, scInit, scFini, scData,
3749 scRData, scSData, scSBss, scBss };
3750
3751 esym.jmptbl = 0;
3752 esym.cobol_main = 0;
3753 esym.weakext = 0;
3754 esym.reserved = 0;
3755 esym.ifd = ifdNil;
3756 esym.asym.iss = issNil;
3757 esym.asym.st = stLocal;
3758 esym.asym.reserved = 0;
3759 esym.asym.index = indexNil;
3760 for (i = 0; i < 8; i++)
3761 {
3762 esym.asym.sc = sc[i];
3763 s = bfd_get_section_by_name (abfd, name[i]);
3764 if (s != NULL)
3765 {
3766 esym.asym.value = s->vma;
3767 last = s->vma + s->_raw_size;
3768 }
3769 else
3770 esym.asym.value = last;
3771
3772 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3773 name[i], &esym))
3774 return false;
3775 }
3776 }
3777
3778 for (p = o->link_order_head;
3779 p != (struct bfd_link_order *) NULL;
3780 p = p->next)
3781 {
3782 asection *input_section;
3783 bfd *input_bfd;
3784 const struct ecoff_debug_swap *input_swap;
3785 struct ecoff_debug_info input_debug;
3786 char *eraw_src;
3787 char *eraw_end;
3788
3789 if (p->type != bfd_indirect_link_order)
3790 {
3791 if (p->type == bfd_fill_link_order)
3792 continue;
3793 abort ();
3794 }
3795
3796 input_section = p->u.indirect.section;
3797 input_bfd = input_section->owner;
3798
3799 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
3800 || (get_elf_backend_data (input_bfd)
3801 ->elf_backend_ecoff_debug_swap) == NULL)
3802 {
3803 /* I don't know what a non MIPS ELF bfd would be
3804 doing with a .mdebug section, but I don't really
3805 want to deal with it. */
3806 continue;
3807 }
3808
3809 input_swap = (get_elf_backend_data (input_bfd)
3810 ->elf_backend_ecoff_debug_swap);
3811
3812 BFD_ASSERT (p->size == input_section->_raw_size);
3813
3814 /* The ECOFF linking code expects that we have already
3815 read in the debugging information and set up an
3816 ecoff_debug_info structure, so we do that now. */
3817 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
3818 &input_debug))
3819 return false;
3820
3821 if (! (bfd_ecoff_debug_accumulate
3822 (mdebug_handle, abfd, &debug, swap, input_bfd,
3823 &input_debug, input_swap, info)))
3824 return false;
3825
3826 /* Loop through the external symbols. For each one with
3827 interesting information, try to find the symbol in
3828 the linker global hash table and save the information
3829 for the output external symbols. */
3830 eraw_src = input_debug.external_ext;
3831 eraw_end = (eraw_src
3832 + (input_debug.symbolic_header.iextMax
3833 * input_swap->external_ext_size));
3834 for (;
3835 eraw_src < eraw_end;
3836 eraw_src += input_swap->external_ext_size)
3837 {
3838 EXTR ext;
3839 const char *name;
3840 struct mips_elf_link_hash_entry *h;
3841
3842 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
3843 if (ext.asym.sc == scNil
3844 || ext.asym.sc == scUndefined
3845 || ext.asym.sc == scSUndefined)
3846 continue;
3847
3848 name = input_debug.ssext + ext.asym.iss;
3849 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
3850 name, false, false, true);
3851 if (h == NULL || h->esym.ifd != -2)
3852 continue;
3853
3854 if (ext.ifd != -1)
3855 {
3856 BFD_ASSERT (ext.ifd
3857 < input_debug.symbolic_header.ifdMax);
3858 ext.ifd = input_debug.ifdmap[ext.ifd];
3859 }
3860
3861 h->esym = ext;
3862 }
3863
3864 /* Free up the information we just read. */
3865 free (input_debug.line);
3866 free (input_debug.external_dnr);
3867 free (input_debug.external_pdr);
3868 free (input_debug.external_sym);
3869 free (input_debug.external_opt);
3870 free (input_debug.external_aux);
3871 free (input_debug.ss);
3872 free (input_debug.ssext);
3873 free (input_debug.external_fdr);
3874 free (input_debug.external_rfd);
3875 free (input_debug.external_ext);
3876
3877 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3878 elf_link_input_bfd ignores this section. */
3879 input_section->flags &=~ SEC_HAS_CONTENTS;
3880 }
3881
3882 if (SGI_COMPAT (abfd) && info->shared)
3883 {
3884 /* Create .rtproc section. */
3885 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
3886 if (rtproc_sec == NULL)
3887 {
3888 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
3889 | SEC_LINKER_CREATED | SEC_READONLY);
3890
3891 rtproc_sec = bfd_make_section (abfd, ".rtproc");
3892 if (rtproc_sec == NULL
3893 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
3894 || ! bfd_set_section_alignment (abfd, rtproc_sec, 12))
3895 return false;
3896 }
3897
3898 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
3899 info, rtproc_sec, &debug))
3900 return false;
3901 }
3902
3903 /* Build the external symbol information. */
3904 einfo.abfd = abfd;
3905 einfo.info = info;
3906 einfo.debug = &debug;
3907 einfo.swap = swap;
3908 einfo.failed = false;
3909 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
3910 mips_elf_output_extsym,
3911 (PTR) &einfo);
3912 if (einfo.failed)
3913 return false;
3914
3915 /* Set the size of the .mdebug section. */
3916 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
3917
3918 /* Skip this section later on (I don't think this currently
3919 matters, but someday it might). */
3920 o->link_order_head = (struct bfd_link_order *) NULL;
3921
3922 mdebug_sec = o;
3923 }
3924
3925 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
3926 {
3927 const char *subname;
3928 unsigned int c;
3929 Elf32_gptab *tab;
3930 Elf32_External_gptab *ext_tab;
3931 unsigned int i;
3932
3933 /* The .gptab.sdata and .gptab.sbss sections hold
3934 information describing how the small data area would
3935 change depending upon the -G switch. These sections
3936 not used in executables files. */
3937 if (! info->relocateable)
3938 {
3939 asection **secpp;
3940
3941 for (p = o->link_order_head;
3942 p != (struct bfd_link_order *) NULL;
3943 p = p->next)
3944 {
3945 asection *input_section;
3946
3947 if (p->type != bfd_indirect_link_order)
3948 {
3949 if (p->type == bfd_fill_link_order)
3950 continue;
3951 abort ();
3952 }
3953
3954 input_section = p->u.indirect.section;
3955
3956 /* Hack: reset the SEC_HAS_CONTENTS flag so that
3957 elf_link_input_bfd ignores this section. */
3958 input_section->flags &=~ SEC_HAS_CONTENTS;
3959 }
3960
3961 /* Skip this section later on (I don't think this
3962 currently matters, but someday it might). */
3963 o->link_order_head = (struct bfd_link_order *) NULL;
3964
3965 /* Really remove the section. */
3966 for (secpp = &abfd->sections;
3967 *secpp != o;
3968 secpp = &(*secpp)->next)
3969 ;
3970 *secpp = (*secpp)->next;
3971 --abfd->section_count;
3972
3973 continue;
3974 }
3975
3976 /* There is one gptab for initialized data, and one for
3977 uninitialized data. */
3978 if (strcmp (o->name, ".gptab.sdata") == 0)
3979 gptab_data_sec = o;
3980 else if (strcmp (o->name, ".gptab.sbss") == 0)
3981 gptab_bss_sec = o;
3982 else
3983 {
3984 (*_bfd_error_handler)
3985 ("%s: illegal section name `%s'",
3986 bfd_get_filename (abfd), o->name);
3987 bfd_set_error (bfd_error_nonrepresentable_section);
3988 return false;
3989 }
3990
3991 /* The linker script always combines .gptab.data and
3992 .gptab.sdata into .gptab.sdata, and likewise for
3993 .gptab.bss and .gptab.sbss. It is possible that there is
3994 no .sdata or .sbss section in the output file, in which
3995 case we must change the name of the output section. */
3996 subname = o->name + sizeof ".gptab" - 1;
3997 if (bfd_get_section_by_name (abfd, subname) == NULL)
3998 {
3999 if (o == gptab_data_sec)
4000 o->name = ".gptab.data";
4001 else
4002 o->name = ".gptab.bss";
4003 subname = o->name + sizeof ".gptab" - 1;
4004 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4005 }
4006
4007 /* Set up the first entry. */
4008 c = 1;
4009 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4010 if (tab == NULL)
4011 return false;
4012 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4013 tab[0].gt_header.gt_unused = 0;
4014
4015 /* Combine the input sections. */
4016 for (p = o->link_order_head;
4017 p != (struct bfd_link_order *) NULL;
4018 p = p->next)
4019 {
4020 asection *input_section;
4021 bfd *input_bfd;
4022 bfd_size_type size;
4023 unsigned long last;
4024 bfd_size_type gpentry;
4025
4026 if (p->type != bfd_indirect_link_order)
4027 {
4028 if (p->type == bfd_fill_link_order)
4029 continue;
4030 abort ();
4031 }
4032
4033 input_section = p->u.indirect.section;
4034 input_bfd = input_section->owner;
4035
4036 /* Combine the gptab entries for this input section one
4037 by one. We know that the input gptab entries are
4038 sorted by ascending -G value. */
4039 size = bfd_section_size (input_bfd, input_section);
4040 last = 0;
4041 for (gpentry = sizeof (Elf32_External_gptab);
4042 gpentry < size;
4043 gpentry += sizeof (Elf32_External_gptab))
4044 {
4045 Elf32_External_gptab ext_gptab;
4046 Elf32_gptab int_gptab;
4047 unsigned long val;
4048 unsigned long add;
4049 boolean exact;
4050 unsigned int look;
4051
4052 if (! (bfd_get_section_contents
4053 (input_bfd, input_section, (PTR) &ext_gptab,
4054 gpentry, sizeof (Elf32_External_gptab))))
4055 {
4056 free (tab);
4057 return false;
4058 }
4059
4060 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4061 &int_gptab);
4062 val = int_gptab.gt_entry.gt_g_value;
4063 add = int_gptab.gt_entry.gt_bytes - last;
4064
4065 exact = false;
4066 for (look = 1; look < c; look++)
4067 {
4068 if (tab[look].gt_entry.gt_g_value >= val)
4069 tab[look].gt_entry.gt_bytes += add;
4070
4071 if (tab[look].gt_entry.gt_g_value == val)
4072 exact = true;
4073 }
4074
4075 if (! exact)
4076 {
4077 Elf32_gptab *new_tab;
4078 unsigned int max;
4079
4080 /* We need a new table entry. */
4081 new_tab = ((Elf32_gptab *)
4082 bfd_realloc ((PTR) tab,
4083 (c + 1) * sizeof (Elf32_gptab)));
4084 if (new_tab == NULL)
4085 {
4086 free (tab);
4087 return false;
4088 }
4089 tab = new_tab;
4090 tab[c].gt_entry.gt_g_value = val;
4091 tab[c].gt_entry.gt_bytes = add;
4092
4093 /* Merge in the size for the next smallest -G
4094 value, since that will be implied by this new
4095 value. */
4096 max = 0;
4097 for (look = 1; look < c; look++)
4098 {
4099 if (tab[look].gt_entry.gt_g_value < val
4100 && (max == 0
4101 || (tab[look].gt_entry.gt_g_value
4102 > tab[max].gt_entry.gt_g_value)))
4103 max = look;
4104 }
4105 if (max != 0)
4106 tab[c].gt_entry.gt_bytes +=
4107 tab[max].gt_entry.gt_bytes;
4108
4109 ++c;
4110 }
4111
4112 last = int_gptab.gt_entry.gt_bytes;
4113 }
4114
4115 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4116 elf_link_input_bfd ignores this section. */
4117 input_section->flags &=~ SEC_HAS_CONTENTS;
4118 }
4119
4120 /* The table must be sorted by -G value. */
4121 if (c > 2)
4122 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4123
4124 /* Swap out the table. */
4125 ext_tab = ((Elf32_External_gptab *)
4126 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4127 if (ext_tab == NULL)
4128 {
4129 free (tab);
4130 return false;
4131 }
4132
4133 for (i = 0; i < c; i++)
4134 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4135 free (tab);
4136
4137 o->_raw_size = c * sizeof (Elf32_External_gptab);
4138 o->contents = (bfd_byte *) ext_tab;
4139
4140 /* Skip this section later on (I don't think this currently
4141 matters, but someday it might). */
4142 o->link_order_head = (struct bfd_link_order *) NULL;
4143 }
4144 }
4145
4146 /* Invoke the regular ELF backend linker to do all the work. */
4147 if (! bfd_elf32_bfd_final_link (abfd, info))
4148 return false;
4149
4150 /* Now write out the computed sections. */
4151
4152 if (reginfo_sec != (asection *) NULL)
4153 {
4154 Elf32_External_RegInfo ext;
4155
4156 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4157 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4158 (file_ptr) 0, sizeof ext))
4159 return false;
4160 }
4161
4162 if (mdebug_sec != (asection *) NULL)
4163 {
4164 BFD_ASSERT (abfd->output_has_begun);
4165 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4166 swap, info,
4167 mdebug_sec->filepos))
4168 return false;
4169
4170 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4171 }
4172
4173 if (gptab_data_sec != (asection *) NULL)
4174 {
4175 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4176 gptab_data_sec->contents,
4177 (file_ptr) 0,
4178 gptab_data_sec->_raw_size))
4179 return false;
4180 }
4181
4182 if (gptab_bss_sec != (asection *) NULL)
4183 {
4184 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4185 gptab_bss_sec->contents,
4186 (file_ptr) 0,
4187 gptab_bss_sec->_raw_size))
4188 return false;
4189 }
4190
4191 if (SGI_COMPAT (abfd))
4192 {
4193 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4194 if (rtproc_sec != NULL)
4195 {
4196 if (! bfd_set_section_contents (abfd, rtproc_sec,
4197 rtproc_sec->contents,
4198 (file_ptr) 0,
4199 rtproc_sec->_raw_size))
4200 return false;
4201 }
4202 }
4203
4204 return true;
4205 }
4206
4207 /* Handle a MIPS ELF HI16 reloc. */
4208
4209 static void
4210 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4211 bfd *input_bfd;
4212 Elf_Internal_Rela *relhi;
4213 Elf_Internal_Rela *rello;
4214 bfd_byte *contents;
4215 bfd_vma addend;
4216 {
4217 bfd_vma insn;
4218 bfd_vma addlo;
4219
4220 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4221
4222 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4223 addlo &= 0xffff;
4224
4225 addend += ((insn & 0xffff) << 16) + addlo;
4226
4227 if ((addlo & 0x8000) != 0)
4228 addend -= 0x10000;
4229 if ((addend & 0x8000) != 0)
4230 addend += 0x10000;
4231
4232 bfd_put_32 (input_bfd,
4233 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4234 contents + relhi->r_offset);
4235 }
4236
4237 /* Handle a MIPS ELF local GOT16 reloc. */
4238
4239 static void
4240 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4241 contents, addend)
4242 bfd *output_bfd;
4243 bfd *input_bfd;
4244 asection *sgot;
4245 Elf_Internal_Rela *relhi;
4246 Elf_Internal_Rela *rello;
4247 bfd_byte *contents;
4248 bfd_vma addend;
4249 {
4250 int local_gotno;
4251 int i;
4252 bfd_vma insn;
4253 bfd_vma addlo;
4254 bfd_vma address;
4255 bfd_vma hipage;
4256 bfd_byte *got_contents;
4257 struct mips_got_info *g;
4258
4259 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4260
4261 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4262 addlo &= 0xffff;
4263
4264 addend += ((insn & 0xffff) << 16) + addlo;
4265
4266 if ((addlo & 0x8000) != 0)
4267 addend -= 0x10000;
4268 if ((addend & 0x8000) != 0)
4269 addend += 0x10000;
4270
4271 /* Get a got entry representing requested hipage. */
4272 BFD_ASSERT (elf_section_data (sgot) != NULL);
4273 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4274 BFD_ASSERT (g != NULL);
4275
4276 local_gotno = g->local_gotno;
4277 got_contents = sgot->contents;
4278 hipage = addend & 0xffff0000;
4279
4280 for (i = MIPS_RESERVED_GOTNO; i < local_gotno; i++)
4281 {
4282 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4283 if (hipage == (address & 0xffff0000))
4284 break;
4285 if (address == (bfd_vma) 0)
4286 {
4287 bfd_put_32 (input_bfd, hipage, got_contents + i * 4);
4288 break;
4289 }
4290 }
4291
4292 BFD_ASSERT (i < local_gotno);
4293 #if 1
4294 if (i == local_gotno)
4295 (*_bfd_error_handler)
4296 ("ELF MIPS linker: more got entries are needed for hipage: %x",
4297 hipage);
4298 #endif
4299
4300 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4301 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4302 contents + relhi->r_offset);
4303 }
4304
4305 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4306
4307 static void
4308 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4309 bfd *input_bfd;
4310 Elf_Internal_Rela *rel;
4311 bfd_byte *contents;
4312 bfd_vma offset;
4313 {
4314 bfd_vma insn;
4315
4316 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4317 bfd_put_32 (input_bfd,
4318 (insn & 0xffff0000) | (offset & 0xffff),
4319 contents + rel->r_offset);
4320 }
4321
4322 /* Relocate a MIPS ELF section. */
4323
4324 static boolean
4325 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4326 contents, relocs, local_syms, local_sections)
4327 bfd *output_bfd;
4328 struct bfd_link_info *info;
4329 bfd *input_bfd;
4330 asection *input_section;
4331 bfd_byte *contents;
4332 Elf_Internal_Rela *relocs;
4333 Elf_Internal_Sym *local_syms;
4334 asection **local_sections;
4335 {
4336 Elf_Internal_Shdr *symtab_hdr;
4337 size_t locsymcount;
4338 size_t extsymoff;
4339 asection *sgot, *sreloc, *scpt;
4340 bfd *dynobj;
4341 bfd_vma gp;
4342 Elf_Internal_Rela *rel;
4343 Elf_Internal_Rela *relend;
4344 struct mips_got_info *g;
4345
4346 dynobj = elf_hash_table (info)->dynobj;
4347 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4348
4349 sgot = NULL;
4350 sreloc = NULL;
4351 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4352 scpt = NULL;
4353 else
4354 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4355 g = NULL;
4356
4357 if (elf_bad_symtab (input_bfd))
4358 {
4359 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4360 extsymoff = 0;
4361 }
4362 else
4363 {
4364 locsymcount = symtab_hdr->sh_info;
4365 extsymoff = symtab_hdr->sh_info;
4366 }
4367
4368 gp = _bfd_get_gp_value (output_bfd);
4369
4370 rel = relocs;
4371 relend = relocs + input_section->reloc_count;
4372 for (; rel < relend; rel++)
4373 {
4374 int r_type;
4375 reloc_howto_type *howto;
4376 unsigned long r_symndx;
4377 bfd_vma addend;
4378 struct elf_link_hash_entry *h;
4379 asection *sec;
4380 Elf_Internal_Sym *sym;
4381 bfd_reloc_status_type r;
4382
4383 r_type = ELF32_R_TYPE (rel->r_info);
4384 if ((r_type < 0 || r_type >= (int) R_MIPS_max) && r_type != R_MIPS16_26)
4385 {
4386 bfd_set_error (bfd_error_bad_value);
4387 return false;
4388 }
4389 if (r_type != R_MIPS16_26)
4390 howto = elf_mips_howto_table + r_type;
4391 else
4392 howto = &elf_mips16_jump_howto;
4393
4394 if (dynobj != NULL
4395 && (r_type == R_MIPS_CALL16
4396 || r_type == R_MIPS_GOT16
4397 || r_type == R_MIPS_CALL_HI16
4398 || r_type == R_MIPS_CALL_LO16
4399 || r_type == R_MIPS_GOT_HI16
4400 || r_type == R_MIPS_GOT_LO16))
4401 {
4402 /* We need the .got section. */
4403 if (sgot == NULL)
4404 {
4405 sgot = bfd_get_section_by_name (dynobj, ".got");
4406 BFD_ASSERT (sgot != NULL);
4407 BFD_ASSERT (elf_section_data (sgot) != NULL);
4408 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4409 BFD_ASSERT (g != NULL);
4410 }
4411 }
4412
4413 r_symndx = ELF32_R_SYM (rel->r_info);
4414
4415 /* Mix in the change in GP address for a GP relative reloc. */
4416 if (r_type != R_MIPS_GPREL16
4417 && r_type != R_MIPS_LITERAL
4418 && r_type != R_MIPS_GPREL32)
4419 addend = 0;
4420 else
4421 {
4422 if (gp == 0)
4423 {
4424 if (! ((*info->callbacks->reloc_dangerous)
4425 (info,
4426 "GP relative relocation when GP not defined",
4427 input_bfd, input_section,
4428 rel->r_offset)))
4429 return false;
4430 /* Only give the error once per link. */
4431 gp = 4;
4432 _bfd_set_gp_value (output_bfd, gp);
4433 }
4434
4435 if (r_symndx < extsymoff
4436 || (elf_bad_symtab (input_bfd)
4437 && local_sections[r_symndx] != NULL))
4438 {
4439 /* This is a relocation against a section. The current
4440 addend in the instruction is the difference between
4441 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4442 must change this to be the difference between the
4443 final definition (which will end up in RELOCATION)
4444 and the GP value of OUTPUT_BFD (which is in GP). */
4445 addend = elf_gp (input_bfd) - gp;
4446 }
4447 else if (! info->relocateable)
4448 {
4449 /* We are doing a final link. The current addend in the
4450 instruction is simply the desired offset into the
4451 symbol (normally zero). We want the instruction to
4452 hold the difference between the final definition of
4453 the symbol (which will end up in RELOCATION) and the
4454 GP value of OUTPUT_BFD (which is in GP). */
4455 addend = - gp;
4456 }
4457 else
4458 {
4459 /* We are generating relocateable output, and we aren't
4460 going to define this symbol, so we just leave the
4461 instruction alone. */
4462 addend = 0;
4463 }
4464 }
4465
4466 h = NULL;
4467 sym = NULL;
4468 sec = NULL;
4469 if (info->relocateable)
4470 {
4471 /* This is a relocateable link. We don't have to change
4472 anything, unless the reloc is against a section symbol,
4473 in which case we have to adjust according to where the
4474 section symbol winds up in the output section. */
4475 if (r_symndx >= locsymcount
4476 || (elf_bad_symtab (input_bfd)
4477 && local_sections[r_symndx] == NULL))
4478 r = bfd_reloc_ok;
4479 else
4480 {
4481 sym = local_syms + r_symndx;
4482 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4483 r = bfd_reloc_ok;
4484 else
4485 {
4486 sec = local_sections[r_symndx];
4487
4488 /* It would be logical to add sym->st_value here,
4489 but Irix 5 sometimes generates a garbage symbol
4490 value. */
4491 addend += sec->output_offset;
4492
4493 /* If this is HI16 or GOT16 with an associated LO16,
4494 adjust the addend accordingly. Otherwise, just
4495 relocate. */
4496 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4497 r = _bfd_relocate_contents (howto, input_bfd,
4498 addend,
4499 contents + rel->r_offset + 4);
4500 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4501 r = _bfd_relocate_contents (howto, input_bfd,
4502 addend,
4503 contents + rel->r_offset);
4504 else
4505 {
4506 Elf_Internal_Rela *lorel;
4507
4508 /* As a GNU extension, permit an arbitrary
4509 number of R_MIPS_HI16 relocs before the
4510 R_MIPS_LO16 reloc. This permits gcc to emit
4511 the HI and LO relocs itself. */
4512 if (r_type == R_MIPS_GOT16)
4513 lorel = rel + 1;
4514 else
4515 {
4516 for (lorel = rel + 1;
4517 (lorel < relend
4518 && (ELF32_R_TYPE (lorel->r_info)
4519 == R_MIPS_HI16));
4520 lorel++)
4521 ;
4522 }
4523 if (lorel < relend
4524 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4525 {
4526 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4527 contents, addend);
4528 r = bfd_reloc_ok;
4529 }
4530 else
4531 r = _bfd_relocate_contents (howto, input_bfd,
4532 addend,
4533 contents + rel->r_offset);
4534 }
4535 }
4536 }
4537 }
4538 else
4539 {
4540 bfd_vma relocation;
4541 boolean local;
4542
4543 /* This is a final link. */
4544 sym = NULL;
4545 if (r_symndx < extsymoff
4546 || (elf_bad_symtab (input_bfd)
4547 && local_sections[r_symndx] != NULL))
4548 {
4549 local = true;
4550 sym = local_syms + r_symndx;
4551 sec = local_sections[r_symndx];
4552 relocation = (sec->output_section->vma
4553 + sec->output_offset);
4554
4555 /* It would be logical to always add sym->st_value here,
4556 but Irix 5 sometimes generates a garbage symbol
4557 value. */
4558 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4559 relocation += sym->st_value;
4560
4561 /* mips16 text labels should be treated as odd. */
4562 if (sym->st_other == STO_MIPS16)
4563 ++relocation;
4564 }
4565 else
4566 {
4567 long indx;
4568
4569 local = false;
4570 indx = r_symndx - extsymoff;
4571 h = elf_sym_hashes (input_bfd)[indx];
4572 while (h->root.type == bfd_link_hash_indirect
4573 || h->root.type == bfd_link_hash_warning)
4574 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4575 if (strcmp (h->root.root.string, "_gp_disp") == 0)
4576 {
4577 if (gp == 0)
4578 {
4579 if (! ((*info->callbacks->reloc_dangerous)
4580 (info,
4581 "_gp_disp used when GP not defined",
4582 input_bfd, input_section,
4583 rel->r_offset)))
4584 return false;
4585 /* Only give the error once per link. */
4586 gp = 4;
4587 _bfd_set_gp_value (output_bfd, gp);
4588 relocation = 0;
4589 }
4590 else
4591 {
4592 sec = input_section;
4593 if (sec->output_section != NULL)
4594 relocation = (gp
4595 - (rel->r_offset
4596 + sec->output_section->vma
4597 + sec->output_offset));
4598 else
4599 relocation = gp - rel->r_offset;
4600 if (r_type == R_MIPS_LO16)
4601 relocation += 4;
4602 }
4603 }
4604 else if (h->root.type == bfd_link_hash_defined
4605 || h->root.type == bfd_link_hash_defweak)
4606 {
4607 sec = h->root.u.def.section;
4608 if (sec->output_section == NULL)
4609 relocation = 0;
4610 else
4611 relocation = (h->root.u.def.value
4612 + sec->output_section->vma
4613 + sec->output_offset);
4614 }
4615 else if (h->root.type == bfd_link_hash_undefweak)
4616 relocation = 0;
4617 else if (info->shared && ! info->symbolic)
4618 relocation = 0;
4619 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
4620 {
4621 /* If this is a dynamic link, we should have created
4622 a _DYNAMIC_LINK symbol in
4623 mips_elf_create_dynamic_sections. Otherwise, we
4624 should define the symbol with a value of 0.
4625 FIXME: It should probably get into the symbol
4626 table somehow as well. */
4627 BFD_ASSERT (! info->shared);
4628 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
4629 ".dynamic") == NULL);
4630 relocation = 0;
4631 }
4632 else
4633 {
4634 if (! ((*info->callbacks->undefined_symbol)
4635 (info, h->root.root.string, input_bfd,
4636 input_section, rel->r_offset)))
4637 return false;
4638 relocation = 0;
4639 }
4640 }
4641
4642 if (r_type == R_MIPS_HI16)
4643 {
4644 Elf_Internal_Rela *lorel;
4645
4646 /* As a GNU extension, permit an arbitrary number of
4647 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
4648 This permits gcc to emit the HI and LO relocs itself. */
4649 for (lorel = rel + 1;
4650 (lorel < relend
4651 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
4652 lorel++)
4653 ;
4654 if (lorel < relend
4655 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4656 {
4657 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4658 contents, relocation + addend);
4659 r = bfd_reloc_ok;
4660 }
4661 else
4662 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4663 contents, rel->r_offset,
4664 relocation, addend);
4665 }
4666 else if (r_type == R_MIPS_GOT16 && local)
4667 {
4668 /* GOT16 must also have an associated LO16 in the local
4669 case. In this case, the addend is extracted and the
4670 section in which the referenced object is determined.
4671 Then the final address of the object is computed and
4672 the GOT entry for the hipage (an aligned 64kb chunk)
4673 is added to .got section if needed. The offset field
4674 of the GOT16-relocated instruction is replaced by the
4675 index of this GOT entry for the hipage. */
4676 if ((rel + 1) < relend
4677 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
4678 {
4679 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot,
4680 rel, rel + 1,
4681 contents,
4682 relocation + addend);
4683 r = bfd_reloc_ok;
4684 }
4685 else
4686 r = bfd_reloc_outofrange;
4687 }
4688 else if (r_type == R_MIPS_CALL16
4689 || r_type == R_MIPS_GOT16
4690 || r_type == R_MIPS_CALL_LO16
4691 || r_type == R_MIPS_GOT_LO16)
4692 {
4693 bfd_vma offset;
4694
4695 /* This symbol must be registered as a global symbol
4696 having the corresponding got entry. */
4697 BFD_ASSERT (h->got_offset != (bfd_vma) -1);
4698
4699 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
4700 BFD_ASSERT (g->local_gotno <= offset
4701 && offset < sgot->_raw_size);
4702 bfd_put_32 (output_bfd, relocation + addend,
4703 sgot->contents + offset);
4704 offset = (sgot->output_section->vma + sgot->output_offset
4705 + offset - gp);
4706 mips_elf_relocate_global_got (input_bfd, rel, contents,
4707 offset);
4708 r = bfd_reloc_ok;
4709 }
4710 else if (r_type == R_MIPS_CALL_HI16
4711 || r_type == R_MIPS_GOT_HI16)
4712 {
4713 bfd_vma offset;
4714
4715 /* This must be a global symbol with a got entry. The
4716 next reloc must be the corresponding LO16 reloc. */
4717 BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
4718 BFD_ASSERT ((rel + 1) < relend);
4719 BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
4720 == (r_type == R_MIPS_CALL_HI16
4721 ? R_MIPS_CALL_LO16
4722 : R_MIPS_GOT_LO16));
4723
4724 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
4725 BFD_ASSERT (g->local_gotno <= offset
4726 && offset < sgot->_raw_size);
4727 bfd_put_32 (output_bfd, relocation + addend,
4728 sgot->contents + offset);
4729 offset = (sgot->output_section->vma + sgot->output_offset
4730 + offset - gp);
4731 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
4732 offset);
4733 r = bfd_reloc_ok;
4734 }
4735 else if (r_type == R_MIPS_REL32
4736 || r_type == R_MIPS_32)
4737 {
4738 Elf_Internal_Rel outrel;
4739 Elf32_crinfo cptrel;
4740 bfd_byte *cr;
4741
4742 if ((info->shared
4743 || (elf_hash_table (info)->dynamic_sections_created
4744 && h != NULL
4745 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
4746 == 0)))
4747 && (input_section->flags & SEC_ALLOC) != 0)
4748 {
4749 /* When generating a shared object, these
4750 relocations are copied into the output file to be
4751 resolved at run time. */
4752 if (sreloc == NULL)
4753 {
4754 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
4755 BFD_ASSERT (sreloc != NULL);
4756 }
4757
4758 outrel.r_offset = (rel->r_offset
4759 + input_section->output_section->vma
4760 + input_section->output_offset);
4761
4762 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4763
4764 if (h != NULL
4765 && (! info->symbolic
4766 || (h->elf_link_hash_flags
4767 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4768 {
4769 BFD_ASSERT (h->dynindx != -1);
4770 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
4771 sec = input_section;
4772 }
4773 else
4774 {
4775 long indx;
4776
4777 if (h == NULL)
4778 sec = local_sections[r_symndx];
4779 else
4780 {
4781 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4782 || (h->root.type
4783 == bfd_link_hash_defweak));
4784 sec = h->root.u.def.section;
4785 }
4786 if (sec != NULL && bfd_is_abs_section (sec))
4787 indx = 0;
4788 else if (sec == NULL || sec->owner == NULL)
4789 {
4790 bfd_set_error (bfd_error_bad_value);
4791 return false;
4792 }
4793 else
4794 {
4795 asection *osec;
4796
4797 osec = sec->output_section;
4798 indx = elf_section_data (osec)->dynindx;
4799 if (indx == 0)
4800 abort ();
4801 }
4802
4803 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
4804 addend += relocation;
4805 }
4806
4807 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
4808 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
4809 (((Elf32_External_Rel *)
4810 sreloc->contents)
4811 + sreloc->reloc_count));
4812 ++sreloc->reloc_count;
4813
4814 if (SGI_COMPAT (output_bfd))
4815 {
4816 if (scpt == NULL)
4817 continue;
4818
4819 /* Make an entry of compact relocation info. */
4820 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4821 cptrel.vaddr = (rel->r_offset
4822 + input_section->output_section->vma
4823 + input_section->output_offset);
4824 if (r_type == R_MIPS_REL32)
4825 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
4826 else
4827 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
4828 mips_elf_set_cr_dist2to (cptrel, 0);
4829 cptrel.konst = addend;
4830
4831 cr = (scpt->contents
4832 + sizeof (Elf32_External_compact_rel));
4833 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4834 ((Elf32_External_crinfo *) cr
4835 + scpt->reloc_count));
4836 ++scpt->reloc_count;
4837 }
4838
4839 /* This reloc will be computed at runtime, so
4840 there's no need to do anything now. */
4841 continue;
4842 }
4843 else
4844 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4845 contents, rel->r_offset,
4846 relocation, addend);
4847 }
4848 else if (r_type == R_MIPS_64)
4849 {
4850 bfd_size_type addr;
4851 unsigned long val;
4852
4853 /* Do a 32 bit relocation, and sign extend to 64 bits. */
4854 addr = rel->r_offset;
4855 if (bfd_big_endian (input_bfd))
4856 addr += 4;
4857 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4858 contents, addr, relocation,
4859 addend);
4860 val = bfd_get_32 (input_bfd, contents + addr);
4861 if ((val & 0x80000000) != 0)
4862 val = 0xffffffff;
4863 else
4864 val = 0;
4865 addr = rel->r_offset;
4866 if (bfd_little_endian (input_bfd))
4867 addr += 4;
4868 bfd_put_32 (input_bfd, val, contents + addr);
4869 }
4870 else if (r_type == R_MIPS_26
4871 && ((h != NULL && h->other == STO_MIPS16)
4872 || (sym != NULL && sym->st_other == STO_MIPS16)))
4873 {
4874 unsigned long insn;
4875
4876 /* This is a jump to a mips16 routine from a mips32
4877 routine. We need to change jal into jalx. */
4878 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4879 if (((insn >> 26) & 0x3f) != 0x3
4880 && ((insn >> 26) & 0x3f) != 0x1d)
4881 {
4882 (*_bfd_error_handler)
4883 ("%s: %s+0x%lx: jump to mips16 routine which is not jal",
4884 bfd_get_filename (input_bfd),
4885 input_section->name,
4886 (unsigned long) rel->r_offset);
4887 bfd_set_error (bfd_error_bad_value);
4888 return false;
4889 }
4890 insn = (insn & 0x3ffffff) | (0x1d << 26);
4891 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
4892 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4893 contents, rel->r_offset,
4894 relocation, addend);
4895 }
4896 else if (r_type == R_MIPS16_26)
4897 {
4898 /* It's easiest to do the normal relocation, and then
4899 dig out the instruction and swap the first word the
4900 way the mips16 expects it. */
4901 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4902 contents, rel->r_offset,
4903 relocation, addend);
4904 if (r == bfd_reloc_ok)
4905 {
4906 unsigned long insn;
4907
4908 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
4909 insn = ((insn & 0xfc00)
4910 | ((insn & 0x1f) << 5)
4911 | ((insn & 0x3e0) >> 5));
4912 /* If this is a jump to a 32 bit routine, then make
4913 it jalx. */
4914 if ((h != NULL && h->other != STO_MIPS16)
4915 || (sym != NULL && sym->st_other != STO_MIPS16))
4916 insn |= 0x400;
4917 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
4918 }
4919 }
4920 else
4921 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4922 contents, rel->r_offset,
4923 relocation, addend);
4924
4925 if (SGI_COMPAT (abfd)
4926 && scpt != NULL
4927 && (input_section->flags & SEC_ALLOC) != 0)
4928 {
4929 Elf32_crinfo cptrel;
4930 bfd_byte *cr;
4931
4932 /* Make an entry of compact relocation info. */
4933 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
4934 cptrel.vaddr = (rel->r_offset
4935 + input_section->output_section->vma
4936 + input_section->output_offset);
4937
4938 switch (r_type)
4939 {
4940 case R_MIPS_26:
4941 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
4942 /* XXX How should we set dist2to in this case. */
4943 mips_elf_set_cr_dist2to (cptrel, 8);
4944 cptrel.konst = addend + relocation;
4945 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
4946 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4947 ((Elf32_External_crinfo *) cr
4948 + scpt->reloc_count));
4949 ++scpt->reloc_count;
4950 break;
4951
4952 case R_MIPS_GPREL16:
4953 case R_MIPS_LITERAL:
4954 case R_MIPS_GPREL32:
4955 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
4956 cptrel.konst = gp - cptrel.vaddr;
4957 mips_elf_set_cr_dist2to (cptrel, 4);
4958 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
4959 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
4960 ((Elf32_External_crinfo *) cr
4961 + scpt->reloc_count));
4962 ++scpt->reloc_count;
4963 break;
4964
4965 default:
4966 break;
4967 }
4968 }
4969 }
4970
4971 if (r != bfd_reloc_ok)
4972 {
4973 switch (r)
4974 {
4975 default:
4976 case bfd_reloc_outofrange:
4977 abort ();
4978 case bfd_reloc_overflow:
4979 {
4980 const char *name;
4981
4982 if (h != NULL)
4983 name = h->root.root.string;
4984 else
4985 {
4986 name = bfd_elf_string_from_elf_section (input_bfd,
4987 symtab_hdr->sh_link,
4988 sym->st_name);
4989 if (name == NULL)
4990 return false;
4991 if (*name == '\0')
4992 name = bfd_section_name (input_bfd, sec);
4993 }
4994 if (! ((*info->callbacks->reloc_overflow)
4995 (info, name, howto->name, (bfd_vma) 0,
4996 input_bfd, input_section, rel->r_offset)))
4997 return false;
4998 }
4999 break;
5000 }
5001 }
5002 }
5003
5004 return true;
5005 }
5006
5007 /* This hook function is called before the linker writes out a global
5008 symbol. This is where we undo the increment of the value for a
5009 mips16 symbol. */
5010
5011 /*ARGSIGNORED*/
5012 static boolean
5013 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5014 bfd *abfd;
5015 struct bfd_link_info *info;
5016 const char *name;
5017 Elf_Internal_Sym *sym;
5018 asection *input_sec;
5019 {
5020 if (sym->st_other == STO_MIPS16
5021 && (sym->st_value & 1) != 0)
5022 --sym->st_value;
5023 return true;
5024 }
5025 \f
5026 /* Functions for the dynamic linker. */
5027
5028 /* The name of the dynamic interpreter. This is put in the .interp
5029 section. */
5030
5031 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5032
5033 /* Create dynamic sections when linking against a dynamic object. */
5034
5035 static boolean
5036 mips_elf_create_dynamic_sections (abfd, info)
5037 bfd *abfd;
5038 struct bfd_link_info *info;
5039 {
5040 struct elf_link_hash_entry *h;
5041 flagword flags;
5042 register asection *s;
5043 const char * const *namep;
5044
5045 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5046 | SEC_LINKER_CREATED | SEC_READONLY);
5047
5048 /* Mips ABI requests the .dynamic section to be read only. */
5049 s = bfd_get_section_by_name (abfd, ".dynamic");
5050 if (s != NULL)
5051 {
5052 if (! bfd_set_section_flags (abfd, s, flags))
5053 return false;
5054 }
5055
5056 /* We need to create .got section. */
5057 if (! mips_elf_create_got_section (abfd, info))
5058 return false;
5059
5060 /* Create .stub section. */
5061 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5062 {
5063 s = bfd_make_section (abfd, ".stub");
5064 if (s == NULL
5065 || ! bfd_set_section_flags (abfd, s, flags)
5066 || ! bfd_set_section_alignment (abfd, s, 2))
5067 return false;
5068 }
5069
5070 if (SGI_COMPAT (abfd)
5071 && !info->shared
5072 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5073 {
5074 s = bfd_make_section (abfd, ".rld_map");
5075 if (s == NULL
5076 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5077 || ! bfd_set_section_alignment (abfd, s, 2))
5078 return false;
5079 }
5080
5081 if (SGI_COMPAT (abfd))
5082 {
5083 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5084 {
5085 h = NULL;
5086 if (! (_bfd_generic_link_add_one_symbol
5087 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5088 (bfd_vma) 0, (const char *) NULL, false,
5089 get_elf_backend_data (abfd)->collect,
5090 (struct bfd_link_hash_entry **) &h)))
5091 return false;
5092 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5093 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5094 h->type = STT_SECTION;
5095
5096 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5097 return false;
5098 }
5099
5100 /* We need to create a .compact_rel section. */
5101 if (! mips_elf_create_compact_rel_section (abfd, info))
5102 return false;
5103
5104 /* Change aligments of some sections. */
5105 s = bfd_get_section_by_name (abfd, ".hash");
5106 if (s != NULL)
5107 bfd_set_section_alignment (abfd, s, 4);
5108 s = bfd_get_section_by_name (abfd, ".dynsym");
5109 if (s != NULL)
5110 bfd_set_section_alignment (abfd, s, 4);
5111 s = bfd_get_section_by_name (abfd, ".dynstr");
5112 if (s != NULL)
5113 bfd_set_section_alignment (abfd, s, 4);
5114 s = bfd_get_section_by_name (abfd, ".reginfo");
5115 if (s != NULL)
5116 bfd_set_section_alignment (abfd, s, 4);
5117 s = bfd_get_section_by_name (abfd, ".dynamic");
5118 if (s != NULL)
5119 bfd_set_section_alignment (abfd, s, 4);
5120 }
5121
5122 if (!info->shared)
5123 {
5124 h = NULL;
5125 if (! (_bfd_generic_link_add_one_symbol
5126 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5127 (bfd_vma) 0, (const char *) NULL, false,
5128 get_elf_backend_data (abfd)->collect,
5129 (struct bfd_link_hash_entry **) &h)))
5130 return false;
5131 h->elf_link_hash_flags ^=~ ELF_LINK_NON_ELF;
5132 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5133 h->type = STT_SECTION;
5134
5135 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5136 return false;
5137
5138 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5139 {
5140 /* __rld_map is a four byte word located in the .data section
5141 and is filled in by the rtld to contain a pointer to
5142 the _r_debug structure. Its symbol value will be set in
5143 mips_elf_finish_dynamic_symbol. */
5144 s = bfd_get_section_by_name (abfd, ".rld_map");
5145 BFD_ASSERT (s != NULL);
5146
5147 h = NULL;
5148 if (! (_bfd_generic_link_add_one_symbol
5149 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5150 (bfd_vma) 0, (const char *) NULL, false,
5151 get_elf_backend_data (abfd)->collect,
5152 (struct bfd_link_hash_entry **) &h)))
5153 return false;
5154 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5155 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5156 h->type = STT_OBJECT;
5157
5158 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5159 return false;
5160 }
5161 }
5162
5163 return true;
5164 }
5165
5166 /* Create the .compact_rel section. */
5167
5168 static boolean
5169 mips_elf_create_compact_rel_section (abfd, info)
5170 bfd *abfd;
5171 struct bfd_link_info *info;
5172 {
5173 flagword flags;
5174 register asection *s;
5175
5176 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5177 {
5178 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5179 | SEC_READONLY);
5180
5181 s = bfd_make_section (abfd, ".compact_rel");
5182 if (s == NULL
5183 || ! bfd_set_section_flags (abfd, s, flags)
5184 || ! bfd_set_section_alignment (abfd, s, 2))
5185 return false;
5186
5187 s->_raw_size = sizeof (Elf32_External_compact_rel);
5188 }
5189
5190 return true;
5191 }
5192
5193 /* Create the .got section to hold the global offset table. */
5194
5195 static boolean
5196 mips_elf_create_got_section (abfd, info)
5197 bfd *abfd;
5198 struct bfd_link_info *info;
5199 {
5200 flagword flags;
5201 register asection *s;
5202 struct elf_link_hash_entry *h;
5203 struct mips_got_info *g;
5204
5205 /* This function may be called more than once. */
5206 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5207 return true;
5208
5209 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5210 | SEC_LINKER_CREATED);
5211
5212 s = bfd_make_section (abfd, ".got");
5213 if (s == NULL
5214 || ! bfd_set_section_flags (abfd, s, flags)
5215 || ! bfd_set_section_alignment (abfd, s, 4))
5216 return false;
5217
5218 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5219 linker script because we don't want to define the symbol if we
5220 are not creating a global offset table. */
5221 h = NULL;
5222 if (! (_bfd_generic_link_add_one_symbol
5223 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5224 (bfd_vma) 0, (const char *) NULL, false,
5225 get_elf_backend_data (abfd)->collect,
5226 (struct bfd_link_hash_entry **) &h)))
5227 return false;
5228 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5229 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5230 h->type = STT_OBJECT;
5231
5232 if (info->shared
5233 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5234 return false;
5235
5236 /* The first several global offset table entries are reserved. */
5237 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5238
5239 g = (struct mips_got_info *) bfd_alloc (abfd,
5240 sizeof (struct mips_got_info));
5241 if (g == NULL)
5242 return false;
5243 g->global_gotsym = 0;
5244 g->local_gotno = MIPS_RESERVED_GOTNO;
5245 if (elf_section_data (s) == NULL)
5246 {
5247 s->used_by_bfd =
5248 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5249 if (elf_section_data (s) == NULL)
5250 return false;
5251 }
5252 elf_section_data (s)->tdata = (PTR) g;
5253
5254 return true;
5255 }
5256
5257 /* Look through the relocs for a section during the first phase, and
5258 allocate space in the global offset table. */
5259
5260 static boolean
5261 mips_elf_check_relocs (abfd, info, sec, relocs)
5262 bfd *abfd;
5263 struct bfd_link_info *info;
5264 asection *sec;
5265 const Elf_Internal_Rela *relocs;
5266 {
5267 bfd *dynobj;
5268 Elf_Internal_Shdr *symtab_hdr;
5269 struct elf_link_hash_entry **sym_hashes;
5270 struct mips_got_info *g;
5271 size_t extsymoff;
5272 const Elf_Internal_Rela *rel;
5273 const Elf_Internal_Rela *rel_end;
5274 asection *sgot;
5275 asection *sreloc;
5276
5277 if (info->relocateable)
5278 return true;
5279
5280 dynobj = elf_hash_table (info)->dynobj;
5281 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5282 sym_hashes = elf_sym_hashes (abfd);
5283 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5284
5285 if (dynobj == NULL)
5286 {
5287 sgot = NULL;
5288 g = NULL;
5289 }
5290 else
5291 {
5292 sgot = bfd_get_section_by_name (dynobj, ".got");
5293 if (sgot == NULL)
5294 g = NULL;
5295 else
5296 {
5297 BFD_ASSERT (elf_section_data (sgot) != NULL);
5298 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5299 BFD_ASSERT (g != NULL);
5300 }
5301 }
5302
5303 sreloc = NULL;
5304
5305 rel_end = relocs + sec->reloc_count;
5306 for (rel = relocs; rel < rel_end; rel++)
5307 {
5308 unsigned long r_symndx;
5309 struct elf_link_hash_entry *h;
5310
5311 r_symndx = ELF32_R_SYM (rel->r_info);
5312
5313 if (r_symndx < extsymoff)
5314 h = NULL;
5315 else
5316 h = sym_hashes[r_symndx - extsymoff];
5317
5318 /* Some relocs require a global offset table. */
5319 if (dynobj == NULL || sgot == NULL)
5320 {
5321 switch (ELF32_R_TYPE (rel->r_info))
5322 {
5323 case R_MIPS_GOT16:
5324 case R_MIPS_CALL16:
5325 case R_MIPS_CALL_HI16:
5326 case R_MIPS_CALL_LO16:
5327 case R_MIPS_GOT_HI16:
5328 case R_MIPS_GOT_LO16:
5329 if (dynobj == NULL)
5330 elf_hash_table (info)->dynobj = dynobj = abfd;
5331 if (! mips_elf_create_got_section (dynobj, info))
5332 return false;
5333 sgot = bfd_get_section_by_name (dynobj, ".got");
5334 BFD_ASSERT (sgot != NULL);
5335 BFD_ASSERT (elf_section_data (sgot) != NULL);
5336 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5337 BFD_ASSERT (g != NULL);
5338 break;
5339
5340 case R_MIPS_32:
5341 case R_MIPS_REL32:
5342 if (dynobj == NULL
5343 && (info->shared || h != NULL)
5344 && (sec->flags & SEC_ALLOC) != 0)
5345 elf_hash_table (info)->dynobj = dynobj = abfd;
5346 break;
5347
5348 default:
5349 break;
5350 }
5351 }
5352
5353 switch (ELF32_R_TYPE (rel->r_info))
5354 {
5355 case R_MIPS_CALL16:
5356 case R_MIPS_CALL_HI16:
5357 case R_MIPS_CALL_LO16:
5358 /* This symbol requires a global offset table entry. */
5359
5360 BFD_ASSERT (h != NULL);
5361
5362 /* Make sure this symbol is output as a dynamic symbol. */
5363 if (h->dynindx == -1)
5364 {
5365 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5366 return false;
5367 }
5368
5369 if (h->got_offset != (bfd_vma) -1)
5370 {
5371 /* We have already allocated space in the .got. */
5372 break;
5373 }
5374
5375 /* Note the index of the first global got symbol in .dynsym. */
5376 if (g->global_gotsym == 0
5377 || g->global_gotsym > (unsigned long) h->dynindx)
5378 g->global_gotsym = h->dynindx;
5379
5380 /* Make this symbol to have the corresponding got entry. */
5381 h->got_offset = 0;
5382
5383 /* We need a stub, not a plt entry for the undefined
5384 function. But we record it as if it needs plt. See
5385 elf_adjust_dynamic_symbol in elflink.h. */
5386 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
5387 h->type = STT_FUNC;
5388
5389 break;
5390
5391 case R_MIPS_GOT16:
5392 case R_MIPS_GOT_HI16:
5393 case R_MIPS_GOT_LO16:
5394 /* This symbol requires a global offset table entry. */
5395
5396 if (h != NULL)
5397 {
5398 /* Make sure this symbol is output as a dynamic symbol. */
5399 if (h->dynindx == -1)
5400 {
5401 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5402 return false;
5403 }
5404
5405 if (h->got_offset != (bfd_vma) -1)
5406 {
5407 /* We have already allocated space in the .got. */
5408 break;
5409 }
5410 /* Note the index of the first global got symbol in
5411 .dynsym. */
5412 if (g->global_gotsym == 0
5413 || g->global_gotsym > (unsigned long) h->dynindx)
5414 g->global_gotsym = h->dynindx;
5415
5416 /* Make this symbol to be the global got symbol. */
5417 h->got_offset = 0;
5418 }
5419
5420 break;
5421
5422 case R_MIPS_32:
5423 case R_MIPS_REL32:
5424 if ((info->shared || h != NULL)
5425 && (sec->flags & SEC_ALLOC) != 0)
5426 {
5427 if (sreloc == NULL)
5428 {
5429 const char *name = ".rel.dyn";
5430
5431 sreloc = bfd_get_section_by_name (dynobj, name);
5432 if (sreloc == NULL)
5433 {
5434 sreloc = bfd_make_section (dynobj, name);
5435 if (sreloc == NULL
5436 || ! bfd_set_section_flags (dynobj, sreloc,
5437 (SEC_ALLOC
5438 | SEC_LOAD
5439 | SEC_HAS_CONTENTS
5440 | SEC_IN_MEMORY
5441 | SEC_LINKER_CREATED
5442 | SEC_READONLY))
5443 || ! bfd_set_section_alignment (dynobj, sreloc,
5444 4))
5445 return false;
5446 }
5447 }
5448 if (info->shared)
5449 {
5450 /* When creating a shared object, we must copy these
5451 reloc types into the output file as R_MIPS_REL32
5452 relocs. We make room for this reloc in the
5453 .rel.dyn reloc section */
5454 if (sreloc->_raw_size == 0)
5455 {
5456 /* Add a null element. */
5457 sreloc->_raw_size += sizeof (Elf32_External_Rel);
5458 ++sreloc->reloc_count;
5459 }
5460 sreloc->_raw_size += sizeof (Elf32_External_Rel);
5461 }
5462 else
5463 {
5464 struct mips_elf_link_hash_entry *hmips;
5465
5466 /* We only need to copy this reloc if the symbol is
5467 defined in a dynamic object. */
5468 hmips = (struct mips_elf_link_hash_entry *) h;
5469 ++hmips->mips_32_relocs;
5470 }
5471 }
5472
5473 if (SGI_COMPAT (abfd))
5474 mips_elf_hash_table (info)->compact_rel_size +=
5475 sizeof (Elf32_External_crinfo);
5476
5477 break;
5478
5479 case R_MIPS_26:
5480 case R_MIPS_GPREL16:
5481 case R_MIPS_LITERAL:
5482 case R_MIPS_GPREL32:
5483 if (SGI_COMPAT (abfd))
5484 mips_elf_hash_table (info)->compact_rel_size +=
5485 sizeof (Elf32_External_crinfo);
5486 break;
5487
5488 default:
5489 break;
5490 }
5491 }
5492
5493 return true;
5494 }
5495
5496 /* Adjust a symbol defined by a dynamic object and referenced by a
5497 regular object. The current definition is in some section of the
5498 dynamic object, but we're not including those sections. We have to
5499 change the definition to something the rest of the link can
5500 understand. */
5501
5502 static boolean
5503 mips_elf_adjust_dynamic_symbol (info, h)
5504 struct bfd_link_info *info;
5505 struct elf_link_hash_entry *h;
5506 {
5507 bfd *dynobj;
5508 struct mips_elf_link_hash_entry *hmips;
5509 asection *s;
5510
5511 dynobj = elf_hash_table (info)->dynobj;
5512
5513 /* Make sure we know what is going on here. */
5514 BFD_ASSERT (dynobj != NULL
5515 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
5516 || h->weakdef != NULL
5517 || ((h->elf_link_hash_flags
5518 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5519 && (h->elf_link_hash_flags
5520 & ELF_LINK_HASH_REF_REGULAR) != 0
5521 && (h->elf_link_hash_flags
5522 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
5523
5524 /* If this symbol is defined in a dynamic object, we need to copy
5525 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
5526 file. */
5527 hmips = (struct mips_elf_link_hash_entry *) h;
5528 if (! info->relocateable
5529 && hmips->mips_32_relocs != 0
5530 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5531 {
5532 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5533 BFD_ASSERT (s != NULL);
5534
5535 if (s->_raw_size == 0)
5536 {
5537 /* Make room for a null element. */
5538 s->_raw_size += sizeof (Elf32_External_Rel);
5539 ++s->reloc_count;
5540 }
5541 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
5542 }
5543
5544 /* For a function, create a stub, if needed. */
5545 if (h->type == STT_FUNC
5546 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
5547 {
5548 if (! elf_hash_table (info)->dynamic_sections_created)
5549 return true;
5550
5551 /* If this symbol is not defined in a regular file, then set
5552 the symbol to the stub location. This is required to make
5553 function pointers compare as equal between the normal
5554 executable and the shared library. */
5555 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5556 {
5557 /* We need .stub section. */
5558 s = bfd_get_section_by_name (dynobj, ".stub");
5559 BFD_ASSERT (s != NULL);
5560
5561 h->root.u.def.section = s;
5562 h->root.u.def.value = s->_raw_size;
5563
5564 /* XXX Write this stub address somewhere. */
5565 h->plt_offset = s->_raw_size;
5566
5567 /* Make room for this stub code. */
5568 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
5569
5570 /* The last half word of the stub will be filled with the index
5571 of this symbol in .dynsym section. */
5572 return true;
5573 }
5574 }
5575
5576 /* If this is a weak symbol, and there is a real definition, the
5577 processor independent code will have arranged for us to see the
5578 real definition first, and we can just use the same value. */
5579 if (h->weakdef != NULL)
5580 {
5581 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
5582 || h->weakdef->root.type == bfd_link_hash_defweak);
5583 h->root.u.def.section = h->weakdef->root.u.def.section;
5584 h->root.u.def.value = h->weakdef->root.u.def.value;
5585 return true;
5586 }
5587
5588 /* This is a reference to a symbol defined by a dynamic object which
5589 is not a function. */
5590
5591 return true;
5592 }
5593
5594 /* Set the sizes of the dynamic sections. */
5595
5596 static boolean
5597 mips_elf_size_dynamic_sections (output_bfd, info)
5598 bfd *output_bfd;
5599 struct bfd_link_info *info;
5600 {
5601 bfd *dynobj;
5602 asection *s;
5603 boolean reltext;
5604 asection *sgot;
5605 struct mips_got_info *g;
5606
5607 dynobj = elf_hash_table (info)->dynobj;
5608 BFD_ASSERT (dynobj != NULL);
5609
5610 if (elf_hash_table (info)->dynamic_sections_created)
5611 {
5612 /* Set the contents of the .interp section to the interpreter. */
5613 if (! info->shared)
5614 {
5615 s = bfd_get_section_by_name (dynobj, ".interp");
5616 BFD_ASSERT (s != NULL);
5617 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5618 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5619 }
5620 }
5621
5622 /* Recompute the size of .got for local entires (reserved and
5623 hipages) if needed. To estimate it, get the upper bound of total
5624 size of loadable sections. */
5625 sgot = bfd_get_section_by_name (dynobj, ".got");
5626
5627 if (sgot != NULL)
5628 {
5629 bfd_size_type loadable_size = 0;
5630 bfd_size_type local_gotno;
5631 struct _bfd *sub;
5632
5633 BFD_ASSERT (elf_section_data (sgot) != NULL);
5634 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5635 BFD_ASSERT (g != NULL);
5636
5637 for (sub = info->input_bfds; sub; sub = sub->link_next)
5638 for (s = sub->sections; s != NULL; s = s->next)
5639 {
5640 if ((s->flags & SEC_ALLOC) == 0)
5641 continue;
5642 loadable_size += (s->_raw_size + 0xf) & ~0xf;
5643 }
5644
5645 loadable_size += MIPS_FUNCTION_STUB_SIZE;
5646
5647 /* Assume there are two loadable segments consisting of
5648 contiguous sections. Is 5 enough? */
5649 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
5650 g->local_gotno = local_gotno;
5651 sgot->_raw_size += local_gotno * 4;
5652 }
5653
5654 /* The check_relocs and adjust_dynamic_symbol entry points have
5655 determined the sizes of the various dynamic sections. Allocate
5656 memory for them. */
5657 reltext = false;
5658 for (s = dynobj->sections; s != NULL; s = s->next)
5659 {
5660 const char *name;
5661 boolean strip;
5662
5663 /* It's OK to base decisions on the section name, because none
5664 of the dynobj section names depend upon the input files. */
5665 name = bfd_get_section_name (dynobj, s);
5666
5667 if ((s->flags & SEC_LINKER_CREATED) == 0)
5668 continue;
5669
5670 strip = false;
5671
5672 if (strncmp (name, ".rel", 4) == 0)
5673 {
5674 if (s->_raw_size == 0)
5675 strip = true;
5676 else
5677 {
5678 const char *outname;
5679 asection *target;
5680
5681 /* If this relocation section applies to a read only
5682 section, then we probably need a DT_TEXTREL entry.
5683 If the relocation section is .rel.dyn, we always
5684 assert a DT_TEXTREL entry rather than testing whether
5685 there exists a relocation to a read only section or
5686 not. */
5687 outname = bfd_get_section_name (output_bfd,
5688 s->output_section);
5689 target = bfd_get_section_by_name (output_bfd, outname + 4);
5690 if ((target != NULL && (target->flags & SEC_READONLY) != 0)
5691 || strcmp (outname, ".rel.dyn") == 0)
5692 reltext = true;
5693
5694 /* We use the reloc_count field as a counter if we need
5695 to copy relocs into the output file. */
5696 if (strcmp (name, ".rel.dyn") != 0)
5697 s->reloc_count = 0;
5698 }
5699 }
5700 else if (strncmp (name, ".got", 4) == 0)
5701 {
5702 int i;
5703
5704 BFD_ASSERT (elf_section_data (s) != NULL);
5705 g = (struct mips_got_info *) elf_section_data (s)->tdata;
5706 BFD_ASSERT (g != NULL);
5707
5708 /* Fix the size of .got section for the correspondence of
5709 global symbols and got entries. This adds some useless
5710 got entries. Is this required by ABI really? */
5711 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
5712 s->_raw_size += i * 4;
5713 }
5714 else if (strncmp (name, ".stub", 5) == 0)
5715 {
5716 /* Irix rld assumes that the function stub isn't at the end
5717 of .text section. So put a dummy. XXX */
5718 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
5719 }
5720 else if (! info->shared
5721 && ! mips_elf_hash_table (info)->use_rld_obj_head
5722 && strncmp (name, ".rld_map", 8) == 0)
5723 {
5724 /* We add a room for __rld_map. It will be filled in by the
5725 rtld to contain a pointer to the _r_debug structure. */
5726 s->_raw_size += 4;
5727 }
5728 else if (SGI_COMPAT (output_bfd)
5729 && strncmp (name, ".compact_rel", 12) == 0)
5730 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
5731 else if (strncmp (name, ".init", 5) != 0)
5732 {
5733 /* It's not one of our sections, so don't allocate space. */
5734 continue;
5735 }
5736
5737 if (strip)
5738 {
5739 asection **spp;
5740
5741 for (spp = &s->output_section->owner->sections;
5742 *spp != s->output_section;
5743 spp = &(*spp)->next)
5744 ;
5745 *spp = s->output_section->next;
5746 --s->output_section->owner->section_count;
5747
5748 continue;
5749 }
5750
5751 /* Allocate memory for the section contents. */
5752 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
5753 if (s->contents == NULL && s->_raw_size != 0)
5754 {
5755 bfd_set_error (bfd_error_no_memory);
5756 return false;
5757 }
5758 memset (s->contents, 0, s->_raw_size);
5759 }
5760
5761 if (elf_hash_table (info)->dynamic_sections_created)
5762 {
5763 /* Add some entries to the .dynamic section. We fill in the
5764 values later, in elf_mips_finish_dynamic_sections, but we
5765 must add the entries now so that we get the correct size for
5766 the .dynamic section. The DT_DEBUG entry is filled in by the
5767 dynamic linker and used by the debugger. */
5768 if (! info->shared)
5769 {
5770 if (SGI_COMPAT (output_bfd))
5771 {
5772 /* SGI object has the equivalence of DT_DEBUG in the
5773 DT_MIPS_RLD_MAP entry. */
5774 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
5775 return false;
5776 }
5777 else
5778 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
5779 return false;
5780 }
5781
5782 if (reltext)
5783 {
5784 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
5785 return false;
5786 }
5787
5788 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
5789 return false;
5790
5791 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
5792 {
5793 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
5794 return false;
5795
5796 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
5797 return false;
5798
5799 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
5800 return false;
5801 }
5802
5803 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
5804 return false;
5805
5806 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
5807 return false;
5808
5809 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
5810 {
5811 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
5812 return false;
5813
5814 s = bfd_get_section_by_name (dynobj, ".liblist");
5815 BFD_ASSERT (s != NULL);
5816
5817 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
5818 return false;
5819 }
5820
5821 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
5822 return false;
5823
5824 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
5825 return false;
5826
5827 #if 0
5828 /* Time stamps in executable files are a bad idea. */
5829 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
5830 return false;
5831 #endif
5832
5833 #if 0 /* FIXME */
5834 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
5835 return false;
5836 #endif
5837
5838 #if 0 /* FIXME */
5839 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
5840 return false;
5841 #endif
5842
5843 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
5844 return false;
5845
5846 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
5847 return false;
5848
5849 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
5850 return false;
5851
5852 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
5853 return false;
5854
5855 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
5856 return false;
5857
5858 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
5859 return false;
5860
5861 #if 0 /* (SGI_COMPAT) */
5862 if (! bfd_get_section_by_name (dynobj, ".init"))
5863 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
5864 return false;
5865
5866 if (! bfd_get_section_by_name (dynobj, ".fini"))
5867 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
5868 return false;
5869 #endif
5870 }
5871
5872 /* If we use dynamic linking, we generate a section symbol for each
5873 output section. These are local symbols, which means that they
5874 must come first in the dynamic symbol table.
5875 That means we must increment the dynamic symbol index of every
5876 other dynamic symbol. */
5877 {
5878 const char * const *namep;
5879 unsigned int c, i;
5880 bfd_size_type strindex;
5881 struct bfd_strtab_hash *dynstr;
5882 struct mips_got_info *g;
5883
5884 c = 0;
5885 if (elf_hash_table (info)->dynamic_sections_created)
5886 {
5887 if (SGI_COMPAT (output_bfd))
5888 {
5889 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
5890 elf_link_hash_traverse (elf_hash_table (info),
5891 mips_elf_adjust_dynindx,
5892 (PTR) &c);
5893 elf_hash_table (info)->dynsymcount += c;
5894
5895 dynstr = elf_hash_table (info)->dynstr;
5896 BFD_ASSERT (dynstr != NULL);
5897
5898 for (i = 1, namep = mips_elf_dynsym_sec_names;
5899 *namep != NULL;
5900 i++, namep++)
5901 {
5902 s = bfd_get_section_by_name (output_bfd, *namep);
5903 if (s != NULL)
5904 elf_section_data (s)->dynindx = i;
5905
5906 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
5907 if (strindex == (bfd_size_type) -1)
5908 return false;
5909
5910 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
5911 }
5912 }
5913 else
5914 {
5915 c = bfd_count_sections (output_bfd);
5916 elf_link_hash_traverse (elf_hash_table (info),
5917 mips_elf_adjust_dynindx,
5918 (PTR) &c);
5919 elf_hash_table (info)->dynsymcount += c;
5920
5921 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
5922 {
5923 elf_section_data (s)->dynindx = i;
5924 /* These symbols will have no names, so we don't need to
5925 fiddle with dynstr_index. */
5926 }
5927 }
5928 }
5929
5930 if (sgot != NULL)
5931 {
5932 BFD_ASSERT (elf_section_data (sgot) != NULL);
5933 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5934 BFD_ASSERT (g != NULL);
5935
5936 /* If there are no global got symbols, fake the last symbol so
5937 for safety. */
5938 if (g->global_gotsym)
5939 g->global_gotsym += c;
5940 else
5941 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
5942 }
5943 }
5944
5945 return true;
5946 }
5947
5948 /* Increment the index of a dynamic symbol by a given amount. Called
5949 via elf_link_hash_traverse. */
5950
5951 static boolean
5952 mips_elf_adjust_dynindx (h, cparg)
5953 struct elf_link_hash_entry *h;
5954 PTR cparg;
5955 {
5956 unsigned int *cp = (unsigned int *) cparg;
5957
5958 if (h->dynindx != -1)
5959 h->dynindx += *cp;
5960 return true;
5961 }
5962
5963 /* Finish up dynamic symbol handling. We set the contents of various
5964 dynamic sections here. */
5965
5966 static boolean
5967 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5968 bfd *output_bfd;
5969 struct bfd_link_info *info;
5970 struct elf_link_hash_entry *h;
5971 Elf_Internal_Sym *sym;
5972 {
5973 bfd *dynobj;
5974 bfd_vma gval;
5975 asection *sgot;
5976 struct mips_got_info *g;
5977 const char *name;
5978
5979 dynobj = elf_hash_table (info)->dynobj;
5980 gval = sym->st_value;
5981
5982 if (h->plt_offset != (bfd_vma) -1)
5983 {
5984 asection *s;
5985 bfd_byte *p;
5986 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5987
5988 /* This symbol has a stub. Set it up. */
5989
5990 BFD_ASSERT (h->dynindx != -1);
5991
5992 s = bfd_get_section_by_name (dynobj, ".stub");
5993 BFD_ASSERT (s != NULL);
5994
5995 /* Fill the stub. */
5996 p = stub;
5997 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
5998 p += 4;
5999 bfd_put_32 (output_bfd, STUB_MOVE, p);
6000 p += 4;
6001
6002 /* FIXME: Can h->dynindex be more than 64K? */
6003 if (h->dynindx & 0xffff0000)
6004 return false;
6005
6006 bfd_put_32 (output_bfd, STUB_JALR, p);
6007 p += 4;
6008 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
6009
6010 BFD_ASSERT (h->plt_offset <= s->_raw_size);
6011 memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
6012
6013 /* Mark the symbol as undefined. plt_offset != -1 occurs
6014 only for the referenced symbol. */
6015 sym->st_shndx = SHN_UNDEF;
6016
6017 /* The run-time linker uses the st_value field of the symbol
6018 to reset the global offset table entry for this external
6019 to its stub address when unlinking a shared object. */
6020 gval = s->output_section->vma + s->output_offset + h->plt_offset;
6021 sym->st_value = gval;
6022 }
6023
6024 BFD_ASSERT (h->dynindx != -1);
6025
6026 sgot = bfd_get_section_by_name (dynobj, ".got");
6027 BFD_ASSERT (sgot != NULL);
6028 BFD_ASSERT (elf_section_data (sgot) != NULL);
6029 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6030 BFD_ASSERT (g != NULL);
6031
6032 if ((unsigned long) h->dynindx >= g->global_gotsym)
6033 {
6034 bfd_size_type offset;
6035
6036 /* This symbol has an entry in the global offset table. Set its
6037 value to the corresponding got entry, if needed. */
6038 if (h->got_offset == (bfd_vma) -1)
6039 {
6040 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
6041 BFD_ASSERT (g->local_gotno * 4 <= offset
6042 && offset < sgot->_raw_size);
6043 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
6044 }
6045 }
6046
6047 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
6048 name = h->root.root.string;
6049 if (strcmp (name, "_DYNAMIC") == 0
6050 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
6051 sym->st_shndx = SHN_ABS;
6052 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
6053 {
6054 sym->st_shndx = SHN_ABS;
6055 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6056 sym->st_value = 1;
6057 }
6058 else if (SGI_COMPAT (output_bfd))
6059 {
6060 if (strcmp (name, "_gp_disp") == 0)
6061 {
6062 sym->st_shndx = SHN_ABS;
6063 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6064 sym->st_value = elf_gp (output_bfd);
6065 }
6066 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
6067 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
6068 {
6069 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6070 sym->st_other = STO_PROTECTED;
6071 sym->st_value = 0;
6072 sym->st_shndx = SHN_MIPS_DATA;
6073 }
6074 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
6075 {
6076 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6077 sym->st_other = STO_PROTECTED;
6078 sym->st_value = mips_elf_hash_table (info)->procedure_count;
6079 sym->st_shndx = SHN_ABS;
6080 }
6081 else if (sym->st_shndx != SHN_UNDEF)
6082 {
6083 if (h->type == STT_FUNC)
6084 sym->st_shndx = SHN_MIPS_TEXT;
6085 else if (h->type == STT_OBJECT)
6086 sym->st_shndx = SHN_MIPS_DATA;
6087 }
6088 }
6089
6090 if (SGI_COMPAT (output_bfd)
6091 && ! info->shared)
6092 {
6093 if (! mips_elf_hash_table (info)->use_rld_obj_head
6094 && strcmp (name, "__rld_map") == 0)
6095 {
6096 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6097 BFD_ASSERT (s != NULL);
6098 sym->st_value = s->output_section->vma + s->output_offset;
6099 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
6100 if (mips_elf_hash_table (info)->rld_value == 0)
6101 mips_elf_hash_table (info)->rld_value = sym->st_value;
6102 }
6103 else if (mips_elf_hash_table (info)->use_rld_obj_head
6104 && strcmp (name, "__rld_obj_head") == 0)
6105 {
6106 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6107 BFD_ASSERT (s != NULL);
6108 mips_elf_hash_table (info)->rld_value = sym->st_value;
6109 }
6110 }
6111
6112 /* If this is a mips16 symbol, force the value to be even. */
6113 if (sym->st_other == STO_MIPS16
6114 && (sym->st_value & 1) != 0)
6115 --sym->st_value;
6116
6117 return true;
6118 }
6119
6120 /* Finish up the dynamic sections. */
6121
6122 static boolean
6123 mips_elf_finish_dynamic_sections (output_bfd, info)
6124 bfd *output_bfd;
6125 struct bfd_link_info *info;
6126 {
6127 bfd *dynobj;
6128 asection *sdyn;
6129 asection *sgot;
6130 struct mips_got_info *g;
6131
6132 dynobj = elf_hash_table (info)->dynobj;
6133
6134 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6135
6136 sgot = bfd_get_section_by_name (dynobj, ".got");
6137 if (sgot == NULL)
6138 g = NULL;
6139 else
6140 {
6141 BFD_ASSERT (elf_section_data (sgot) != NULL);
6142 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6143 BFD_ASSERT (g != NULL);
6144 }
6145
6146 if (elf_hash_table (info)->dynamic_sections_created)
6147 {
6148 Elf32_External_Dyn *dyncon, *dynconend;
6149
6150 BFD_ASSERT (sdyn != NULL);
6151 BFD_ASSERT (g != NULL);
6152
6153 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6154 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6155 for (; dyncon < dynconend; dyncon++)
6156 {
6157 Elf_Internal_Dyn dyn;
6158 const char *name;
6159 size_t elemsize;
6160 asection *s;
6161
6162 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6163
6164 switch (dyn.d_tag)
6165 {
6166 default:
6167 break;
6168
6169 case DT_RELENT:
6170 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6171 BFD_ASSERT (s != NULL);
6172 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
6173 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6174 break;
6175
6176 case DT_STRSZ:
6177 /* Rewrite DT_STRSZ. */
6178 dyn.d_un.d_val =
6179 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
6180 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6181 break;
6182
6183 case DT_PLTGOT:
6184 name = ".got";
6185 goto get_vma;
6186 case DT_MIPS_CONFLICT:
6187 name = ".conflict";
6188 goto get_vma;
6189 case DT_MIPS_LIBLIST:
6190 name = ".liblist";
6191 get_vma:
6192 s = bfd_get_section_by_name (output_bfd, name);
6193 BFD_ASSERT (s != NULL);
6194 dyn.d_un.d_ptr = s->vma;
6195 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6196 break;
6197
6198 case DT_MIPS_RLD_VERSION:
6199 dyn.d_un.d_val = 1; /* XXX */
6200 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6201 break;
6202
6203 case DT_MIPS_FLAGS:
6204 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
6205 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6206 break;
6207
6208 case DT_MIPS_CONFLICTNO:
6209 name = ".conflict";
6210 elemsize = sizeof (Elf32_Conflict);
6211 goto set_elemno;
6212
6213 case DT_MIPS_LIBLISTNO:
6214 name = ".liblist";
6215 elemsize = sizeof (Elf32_Lib);
6216 set_elemno:
6217 s = bfd_get_section_by_name (output_bfd, name);
6218 if (s != NULL)
6219 {
6220 if (s->_cooked_size != 0)
6221 dyn.d_un.d_val = s->_cooked_size / elemsize;
6222 else
6223 dyn.d_un.d_val = s->_raw_size / elemsize;
6224 }
6225 else
6226 dyn.d_un.d_val = 0;
6227
6228 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6229 break;
6230
6231 case DT_MIPS_TIME_STAMP:
6232 time ((time_t *) &dyn.d_un.d_val);
6233 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6234 break;
6235
6236 case DT_MIPS_ICHECKSUM:
6237 /* XXX FIXME: */
6238 break;
6239
6240 case DT_MIPS_IVERSION:
6241 /* XXX FIXME: */
6242 break;
6243
6244 case DT_MIPS_BASE_ADDRESS:
6245 s = output_bfd->sections;
6246 BFD_ASSERT (s != NULL);
6247 dyn.d_un.d_ptr = s->vma & ~(0xffff);
6248 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6249 break;
6250
6251 case DT_MIPS_LOCAL_GOTNO:
6252 dyn.d_un.d_val = g->local_gotno;
6253 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6254 break;
6255
6256 case DT_MIPS_SYMTABNO:
6257 name = ".dynsym";
6258 elemsize = sizeof (Elf32_External_Sym);
6259 s = bfd_get_section_by_name (output_bfd, name);
6260 BFD_ASSERT (s != NULL);
6261
6262 if (s->_cooked_size != 0)
6263 dyn.d_un.d_val = s->_cooked_size / elemsize;
6264 else
6265 dyn.d_un.d_val = s->_raw_size / elemsize;
6266 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6267 break;
6268
6269 case DT_MIPS_UNREFEXTNO:
6270 /* XXX FIXME: */
6271 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
6272 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6273 break;
6274
6275 case DT_MIPS_GOTSYM:
6276 dyn.d_un.d_val = g->global_gotsym;
6277 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6278 break;
6279
6280 case DT_MIPS_HIPAGENO:
6281 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
6282 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6283 break;
6284
6285 case DT_MIPS_RLD_MAP:
6286 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
6287 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6288 break;
6289
6290 }
6291 }
6292 }
6293
6294 /* The first entry of the global offset table will be filled at
6295 runtime. The second entry will be used by some runtime loaders.
6296 This isn't the case of Irix rld. */
6297 if (sgot != NULL && sgot->_raw_size > 0)
6298 {
6299 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
6300 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
6301 }
6302
6303 if (sgot != NULL)
6304 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
6305
6306 {
6307 asection *sdynsym;
6308 asection *s;
6309 unsigned int i;
6310 bfd_vma last;
6311 Elf_Internal_Sym sym;
6312 long dindx;
6313 const char *name;
6314 const char * const * namep = mips_elf_dynsym_sec_names;
6315 Elf32_compact_rel cpt;
6316
6317 /* Set up the section symbols for the output sections. SGI sets
6318 the STT_NOTYPE attribute for these symbols. Should we do so? */
6319
6320 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
6321 if (sdynsym != NULL)
6322 {
6323 if (SGI_COMPAT (output_bfd))
6324 {
6325 sym.st_size = 0;
6326 sym.st_name = 0;
6327 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
6328 sym.st_other = 0;
6329
6330 i = 0;
6331 while ((name = *namep++) != NULL)
6332 {
6333 s = bfd_get_section_by_name (output_bfd, name);
6334 if (s != NULL)
6335 {
6336 sym.st_value = s->vma;
6337 dindx = elf_section_data (s)->dynindx;
6338 last = s->vma + s->_raw_size;
6339 }
6340 else
6341 {
6342 sym.st_value = last;
6343 dindx++;
6344 }
6345
6346 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
6347 ? SHN_MIPS_TEXT
6348 : SHN_MIPS_DATA);
6349 ++i;
6350 sym.st_name =
6351 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
6352
6353 bfd_elf32_swap_symbol_out (output_bfd, &sym,
6354 (((Elf32_External_Sym *)
6355 sdynsym->contents)
6356 + dindx));
6357 }
6358
6359 /* Set the sh_info field of the output .dynsym section to
6360 the index of the first global symbol. */
6361 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
6362 SIZEOF_MIPS_DYNSYM_SECNAMES;
6363 }
6364 else
6365 {
6366 sym.st_size = 0;
6367 sym.st_name = 0;
6368 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6369 sym.st_other = 0;
6370
6371 for (s = output_bfd->sections; s != NULL; s = s->next)
6372 {
6373 int indx;
6374
6375 sym.st_value = s->vma;
6376
6377 indx = elf_section_data (s)->this_idx;
6378 BFD_ASSERT (indx > 0);
6379 sym.st_shndx = indx;
6380
6381 bfd_elf32_swap_symbol_out (output_bfd, &sym,
6382 (((Elf32_External_Sym *)
6383 sdynsym->contents)
6384 + elf_section_data (s)->dynindx));
6385 }
6386
6387 /* Set the sh_info field of the output .dynsym section to
6388 the index of the first global symbol. */
6389 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
6390 bfd_count_sections (output_bfd) + 1;
6391 }
6392 }
6393
6394 if (SGI_COMPAT (output_bfd))
6395 {
6396 /* Write .compact_rel section out. */
6397 s = bfd_get_section_by_name (dynobj, ".compact_rel");
6398 if (s != NULL)
6399 {
6400 cpt.id1 = 1;
6401 cpt.num = s->reloc_count;
6402 cpt.id2 = 2;
6403 cpt.offset = (s->output_section->filepos
6404 + sizeof (Elf32_External_compact_rel));
6405 cpt.reserved0 = 0;
6406 cpt.reserved1 = 0;
6407 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
6408 ((Elf32_External_compact_rel *)
6409 s->contents));
6410
6411 /* Clean up a dummy stub function entry in .text. */
6412 s = bfd_get_section_by_name (dynobj, ".stub");
6413 if (s != NULL)
6414 {
6415 file_ptr dummy_offset;
6416
6417 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
6418 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
6419 memset (s->contents + dummy_offset, 0,
6420 MIPS_FUNCTION_STUB_SIZE);
6421 }
6422 }
6423 }
6424
6425 /* Clean up a first relocation in .rel.dyn. */
6426 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6427 if (s != NULL && s->_raw_size > 0)
6428 memset (s->contents, 0, sizeof (Elf32_External_Rel));
6429 }
6430
6431 return true;
6432 }
6433 \f
6434 /* This is almost identical to bfd_generic_get_... except that some
6435 MIPS relocations need to be handled specially. Sigh. */
6436
6437 static bfd_byte *
6438 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
6439 relocateable, symbols)
6440 bfd *abfd;
6441 struct bfd_link_info *link_info;
6442 struct bfd_link_order *link_order;
6443 bfd_byte *data;
6444 boolean relocateable;
6445 asymbol **symbols;
6446 {
6447 /* Get enough memory to hold the stuff */
6448 bfd *input_bfd = link_order->u.indirect.section->owner;
6449 asection *input_section = link_order->u.indirect.section;
6450
6451 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
6452 arelent **reloc_vector = NULL;
6453 long reloc_count;
6454
6455 if (reloc_size < 0)
6456 goto error_return;
6457
6458 reloc_vector = (arelent **) bfd_malloc (reloc_size);
6459 if (reloc_vector == NULL && reloc_size != 0)
6460 goto error_return;
6461
6462 /* read in the section */
6463 if (!bfd_get_section_contents (input_bfd,
6464 input_section,
6465 (PTR) data,
6466 0,
6467 input_section->_raw_size))
6468 goto error_return;
6469
6470 /* We're not relaxing the section, so just copy the size info */
6471 input_section->_cooked_size = input_section->_raw_size;
6472 input_section->reloc_done = true;
6473
6474 reloc_count = bfd_canonicalize_reloc (input_bfd,
6475 input_section,
6476 reloc_vector,
6477 symbols);
6478 if (reloc_count < 0)
6479 goto error_return;
6480
6481 if (reloc_count > 0)
6482 {
6483 arelent **parent;
6484 /* for mips */
6485 int gp_found;
6486 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
6487
6488 {
6489 struct bfd_hash_entry *h;
6490 struct bfd_link_hash_entry *lh;
6491 /* Skip all this stuff if we aren't mixing formats. */
6492 if (abfd && input_bfd
6493 && abfd->xvec == input_bfd->xvec)
6494 lh = 0;
6495 else
6496 {
6497 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
6498 lh = (struct bfd_link_hash_entry *) h;
6499 }
6500 lookup:
6501 if (lh)
6502 {
6503 switch (lh->type)
6504 {
6505 case bfd_link_hash_undefined:
6506 case bfd_link_hash_undefweak:
6507 case bfd_link_hash_common:
6508 gp_found = 0;
6509 break;
6510 case bfd_link_hash_defined:
6511 case bfd_link_hash_defweak:
6512 gp_found = 1;
6513 gp = lh->u.def.value;
6514 break;
6515 case bfd_link_hash_indirect:
6516 case bfd_link_hash_warning:
6517 lh = lh->u.i.link;
6518 /* @@FIXME ignoring warning for now */
6519 goto lookup;
6520 case bfd_link_hash_new:
6521 default:
6522 abort ();
6523 }
6524 }
6525 else
6526 gp_found = 0;
6527 }
6528 /* end mips */
6529 for (parent = reloc_vector; *parent != (arelent *) NULL;
6530 parent++)
6531 {
6532 char *error_message = (char *) NULL;
6533 bfd_reloc_status_type r;
6534
6535 /* Specific to MIPS: Deal with relocation types that require
6536 knowing the gp of the output bfd. */
6537 asymbol *sym = *(*parent)->sym_ptr_ptr;
6538 if (bfd_is_abs_section (sym->section) && abfd)
6539 {
6540 /* The special_function wouldn't get called anyways. */
6541 }
6542 else if (!gp_found)
6543 {
6544 /* The gp isn't there; let the special function code
6545 fall over on its own. */
6546 }
6547 else if ((*parent)->howto->special_function
6548 == _bfd_mips_elf_gprel16_reloc)
6549 {
6550 /* bypass special_function call */
6551 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
6552 relocateable, (PTR) data, gp);
6553 goto skip_bfd_perform_relocation;
6554 }
6555 /* end mips specific stuff */
6556
6557 r = bfd_perform_relocation (input_bfd,
6558 *parent,
6559 (PTR) data,
6560 input_section,
6561 relocateable ? abfd : (bfd *) NULL,
6562 &error_message);
6563 skip_bfd_perform_relocation:
6564
6565 if (relocateable)
6566 {
6567 asection *os = input_section->output_section;
6568
6569 /* A partial link, so keep the relocs */
6570 os->orelocation[os->reloc_count] = *parent;
6571 os->reloc_count++;
6572 }
6573
6574 if (r != bfd_reloc_ok)
6575 {
6576 switch (r)
6577 {
6578 case bfd_reloc_undefined:
6579 if (!((*link_info->callbacks->undefined_symbol)
6580 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6581 input_bfd, input_section, (*parent)->address)))
6582 goto error_return;
6583 break;
6584 case bfd_reloc_dangerous:
6585 BFD_ASSERT (error_message != (char *) NULL);
6586 if (!((*link_info->callbacks->reloc_dangerous)
6587 (link_info, error_message, input_bfd, input_section,
6588 (*parent)->address)))
6589 goto error_return;
6590 break;
6591 case bfd_reloc_overflow:
6592 if (!((*link_info->callbacks->reloc_overflow)
6593 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6594 (*parent)->howto->name, (*parent)->addend,
6595 input_bfd, input_section, (*parent)->address)))
6596 goto error_return;
6597 break;
6598 case bfd_reloc_outofrange:
6599 default:
6600 abort ();
6601 break;
6602 }
6603
6604 }
6605 }
6606 }
6607 if (reloc_vector != NULL)
6608 free (reloc_vector);
6609 return data;
6610
6611 error_return:
6612 if (reloc_vector != NULL)
6613 free (reloc_vector);
6614 return NULL;
6615 }
6616 #define bfd_elf32_bfd_get_relocated_section_contents \
6617 elf32_mips_get_relocated_section_contents
6618 \f
6619 /* ECOFF swapping routines. These are used when dealing with the
6620 .mdebug section, which is in the ECOFF debugging format. */
6621 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
6622 {
6623 /* Symbol table magic number. */
6624 magicSym,
6625 /* Alignment of debugging information. E.g., 4. */
6626 4,
6627 /* Sizes of external symbolic information. */
6628 sizeof (struct hdr_ext),
6629 sizeof (struct dnr_ext),
6630 sizeof (struct pdr_ext),
6631 sizeof (struct sym_ext),
6632 sizeof (struct opt_ext),
6633 sizeof (struct fdr_ext),
6634 sizeof (struct rfd_ext),
6635 sizeof (struct ext_ext),
6636 /* Functions to swap in external symbolic data. */
6637 ecoff_swap_hdr_in,
6638 ecoff_swap_dnr_in,
6639 ecoff_swap_pdr_in,
6640 ecoff_swap_sym_in,
6641 ecoff_swap_opt_in,
6642 ecoff_swap_fdr_in,
6643 ecoff_swap_rfd_in,
6644 ecoff_swap_ext_in,
6645 _bfd_ecoff_swap_tir_in,
6646 _bfd_ecoff_swap_rndx_in,
6647 /* Functions to swap out external symbolic data. */
6648 ecoff_swap_hdr_out,
6649 ecoff_swap_dnr_out,
6650 ecoff_swap_pdr_out,
6651 ecoff_swap_sym_out,
6652 ecoff_swap_opt_out,
6653 ecoff_swap_fdr_out,
6654 ecoff_swap_rfd_out,
6655 ecoff_swap_ext_out,
6656 _bfd_ecoff_swap_tir_out,
6657 _bfd_ecoff_swap_rndx_out,
6658 /* Function to read in symbolic data. */
6659 _bfd_mips_elf_read_ecoff_info
6660 };
6661 \f
6662 #define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
6663 #define TARGET_LITTLE_NAME "elf32-littlemips"
6664 #define TARGET_BIG_SYM bfd_elf32_bigmips_vec
6665 #define TARGET_BIG_NAME "elf32-bigmips"
6666 #define ELF_ARCH bfd_arch_mips
6667 #define ELF_MACHINE_CODE EM_MIPS
6668
6669 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
6670 a value of 0x1000, and we are compatible. */
6671 #define ELF_MAXPAGESIZE 0x1000
6672
6673 #define elf_backend_collect true
6674 #define elf_backend_type_change_ok true
6675 #define elf_info_to_howto 0
6676 #define elf_info_to_howto_rel mips_info_to_howto_rel
6677 #define elf_backend_sym_is_global mips_elf_sym_is_global
6678 #define elf_backend_object_p mips_elf32_object_p
6679 #define elf_backend_section_from_shdr mips_elf32_section_from_shdr
6680 #define elf_backend_fake_sections _bfd_mips_elf_fake_sections
6681 #define elf_backend_section_from_bfd_section \
6682 _bfd_mips_elf_section_from_bfd_section
6683 #define elf_backend_section_processing mips_elf32_section_processing
6684 #define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
6685 #define elf_backend_additional_program_headers \
6686 mips_elf_additional_program_headers
6687 #define elf_backend_modify_segment_map mips_elf_modify_segment_map
6688 #define elf_backend_final_write_processing \
6689 _bfd_mips_elf_final_write_processing
6690 #define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
6691
6692 #define bfd_elf32_bfd_is_local_label mips_elf_is_local_label
6693 #define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
6694 #define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
6695 #define bfd_elf32_bfd_link_hash_table_create \
6696 mips_elf_link_hash_table_create
6697 #define bfd_elf32_bfd_final_link mips_elf_final_link
6698 #define bfd_elf32_bfd_copy_private_bfd_data \
6699 _bfd_mips_elf_copy_private_bfd_data
6700 #define bfd_elf32_bfd_merge_private_bfd_data \
6701 _bfd_mips_elf_merge_private_bfd_data
6702 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
6703 #define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
6704 #define elf_backend_create_dynamic_sections \
6705 mips_elf_create_dynamic_sections
6706 #define elf_backend_check_relocs mips_elf_check_relocs
6707 #define elf_backend_adjust_dynamic_symbol \
6708 mips_elf_adjust_dynamic_symbol
6709 #define elf_backend_size_dynamic_sections \
6710 mips_elf_size_dynamic_sections
6711 #define elf_backend_relocate_section mips_elf_relocate_section
6712 #define elf_backend_link_output_symbol_hook \
6713 mips_elf_link_output_symbol_hook
6714 #define elf_backend_finish_dynamic_symbol \
6715 mips_elf_finish_dynamic_symbol
6716 #define elf_backend_finish_dynamic_sections \
6717 mips_elf_finish_dynamic_sections
6718
6719 #include "elf32-target.h"
This page took 0.18757 seconds and 5 git commands to generate.