Initial revision
[deliverable/binutils-gdb.git] / bfd / libaout.h
CommitLineData
69ebee86 1/* BFD back-end data structures for a.out (and similar) files.
9e2dad8e
JG
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
69ebee86 4
9e2dad8e 5This file is part of BFD, the Binary File Descriptor library.
69ebee86 6
9e2dad8e 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
9e2dad8e
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
9e2dad8e 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9e2dad8e
JG
18along with this program; if not, write to the Free Software
19Foundation, 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. */
4a81b561
DHW
24
25/* $Id$ */
26
c0e5039e
JG
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
359f1dee
JG
33/* Parameterize the a.out code based on whether it is being built
34 for a 32-bit architecture or a 64-bit architecture. */
c0e5039e
JG
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
9e2dad8e
JG
51/* Declare these types at file level, since they are used in parameter
52 lists, which have wierd scope. */
53struct external_exec;
54struct internal_exec;
55
0fa4f690
JG
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
62struct 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 >
823130292827262524232221201918171615141312111009080706050403020100
83< FLAGS >< MACHINE TYPE >< MAGIC NUMBER >
84*/
85enum 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
69ebee86 119typedef struct aout_symbol {
4a81b561
DHW
120 asymbol symbol;
121 short desc;
69ebee86
JG
122 char other;
123 unsigned char type;
4a81b561
DHW
124} aout_symbol_type;
125
0fa4f690
JG
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
69ebee86 130struct aoutdata {
0fa4f690 131 struct internal_exec *hdr; /* exec file header */
4a81b561 132 aout_symbol_type *symbols; /* symtab for input bfd */
4a81b561
DHW
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;
4a81b561 143
0fa4f690 144 /* Size of a relocation entry in external form */
69ebee86 145 unsigned reloc_entry_size;
4a81b561 146
0fa4f690
JG
147 /* Size of a symbol table entry in external form */
148 unsigned symbol_entry_size;
4a81b561 149
0fa4f690
JG
150 /* Page size - needed for alignment of demand paged files. */
151 unsigned long page_size;
4a81b561 152
0fa4f690
JG
153 /* Segment size - needed for alignment of demand paged files. */
154 unsigned long segment_size;
4a81b561 155
0fa4f690
JG
156 unsigned exec_bytes_size;
157};
4a81b561 158
0fa4f690
JG
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)
69ebee86 169
0fa4f690
JG
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))
69ebee86 173
c0e5039e 174/* Prototype declarations for functions defined in aoutx.h */
69ebee86 175
c0e5039e 176PROTO (boolean, NAME(aout,squirt_out_relocs),(bfd *abfd, asection *section));
69ebee86 177
c0e5039e 178PROTO (bfd_target *, NAME(aout,some_aout_object_p), (bfd *abfd,
0fa4f690 179 struct internal_exec *execp,
69ebee86 180 bfd_target *(*callback)()));
c0e5039e
JG
181PROTO (boolean, NAME(aout,mkobject), (bfd *abfd));
182PROTO (enum machine_type, NAME(aout,machine_type), (enum bfd_architecture arch,
69ebee86 183 unsigned long machine));
c0e5039e 184PROTO (boolean, NAME(aout,set_arch_mach), (bfd *abfd, enum bfd_architecture arch,
69ebee86
JG
185 unsigned long machine));
186
c0e5039e
JG
187PROTO (boolean, NAME(aout,new_section_hook), (bfd *abfd, asection *newsect));
188PROTO (boolean, NAME(aout,set_section_contents), (bfd *abfd, sec_ptr section,
189 PTR location, file_ptr offset, bfd_size_type count));
190
191PROTO (asymbol *,NAME(aout,make_empty_symbol), (bfd *abfd));
192PROTO (boolean, NAME(aout,slurp_symbol_table), (bfd *abfd));
193PROTO (void, NAME(aout,write_syms), (bfd *abfd));
194PROTO (void, NAME(aout,reclaim_symbol_table), (bfd *abfd));
195PROTO (unsigned int, NAME(aout,get_symtab_upper_bound), (bfd *abfd));
196PROTO (unsigned int, NAME(aout,get_symtab), (bfd *abfd, asymbol **location));
197PROTO (boolean, NAME(aout,slurp_reloc_table), (bfd *abfd, sec_ptr asect,
69ebee86 198 asymbol **symbols));
c0e5039e 199PROTO (unsigned int, NAME(aout,canonicalize_reloc), (bfd *abfd, sec_ptr section,
69ebee86 200 arelent **relptr, asymbol **symbols));
c0e5039e
JG
201PROTO (unsigned int, NAME(aout,get_reloc_upper_bound), (bfd *abfd, sec_ptr asect));
202PROTO (void, NAME(aout,reclaim_reloc), (bfd *ignore_abfd, sec_ptr ignore));
203PROTO (alent *, NAME(aout,get_lineno), (bfd *ignore_abfd, asymbol *ignore_symbol));
204PROTO (void, NAME(aout,print_symbol), (bfd *ignore_abfd, PTR file,
9e2dad8e 205 asymbol *symbol, bfd_print_symbol_type how));
c0e5039e
JG
206PROTO (boolean, NAME(aout,close_and_cleanup), (bfd *abfd));
207PROTO (boolean, NAME(aout,find_nearest_line), (bfd *abfd, asection *section,
69ebee86
JG
208 asymbol **symbols, bfd_vma offset, CONST char **filename_ptr,
209 CONST char **functionname_ptr, unsigned int *line_ptr));
c0e5039e 210PROTO (int, NAME(aout,sizeof_headers), (bfd *ignore_abfd, boolean exec));
69ebee86 211
69ebee86 212
c0e5039e
JG
213PROTO (void, NAME(aout,swap_exec_header_in), (bfd *abfd,
214 struct external_exec *raw_bytes, struct internal_exec *execp));
69ebee86 215
c0e5039e
JG
216PROTO (void, NAME(aout,swap_exec_header_out),(bfd *abfd, struct internal_exec *execp,
217 struct external_exec *raw_bytes));
69ebee86
JG
218
219/* A.out uses the generic versions of these routines... */
220
c0e5039e
JG
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) \
0fa4f690 229 /* The virtual memory addresses of the sections */ \
c0e5039e
JG
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 { \
0fa4f690
JG
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; \
c0e5039e
JG
256 N_SET_MAGIC (*execp, ZMAGIC); \
257 } \
9e2dad8e 258 else \
c0e5039e 259 { \
9e2dad8e
JG
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); } \
c0e5039e 265 } \
c0e5039e
JG
266 if (abfd->flags & D_PAGED) \
267 { \
0fa4f690
JG
268 data_pad = ALIGN(obj_datasec(abfd)->size, PAGE_SIZE) \
269 - obj_datasec(abfd)->size; \
c0e5039e
JG
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 \
0fa4f690 283 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
c0e5039e
JG
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 }
This page took 0.047352 seconds and 4 git commands to generate.