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.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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. */
28 #define CAT3(a,b,c) a##b##c
30 #define CAT3(a,b,c) a/**/b/**/c
33 /* Parameterize the a.out code based on whether it is being built
34 for a 32-bit architecture or a 64-bit architecture. */
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
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
51 /* Declare these types at file level, since they are used in parameter
52 lists, which have wierd scope. */
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). */
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 */
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 */
80 /* Magic number is written
82 3130292827262524232221201918171615141312111009080706050403020100
83 < FLAGS >< MACHINE TYPE >< MAGIC NUMBER >
90 /* skip a bunch so we dont run into any of suns numbers */
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 */
98 #define N_DYNAMIC(exec) ((exec).a_info & 0x8000000)
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))
108 #define N_SET_MAGIC(exec, magic) \
109 ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
111 #define N_SET_MACHTYPE(exec, machtype) \
113 ((exec).a_info&0xff00ffff) | ((((int)(machtype))&0xff) << 16))
115 #define N_SET_FLAGS(exec, flags) \
117 ((exec).a_info&0x00ffffff) | (((flags) & 0xff) << 24))
119 typedef struct aout_symbol {
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. */
131 struct internal_exec *hdr; /* exec file header */
132 aout_symbol_type *symbols; /* symtab for input bfd */
134 /* For ease, we do this */
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;
144 /* Size of a relocation entry in external form */
145 unsigned reloc_entry_size;
147 /* Size of a symbol table entry in external form */
148 unsigned symbol_entry_size;
150 /* Page size - needed for alignment of demand paged files. */
151 unsigned long page_size;
153 /* Segment size - needed for alignment of demand paged files. */
154 unsigned long segment_size;
156 unsigned exec_bytes_size;
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)
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))
174 /* Prototype declarations for functions defined in aoutx.h */
176 PROTO (boolean, NAME(aout,squirt_out_relocs),(bfd *abfd, asection *section));
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));
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));
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,
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 *abfd, boolean exec));
213 PROTO (void, NAME(aout,swap_exec_header_in), (bfd *abfd,
214 struct external_exec *raw_bytes, struct internal_exec *execp));
216 PROTO (void, NAME(aout,swap_exec_header_out),(bfd *abfd, struct internal_exec *execp,
217 struct external_exec *raw_bytes));
219 /* A.out uses the generic versions of these routines... */
221 #define aout_32_get_section_contents bfd_generic_get_section_contents
222 #define aout_32_close_and_cleanup bfd_generic_close_and_cleanup
224 #define aout_64_get_section_contents bfd_generic_get_section_contents
225 #define aout_64_close_and_cleanup bfd_generic_close_and_cleanup
227 /* Calculate the file positions of the parts of a newly read aout header */
228 #define WORK_OUT_FILE_POSITIONS(abfd, execp) \
229 obj_textsec (abfd)->size = N_TXTSIZE(*execp); \
231 /* The virtual memory addresses of the sections */ \
232 obj_textsec (abfd)->vma = N_TXTADDR(*execp); \
233 obj_datasec (abfd)->vma = N_DATADDR(*execp); \
234 obj_bsssec (abfd)->vma = N_BSSADDR(*execp); \
236 /* The file offsets of the sections */ \
237 obj_textsec (abfd)->filepos = N_TXTOFF (*execp); \
238 obj_datasec (abfd)->filepos = N_DATOFF (*execp); \
240 /* The file offsets of the relocation info */ \
241 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp); \
242 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp); \
244 /* The file offsets of the string table and symbol table. */ \
245 obj_sym_filepos (abfd) = N_SYMOFF (*execp); \
246 obj_str_filepos (abfd) = N_STROFF (*execp); \
249 #define WRITE_HEADERS(abfd, execp) \
251 if (abfd->flags & D_PAGED) \
253 execp->a_text = obj_textsec (abfd)->size; \
254 /* Kludge to distinguish old- and new-style ZMAGIC. \
255 The latter includes the exec header in the text size. */ \
256 if (obj_textsec(abfd)->filepos == EXEC_BYTES_SIZE) \
257 execp->a_text += EXEC_BYTES_SIZE; \
258 N_SET_MAGIC (*execp, ZMAGIC); \
262 execp->a_text = obj_textsec (abfd)->size; \
263 if (abfd->flags & WP_TEXT) \
264 { N_SET_MAGIC (*execp, NMAGIC); } \
266 { N_SET_MAGIC(*execp, OMAGIC); } \
268 if (abfd->flags & D_PAGED) \
270 data_pad = ALIGN(obj_datasec(abfd)->size, PAGE_SIZE) \
271 - obj_datasec(abfd)->size; \
273 if (data_pad > obj_bsssec(abfd)->size) \
276 execp->a_bss = obj_bsssec(abfd)->size - data_pad; \
277 execp->a_data = obj_datasec(abfd)->size + data_pad; \
281 execp->a_data = obj_datasec (abfd)->size; \
282 execp->a_bss = obj_bsssec (abfd)->size; \
285 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
286 execp->a_entry = bfd_get_start_address (abfd); \
288 execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \
289 obj_reloc_entry_size (abfd)); \
290 execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \
291 obj_reloc_entry_size (abfd)); \
292 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); \
294 bfd_seek (abfd, 0L, false); \
295 bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd); \
296 /* Now write out reloc info, followed by syms and strings */ \
298 if (bfd_get_symcount (abfd) != 0) \
301 (long)(N_SYMOFF(*execp)), false); \
303 NAME(aout,write_syms)(abfd); \
305 bfd_seek (abfd, (long)(N_TRELOFF(*execp)), false); \
307 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) return false; \
308 bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false); \
310 if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) return false; \