(init_os): Make sure that the newly allocated userdata structure is zeroed out.
[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,
72adc230 3 2003, 2004 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
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24
25
26#include "bfd.h"
27#include "sysdep.h"
28#include "libbfd.h"
29#include "libaout.h"
30
31#if 0
32struct exe_header
33{
e4b17274
NC
34 unsigned short magic;
35 unsigned short bytes_in_last_page;
36 unsigned short npages; /* number of 512-byte "pages" including this header */
37 unsigned short nrelocs;
38 unsigned short header_paras; /* number of 16-byte paragraphs in header */
39 unsigned short reserved;
40 unsigned short load_switch;
41 unsigned short ss_ofs;
42 unsigned short sp;
43 unsigned short checksum;
44 unsigned short ip;
45 unsigned short cs_ofs;
46 unsigned short reloc_ofs;
47 unsigned short reserved2;
48 unsigned short something1;
49 unsigned short something2;
50 unsigned short something3;
252b5132
RH
51};
52#endif
53
54#define EXE_MAGIC 0x5a4d
55#define EXE_LOAD_HIGH 0x0000
56#define EXE_LOAD_LOW 0xffff
57#define EXE_PAGE_SIZE 512
58
b34976b6
AM
59static int msdos_sizeof_headers
60 PARAMS ((bfd *, bfd_boolean));
61static bfd_boolean msdos_write_object_contents
62 PARAMS ((bfd *));
63static bfd_boolean msdos_set_section_contents
0f867abe 64 PARAMS ((bfd *, sec_ptr, const PTR, file_ptr, bfd_size_type));
252b5132
RH
65
66static int
67msdos_sizeof_headers (abfd, exec)
5f771d47 68 bfd *abfd ATTRIBUTE_UNUSED;
b34976b6 69 bfd_boolean exec ATTRIBUTE_UNUSED;
252b5132
RH
70{
71 return 0;
72}
73
b34976b6 74static bfd_boolean
252b5132
RH
75msdos_write_object_contents (abfd)
76 bfd *abfd;
77{
78 static char hdr[EXE_PAGE_SIZE];
79 file_ptr outfile_size = sizeof(hdr);
80 bfd_vma high_vma = 0;
81 asection *sec;
82
83 /* Find the total size of the program on disk and in memory. */
84 for (sec = abfd->sections; sec != (asection *) NULL; sec = sec->next)
85 {
eea6121a 86 if (sec->size == 0)
252b5132
RH
87 continue;
88 if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
89 {
eea6121a 90 bfd_vma sec_vma = bfd_get_section_vma (abfd, sec) + sec->size;
252b5132
RH
91 if (sec_vma > high_vma)
92 high_vma = sec_vma;
93 }
94 if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
95 {
911d08a7
AM
96 file_ptr sec_end = (sizeof (hdr)
97 + bfd_get_section_vma (abfd, sec)
eea6121a 98 + sec->size);
252b5132
RH
99 if (sec_end > outfile_size)
100 outfile_size = sec_end;
101 }
102 }
103
104 /* Make sure the program isn't too big. */
105 if (high_vma > (bfd_vma)0xffff)
106 {
107 bfd_set_error(bfd_error_file_too_big);
b34976b6 108 return FALSE;
252b5132
RH
109 }
110
e4b17274 111 /* Constants. */
dc810e39
AM
112 H_PUT_16 (abfd, EXE_MAGIC, &hdr[0]);
113 H_PUT_16 (abfd, EXE_PAGE_SIZE / 16, &hdr[8]);
114 H_PUT_16 (abfd, EXE_LOAD_LOW, &hdr[12]);
115 H_PUT_16 (abfd, 0x3e, &hdr[24]);
116 H_PUT_16 (abfd, 0x0001, &hdr[28]); /* XXX??? */
117 H_PUT_16 (abfd, 0x30fb, &hdr[30]); /* XXX??? */
118 H_PUT_16 (abfd, 0x726a, &hdr[32]); /* XXX??? */
252b5132 119
e4b17274 120 /* Bytes in last page (0 = full page). */
dc810e39 121 H_PUT_16 (abfd, outfile_size & (EXE_PAGE_SIZE - 1), &hdr[2]);
252b5132 122
e4b17274 123 /* Number of pages. */
dc810e39 124 H_PUT_16 (abfd, (outfile_size + EXE_PAGE_SIZE - 1) / EXE_PAGE_SIZE, &hdr[4]);
252b5132
RH
125
126 /* Set the initial stack pointer to the end of the bss.
127 The program's crt0 code must relocate it to a real stack. */
dc810e39 128 H_PUT_16 (abfd, high_vma, &hdr[16]);
252b5132
RH
129
130 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
dc810e39 131 || bfd_bwrite (hdr, (bfd_size_type) sizeof(hdr), abfd) != sizeof(hdr))
b34976b6 132 return FALSE;
252b5132 133
b34976b6 134 return TRUE;
252b5132
RH
135}
136
b34976b6 137static bfd_boolean
252b5132
RH
138msdos_set_section_contents (abfd, section, location, offset, count)
139 bfd *abfd;
140 sec_ptr section;
0f867abe 141 const PTR location;
252b5132
RH
142 file_ptr offset;
143 bfd_size_type count;
144{
145
146 if (count == 0)
b34976b6 147 return TRUE;
252b5132
RH
148
149 section->filepos = EXE_PAGE_SIZE + bfd_get_section_vma (abfd, section);
150
151 if (bfd_get_section_flags (abfd, section) & SEC_LOAD)
152 {
dc810e39
AM
153 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
154 || bfd_bwrite (location, count, abfd) != count)
b34976b6 155 return FALSE;
252b5132
RH
156 }
157
b34976b6 158 return TRUE;
252b5132
RH
159}
160
161
162
163#define msdos_mkobject aout_32_mkobject
164#define msdos_make_empty_symbol aout_32_make_empty_symbol
165#define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup
166
167#define msdos_close_and_cleanup _bfd_generic_close_and_cleanup
168#define msdos_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
169#define msdos_new_section_hook _bfd_generic_new_section_hook
170#define msdos_get_section_contents _bfd_generic_get_section_contents
171#define msdos_get_section_contents_in_window \
172 _bfd_generic_get_section_contents_in_window
173#define msdos_bfd_get_relocated_section_contents \
174 bfd_generic_get_relocated_section_contents
175#define msdos_bfd_relax_section bfd_generic_relax_section
176#define msdos_bfd_gc_sections bfd_generic_gc_sections
8550eb6e 177#define msdos_bfd_merge_sections bfd_generic_merge_sections
72adc230 178#define msdos_bfd_is_group_section bfd_generic_is_group_section
e61463e1 179#define msdos_bfd_discard_group bfd_generic_discard_group
252b5132 180#define msdos_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
e2d34d7d 181#define msdos_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
252b5132 182#define msdos_bfd_link_add_symbols _bfd_generic_link_add_symbols
2d653fc7 183#define msdos_bfd_link_just_syms _bfd_generic_link_just_syms
252b5132
RH
184#define msdos_bfd_final_link _bfd_generic_final_link
185#define msdos_bfd_link_split_section _bfd_generic_link_split_section
186#define msdos_set_arch_mach _bfd_generic_set_arch_mach
187
188#define msdos_get_symtab_upper_bound _bfd_nosymbols_get_symtab_upper_bound
6cee3f79 189#define msdos_canonicalize_symtab _bfd_nosymbols_canonicalize_symtab
252b5132
RH
190#define msdos_print_symbol _bfd_nosymbols_print_symbol
191#define msdos_get_symbol_info _bfd_nosymbols_get_symbol_info
192#define msdos_find_nearest_line _bfd_nosymbols_find_nearest_line
193#define msdos_get_lineno _bfd_nosymbols_get_lineno
194#define msdos_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
195#define msdos_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
196#define msdos_read_minisymbols _bfd_nosymbols_read_minisymbols
197#define msdos_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
198
199#define msdos_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
200#define msdos_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
201#define msdos_32_bfd_link_split_section _bfd_generic_link_split_section
202
203const bfd_target i386msdos_vec =
252b5132 204 {
e4b17274
NC
205 "msdos", /* name */
206 bfd_target_msdos_flavour,
207 BFD_ENDIAN_LITTLE, /* target byte order */
208 BFD_ENDIAN_LITTLE, /* target headers byte order */
209 (EXEC_P), /* object flags */
210 (SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS
211 | SEC_ALLOC | SEC_LOAD), /* section flags */
212 0, /* leading underscore */
213 ' ', /* ar_pad_char */
214 16, /* ar_max_namelen */
215 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
216 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
217 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
218 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
219 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
220 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
221
222 {
223 _bfd_dummy_target,
224 _bfd_dummy_target, /* bfd_check_format */
225 _bfd_dummy_target,
226 _bfd_dummy_target,
227 },
228 {
229 bfd_false,
230 msdos_mkobject,
231 _bfd_generic_mkarchive,
232 bfd_false,
233 },
234 { /* bfd_write_contents */
235 bfd_false,
236 msdos_write_object_contents,
237 _bfd_write_archive_contents,
238 bfd_false,
239 },
240
241 BFD_JUMP_TABLE_GENERIC (msdos),
242 BFD_JUMP_TABLE_COPY (_bfd_generic),
243 BFD_JUMP_TABLE_CORE (_bfd_nocore),
244 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
245 BFD_JUMP_TABLE_SYMBOLS (msdos),
246 BFD_JUMP_TABLE_RELOCS (msdos),
247 BFD_JUMP_TABLE_WRITE (msdos),
248 BFD_JUMP_TABLE_LINK (msdos),
249 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
250
251 NULL,
dc810e39 252
e4b17274
NC
253 (PTR) 0
254 };
252b5132
RH
255
256
This page took 0.287389 seconds and 4 git commands to generate.