]>
Commit | Line | Data |
---|---|---|
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 | 5 | This file is part of BFD, the Binary File Descriptor library. |
69ebee86 | 6 | |
9e2dad8e | 7 | This program is free software; you can redistribute it and/or modify |
4a81b561 | 8 | it under the terms of the GNU General Public License as published by |
9e2dad8e JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
4a81b561 | 11 | |
9e2dad8e | 12 | This program is distributed in the hope that it will be useful, |
4a81b561 DHW |
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 | |
9e2dad8e JG |
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. */ | |
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. */ | |
53 | struct external_exec; | |
54 | struct 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 | ||
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 | ||
69ebee86 | 119 | typedef 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 | 130 | struct 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 | 176 | PROTO (boolean, NAME(aout,squirt_out_relocs),(bfd *abfd, asection *section)); |
69ebee86 | 177 | |
c0e5039e | 178 | PROTO (bfd_target *, NAME(aout,some_aout_object_p), (bfd *abfd, |
0fa4f690 | 179 | struct internal_exec *execp, |
69ebee86 | 180 | bfd_target *(*callback)())); |
c0e5039e JG |
181 | PROTO (boolean, NAME(aout,mkobject), (bfd *abfd)); |
182 | PROTO (enum machine_type, NAME(aout,machine_type), (enum bfd_architecture arch, | |
69ebee86 | 183 | unsigned long machine)); |
c0e5039e | 184 | PROTO (boolean, NAME(aout,set_arch_mach), (bfd *abfd, enum bfd_architecture arch, |
69ebee86 JG |
185 | unsigned long machine)); |
186 | ||
c0e5039e JG |
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, | |
69ebee86 | 198 | asymbol **symbols)); |
c0e5039e | 199 | PROTO (unsigned int, NAME(aout,canonicalize_reloc), (bfd *abfd, sec_ptr section, |
69ebee86 | 200 | arelent **relptr, asymbol **symbols)); |
c0e5039e JG |
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, | |
9e2dad8e | 205 | asymbol *symbol, bfd_print_symbol_type how)); |
c0e5039e JG |
206 | PROTO (boolean, NAME(aout,close_and_cleanup), (bfd *abfd)); |
207 | PROTO (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)); | |
98d43107 | 210 | PROTO (int, NAME(aout,sizeof_headers), (bfd *abfd, boolean exec)); |
69ebee86 | 211 | |
69ebee86 | 212 | |
c0e5039e JG |
213 | PROTO (void, NAME(aout,swap_exec_header_in), (bfd *abfd, |
214 | struct external_exec *raw_bytes, struct internal_exec *execp)); | |
69ebee86 | 215 | |
c0e5039e JG |
216 | PROTO (void, NAME(aout,swap_exec_header_out),(bfd *abfd, struct internal_exec *execp, |
217 | struct external_exec *raw_bytes)); | |
69ebee86 | 218 | |
7de245d3 PB |
219 | /* Prototypes for functions in stab-syms.c. */ |
220 | ||
221 | PROTO(char *, aout_stab_name, (int code)); | |
222 | ||
69ebee86 JG |
223 | /* A.out uses the generic versions of these routines... */ |
224 | ||
c0e5039e JG |
225 | #define aout_32_get_section_contents bfd_generic_get_section_contents |
226 | #define aout_32_close_and_cleanup bfd_generic_close_and_cleanup | |
227 | ||
228 | #define aout_64_get_section_contents bfd_generic_get_section_contents | |
229 | #define aout_64_close_and_cleanup bfd_generic_close_and_cleanup | |
230 | ||
231 | /* Calculate the file positions of the parts of a newly read aout header */ | |
232 | #define WORK_OUT_FILE_POSITIONS(abfd, execp) \ | |
98d43107 JG |
233 | obj_textsec (abfd)->size = N_TXTSIZE(*execp); \ |
234 | \ | |
0fa4f690 | 235 | /* The virtual memory addresses of the sections */ \ |
c0e5039e | 236 | obj_textsec (abfd)->vma = N_TXTADDR(*execp); \ |
98d43107 JG |
237 | obj_datasec (abfd)->vma = N_DATADDR(*execp); \ |
238 | obj_bsssec (abfd)->vma = N_BSSADDR(*execp); \ | |
c0e5039e JG |
239 | \ |
240 | /* The file offsets of the sections */ \ | |
241 | obj_textsec (abfd)->filepos = N_TXTOFF (*execp); \ | |
242 | obj_datasec (abfd)->filepos = N_DATOFF (*execp); \ | |
243 | \ | |
244 | /* The file offsets of the relocation info */ \ | |
245 | obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp); \ | |
246 | obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp); \ | |
247 | \ | |
248 | /* The file offsets of the string table and symbol table. */ \ | |
249 | obj_sym_filepos (abfd) = N_SYMOFF (*execp); \ | |
250 | obj_str_filepos (abfd) = N_STROFF (*execp); \ | |
251 | ||
252 | ||
253 | #define WRITE_HEADERS(abfd, execp) \ | |
254 | { \ | |
255 | if (abfd->flags & D_PAGED) \ | |
256 | { \ | |
0fa4f690 JG |
257 | execp->a_text = obj_textsec (abfd)->size; \ |
258 | /* Kludge to distinguish old- and new-style ZMAGIC. \ | |
259 | The latter includes the exec header in the text size. */ \ | |
260 | if (obj_textsec(abfd)->filepos == EXEC_BYTES_SIZE) \ | |
261 | execp->a_text += EXEC_BYTES_SIZE; \ | |
c0e5039e JG |
262 | N_SET_MAGIC (*execp, ZMAGIC); \ |
263 | } \ | |
9e2dad8e | 264 | else \ |
c0e5039e | 265 | { \ |
9e2dad8e JG |
266 | execp->a_text = obj_textsec (abfd)->size; \ |
267 | if (abfd->flags & WP_TEXT) \ | |
268 | { N_SET_MAGIC (*execp, NMAGIC); } \ | |
269 | else \ | |
270 | { N_SET_MAGIC(*execp, OMAGIC); } \ | |
c0e5039e | 271 | } \ |
c0e5039e JG |
272 | if (abfd->flags & D_PAGED) \ |
273 | { \ | |
0fa4f690 JG |
274 | data_pad = ALIGN(obj_datasec(abfd)->size, PAGE_SIZE) \ |
275 | - obj_datasec(abfd)->size; \ | |
c0e5039e JG |
276 | \ |
277 | if (data_pad > obj_bsssec(abfd)->size) \ | |
278 | execp->a_bss = 0; \ | |
279 | else \ | |
280 | execp->a_bss = obj_bsssec(abfd)->size - data_pad; \ | |
281 | execp->a_data = obj_datasec(abfd)->size + data_pad; \ | |
282 | } \ | |
283 | else \ | |
284 | { \ | |
285 | execp->a_data = obj_datasec (abfd)->size; \ | |
286 | execp->a_bss = obj_bsssec (abfd)->size; \ | |
287 | } \ | |
288 | \ | |
0fa4f690 | 289 | execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \ |
c0e5039e JG |
290 | execp->a_entry = bfd_get_start_address (abfd); \ |
291 | \ | |
292 | execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \ | |
293 | obj_reloc_entry_size (abfd)); \ | |
294 | execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \ | |
295 | obj_reloc_entry_size (abfd)); \ | |
296 | NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); \ | |
297 | \ | |
298 | bfd_seek (abfd, 0L, false); \ | |
299 | bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd); \ | |
300 | /* Now write out reloc info, followed by syms and strings */ \ | |
301 | \ | |
302 | if (bfd_get_symcount (abfd) != 0) \ | |
303 | { \ | |
304 | bfd_seek (abfd, \ | |
305 | (long)(N_SYMOFF(*execp)), false); \ | |
306 | \ | |
307 | NAME(aout,write_syms)(abfd); \ | |
308 | \ | |
309 | bfd_seek (abfd, (long)(N_TRELOFF(*execp)), false); \ | |
310 | \ | |
311 | if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) return false; \ | |
312 | bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false); \ | |
313 | \ | |
314 | if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) return false; \ | |
315 | } \ | |
316 | } |