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