merge from gcc
[binutils.git] / bfd / i386msdos.c
blob935431db69392189c08e07fd23d73b33466ae8f9
1 /* BFD back-end for MS-DOS executables.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
4 Written by Bryan Ford of the University of Utah.
6 Contributed by the Center for Software Science at the
7 University of Utah (pa-gdb-bugs@cs.utah.edu).
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "libbfd.h"
29 #include "libaout.h"
31 #define EXE_MAGIC 0x5a4d
32 #define EXE_LOAD_HIGH 0x0000
33 #define EXE_LOAD_LOW 0xffff
34 #define EXE_PAGE_SIZE 512
36 static int msdos_sizeof_headers
37 PARAMS ((bfd *, bfd_boolean));
38 static bfd_boolean msdos_write_object_contents
39 PARAMS ((bfd *));
40 static bfd_boolean msdos_set_section_contents
41 PARAMS ((bfd *, sec_ptr, const PTR, file_ptr, bfd_size_type));
43 static int
44 msdos_sizeof_headers (abfd, exec)
45 bfd *abfd ATTRIBUTE_UNUSED;
46 bfd_boolean exec ATTRIBUTE_UNUSED;
48 return 0;
51 static bfd_boolean
52 msdos_write_object_contents (abfd)
53 bfd *abfd;
55 static char hdr[EXE_PAGE_SIZE];
56 file_ptr outfile_size = sizeof(hdr);
57 bfd_vma high_vma = 0;
58 asection *sec;
60 /* Find the total size of the program on disk and in memory. */
61 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
63 if (sec->size == 0)
64 continue;
65 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
67 bfd_vma sec_vma = bfd_get_section_vma (abfd, sec) + sec->size;
68 if (sec_vma > high_vma)
69 high_vma = sec_vma;
71 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
73 file_ptr sec_end = (sizeof (hdr)
74 + bfd_get_section_vma (abfd, sec)
75 + sec->size);
76 if (sec_end > outfile_size)
77 outfile_size = sec_end;
81 /* Make sure the program isn't too big. */
82 if (high_vma > (bfd_vma)0xffff)
84 bfd_set_error(bfd_error_file_too_big);
85 return FALSE;
88 /* Constants. */
89 H_PUT_16 (abfd, EXE_MAGIC, &hdr[0]);
90 H_PUT_16 (abfd, EXE_PAGE_SIZE / 16, &hdr[8]);
91 H_PUT_16 (abfd, EXE_LOAD_LOW, &hdr[12]);
92 H_PUT_16 (abfd, 0x3e, &hdr[24]);
93 H_PUT_16 (abfd, 0x0001, &hdr[28]); /* XXX??? */
94 H_PUT_16 (abfd, 0x30fb, &hdr[30]); /* XXX??? */
95 H_PUT_16 (abfd, 0x726a, &hdr[32]); /* XXX??? */
97 /* Bytes in last page (0 = full page). */
98 H_PUT_16 (abfd, outfile_size & (EXE_PAGE_SIZE - 1), &hdr[2]);
100 /* Number of pages. */
101 H_PUT_16 (abfd, (outfile_size + EXE_PAGE_SIZE - 1) / EXE_PAGE_SIZE, &hdr[4]);
103 /* Set the initial stack pointer to the end of the bss.
104 The program's crt0 code must relocate it to a real stack. */
105 H_PUT_16 (abfd, high_vma, &hdr[16]);
107 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
108 || bfd_bwrite (hdr, (bfd_size_type) sizeof(hdr), abfd) != sizeof(hdr))
109 return FALSE;
111 return TRUE;
114 static bfd_boolean
115 msdos_set_section_contents (abfd, section, location, offset, count)
116 bfd *abfd;
117 sec_ptr section;
118 const PTR location;
119 file_ptr offset;
120 bfd_size_type count;
123 if (count == 0)
124 return TRUE;
126 section->filepos = EXE_PAGE_SIZE + bfd_get_section_vma (abfd, section);
128 if (bfd_get_section_flags (abfd, section) & SEC_LOAD)
130 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
131 || bfd_bwrite (location, count, abfd) != count)
132 return FALSE;
135 return TRUE;
140 #define msdos_mkobject aout_32_mkobject
141 #define msdos_make_empty_symbol aout_32_make_empty_symbol
142 #define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup
144 #define msdos_close_and_cleanup _bfd_generic_close_and_cleanup
145 #define msdos_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
146 #define msdos_new_section_hook _bfd_generic_new_section_hook
147 #define msdos_get_section_contents _bfd_generic_get_section_contents
148 #define msdos_get_section_contents_in_window \
149 _bfd_generic_get_section_contents_in_window
150 #define msdos_bfd_get_relocated_section_contents \
151 bfd_generic_get_relocated_section_contents
152 #define msdos_bfd_relax_section bfd_generic_relax_section
153 #define msdos_bfd_gc_sections bfd_generic_gc_sections
154 #define msdos_bfd_merge_sections bfd_generic_merge_sections
155 #define msdos_bfd_is_group_section bfd_generic_is_group_section
156 #define msdos_bfd_discard_group bfd_generic_discard_group
157 #define msdos_section_already_linked \
158 _bfd_generic_section_already_linked
159 #define msdos_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
160 #define msdos_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
161 #define msdos_bfd_link_add_symbols _bfd_generic_link_add_symbols
162 #define msdos_bfd_link_just_syms _bfd_generic_link_just_syms
163 #define msdos_bfd_final_link _bfd_generic_final_link
164 #define msdos_bfd_link_split_section _bfd_generic_link_split_section
165 #define msdos_set_arch_mach _bfd_generic_set_arch_mach
167 #define msdos_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound
168 #define msdos_canonicalize_symtab _bfd_nosymbols_canonicalize_symtab
169 #define msdos_print_symbol _bfd_nosymbols_print_symbol
170 #define msdos_get_symbol_info _bfd_nosymbols_get_symbol_info
171 #define msdos_find_nearest_line _bfd_nosymbols_find_nearest_line
172 #define msdos_get_lineno _bfd_nosymbols_get_lineno
173 #define msdos_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
174 #define msdos_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
175 #define msdos_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
176 #define msdos_read_minisymbols _bfd_nosymbols_read_minisymbols
177 #define msdos_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
179 #define msdos_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
180 #define msdos_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
181 #define msdos_32_bfd_link_split_section _bfd_generic_link_split_section
183 const bfd_target i386msdos_vec =
185 "msdos", /* name */
186 bfd_target_msdos_flavour,
187 BFD_ENDIAN_LITTLE, /* target byte order */
188 BFD_ENDIAN_LITTLE, /* target headers byte order */
189 (EXEC_P), /* object flags */
190 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS
191 | SEC_ALLOC | SEC_LOAD), /* section flags */
192 0, /* leading underscore */
193 ' ', /* ar_pad_char */
194 16, /* ar_max_namelen */
195 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
196 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
197 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
198 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
199 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
200 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
203 _bfd_dummy_target,
204 _bfd_dummy_target, /* bfd_check_format */
205 _bfd_dummy_target,
206 _bfd_dummy_target,
209 bfd_false,
210 msdos_mkobject,
211 _bfd_generic_mkarchive,
212 bfd_false,
214 { /* bfd_write_contents */
215 bfd_false,
216 msdos_write_object_contents,
217 _bfd_write_archive_contents,
218 bfd_false,
221 BFD_JUMP_TABLE_GENERIC (msdos),
222 BFD_JUMP_TABLE_COPY (_bfd_generic),
223 BFD_JUMP_TABLE_CORE (_bfd_nocore),
224 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
225 BFD_JUMP_TABLE_SYMBOLS (msdos),
226 BFD_JUMP_TABLE_RELOCS (msdos),
227 BFD_JUMP_TABLE_WRITE (msdos),
228 BFD_JUMP_TABLE_LINK (msdos),
229 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
231 NULL,
233 (PTR) 0