]>
Commit | Line | Data |
---|---|---|
db15c6b1 | 1 | /* BFD backend for MIPS BSD (a.out) binaries. |
77d11284 | 2 | Copyright (C) 1993, 1994 Free Software Foundation, Inc. |
db15c6b1 KR |
3 | Written by Ralph Campbell. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. */ | |
20 | ||
5cd3dcff KR |
21 | #define BYTES_IN_WORD 4 |
22 | #define ARCH 32 | |
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 */\ | |
29 | ) | |
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 | |
8f8fefcc JG |
35 | #define MACHTYPE_OK(mtype) ((mtype) == M_UNKNOWN \ |
36 | || (mtype) == M_MIPS1 || (mtype) == M_MIPS2) | |
5cd3dcff KR |
37 | #define MY_symbol_leading_char '\0' |
38 | ||
39 | #define MY(OP) CAT(mipsbsd_,OP) | |
40 | ||
41 | #include "bfd.h" | |
42 | #include "sysdep.h" | |
43 | #include "libbfd.h" | |
44 | #include "libaout.h" | |
45 | ||
46 | #define SET_ARCH_MACH(ABFD, EXEC) \ | |
47 | MY(set_arch_mach)(ABFD, N_MACHTYPE (EXEC)); \ | |
48 | MY(choose_reloc_size)(ABFD); | |
98e1c9e5 KR |
49 | void MY(set_arch_mach) PARAMS ((bfd *abfd, int machtype)); |
50 | static void MY(choose_reloc_size) PARAMS ((bfd *abfd)); | |
5cd3dcff KR |
51 | |
52 | #define MY_write_object_contents MY(write_object_contents) | |
98e1c9e5 | 53 | static boolean MY(write_object_contents) PARAMS ((bfd *abfd)); |
5cd3dcff | 54 | |
326e32d7 ILT |
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 | |
59 | ||
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 | |
5cd3dcff KR |
63 | |
64 | #define MY_backend_data &MY(backend_data) | |
65 | #define MY_BFD_TARGET | |
66 | ||
67 | #include "aout-target.h" | |
68 | ||
5cd3dcff | 69 | void |
98e1c9e5 KR |
70 | MY(set_arch_mach) (abfd, machtype) |
71 | bfd *abfd; | |
72 | int machtype; | |
5cd3dcff KR |
73 | { |
74 | enum bfd_architecture arch; | |
75 | long machine; | |
76 | ||
77 | /* Determine the architecture and machine type of the object file. */ | |
78 | switch (machtype) { | |
79 | ||
5cd3dcff KR |
80 | case M_MIPS1: |
81 | arch = bfd_arch_mips; | |
82 | machine = 3000; | |
83 | break; | |
84 | ||
85 | case M_MIPS2: | |
86 | arch = bfd_arch_mips; | |
87 | machine = 4000; | |
88 | break; | |
89 | ||
90 | default: | |
91 | arch = bfd_arch_obscure; | |
92 | machine = 0; | |
93 | break; | |
94 | } | |
95 | bfd_set_arch_mach(abfd, arch, machine); | |
96 | } | |
97 | ||
98 | /* Determine the size of a relocation entry, based on the architecture */ | |
99 | static void | |
98e1c9e5 KR |
100 | MY(choose_reloc_size) (abfd) |
101 | bfd *abfd; | |
5cd3dcff KR |
102 | { |
103 | switch (bfd_get_arch(abfd)) { | |
104 | case bfd_arch_sparc: | |
105 | case bfd_arch_a29k: | |
106 | case bfd_arch_mips: | |
107 | obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE; | |
108 | break; | |
109 | default: | |
110 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; | |
111 | break; | |
112 | } | |
113 | } | |
114 | ||
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. */ | |
118 | ||
119 | static boolean | |
98e1c9e5 KR |
120 | MY(write_object_contents) (abfd) |
121 | bfd *abfd; | |
5cd3dcff KR |
122 | { |
123 | struct external_exec exec_bytes; | |
124 | struct internal_exec *execp = exec_hdr (abfd); | |
125 | ||
126 | /* Magic number, maestro, please! */ | |
127 | switch (bfd_get_arch(abfd)) { | |
128 | case bfd_arch_m68k: | |
129 | switch (bfd_get_mach(abfd)) { | |
130 | case 68010: | |
131 | N_SET_MACHTYPE(*execp, M_68010); | |
132 | break; | |
133 | default: | |
134 | case 68020: | |
135 | N_SET_MACHTYPE(*execp, M_68020); | |
136 | break; | |
137 | } | |
138 | break; | |
139 | case bfd_arch_sparc: | |
140 | N_SET_MACHTYPE(*execp, M_SPARC); | |
141 | break; | |
142 | case bfd_arch_i386: | |
143 | N_SET_MACHTYPE(*execp, M_386); | |
144 | break; | |
145 | case bfd_arch_a29k: | |
146 | N_SET_MACHTYPE(*execp, M_29K); | |
147 | break; | |
148 | case bfd_arch_mips: | |
149 | switch (bfd_get_mach(abfd)) { | |
150 | case 4000: | |
151 | case 6000: | |
152 | N_SET_MACHTYPE(*execp, M_MIPS2); | |
153 | break; | |
154 | default: | |
155 | N_SET_MACHTYPE(*execp, M_MIPS1); | |
156 | break; | |
157 | } | |
158 | break; | |
159 | default: | |
160 | N_SET_MACHTYPE(*execp, M_UNKNOWN); | |
161 | } | |
162 | ||
163 | MY(choose_reloc_size)(abfd); | |
164 | ||
165 | WRITE_HEADERS(abfd, execp); | |
166 | ||
167 | return true; | |
168 | } | |
169 | ||
170 | /* | |
171 | * MIPS relocation types. | |
172 | */ | |
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 | |
179 | ||
180 | /* | |
181 | * This is only called when performing a BFD_RELOC_HI16_S relocation. | |
182 | * We need to see if bit 15 is set in the result. If it is, we add | |
183 | * 0x10000 and continue normally. This will compensate for the sign extension | |
184 | * when the low bits are added at run time. | |
185 | */ | |
326e32d7 ILT |
186 | static bfd_reloc_status_type |
187 | mips_fix_hi16_s PARAMS ((bfd *, arelent *, asymbol *, PTR, | |
188 | asection *, bfd *, char **)); | |
189 | ||
190 | static bfd_reloc_status_type | |
191 | mips_fix_hi16_s (abfd, reloc_entry, symbol, data, input_section, | |
192 | output_bfd, error_message) | |
98e1c9e5 KR |
193 | bfd *abfd; |
194 | arelent *reloc_entry; | |
326e32d7 | 195 | asymbol *symbol; |
98e1c9e5 KR |
196 | PTR data; |
197 | asection *input_section; | |
198 | bfd *output_bfd; | |
326e32d7 | 199 | char **error_message; |
5cd3dcff KR |
200 | { |
201 | bfd_vma relocation; | |
202 | ||
203 | /* If this is a partial relocation, just continue. */ | |
204 | if (output_bfd != (bfd *)NULL) | |
205 | return bfd_reloc_continue; | |
206 | ||
207 | /* | |
208 | * Work out which section the relocation is targetted at and the | |
209 | * initial relocation command value. | |
210 | */ | |
211 | if (symbol->section == &bfd_com_section) | |
212 | relocation = 0; | |
213 | else | |
214 | relocation = symbol->value; | |
215 | ||
216 | relocation += symbol->section->output_section->vma; | |
217 | relocation += symbol->section->output_offset; | |
218 | relocation += reloc_entry->addend; | |
219 | ||
220 | if (relocation & 0x8000) | |
221 | reloc_entry->addend += 0x10000; | |
222 | ||
223 | return bfd_reloc_continue; | |
224 | } | |
225 | ||
226 | static reloc_howto_type mips_howto_table_ext[] = { | |
326e32d7 | 227 | {MIPS_RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, |
8f8fefcc | 228 | "32", false, 0, 0xffffffff, false}, |
326e32d7 | 229 | {MIPS_RELOC_JMP, 2, 2, 26, false, 0, complain_overflow_bitfield, 0, |
8f8fefcc | 230 | "MIPS_JMP", false, 0, 0x03ffffff, false}, |
326e32d7 | 231 | {MIPS_RELOC_WDISP16, 2, 1, 16, true, 0, complain_overflow_signed, 0, |
8f8fefcc | 232 | "WDISP16", false, 0, 0x0000ffff, false}, |
326e32d7 | 233 | {MIPS_RELOC_HI16, 16, 1, 16, false, 0, complain_overflow_bitfield, 0, |
8f8fefcc | 234 | "HI16", false, 0, 0x0000ffff, false}, |
326e32d7 ILT |
235 | {MIPS_RELOC_HI16_S, 16, 1, 16, false, 0, complain_overflow_bitfield, |
236 | mips_fix_hi16_s, | |
237 | "HI16_S", false, 0, 0x0000ffff, false}, | |
238 | {MIPS_RELOC_LO16, 0, 1, 16, false, 0, complain_overflow_dont, 0, | |
8f8fefcc | 239 | "LO16", false, 0, 0x0000ffff, false}, |
5cd3dcff KR |
240 | }; |
241 | ||
326e32d7 | 242 | static const reloc_howto_type * |
5cd3dcff KR |
243 | MY(reloc_howto_type_lookup) (abfd, code) |
244 | bfd *abfd; | |
245 | bfd_reloc_code_real_type code; | |
246 | { | |
5cd3dcff | 247 | |
07bb4e8a KR |
248 | if (bfd_get_arch (abfd) != bfd_arch_mips) |
249 | return 0; | |
250 | ||
251 | switch (code) | |
5cd3dcff | 252 | { |
07bb4e8a KR |
253 | case BFD_RELOC_32: |
254 | return (&mips_howto_table_ext[MIPS_RELOC_32]); | |
255 | case BFD_RELOC_MIPS_JMP: | |
256 | return (&mips_howto_table_ext[MIPS_RELOC_JMP]); | |
257 | case BFD_RELOC_16_PCREL_S2: | |
258 | return (&mips_howto_table_ext[MIPS_RELOC_WDISP16]); | |
259 | case BFD_RELOC_HI16: | |
260 | return (&mips_howto_table_ext[MIPS_RELOC_HI16]); | |
261 | case BFD_RELOC_HI16_S: | |
262 | return (&mips_howto_table_ext[MIPS_RELOC_HI16_S]); | |
263 | case BFD_RELOC_LO16: | |
264 | return (&mips_howto_table_ext[MIPS_RELOC_LO16]); | |
5cd3dcff KR |
265 | default: |
266 | return 0; | |
267 | } | |
268 | } | |
269 | ||
270 | /* | |
271 | * This is just like the standard aoutx.h version but we need to do our | |
272 | * own mapping of external reloc type values to howto entries. | |
273 | */ | |
326e32d7 | 274 | long |
5cd3dcff KR |
275 | MY(canonicalize_reloc)(abfd, section, relptr, symbols) |
276 | bfd *abfd; | |
277 | sec_ptr section; | |
278 | arelent **relptr; | |
279 | asymbol **symbols; | |
280 | { | |
281 | arelent *tblptr = section->relocation; | |
282 | unsigned int count, c; | |
283 | extern reloc_howto_type NAME(aout,ext_howto_table)[]; | |
284 | ||
285 | /* If we have already read in the relocation table, return the values. */ | |
286 | if (section->flags & SEC_CONSTRUCTOR) { | |
287 | arelent_chain *chain = section->constructor_chain; | |
288 | ||
289 | for (count = 0; count < section->reloc_count; count++) { | |
290 | *relptr++ = &chain->relent; | |
291 | chain = chain->next; | |
292 | } | |
293 | *relptr = 0; | |
294 | return section->reloc_count; | |
295 | } | |
296 | if (tblptr && section->reloc_count) { | |
297 | for (count = 0; count++ < section->reloc_count;) | |
298 | *relptr++ = tblptr++; | |
299 | *relptr = 0; | |
300 | return section->reloc_count; | |
301 | } | |
302 | ||
303 | if (!NAME(aout,slurp_reloc_table)(abfd, section, symbols)) | |
326e32d7 | 304 | return -1; |
5cd3dcff | 305 | tblptr = section->relocation; |
5cd3dcff KR |
306 | |
307 | /* fix up howto entries */ | |
308 | for (count = 0; count++ < section->reloc_count;) | |
309 | { | |
310 | c = tblptr->howto - NAME(aout,ext_howto_table); | |
311 | tblptr->howto = &mips_howto_table_ext[c]; | |
312 | ||
313 | *relptr++ = tblptr++; | |
314 | } | |
315 | *relptr = 0; | |
316 | return section->reloc_count; | |
317 | } | |
318 | ||
319 | static CONST struct aout_backend_data MY(backend_data) = { | |
320 | 0, /* zmagic contiguous */ | |
321 | 1, /* text incl header */ | |
77d11284 | 322 | 0, /* exec_hdr_flags */ |
5cd3dcff KR |
323 | PAGE_SIZE, /* text vma */ |
324 | MY_set_sizes, | |
325 | 0, /* text size includes exec header */ | |
326 | }; | |
327 | ||
328 | bfd_target aout_mips_little_vec = | |
329 | { | |
326e32d7 | 330 | "a.out-mips-little", /* name */ |
5cd3dcff KR |
331 | bfd_target_aout_flavour, |
332 | false, /* target byte order (little) */ | |
333 | false, /* target headers byte order (little) */ | |
334 | (HAS_RELOC | EXEC_P | /* object flags */ | |
335 | HAS_LINENO | HAS_DEBUG | | |
326e32d7 | 336 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
5cd3dcff KR |
337 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
338 | MY_symbol_leading_char, | |
339 | ' ', /* ar_pad_char */ | |
340 | 15, /* ar_max_namelen */ | |
341 | 1, /* minimum alignment */ | |
326e32d7 ILT |
342 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
343 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
344 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | |
345 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
346 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
347 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | |
5cd3dcff KR |
348 | {_bfd_dummy_target, MY_object_p, /* bfd_check_format */ |
349 | bfd_generic_archive_p, MY_core_file_p}, | |
350 | {bfd_false, MY_mkobject, /* bfd_set_format */ | |
351 | _bfd_generic_mkarchive, bfd_false}, | |
352 | {bfd_false, MY_write_object_contents, /* bfd_write_contents */ | |
353 | _bfd_write_archive_contents, bfd_false}, | |
6812b607 ILT |
354 | |
355 | BFD_JUMP_TABLE_GENERIC (MY), | |
356 | BFD_JUMP_TABLE_COPY (MY), | |
357 | BFD_JUMP_TABLE_CORE (MY), | |
358 | BFD_JUMP_TABLE_ARCHIVE (MY), | |
359 | BFD_JUMP_TABLE_SYMBOLS (MY), | |
360 | BFD_JUMP_TABLE_RELOCS (MY), | |
361 | BFD_JUMP_TABLE_WRITE (MY), | |
362 | BFD_JUMP_TABLE_LINK (MY), | |
77d11284 | 363 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
6812b607 | 364 | |
5cd3dcff KR |
365 | (PTR) MY_backend_data, |
366 | }; | |
367 | ||
368 | bfd_target aout_mips_big_vec = | |
369 | { | |
326e32d7 | 370 | "a.out-mips-big", /* name */ |
5cd3dcff KR |
371 | bfd_target_aout_flavour, |
372 | true, /* target byte order (big) */ | |
373 | true, /* target headers byte order (big) */ | |
374 | (HAS_RELOC | EXEC_P | /* object flags */ | |
375 | HAS_LINENO | HAS_DEBUG | | |
326e32d7 | 376 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
5cd3dcff KR |
377 | (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
378 | MY_symbol_leading_char, | |
379 | ' ', /* ar_pad_char */ | |
380 | 15, /* ar_max_namelen */ | |
381 | 1, /* minimum alignment */ | |
326e32d7 ILT |
382 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
383 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
384 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ | |
385 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
386 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
387 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ | |
5cd3dcff KR |
388 | {_bfd_dummy_target, MY_object_p, /* bfd_check_format */ |
389 | bfd_generic_archive_p, MY_core_file_p}, | |
390 | {bfd_false, MY_mkobject, /* bfd_set_format */ | |
391 | _bfd_generic_mkarchive, bfd_false}, | |
392 | {bfd_false, MY_write_object_contents, /* bfd_write_contents */ | |
393 | _bfd_write_archive_contents, bfd_false}, | |
6812b607 ILT |
394 | |
395 | BFD_JUMP_TABLE_GENERIC (MY), | |
396 | BFD_JUMP_TABLE_COPY (MY), | |
397 | BFD_JUMP_TABLE_CORE (MY), | |
398 | BFD_JUMP_TABLE_ARCHIVE (MY), | |
399 | BFD_JUMP_TABLE_SYMBOLS (MY), | |
400 | BFD_JUMP_TABLE_RELOCS (MY), | |
401 | BFD_JUMP_TABLE_WRITE (MY), | |
402 | BFD_JUMP_TABLE_LINK (MY), | |
77d11284 | 403 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
6812b607 | 404 | |
5cd3dcff KR |
405 | (PTR) MY_backend_data, |
406 | }; |