1 /* BFD backend for MIPS BSD (a.out) binaries.
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3 Written by Ralph Campbell.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #define BYTES_IN_WORD 4
23 /* #define ENTRY_CAN_BE_ZERO */
24 #define N_HEADER_IN_TEXT(x) 1
25 #define N_SHARED_LIB(x) 0
26 #define N_TXTADDR(x) \
27 (N_MAGIC(x) != ZMAGIC ? (x).a_entry : /* object file or NMAGIC */\
28 TEXT_START_ADDR + EXEC_BYTES_SIZE /* no padding */\
30 #define N_DATADDR(x) (N_TXTADDR(x)+N_TXTSIZE(x))
31 #define TEXT_START_ADDR 4096
32 #define PAGE_SIZE 4096
33 #define SEGMENT_SIZE PAGE_SIZE
34 #define DEFAULT_ARCH bfd_arch_mips
35 #define MACHTYPE_OK(mtype) ((mtype) == M_UNKNOWN \
36 || (mtype) == M_MIPS1 || (mtype) == M_MIPS2)
37 #define MY_symbol_leading_char '\0'
39 #define MY(OP) CAT(mipsbsd_,OP)
46 #define SET_ARCH_MACH(ABFD, EXEC) \
47 MY(set_arch_mach)(ABFD, N_MACHTYPE (EXEC)); \
48 MY(choose_reloc_size)(ABFD);
49 void MY(set_arch_mach
) PARAMS ((bfd
*abfd
, int machtype
));
50 static void MY(choose_reloc_size
) PARAMS ((bfd
*abfd
));
52 #define MY_write_object_contents MY(write_object_contents)
53 static boolean
MY(write_object_contents
) PARAMS ((bfd
*abfd
));
55 /* We can't use MY(x) here because it leads to a recursive call to CAT
56 when expanded inside JUMP_TABLE. */
57 #define MY_bfd_reloc_type_lookup mipsbsd_reloc_howto_type_lookup
58 #define MY_canonicalize_reloc mipsbsd_canonicalize_reloc
60 #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
61 #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols
62 #define MY_bfd_final_link _bfd_generic_final_link
64 #define MY_backend_data &MY(backend_data)
67 #include "aout-target.h"
70 MY(set_arch_mach
) (abfd
, machtype
)
74 enum bfd_architecture arch
;
77 /* Determine the architecture and machine type of the object file. */
91 arch
= bfd_arch_obscure
;
95 bfd_set_arch_mach(abfd
, arch
, machine
);
98 /* Determine the size of a relocation entry, based on the architecture */
100 MY(choose_reloc_size
) (abfd
)
103 switch (bfd_get_arch(abfd
)) {
107 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
110 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
115 /* Write an object file in BSD a.out format.
116 Section contents have already been written. We write the
117 file header, symbols, and relocation. */
120 MY(write_object_contents
) (abfd
)
123 struct external_exec exec_bytes
;
124 struct internal_exec
*execp
= exec_hdr (abfd
);
126 /* Magic number, maestro, please! */
127 switch (bfd_get_arch(abfd
)) {
129 switch (bfd_get_mach(abfd
)) {
131 N_SET_MACHTYPE(*execp
, M_68010
);
135 N_SET_MACHTYPE(*execp
, M_68020
);
140 N_SET_MACHTYPE(*execp
, M_SPARC
);
143 N_SET_MACHTYPE(*execp
, M_386
);
146 N_SET_MACHTYPE(*execp
, M_29K
);
149 switch (bfd_get_mach(abfd
)) {
152 N_SET_MACHTYPE(*execp
, M_MIPS2
);
155 N_SET_MACHTYPE(*execp
, M_MIPS1
);
160 N_SET_MACHTYPE(*execp
, M_UNKNOWN
);
163 MY(choose_reloc_size
)(abfd
);
165 WRITE_HEADERS(abfd
, execp
);
171 * MIPS relocation types.
173 #define MIPS_RELOC_32 0
174 #define MIPS_RELOC_JMP 1
175 #define MIPS_RELOC_WDISP16 2
176 #define MIPS_RELOC_HI16 3
177 #define MIPS_RELOC_HI16_S 4
178 #define MIPS_RELOC_LO16 5
181 * This is only called when performing a BFD_RELOC_MIPS_JMP relocation.
182 * The jump destination address is formed from the upper 4 bits of the
183 * "current" program counter concatenated with the jump instruction's
184 * 26 bit field and two trailing zeros.
185 * If the destination address is not in the same segment as the "current"
186 * program counter, then we need to signal an error.
188 static bfd_reloc_status_type
189 mips_fix_jmp_addr (abfd
,reloc_entry
,symbol
,data
,input_section
,output_bfd
)
191 arelent
*reloc_entry
;
192 struct symbol_cache_entry
*symbol
;
194 asection
*input_section
;
197 bfd_vma relocation
, pc
;
199 /* If this is a partial relocation, just continue. */
200 if (output_bfd
!= (bfd
*)NULL
)
201 return bfd_reloc_continue
;
203 /* If this is an undefined symbol, return error */
204 if (symbol
->section
== &bfd_und_section
205 && (symbol
->flags
& BSF_WEAK
) == 0)
206 return bfd_reloc_undefined
;
209 * Work out which section the relocation is targetted at and the
210 * initial relocation command value.
212 if (bfd_is_com_section (symbol
->section
))
215 relocation
= symbol
->value
;
217 relocation
+= symbol
->section
->output_section
->vma
;
218 relocation
+= symbol
->section
->output_offset
;
219 relocation
+= reloc_entry
->addend
;
221 pc
= input_section
->output_section
->vma
+ input_section
->output_offset
+
222 reloc_entry
->address
+ 4;
224 if ((relocation
& 0xF0000000) != (pc
& 0xF0000000))
225 return bfd_reloc_overflow
;
227 return bfd_reloc_continue
;
231 * This is only called when performing a BFD_RELOC_HI16_S relocation.
232 * We need to see if bit 15 is set in the result. If it is, we add
233 * 0x10000 and continue normally. This will compensate for the sign extension
234 * when the low bits are added at run time.
236 static bfd_reloc_status_type
237 mips_fix_hi16_s
PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
,
238 asection
*, bfd
*, char **));
240 static bfd_reloc_status_type
241 mips_fix_hi16_s (abfd
, reloc_entry
, symbol
, data
, input_section
,
242 output_bfd
, error_message
)
244 arelent
*reloc_entry
;
247 asection
*input_section
;
249 char **error_message
;
253 /* If this is a partial relocation, just continue. */
254 if (output_bfd
!= (bfd
*)NULL
)
255 return bfd_reloc_continue
;
257 /* If this is an undefined symbol, return error */
258 if (symbol
->section
== &bfd_und_section
259 && (symbol
->flags
& BSF_WEAK
) == 0)
260 return bfd_reloc_undefined
;
263 * Work out which section the relocation is targetted at and the
264 * initial relocation command value.
266 if (bfd_is_com_section (symbol
->section
))
269 relocation
= symbol
->value
;
271 relocation
+= symbol
->section
->output_section
->vma
;
272 relocation
+= symbol
->section
->output_offset
;
273 relocation
+= reloc_entry
->addend
;
275 if (relocation
& 0x8000)
276 reloc_entry
->addend
+= 0x10000;
278 return bfd_reloc_continue
;
281 static reloc_howto_type mips_howto_table_ext
[] = {
282 {MIPS_RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0,
283 "32", false, 0, 0xffffffff, false},
284 {MIPS_RELOC_JMP
, 2, 2, 26, false, 0, complain_overflow_dont
,
286 "MIPS_JMP", false, 0, 0x03ffffff, false},
287 {MIPS_RELOC_WDISP16
, 2, 1, 16, true, 0, complain_overflow_signed
, 0,
288 "WDISP16", false, 0, 0x0000ffff, false},
289 {MIPS_RELOC_HI16
, 16, 1, 16, false, 0, complain_overflow_bitfield
, 0,
290 "HI16", false, 0, 0x0000ffff, false},
291 {MIPS_RELOC_HI16_S
, 16, 1, 16, false, 0, complain_overflow_bitfield
,
293 "HI16_S", false, 0, 0x0000ffff, false},
294 {MIPS_RELOC_LO16
, 0, 1, 16, false, 0, complain_overflow_dont
, 0,
295 "LO16", false, 0, 0x0000ffff, false},
298 static const reloc_howto_type
*
299 MY(reloc_howto_type_lookup
) (abfd
, code
)
301 bfd_reloc_code_real_type code
;
304 if (bfd_get_arch (abfd
) != bfd_arch_mips
)
310 return (&mips_howto_table_ext
[MIPS_RELOC_32
]);
311 case BFD_RELOC_MIPS_JMP
:
312 return (&mips_howto_table_ext
[MIPS_RELOC_JMP
]);
313 case BFD_RELOC_16_PCREL_S2
:
314 return (&mips_howto_table_ext
[MIPS_RELOC_WDISP16
]);
316 return (&mips_howto_table_ext
[MIPS_RELOC_HI16
]);
317 case BFD_RELOC_HI16_S
:
318 return (&mips_howto_table_ext
[MIPS_RELOC_HI16_S
]);
320 return (&mips_howto_table_ext
[MIPS_RELOC_LO16
]);
327 * This is just like the standard aoutx.h version but we need to do our
328 * own mapping of external reloc type values to howto entries.
331 MY(canonicalize_reloc
)(abfd
, section
, relptr
, symbols
)
337 arelent
*tblptr
= section
->relocation
;
338 unsigned int count
, c
;
339 extern reloc_howto_type
NAME(aout
,ext_howto_table
)[];
341 /* If we have already read in the relocation table, return the values. */
342 if (section
->flags
& SEC_CONSTRUCTOR
) {
343 arelent_chain
*chain
= section
->constructor_chain
;
345 for (count
= 0; count
< section
->reloc_count
; count
++) {
346 *relptr
++ = &chain
->relent
;
350 return section
->reloc_count
;
352 if (tblptr
&& section
->reloc_count
) {
353 for (count
= 0; count
++ < section
->reloc_count
;)
354 *relptr
++ = tblptr
++;
356 return section
->reloc_count
;
359 if (!NAME(aout
,slurp_reloc_table
)(abfd
, section
, symbols
))
361 tblptr
= section
->relocation
;
363 /* fix up howto entries */
364 for (count
= 0; count
++ < section
->reloc_count
;)
366 c
= tblptr
->howto
- NAME(aout
,ext_howto_table
);
367 tblptr
->howto
= &mips_howto_table_ext
[c
];
369 *relptr
++ = tblptr
++;
372 return section
->reloc_count
;
375 static CONST
struct aout_backend_data
MY(backend_data
) = {
376 0, /* zmagic contiguous */
377 1, /* text incl header */
378 0, /* exec_hdr_flags */
379 PAGE_SIZE
, /* text vma */
381 0, /* text size includes exec header */
384 bfd_target aout_mips_little_vec
=
386 "a.out-mips-little", /* name */
387 bfd_target_aout_flavour
,
388 false, /* target byte order (little) */
389 false, /* target headers byte order (little) */
390 (HAS_RELOC
| EXEC_P
| /* object flags */
391 HAS_LINENO
| HAS_DEBUG
|
392 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
393 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
394 MY_symbol_leading_char
,
395 ' ', /* ar_pad_char */
396 15, /* ar_max_namelen */
397 1, /* minimum alignment */
398 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
399 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
400 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
401 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
402 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
403 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
404 {_bfd_dummy_target
, MY_object_p
, /* bfd_check_format */
405 bfd_generic_archive_p
, MY_core_file_p
},
406 {bfd_false
, MY_mkobject
, /* bfd_set_format */
407 _bfd_generic_mkarchive
, bfd_false
},
408 {bfd_false
, MY_write_object_contents
, /* bfd_write_contents */
409 _bfd_write_archive_contents
, bfd_false
},
411 BFD_JUMP_TABLE_GENERIC (MY
),
412 BFD_JUMP_TABLE_COPY (MY
),
413 BFD_JUMP_TABLE_CORE (MY
),
414 BFD_JUMP_TABLE_ARCHIVE (MY
),
415 BFD_JUMP_TABLE_SYMBOLS (MY
),
416 BFD_JUMP_TABLE_RELOCS (MY
),
417 BFD_JUMP_TABLE_WRITE (MY
),
418 BFD_JUMP_TABLE_LINK (MY
),
419 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
421 (PTR
) MY_backend_data
,
424 bfd_target aout_mips_big_vec
=
426 "a.out-mips-big", /* name */
427 bfd_target_aout_flavour
,
428 true, /* target byte order (big) */
429 true, /* target headers byte order (big) */
430 (HAS_RELOC
| EXEC_P
| /* object flags */
431 HAS_LINENO
| HAS_DEBUG
|
432 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
433 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
434 MY_symbol_leading_char
,
435 ' ', /* ar_pad_char */
436 15, /* ar_max_namelen */
437 1, /* minimum alignment */
438 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
439 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
440 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
441 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
442 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
443 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
444 {_bfd_dummy_target
, MY_object_p
, /* bfd_check_format */
445 bfd_generic_archive_p
, MY_core_file_p
},
446 {bfd_false
, MY_mkobject
, /* bfd_set_format */
447 _bfd_generic_mkarchive
, bfd_false
},
448 {bfd_false
, MY_write_object_contents
, /* bfd_write_contents */
449 _bfd_write_archive_contents
, bfd_false
},
451 BFD_JUMP_TABLE_GENERIC (MY
),
452 BFD_JUMP_TABLE_COPY (MY
),
453 BFD_JUMP_TABLE_CORE (MY
),
454 BFD_JUMP_TABLE_ARCHIVE (MY
),
455 BFD_JUMP_TABLE_SYMBOLS (MY
),
456 BFD_JUMP_TABLE_RELOCS (MY
),
457 BFD_JUMP_TABLE_WRITE (MY
),
458 BFD_JUMP_TABLE_LINK (MY
),
459 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
461 (PTR
) MY_backend_data
,