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