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