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