* host-aout.c: New strategy. We use common code, include files,
[external/binutils.git] / bfd / libaout.h
1 /* BFD back-end data structures for a.out (and similar) files.
2    Copyright (C) 1990-1991 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* We try to encapsulate the differences in the various a.out file
22    variants in a few routines, and otherwise share large masses of code.
23    This means we only have to fix bugs in one place, most of the time.  */
24
25 /* $Id$ */
26
27 #ifdef __STDC__
28 #define CAT3(a,b,c) a##b##c
29 #else
30 #define CAT3(a,b,c) a/**/b/**/c
31 #endif
32
33 /* Parameterize the a.out code based on whether it is being built
34    for a 32-bit architecture or a 64-bit architecture.  */
35 #if ARCH_SIZE==64
36 #define GET_WORD bfd_h_get_64
37 #define GET_SWORD (int64_type)GET_WORD
38 #define PUT_WORD bfd_h_put_64
39 #define NAME(x,y) CAT3(x,_64_,y)
40 #define JNAME(x) CAT(x,_64)
41 #define BYTES_IN_WORD 8
42 #else
43 #define GET_WORD bfd_h_get_32
44 #define GET_SWORD (int32_type)GET_WORD
45 #define PUT_WORD bfd_h_put_32
46 #define NAME(x,y) CAT3(x,_32_,y)
47 #define JNAME(x) CAT(x,_32)
48 #define BYTES_IN_WORD 4
49 #endif
50
51 /* Declare these types at file level, since they are used in parameter
52    lists, which have wierd scope.  */
53 struct external_exec;
54 struct internal_exec;
55
56 /* This is the layout in memory of a "struct exec" while we process it.
57    All 'lengths' are given as a number of bytes.
58    All 'alignments' are for relinkable files only;  an alignment of
59         'n' indicates the corresponding segment must begin at an
60         address that is a multiple of (2**n).  */
61
62 struct internal_exec
63 {
64     long a_info;                /* Magic number and flags, packed */
65     bfd_vma a_text;             /* length of text, in bytes  */
66     bfd_vma a_data;             /* length of data, in bytes  */
67     bfd_vma a_bss;              /* length of uninitialized data area in mem */
68     bfd_vma a_syms;             /* length of symbol table data in file */
69     bfd_vma a_entry;            /* start address */
70     bfd_vma a_trsize;           /* length of text's relocation info, in bytes */
71     bfd_vma a_drsize;           /* length of data's relocation info, in bytes */
72     /* Added for i960 */
73     bfd_vma a_tload;            /* Text runtime load address */
74     bfd_vma a_dload;            /* Data runtime load address */
75     unsigned char a_talign;     /* Alignment of text segment */
76     unsigned char a_dalign;     /* Alignment of data segment */
77     unsigned char a_balign;     /* Alignment of bss segment */
78 };
79
80 /* Magic number is written 
81 < MSB        >
82 3130292827262524232221201918171615141312111009080706050403020100
83 < FLAGS      >< MACHINE TYPE ><  MAGIC NUMBER                  >
84 */
85 enum machine_type {
86   M_UNKNOWN = 0,
87   M_68010 = 1,
88   M_68020 = 2,
89   M_SPARC = 3,
90   /* skip a bunch so we dont run into any of suns numbers */
91   M_386 = 100,
92   M_29K = 101,
93   M_HP200 = 200,        /* HP 200 (68010) BSD binary */
94   M_HP300 = (300 % 256), /* HP 300 (68020+68881) BSD binary */
95   M_HPUX = (0x20c % 256),/* HP 200/300 HPUX binary */
96 };
97
98 #define N_DYNAMIC(exec) ((exec).a_info & 0x8000000)
99
100 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
101 #define N_MACHTYPE(exec) ((enum machine_type)(((exec).a_info >> 16) & 0xff))
102 #define N_FLAGS(exec) (((exec).a_info >> 24) & 0xff)
103 #define N_SET_INFO(exec, magic, type, flags) \
104 ((exec).a_info = ((magic) & 0xffff) \
105  | (((int)(type) & 0xff) << 16) \
106  | (((flags) & 0xff) << 24))
107
108 #define N_SET_MAGIC(exec, magic) \
109 ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
110
111 #define N_SET_MACHTYPE(exec, machtype) \
112 ((exec).a_info = \
113  ((exec).a_info&0xff00ffff) | ((((int)(machtype))&0xff) << 16))
114
115 #define N_SET_FLAGS(exec, flags) \
116 ((exec).a_info = \
117  ((exec).a_info&0x00ffffff) | (((flags) & 0xff) << 24))
118
119 typedef struct aout_symbol {
120   asymbol symbol;
121   short desc;
122   char other;
123   unsigned char type;
124 } aout_symbol_type;
125
126 /* The `tdata' struct for all a.out-like object file formats.
127    Various things depend on this struct being around any time an a.out
128    file is being handled.  An example is dbxread.c in GDB.  */
129
130 struct aoutdata {
131   struct internal_exec *hdr;            /* exec file header */
132   aout_symbol_type *symbols;            /* symtab for input bfd */
133   
134   /* For ease, we do this */
135   asection *textsec;
136   asection *datasec;
137   asection *bsssec;
138
139   /* We remember these offsets so that after check_file_format, we have
140      no dependencies on the particular format of the exec_hdr.  */
141   file_ptr sym_filepos;
142   file_ptr str_filepos;
143
144   /* Size of a relocation entry in external form */
145   unsigned reloc_entry_size;
146
147   /* Size of a symbol table entry in external form */
148   unsigned symbol_entry_size;
149
150   /* Page size - needed for alignment of demand paged files. */
151   unsigned long page_size;
152
153   /* Segment size - needed for alignment of demand paged files. */
154   unsigned long segment_size;
155
156   unsigned exec_bytes_size;
157 };
158
159 #define adata(bfd)              ((struct aoutdata *) ((bfd)->tdata))
160 #define exec_hdr(bfd)           (adata(bfd)->hdr)
161 #define obj_aout_symbols(bfd)   (adata(bfd)->symbols)
162 #define obj_textsec(bfd)        (adata(bfd)->textsec)
163 #define obj_datasec(bfd)        (adata(bfd)->datasec)
164 #define obj_bsssec(bfd)         (adata(bfd)->bsssec)
165 #define obj_sym_filepos(bfd)    (adata(bfd)->sym_filepos)
166 #define obj_str_filepos(bfd)    (adata(bfd)->str_filepos)
167 #define obj_reloc_entry_size(bfd) (adata(bfd)->reloc_entry_size)
168 #define obj_symbol_entry_size(bfd) (adata(bfd)->symbol_entry_size)
169
170 /* We take the address of the first element of an asymbol to ensure that the
171    macro is only ever applied to an asymbol */
172 #define aout_symbol(asymbol) ((aout_symbol_type *)(&(asymbol)->the_bfd))
173
174 /* Prototype declarations for functions defined in aoutx.h  */
175
176 PROTO (boolean, NAME(aout,squirt_out_relocs),(bfd *abfd, asection *section));
177
178 PROTO (bfd_target *, NAME(aout,some_aout_object_p), (bfd *abfd,
179                                           struct internal_exec *execp,
180                                           bfd_target *(*callback)()));
181 PROTO (boolean, NAME(aout,mkobject), (bfd *abfd));
182 PROTO (enum machine_type, NAME(aout,machine_type), (enum bfd_architecture arch,
183                                                 unsigned long machine));
184 PROTO (boolean, NAME(aout,set_arch_mach), (bfd *abfd, enum bfd_architecture arch,
185                                                 unsigned long machine));
186
187 PROTO (boolean, NAME(aout,new_section_hook), (bfd *abfd, asection *newsect));
188 PROTO (boolean, NAME(aout,set_section_contents), (bfd *abfd, sec_ptr section,
189                          PTR location, file_ptr offset, bfd_size_type count));
190
191 PROTO (asymbol *,NAME(aout,make_empty_symbol), (bfd *abfd));
192 PROTO (boolean, NAME(aout,slurp_symbol_table), (bfd *abfd));
193 PROTO (void,    NAME(aout,write_syms), (bfd *abfd));
194 PROTO (void,    NAME(aout,reclaim_symbol_table), (bfd *abfd));
195 PROTO (unsigned int, NAME(aout,get_symtab_upper_bound), (bfd *abfd));
196 PROTO (unsigned int, NAME(aout,get_symtab), (bfd *abfd, asymbol **location));
197 PROTO (boolean, NAME(aout,slurp_reloc_table), (bfd *abfd, sec_ptr asect,
198                                          asymbol **symbols));
199 PROTO (unsigned int, NAME(aout,canonicalize_reloc), (bfd *abfd, sec_ptr section,
200                                          arelent **relptr, asymbol **symbols));
201 PROTO (unsigned int, NAME(aout,get_reloc_upper_bound), (bfd *abfd, sec_ptr asect));
202 PROTO (void,    NAME(aout,reclaim_reloc), (bfd *ignore_abfd, sec_ptr ignore));
203 PROTO (alent *, NAME(aout,get_lineno), (bfd *ignore_abfd, asymbol *ignore_symbol));
204 PROTO (void,    NAME(aout,print_symbol), (bfd *ignore_abfd, PTR file,
205                             asymbol *symbol, bfd_print_symbol_type how));
206 PROTO (boolean, NAME(aout,close_and_cleanup), (bfd *abfd));
207 PROTO (boolean, NAME(aout,find_nearest_line), (bfd *abfd, asection *section,
208       asymbol **symbols, bfd_vma offset, CONST char **filename_ptr,
209       CONST char **functionname_ptr, unsigned int *line_ptr));
210 PROTO (int,     NAME(aout,sizeof_headers), (bfd *ignore_abfd, boolean exec));
211
212
213 PROTO (void,    NAME(aout,swap_exec_header_in), (bfd *abfd,
214                          struct external_exec *raw_bytes, struct internal_exec *execp));
215
216 PROTO (void,    NAME(aout,swap_exec_header_out),(bfd *abfd, struct internal_exec *execp,
217                          struct external_exec *raw_bytes));
218
219 /* A.out uses the generic versions of these routines... */
220
221 #define aout_32_get_section_contents    bfd_generic_get_section_contents
222 #define aout_32_close_and_cleanup       bfd_generic_close_and_cleanup
223
224 #define aout_64_get_section_contents    bfd_generic_get_section_contents
225 #define aout_64_close_and_cleanup       bfd_generic_close_and_cleanup
226
227 /* Calculate the file positions of the parts of a newly read aout header */
228 #define WORK_OUT_FILE_POSITIONS(abfd, execp)                            \
229   /* The virtual memory addresses of the sections */                    \
230   obj_datasec (abfd)->vma = N_DATADDR(*execp);                          \
231   obj_bsssec (abfd)->vma = N_BSSADDR(*execp);                           \
232   obj_textsec (abfd)->vma = N_TXTADDR(*execp);                          \
233                                                                         \
234   /* The file offsets of the sections */                                \
235   obj_textsec (abfd)->filepos = N_TXTOFF (*execp);                      \
236   obj_datasec (abfd)->filepos = N_DATOFF (*execp);                      \
237                                                                         \
238   /* The file offsets of the relocation info */                         \
239   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);                  \
240   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);                  \
241                                                                         \
242   /* The file offsets of the string table and symbol table.  */         \
243   obj_sym_filepos (abfd) = N_SYMOFF (*execp);                           \
244   obj_str_filepos (abfd) = N_STROFF (*execp);                           \
245
246
247 #define WRITE_HEADERS(abfd, execp)                                            \
248       {                                                                       \
249         if (abfd->flags & D_PAGED)                                            \
250             {                                                                 \
251               execp->a_text = obj_textsec (abfd)->size;                       \
252               /* Kludge to distinguish old- and new-style ZMAGIC.             \
253                  The latter includes the exec header in the text size. */     \
254               if (obj_textsec(abfd)->filepos == EXEC_BYTES_SIZE)              \
255                 execp->a_text += EXEC_BYTES_SIZE;                             \
256               N_SET_MAGIC (*execp, ZMAGIC);                                   \
257             }                                                                 \
258         else                                                                  \
259             {                                                                 \
260               execp->a_text = obj_textsec (abfd)->size;                       \
261               if (abfd->flags & WP_TEXT)                                      \
262                 { N_SET_MAGIC (*execp, NMAGIC); }                             \
263               else                                                            \
264                 { N_SET_MAGIC(*execp, OMAGIC); }                              \
265             }                                                                 \
266         if (abfd->flags & D_PAGED)                                            \
267             {                                                                 \
268               data_pad = ALIGN(obj_datasec(abfd)->size, PAGE_SIZE)            \
269                   - obj_datasec(abfd)->size;                                  \
270                                                                               \
271               if (data_pad > obj_bsssec(abfd)->size)                          \
272                 execp->a_bss = 0;                                             \
273               else                                                            \
274                 execp->a_bss = obj_bsssec(abfd)->size - data_pad;             \
275               execp->a_data = obj_datasec(abfd)->size + data_pad;             \
276             }                                                                 \
277         else                                                                  \
278             {                                                                 \
279               execp->a_data = obj_datasec (abfd)->size;                       \
280               execp->a_bss = obj_bsssec (abfd)->size;                         \
281             }                                                                 \
282                                                                               \
283         execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;        \
284         execp->a_entry = bfd_get_start_address (abfd);                        \
285                                                                               \
286         execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *                \
287                            obj_reloc_entry_size (abfd));                      \
288         execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *                \
289                            obj_reloc_entry_size (abfd));                      \
290         NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes);           \
291                                                                               \
292         bfd_seek (abfd, 0L, false);                                           \
293         bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);              \
294         /* Now write out reloc info, followed by syms and strings */          \
295                                                                               \
296         if (bfd_get_symcount (abfd) != 0)                                     \
297             {                                                                 \
298               bfd_seek (abfd,                                                 \
299                         (long)(N_SYMOFF(*execp)), false);                     \
300                                                                               \
301               NAME(aout,write_syms)(abfd);                                    \
302                                                                               \
303               bfd_seek (abfd,   (long)(N_TRELOFF(*execp)), false);            \
304                                                                               \
305               if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) return false; \
306               bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false);              \
307                                                                               \
308               if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) return false; \
309             }                                                                 \
310       }