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