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