Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* BFD back-end for MS-DOS executables. |
2 | Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc. | |
3 | Written by Bryan Ford of the University of Utah. | |
4 | ||
5 | Contributed by the Center for Software Science at the | |
6 | University of Utah (pa-gdb-bugs@cs.utah.edu). | |
7 | ||
8 | This file is part of BFD, the Binary File Descriptor library. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software | |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | ||
25 | #include "bfd.h" | |
26 | #include "sysdep.h" | |
27 | #include "libbfd.h" | |
28 | #include "libaout.h" | |
29 | ||
30 | #if 0 | |
31 | struct exe_header | |
32 | { | |
33 | unsigned short magic; | |
34 | unsigned short bytes_in_last_page; | |
35 | unsigned short npages; /* number of 512-byte "pages" including this header */ | |
36 | unsigned short nrelocs; | |
37 | unsigned short header_paras; /* number of 16-byte paragraphs in header */ | |
38 | unsigned short reserved; | |
39 | unsigned short load_switch; | |
40 | unsigned short ss_ofs; | |
41 | unsigned short sp; | |
42 | unsigned short checksum; | |
43 | unsigned short ip; | |
44 | unsigned short cs_ofs; | |
45 | unsigned short reloc_ofs; | |
46 | unsigned short reserved2; | |
47 | unsigned short something1; | |
48 | unsigned short something2; | |
49 | unsigned short something3; | |
50 | }; | |
51 | #endif | |
52 | ||
53 | #define EXE_MAGIC 0x5a4d | |
54 | #define EXE_LOAD_HIGH 0x0000 | |
55 | #define EXE_LOAD_LOW 0xffff | |
56 | #define EXE_PAGE_SIZE 512 | |
57 | ||
58 | ||
59 | static int | |
60 | msdos_sizeof_headers (abfd, exec) | |
61 | bfd *abfd; | |
62 | boolean exec; | |
63 | { | |
64 | return 0; | |
65 | } | |
66 | ||
67 | static boolean | |
68 | msdos_write_object_contents (abfd) | |
69 | bfd *abfd; | |
70 | { | |
71 | static char hdr[EXE_PAGE_SIZE]; | |
72 | file_ptr outfile_size = sizeof(hdr); | |
73 | bfd_vma high_vma = 0; | |
74 | asection *sec; | |
75 | ||
76 | /* Find the total size of the program on disk and in memory. */ | |
77 | for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next) | |
78 | { | |
79 | if (bfd_get_section_size_before_reloc (sec) == 0) | |
80 | continue; | |
81 | if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC) | |
82 | { | |
83 | bfd_vma sec_vma = bfd_get_section_vma (abfd, sec) | |
84 | + bfd_get_section_size_before_reloc (sec); | |
85 | if (sec_vma > high_vma) | |
86 | high_vma = sec_vma; | |
87 | } | |
88 | if (bfd_get_section_flags (abfd, sec) & SEC_LOAD) | |
89 | { | |
90 | file_ptr sec_end = sizeof(hdr) | |
91 | + bfd_get_section_vma (abfd, sec) | |
92 | + bfd_get_section_size_before_reloc (sec); | |
93 | if (sec_end > outfile_size) | |
94 | outfile_size = sec_end; | |
95 | } | |
96 | } | |
97 | ||
98 | /* Make sure the program isn't too big. */ | |
99 | if (high_vma > (bfd_vma)0xffff) | |
100 | { | |
101 | bfd_set_error(bfd_error_file_too_big); | |
102 | return false; | |
103 | } | |
104 | ||
105 | /* constants */ | |
106 | bfd_h_put_16(abfd, EXE_MAGIC, &hdr[0]); | |
107 | bfd_h_put_16(abfd, EXE_PAGE_SIZE / 16, &hdr[8]); | |
108 | bfd_h_put_16(abfd, EXE_LOAD_LOW, &hdr[12]); | |
109 | bfd_h_put_16(abfd, 0x3e, &hdr[24]); | |
110 | bfd_h_put_16(abfd, 0x0001, &hdr[28]); /* XXX??? */ | |
111 | bfd_h_put_16(abfd, 0x30fb, &hdr[30]); /* XXX??? */ | |
112 | bfd_h_put_16(abfd, 0x726a, &hdr[32]); /* XXX??? */ | |
113 | ||
114 | /* bytes in last page (0 = full page) */ | |
115 | bfd_h_put_16(abfd, outfile_size & (EXE_PAGE_SIZE - 1), &hdr[2]); | |
116 | ||
117 | /* number of pages */ | |
118 | bfd_h_put_16(abfd, (outfile_size + EXE_PAGE_SIZE - 1) / EXE_PAGE_SIZE, | |
119 | &hdr[4]); | |
120 | ||
121 | /* Set the initial stack pointer to the end of the bss. | |
122 | The program's crt0 code must relocate it to a real stack. */ | |
123 | bfd_h_put_16(abfd, high_vma, &hdr[16]); | |
124 | ||
125 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 | |
126 | || bfd_write (hdr, 1, sizeof(hdr), abfd) != sizeof(hdr)) | |
127 | return false; | |
128 | ||
129 | return true; | |
130 | } | |
131 | ||
132 | static boolean | |
133 | msdos_set_section_contents (abfd, section, location, offset, count) | |
134 | bfd *abfd; | |
135 | sec_ptr section; | |
136 | PTR location; | |
137 | file_ptr offset; | |
138 | bfd_size_type count; | |
139 | { | |
140 | ||
141 | if (count == 0) | |
142 | return true; | |
143 | ||
144 | section->filepos = EXE_PAGE_SIZE + bfd_get_section_vma (abfd, section); | |
145 | ||
146 | if (bfd_get_section_flags (abfd, section) & SEC_LOAD) | |
147 | { | |
148 | if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0 | |
149 | || bfd_write (location, 1, count, abfd) != count) | |
150 | return false; | |
151 | } | |
152 | ||
153 | return true; | |
154 | } | |
155 | ||
156 | ||
157 | ||
158 | #define msdos_mkobject aout_32_mkobject | |
159 | #define msdos_make_empty_symbol aout_32_make_empty_symbol | |
160 | #define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup | |
161 | ||
162 | #define msdos_close_and_cleanup _bfd_generic_close_and_cleanup | |
163 | #define msdos_bfd_free_cached_info _bfd_generic_bfd_free_cached_info | |
164 | #define msdos_new_section_hook _bfd_generic_new_section_hook | |
165 | #define msdos_get_section_contents _bfd_generic_get_section_contents | |
166 | #define msdos_get_section_contents_in_window \ | |
167 | _bfd_generic_get_section_contents_in_window | |
168 | #define msdos_bfd_get_relocated_section_contents \ | |
169 | bfd_generic_get_relocated_section_contents | |
170 | #define msdos_bfd_relax_section bfd_generic_relax_section | |
171 | #define msdos_bfd_gc_sections bfd_generic_gc_sections | |
172 | #define msdos_bfd_link_hash_table_create _bfd_generic_link_hash_table_create | |
173 | #define msdos_bfd_link_add_symbols _bfd_generic_link_add_symbols | |
174 | #define msdos_bfd_final_link _bfd_generic_final_link | |
175 | #define msdos_bfd_link_split_section _bfd_generic_link_split_section | |
176 | #define msdos_set_arch_mach _bfd_generic_set_arch_mach | |
177 | ||
178 | #define msdos_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound | |
179 | #define msdos_get_symtab _bfd_nosymbols_get_symtab | |
180 | #define msdos_print_symbol _bfd_nosymbols_print_symbol | |
181 | #define msdos_get_symbol_info _bfd_nosymbols_get_symbol_info | |
182 | #define msdos_find_nearest_line _bfd_nosymbols_find_nearest_line | |
183 | #define msdos_get_lineno _bfd_nosymbols_get_lineno | |
184 | #define msdos_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name | |
185 | #define msdos_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol | |
186 | #define msdos_read_minisymbols _bfd_nosymbols_read_minisymbols | |
187 | #define msdos_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol | |
188 | ||
189 | #define msdos_canonicalize_reloc _bfd_norelocs_canonicalize_reloc | |
190 | #define msdos_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound | |
191 | #define msdos_32_bfd_link_split_section _bfd_generic_link_split_section | |
192 | ||
193 | const bfd_target i386msdos_vec = | |
194 | { | |
195 | "msdos", /* name */ | |
196 | bfd_target_msdos_flavour, | |
197 | BFD_ENDIAN_LITTLE, /* target byte order */ | |
198 | BFD_ENDIAN_LITTLE, /* target headers byte order */ | |
199 | (EXEC_P), /* object flags */ | |
200 | (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | |
201 | | SEC_ALLOC | SEC_LOAD), /* section flags */ | |
202 | 0, /* leading underscore */ | |
203 | ' ', /* ar_pad_char */ | |
204 | 16, /* ar_max_namelen */ | |
205 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
206 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
207 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */ | |
208 | bfd_getl64, bfd_getl_signed_64, bfd_putl64, | |
209 | bfd_getl32, bfd_getl_signed_32, bfd_putl32, | |
210 | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */ | |
211 | ||
212 | { | |
213 | _bfd_dummy_target, | |
214 | _bfd_dummy_target, /* bfd_check_format */ | |
215 | _bfd_dummy_target, | |
216 | _bfd_dummy_target, | |
217 | }, | |
218 | { | |
219 | bfd_false, | |
220 | msdos_mkobject, | |
221 | _bfd_generic_mkarchive, | |
222 | bfd_false, | |
223 | }, | |
224 | { /* bfd_write_contents */ | |
225 | bfd_false, | |
226 | msdos_write_object_contents, | |
227 | _bfd_write_archive_contents, | |
228 | bfd_false, | |
229 | }, | |
230 | ||
231 | BFD_JUMP_TABLE_GENERIC (msdos), | |
232 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
233 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
234 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), | |
235 | BFD_JUMP_TABLE_SYMBOLS (msdos), | |
236 | BFD_JUMP_TABLE_RELOCS (msdos), | |
237 | BFD_JUMP_TABLE_WRITE (msdos), | |
238 | BFD_JUMP_TABLE_LINK (msdos), | |
239 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
240 | ||
241 | (PTR) 0 | |
242 | }; | |
243 | ||
244 |