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