2001-02-17 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / aout-encap.c
blob5f8a511a72e06eae5079781293bcf5375d597297
1 /* BFD back-end for a.out files encapsulated with COFF headers.
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* THIS MODULE IS NOT FINISHED. IT PROBABLY DOESN'T EVEN COMPILE. */
22 #if 0
23 #define TARGET_PAGE_SIZE 4096
24 #define SEGMENT_SIZE TARGET_PAGE_SIZE
25 #define TEXT_START_ADDR 0
26 #define BYTES_IN_WORD 4
27 #endif
29 #include "bfd.h"
30 #include <sysdep.h>
31 #include "libbfd.h"
32 #include <aout/aout64.h>
33 #include "aout/stab_gnu.h"
34 #include "aout/ar.h"
35 #include "libaout.h" /* BFD a.out internal data structures */
37 const bfd_target *encap_real_callback ();
39 const bfd_target *
40 encap_object_p (abfd)
41 bfd *abfd;
43 unsigned char magicbuf[4]; /* Raw bytes of magic number from file */
44 unsigned long magic; /* Swapped magic number */
45 short coff_magic;
46 struct external_exec exec_bytes;
47 struct internal_exec exec;
49 if (bfd_read ((PTR)magicbuf, 1, sizeof (magicbuf), abfd) !=
50 sizeof (magicbuf))
52 if (bfd_get_error () != bfd_error_system_call)
53 bfd_set_error (bfd_error_wrong_format);
54 return 0;
57 coff_magic = bfd_h_get_16 (abfd, magicbuf);
58 if (coff_magic != COFF_MAGIC)
59 return 0; /* Not an encap coff file */
61 __header_offset_temp==COFF_MAGIC ? sizeof (struct coffheader) : 0)
62 (fseek ((f), HEADER_OFFSET((f)), 1))
64 magic = bfd_h_get_32 (abfd, magicbuf);
66 if (N_BADMAG (*((struct internal_exec *) &magic))) return 0;
68 struct external_exec exec_bytes;
69 if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
70 != EXEC_BYTES_SIZE) {
71 if (bfd_get_error () != bfd_error_system_call)
72 bfd_set_error (bfd_error_wrong_format);
73 return 0;
75 NAME(aout,swap_exec_header_in) (abfd, &exec_bytes, &exec);
77 return aout_32_some_aout_object_p (abfd, &exec, encap_realcallback);
80 /* Finish up the reading of a encapsulated-coff a.out file header */
81 const bfd_target *
82 encap_real_callback (abfd)
83 bfd *abfd;
85 struct internal_exec *execp = exec_hdr (abfd);
87 MY(callback) (abfd, execp);
89 /* If we have a coff header, it can give us better values for
90 text_start and exec_data_start. This is particularly useful
91 for remote debugging of embedded systems. */
92 if (N_FLAGS(exec_aouthdr) & N_FLAGS_COFF_ENCAPSULATE)
94 struct coffheader ch;
95 int val;
96 val = lseek (execchan, -(sizeof (AOUTHDR) + sizeof (ch)), 1);
97 if (val == -1)
98 perror_with_name (filename);
99 val = myread (execchan, &ch, sizeof (ch));
100 if (val < 0)
101 perror_with_name (filename);
102 text_start = ch.text_start;
103 exec_data_start = ch.data_start;
104 } else
106 text_start =
107 IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
108 exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
109 ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
112 /* Determine the architecture and machine type of the object file. */
113 bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0); /* FIXME */
115 return abfd->xvec;
118 /* Write an object file in Encapsulated COFF format.
119 Section contents have already been written. We write the
120 file header, symbols, and relocation. */
122 boolean
123 encap_write_object_contents (abfd)
124 bfd *abfd;
126 bfd_size_type data_pad = 0;
127 struct external_exec exec_bytes;
128 struct internal_exec *execp = exec_hdr (abfd);
130 /****** FIXME: Fragments from the old GNU LD program for dealing with
131 encap coff. */
132 struct coffheader coffheader;
133 int need_coff_header;
135 /* Determine whether to count the header as part of
136 the text size, and initialize the text size accordingly.
137 This depends on the kind of system and on the output format selected. */
139 N_SET_MAGIC (outheader, magic);
140 #ifdef INITIALIZE_HEADER
141 INITIALIZE_HEADER;
142 #endif
144 text_size = sizeof (struct exec);
145 #ifdef COFF_ENCAPSULATE
146 if (relocatable_output == 0 && file_table[0].just_syms_flag == 0)
148 need_coff_header = 1;
149 /* set this flag now, since it will change the values of N_TXTOFF, etc */
150 N_SET_FLAGS (outheader, aout_backend_info (abfd)->exec_hdr_flags);
151 text_size += sizeof (struct coffheader);
153 #endif
155 #ifdef COFF_ENCAPSULATE
156 if (need_coff_header)
158 /* We are encapsulating BSD format within COFF format. */
159 struct coffscn *tp, *dp, *bp;
161 tp = &coffheader.scns[0];
162 dp = &coffheader.scns[1];
163 bp = &coffheader.scns[2];
165 strcpy (tp->s_name, ".text");
166 tp->s_paddr = text_start;
167 tp->s_vaddr = text_start;
168 tp->s_size = text_size;
169 tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
170 tp->s_relptr = 0;
171 tp->s_lnnoptr = 0;
172 tp->s_nreloc = 0;
173 tp->s_nlnno = 0;
174 tp->s_flags = 0x20;
175 strcpy (dp->s_name, ".data");
176 dp->s_paddr = data_start;
177 dp->s_vaddr = data_start;
178 dp->s_size = data_size;
179 dp->s_scnptr = tp->s_scnptr + tp->s_size;
180 dp->s_relptr = 0;
181 dp->s_lnnoptr = 0;
182 dp->s_nreloc = 0;
183 dp->s_nlnno = 0;
184 dp->s_flags = 0x40;
185 strcpy (bp->s_name, ".bss");
186 bp->s_paddr = dp->s_vaddr + dp->s_size;
187 bp->s_vaddr = bp->s_paddr;
188 bp->s_size = bss_size;
189 bp->s_scnptr = 0;
190 bp->s_relptr = 0;
191 bp->s_lnnoptr = 0;
192 bp->s_nreloc = 0;
193 bp->s_nlnno = 0;
194 bp->s_flags = 0x80;
196 coffheader.f_magic = COFF_MAGIC;
197 coffheader.f_nscns = 3;
198 /* store an unlikely time so programs can
199 * tell that there is a bsd header
201 coffheader.f_timdat = 1;
202 coffheader.f_symptr = 0;
203 coffheader.f_nsyms = 0;
204 coffheader.f_opthdr = 28;
205 coffheader.f_flags = 0x103;
206 /* aouthdr */
207 coffheader.magic = ZMAGIC;
208 coffheader.vstamp = 0;
209 coffheader.tsize = tp->s_size;
210 coffheader.dsize = dp->s_size;
211 coffheader.bsize = bp->s_size;
212 coffheader.entry = outheader.a_entry;
213 coffheader.text_start = tp->s_vaddr;
214 coffheader.data_start = dp->s_vaddr;
216 #endif
218 #ifdef COFF_ENCAPSULATE
219 if (need_coff_header)
220 mywrite (&coffheader, sizeof coffheader, 1, outdesc);
221 #endif
223 #ifndef COFF_ENCAPSULATE
224 padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
225 #endif
227 text_size -= N_TXTOFF (outheader);
228 WRITE_HEADERS(abfd, execp);
229 return true;
232 #define MY_write_object_content encap_write_object_contents
233 #define MY_object_p encap_object_p
234 #define MY_exec_hdr_flags N_FLAGS_COFF_ENCAPSULATE
236 #include "aout-target.h"