bfd/
[deliverable/binutils-gdb.git] / bfd / i386msdos.c
CommitLineData
252b5132 1/* BFD back-end for MS-DOS executables.
0f867abe 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002,
a6b96beb 3 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
252b5132
RH
4 Written by Bryan Ford of the University of Utah.
5
6 Contributed by the Center for Software Science at the
7 University of Utah (pa-gdb-bugs@cs.utah.edu).
8
9 This file is part of BFD, the Binary File Descriptor library.
10
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.
15
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.
20
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
3e110533 23 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
24
25
26#include "bfd.h"
27#include "sysdep.h"
28#include "libbfd.h"
29#include "libaout.h"
30
252b5132
RH
31#define EXE_MAGIC 0x5a4d
32#define EXE_LOAD_HIGH 0x0000
33#define EXE_LOAD_LOW 0xffff
34#define EXE_PAGE_SIZE 512
35
252b5132 36static int
a6b96beb
AM
37msdos_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
38 struct bfd_link_info *info ATTRIBUTE_UNUSED)
252b5132
RH
39{
40 return 0;
41}
42
b34976b6 43static bfd_boolean
a6b96beb 44msdos_write_object_contents (bfd *abfd)
252b5132
RH
45{
46 static char hdr[EXE_PAGE_SIZE];
47 file_ptr outfile_size = sizeof(hdr);
48 bfd_vma high_vma = 0;
49 asection *sec;
50
51 /* Find the total size of the program on disk and in memory. */
52 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
53 {
eea6121a 54 if (sec->size == 0)
252b5132
RH
55 continue;
56 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
57 {
eea6121a 58 bfd_vma sec_vma = bfd_get_section_vma (abfd, sec) + sec->size;
252b5132
RH
59 if (sec_vma > high_vma)
60 high_vma = sec_vma;
61 }
62 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
63 {
911d08a7
AM
64 file_ptr sec_end = (sizeof (hdr)
65 + bfd_get_section_vma (abfd, sec)
eea6121a 66 + sec->size);
252b5132
RH
67 if (sec_end > outfile_size)
68 outfile_size = sec_end;
69 }
70 }
71
72 /* Make sure the program isn't too big. */
73 if (high_vma > (bfd_vma)0xffff)
74 {
75 bfd_set_error(bfd_error_file_too_big);
b34976b6 76 return FALSE;
252b5132
RH
77 }
78
e4b17274 79 /* Constants. */
dc810e39
AM
80 H_PUT_16 (abfd, EXE_MAGIC, &hdr[0]);
81 H_PUT_16 (abfd, EXE_PAGE_SIZE / 16, &hdr[8]);
82 H_PUT_16 (abfd, EXE_LOAD_LOW, &hdr[12]);
83 H_PUT_16 (abfd, 0x3e, &hdr[24]);
84 H_PUT_16 (abfd, 0x0001, &hdr[28]); /* XXX??? */
85 H_PUT_16 (abfd, 0x30fb, &hdr[30]); /* XXX??? */
86 H_PUT_16 (abfd, 0x726a, &hdr[32]); /* XXX??? */
252b5132 87
e4b17274 88 /* Bytes in last page (0 = full page). */
dc810e39 89 H_PUT_16 (abfd, outfile_size & (EXE_PAGE_SIZE - 1), &hdr[2]);
252b5132 90
e4b17274 91 /* Number of pages. */
dc810e39 92 H_PUT_16 (abfd, (outfile_size + EXE_PAGE_SIZE - 1) / EXE_PAGE_SIZE, &hdr[4]);
252b5132
RH
93
94 /* Set the initial stack pointer to the end of the bss.
95 The program's crt0 code must relocate it to a real stack. */
dc810e39 96 H_PUT_16 (abfd, high_vma, &hdr[16]);
252b5132
RH
97
98 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
dc810e39 99 || bfd_bwrite (hdr, (bfd_size_type) sizeof(hdr), abfd) != sizeof(hdr))
b34976b6 100 return FALSE;
252b5132 101
b34976b6 102 return TRUE;
252b5132
RH
103}
104
b34976b6 105static bfd_boolean
a6b96beb
AM
106msdos_set_section_contents (bfd *abfd,
107 sec_ptr section,
108 const void *location,
109 file_ptr offset,
110 bfd_size_type count)
252b5132
RH
111{
112
113 if (count == 0)
b34976b6 114 return TRUE;
252b5132
RH
115
116 section->filepos = EXE_PAGE_SIZE + bfd_get_section_vma (abfd, section);
117
118 if (bfd_get_section_flags (abfd, section) & SEC_LOAD)
119 {
dc810e39
AM
120 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
121 || bfd_bwrite (location, count, abfd) != count)
b34976b6 122 return FALSE;
252b5132
RH
123 }
124
b34976b6 125 return TRUE;
252b5132
RH
126}
127
128
129
130#define msdos_mkobject aout_32_mkobject
131#define msdos_make_empty_symbol aout_32_make_empty_symbol
132#define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup
133
134#define msdos_close_and_cleanup _bfd_generic_close_and_cleanup
135#define msdos_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
136#define msdos_new_section_hook _bfd_generic_new_section_hook
137#define msdos_get_section_contents _bfd_generic_get_section_contents
138#define msdos_get_section_contents_in_window \
139 _bfd_generic_get_section_contents_in_window
140#define msdos_bfd_get_relocated_section_contents \
141 bfd_generic_get_relocated_section_contents
142#define msdos_bfd_relax_section bfd_generic_relax_section
143#define msdos_bfd_gc_sections bfd_generic_gc_sections
8550eb6e 144#define msdos_bfd_merge_sections bfd_generic_merge_sections
72adc230 145#define msdos_bfd_is_group_section bfd_generic_is_group_section
e61463e1 146#define msdos_bfd_discard_group bfd_generic_discard_group
082b7297
L
147#define msdos_section_already_linked \
148 _bfd_generic_section_already_linked
252b5132 149#define msdos_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
e2d34d7d 150#define msdos_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
252b5132 151#define msdos_bfd_link_add_symbols _bfd_generic_link_add_symbols
2d653fc7 152#define msdos_bfd_link_just_syms _bfd_generic_link_just_syms
252b5132
RH
153#define msdos_bfd_final_link _bfd_generic_final_link
154#define msdos_bfd_link_split_section _bfd_generic_link_split_section
155#define msdos_set_arch_mach _bfd_generic_set_arch_mach
156
157#define msdos_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound
6cee3f79 158#define msdos_canonicalize_symtab _bfd_nosymbols_canonicalize_symtab
252b5132
RH
159#define msdos_print_symbol _bfd_nosymbols_print_symbol
160#define msdos_get_symbol_info _bfd_nosymbols_get_symbol_info
161#define msdos_find_nearest_line _bfd_nosymbols_find_nearest_line
4ab527b0 162#define msdos_find_inliner_info _bfd_nosymbols_find_inliner_info
252b5132 163#define msdos_get_lineno _bfd_nosymbols_get_lineno
3c9458e9 164#define msdos_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
252b5132
RH
165#define msdos_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
166#define msdos_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
167#define msdos_read_minisymbols _bfd_nosymbols_read_minisymbols
168#define msdos_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
169
170#define msdos_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
171#define msdos_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
172#define msdos_32_bfd_link_split_section _bfd_generic_link_split_section
173
174const bfd_target i386msdos_vec =
252b5132 175 {
e4b17274
NC
176 "msdos", /* name */
177 bfd_target_msdos_flavour,
178 BFD_ENDIAN_LITTLE, /* target byte order */
179 BFD_ENDIAN_LITTLE, /* target headers byte order */
180 (EXEC_P), /* object flags */
181 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS
182 | SEC_ALLOC | SEC_LOAD), /* section flags */
183 0, /* leading underscore */
184 ' ', /* ar_pad_char */
185 16, /* ar_max_namelen */
186 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
187 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
188 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
189 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
190 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
191 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
192
193 {
194 _bfd_dummy_target,
195 _bfd_dummy_target, /* bfd_check_format */
196 _bfd_dummy_target,
197 _bfd_dummy_target,
198 },
199 {
200 bfd_false,
201 msdos_mkobject,
202 _bfd_generic_mkarchive,
203 bfd_false,
204 },
205 { /* bfd_write_contents */
206 bfd_false,
207 msdos_write_object_contents,
208 _bfd_write_archive_contents,
209 bfd_false,
210 },
211
212 BFD_JUMP_TABLE_GENERIC (msdos),
213 BFD_JUMP_TABLE_COPY (_bfd_generic),
214 BFD_JUMP_TABLE_CORE (_bfd_nocore),
215 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
216 BFD_JUMP_TABLE_SYMBOLS (msdos),
217 BFD_JUMP_TABLE_RELOCS (msdos),
218 BFD_JUMP_TABLE_WRITE (msdos),
219 BFD_JUMP_TABLE_LINK (msdos),
220 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
221
222 NULL,
dc810e39 223
e4b17274
NC
224 (PTR) 0
225 };
252b5132
RH
226
227
This page took 0.383781 seconds and 4 git commands to generate.