1 /**
2  * @namespace   biew_plugins_auto
3  * @file        plugins/bin/aout64.h
4  * @brief       This file contains `a.out' object-file definitions, including
5  *              extensions to 64-bit fields.
6  * @version     -
7  * @remark      Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation,
8  *              Inc. This file is part of the GNU C Library.
9  *              The GNU C Library is free software; you can redistribute it and/or
10  *              modify it under the terms of the GNU Library General Public License as
11  *              published by the Free Software Foundation; either version 2 of the
12  *              License, or (at your option) any later version.
13  *              The GNU C Library is distributed in the hope that it will be useful,
14  *              but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *              Library General Public License for more details.
17  *              You should have received a copy of the GNU Library General Public
18  *              License along with the GNU C Library; see the file COPYING.LIB.  If not,
19  *              write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  *              Boston, MA 02111-1307, USA.
21  * @note        Requires POSIX compatible development system
22  *
23  * @author      GNU FSF
24  * @since       1995
25 **/
26 #ifndef __A_OUT_64_H__
27 #define __A_OUT_64_H__
28 
29 #ifndef __SYS_DEP_H
30 #include "_sys_dep.h"
31 #endif
32 
33 #ifdef __HAVE_PRAGMA_PACK__
34 #pragma pack(1)
35 #endif
36 
37 /** This is the layout on disk of the 32-bit or 64-bit exec header. */
38 
39 #define BYTES_IN_WORD 4
40 #define N_MAGIC(x) ((x) & 0xFFFF)
41 #define N_MACHTYPE(x) (((x) >> 16) & 0xff)
42 #define N_FLAGS(x) (((x) >> 24) & 0xff)
43 
44 #ifndef external_exec
45 struct external_exec
46 {
47   tUInt8 e_info[4];		/**< magic number and stuff		*/
48   tUInt8 e_text[BYTES_IN_WORD]; /**< length of text section in bytes	*/
49   tUInt8 e_data[BYTES_IN_WORD]; /**< length of data section in bytes	*/
50   tUInt8 e_bss[BYTES_IN_WORD]; /**< length of bss area in bytes 		*/
51   tUInt8 e_syms[BYTES_IN_WORD]; /**< length of symbol table in bytes 	*/
52   tUInt8 e_entry[BYTES_IN_WORD]; /**< start address 			*/
53   tUInt8 e_trsize[BYTES_IN_WORD]; /**< length of text relocation info	*/
54   tUInt8 e_drsize[BYTES_IN_WORD]; /**< length of data relocation info 	*/
55 };
56 
57 #define	EXEC_BYTES_SIZE	(4 + BYTES_IN_WORD * 7)
58 
59 /** Magic numbers for a.out files */
60 
61 #define OMAGIC64 0x1001		/**< Code indicating object file  */
62 #define ZMAGIC64 0x1002		/**< Code indicating demand-paged executable.  */
63 #define NMAGIC64 0x1003		/**< Code indicating pure executable.  */
64 
65 /* There is no 64-bit QMAGIC as far as I know.  */
66 
67 #define N_BADMAG64(x)	  (N_MAGIC(x) != OMAGIC64	\
68 			&& N_MAGIC(x) != NMAGIC64	\
69   			&& N_MAGIC(x) != ZMAGIC64)
70 
71 #define OMAGIC 0x0107		/**< ...object file or impure executable.  */
72 #define NMAGIC 0x0108		/**< Code indicating pure executable.  */
73 #define ZMAGIC 0x010B		/**< Code indicating demand-paged executable.  */
74 #define BMAGIC 0x010D		/**< Used by a b.out object.  */
75 
76 #define CMAGIC 0x0111		/**< Code indicating core file.  */
77 
78 /** This indicates a demand-paged executable with the header in the text.
79     It is used by 386BSD (and variants) and Linux, at least.  */
80 #ifndef QMAGIC
81 #define QMAGIC 0x00CC
82 #endif
83 # ifndef N_BADMAG
84 #  define N_BADMAG(x)	  (N_MAGIC(x) != OMAGIC	\
85 			&& N_MAGIC(x) != NMAGIC	\
86   			&& N_MAGIC(x) != ZMAGIC \
87 		        && N_MAGIC(x) != QMAGIC)
88 # endif /* N_BADMAG */
89 
90 #endif
91 
92 #ifdef QMAGIC
93 #define N_IS_QMAGIC(x) (N_MAGIC (x) == QMAGIC)
94 #else
95 #define N_IS_QMAGIC(x) (0)
96 #endif
97 
98 /**
99    The difference between TARGET_PAGE_SIZE and N_SEGSIZE is that TARGET_PAGE_SIZE is
100    the finest granularity at which you can page something, thus it
101    controls the padding (if any) before the text segment of a ZMAGIC
102    file.  N_SEGSIZE is the resolution at which things can be marked as
103    read-only versus read/write, so it controls the padding between the
104    text segment and the data segment (in memory; on disk the padding
105    between them is TARGET_PAGE_SIZE).  TARGET_PAGE_SIZE and N_SEGSIZE are the same
106    for most machines, but different for sun3.  */
107 
108 /**
109    By default, segment size is constant.  But some machines override this
110    to be a function of the a.out header (e.g. machine type).  */
111 
112 #ifndef	N_SEGSIZE
113 #define	N_SEGSIZE(x)	SEGMENT_SIZE
114 #endif
115 
116 /**
117    Virtual memory address of the text section.
118    This is getting very complicated.  A good reason to discard a.out format
119    for something that specifies these fields explicitly.  But til then...
120 
121    * OMAGIC and NMAGIC files:
122        (object files: text for "relocatable addr 0" right after the header)
123        start at 0, offset is EXEC_BYTES_SIZE, size as stated.
124    * The text address, offset, and size of ZMAGIC files depend
125      on the entry point of the file:
126      * entry point below TEXT_START_ADDR:
127        (hack for SunOS shared libraries)
128        start at 0, offset is 0, size as stated.
129      * If N_HEADER_IN_TEXT(x) is true (which defaults to being the
130        case when the entry point is EXEC_BYTES_SIZE or further into a page):
131        no padding is needed; text can start after exec header.  Sun
132        considers the text segment of such files to include the exec header;
133        for BFD's purposes, we don't, which makes more work for us.
134        start at TEXT_START_ADDR + EXEC_BYTES_SIZE, offset is EXEC_BYTES_SIZE,
135        size as stated minus EXEC_BYTES_SIZE.
136      * If N_HEADER_IN_TEXT(x) is false (which defaults to being the case when
137        the entry point is less than EXEC_BYTES_SIZE into a page (e.g. page
138        aligned)): (padding is needed so that text can start at a page boundary)
139        start at TEXT_START_ADDR, offset TARGET_PAGE_SIZE, size as stated.
140 
141     Specific configurations may want to hardwire N_HEADER_IN_TEXT,
142     for efficiency or to allow people to play games with the entry point.
143     In that case, you would #define N_HEADER_IN_TEXT(x) as 1 for sunos,
144     and as 0 for most other hosts (Sony News, Vax Ultrix, etc).
145     (Do this in the appropriate bfd target file.)
146     (The default is a heuristic that will break if people try changing
147     the entry point, perhaps with the ld -e flag.)
148 
149     * QMAGIC is always like a ZMAGIC for which N_HEADER_IN_TEXT is true,
150     and for which the starting address is TARGET_PAGE_SIZE (or should this be
151     SEGMENT_SIZE?) (TEXT_START_ADDR only applies to ZMAGIC, not to QMAGIC).
152     */
153 
154 /** This macro is only relevant for ZMAGIC files; QMAGIC always has the header
155     in the text.  */
156 #ifndef N_HEADER_IN_TEXT
157 #define N_HEADER_IN_TEXT(x) (((x).a_entry & (TARGET_PAGE_SIZE-1)) >= EXEC_BYTES_SIZE)
158 #endif
159 
160 /** Sun shared libraries, not linux.  This macro is only relevant for ZMAGIC
161     files.  */
162 #ifndef N_SHARED_LIB
163 #define N_SHARED_LIB(x) ((x).a_entry < TEXT_START_ADDR)
164 #endif
165 
166 /**
167    Returning 0 not TEXT_START_ADDR for OMAGIC and NMAGIC is based on
168    the assumption that we are dealing with a .o file, not an
169    executable.  This is necessary for OMAGIC (but means we don't work
170    right on the output from ld -N); more questionable for NMAGIC.  */
171 
172 #ifndef N_TXTADDR
173 #define N_TXTADDR(x) \
174     (/** The address of a QMAGIC file is always one page in, */ \
175      /** with the header in the text.  */ \
176      N_IS_QMAGIC (x) ? TARGET_PAGE_SIZE + EXEC_BYTES_SIZE : \
177      N_MAGIC(x) != ZMAGIC ? 0 :	/**< object file or NMAGIC */\
178      N_SHARED_LIB(x) ? 0 :	\
179      N_HEADER_IN_TEXT(x)  ?	\
180 	    TEXT_START_ADDR + EXEC_BYTES_SIZE :	/**< no padding */\
181 	    TEXT_START_ADDR			/**< a page of padding */\
182     )
183 #endif
184 
185 /**
186    If N_HEADER_IN_TEXT is not true for ZMAGIC, there is some padding
187    to make the text segment start at a certain boundary.  For most
188    systems, this boundary is TARGET_PAGE_SIZE.  But for Linux, in the
189    time-honored tradition of crazy ZMAGIC hacks, it is 1024 which is
190    not what TARGET_PAGE_SIZE needs to be for QMAGIC.  */
191 
192 #ifndef ZMAGIC_DISK_BLOCK_SIZE
193 #define ZMAGIC_DISK_BLOCK_SIZE TARGET_PAGE_SIZE
194 #endif
195 
196 #define N_DISK_BLOCK_SIZE(x) \
197   (N_MAGIC(x) == ZMAGIC ? ZMAGIC_DISK_BLOCK_SIZE : TARGET_PAGE_SIZE)
198 
199 /** Offset in an a.out of the start of the text section. */
200 #ifndef N_TXTOFF
201 #define N_TXTOFF(x)	\
202     (/** For {O,N,Q}MAGIC, no padding.  */ \
203      N_MAGIC(x) != ZMAGIC ? EXEC_BYTES_SIZE : \
204      N_SHARED_LIB(x) ? 0 : \
205      N_HEADER_IN_TEXT(x) ?	\
206 	    EXEC_BYTES_SIZE :			/**< no padding */\
207 	    ZMAGIC_DISK_BLOCK_SIZE		/**< a page of padding */\
208     )
209 #endif
210 /**
211    Size of the text section.  It's always as stated, except that we
212    offset it to `undo' the adjustment to N_TXTADDR and N_TXTOFF
213    for ZMAGIC files that nominally include the exec header
214    as part of the first page of text.  (BFD doesn't consider the
215    exec header to be part of the text segment.)  */
216 #ifndef N_TXTSIZE
217 #define	N_TXTSIZE(x) \
218     (/** For QMAGIC, we don't consider the header part of the text section.  */\
219      N_IS_QMAGIC (x) ? (x).a_text - EXEC_BYTES_SIZE : \
220      (N_MAGIC(x) != ZMAGIC || N_SHARED_LIB(x)) ? (x).a_text : \
221      N_HEADER_IN_TEXT(x)  ?	\
222 	    (x).a_text - EXEC_BYTES_SIZE:	/**< no padding */\
223 	    (x).a_text				/**< a page of padding */\
224     )
225 #endif
226 /**
227    The address of the data segment in virtual memory.
228    It is the text segment address, plus text segment size, rounded
229    up to a N_SEGSIZE boundary for pure or pageable files. */
230 #ifndef N_DATADDR
231 #define N_DATADDR(x) \
232     (N_MAGIC(x)==OMAGIC? (N_TXTADDR(x)+N_TXTSIZE(x)) \
233      :  (N_SEGSIZE(x) + ((N_TXTADDR(x)+N_TXTSIZE(x)-1) & ~(N_SEGSIZE(x)-1))))
234 #endif
235 /** The address of the BSS segment -- immediately after the data segment.  */
236 
237 #define N_BSSADDR(x)	(N_DATADDR(x) + (x).a_data)
238 
239 /** Offsets of the various portions of the file after the text segment.  */
240 
241 /**
242    For {Q,Z}MAGIC, there is padding to make the data segment start on
243    a page boundary.  Most of the time the a_text field (and thus
244    N_TXTSIZE) already contains this padding.  It is possible that for
245    BSDI and/or 386BSD it sometimes doesn't contain the padding, and
246    perhaps we should be adding it here.  But this seems kind of
247    questionable and probably should be BSDI/386BSD-specific if we do
248    do it.
249 
250    For NMAGIC (at least for hp300 BSD, probably others), there is
251    padding in memory only, not on disk, so we must *not* ever pad here
252    for NMAGIC.  */
253 
254 #ifndef N_DATOFF
255 #define N_DATOFF(x) \
256  (N_TXTOFF(x) + N_TXTSIZE(x))
257 #endif
258 
259 #ifndef N_TRELOFF
260 #define N_TRELOFF(x)	( N_DATOFF(x) + (x).a_data )
261 #endif
262 #ifndef N_DRELOFF
263 #define N_DRELOFF(x)	( N_TRELOFF(x) + (x).a_trsize )
264 #endif
265 #ifndef N_SYMOFF
266 #define N_SYMOFF(x)	( N_DRELOFF(x) + (x).a_drsize )
267 #endif
268 #ifndef N_STROFF
269 #define N_STROFF(x)	( N_SYMOFF(x) + (x).a_syms )
270 #endif
271 
272 /** Symbols */
273 #ifndef external_nlist
274 struct external_nlist {
275   tUInt8 e_strx[BYTES_IN_WORD];	/**< index into string table of name */
276   tUInt8 e_type[1];			/**< type of symbol */
277   tUInt8 e_other[1];			/**< misc info (usually empty) */
278   tUInt8 e_desc[2];			/**< description field */
279   tUInt8 e_value[BYTES_IN_WORD];	/**< value of symbol */
280 };
281 #define EXTERNAL_NLIST_SIZE (BYTES_IN_WORD+4+BYTES_IN_WORD)
282 #endif
283 
284 struct internal_nlist {
285   tUInt32  n_strx;			/**< index into string table of name */
286   tUInt8   n_type;			/**< type of symbol */
287   tUInt8   n_other;		        /**< misc info (usually empty) */
288   tUInt16  n_desc;		        /**< description field */
289   tUInt32  n_value;			/**< value of symbol */
290 };
291 
292 /** The n_type field is the symbol type, containing:  */
293 
294 #define N_UNDF	0	/**< Undefined symbol */
295 #define N_ABS 	2	/**< Absolute symbol -- defined at particular addr */
296 #define N_TEXT 	4	/**< Text sym -- defined at offset in text seg */
297 #define N_DATA 	6	/**< Data sym -- defined at offset in data seg */
298 #define N_BSS 	8	/**< BSS  sym -- defined at offset in zero'd seg */
299 #define	N_COMM	0x12	/**< Common symbol (visible after shared lib dynlink) */
300 #define N_FN	0x1f	/**< File name of .o file */
301 #define	N_FN_SEQ 0x0C	/**< N_FN from Sequent compilers (sigh) */
302 /** Note: N_EXT can only be usefully OR-ed with N_UNDF, N_ABS, N_TEXT,
303    N_DATA, or N_BSS.  When the low-order bit of other types is set,
304    (e.g. N_WARNING versus N_FN), they are two different types.  */
305 #define N_EXT 	1	/**< External symbol (as opposed to local-to-this-file) */
306 #define N_TYPE  0x1e
307 #define N_STAB 	0xe0	/**< If any of these bits are on, it's a debug symbol */
308 
309 #define N_INDR 0x0a
310 
311 /** The following symbols refer to set elements.
312    All the N_SET[ATDB] symbols with the same name form one set.
313    Space is allocated for the set in the text section, and each set
314    elements value is stored into one word of the space.
315    The first word of the space is the length of the set (number of elements).
316 
317    The address of the set is made into an N_SETV symbol
318    whose name is the same as the name of the set.
319    This symbol acts like a N_DATA global symbol
320    in that it can satisfy undefined external references.  */
321 
322 /** These appear as input to LD, in a .o file.  */
323 #define	N_SETA	0x14		/**< Absolute set element symbol */
324 #define	N_SETT	0x16		/**< Text set element symbol */
325 #define	N_SETD	0x18		/**< Data set element symbol */
326 #define	N_SETB	0x1A		/**< Bss set element symbol */
327 
328 /** This is output from LD.  */
329 #define N_SETV	0x1C		/**< Pointer to set vector in data area.  */
330 
331 /** Warning symbol. The text gives a warning message, the next symbol
332    in the table will be undefined. When the symbol is referenced, the
333    message is printed.  */
334 
335 #define	N_WARNING 0x1e
336 
337 /** Weak symbols.  These are a GNU extension to the a.out format.  The
338    semantics are those of ELF weak symbols.  Weak symbols are always
339    externally visible.  The N_WEAK? values are squeezed into the
340    available slots.  The value of a N_WEAKU symbol is 0.  The values
341    of the other types are the definitions.  */
342 #define N_WEAKU	0x0d		/**< Weak undefined symbol.  */
343 #define N_WEAKA 0x0e		/**< Weak absolute symbol.  */
344 #define N_WEAKT 0x0f		/**< Weak text symbol.  */
345 #define N_WEAKD 0x10		/**< Weak data symbol.  */
346 #define N_WEAKB 0x11		/**< Weak bss symbol.  */
347 
348 /** Relocations
349 
350   There	are two types of relocation flavours for a.out systems,
351   standard and extended. The standard form is used on systems where the
352   instruction has room for all the bits of an offset to the operand, whilst
353   the extended form is used when an address operand has to be split over n
354   instructions. Eg, on the 68k, each move instruction can reference
355   the target with a displacement of 16 or 32 bits. On the sparc, move
356   instructions use an offset of 14 bits, so the offset is stored in
357   the reloc field, and the data in the section is ignored.
358 */
359 
360 /** This structure describes a single relocation to be performed.
361    The text-relocation section of the file is a vector of these structures,
362    all of which apply to the text section.
363    Likewise, the data-relocation section applies to the data section.  */
364 
365 struct reloc_std_external {
366   tUInt8 r_address[BYTES_IN_WORD];	/**< offset of of data to relocate 	*/
367   tUInt8 r_index[3];	/**< symbol table index of symbol 	*/
368   tUInt8 r_type[1];	/**< relocation type			*/
369 };
370 
371 #define	RELOC_STD_BITS_PCREL_BIG	((unsigned int) 0x80)
372 #define	RELOC_STD_BITS_PCREL_LITTLE	((unsigned int) 0x01)
373 
374 #define	RELOC_STD_BITS_LENGTH_BIG	((unsigned int) 0x60)
375 #define	RELOC_STD_BITS_LENGTH_SH_BIG	5
376 #define	RELOC_STD_BITS_LENGTH_LITTLE	((unsigned int) 0x06)
377 #define	RELOC_STD_BITS_LENGTH_SH_LITTLE	1
378 
379 #define	RELOC_STD_BITS_EXTERN_BIG	((unsigned int) 0x10)
380 #define	RELOC_STD_BITS_EXTERN_LITTLE	((unsigned int) 0x08)
381 
382 #define	RELOC_STD_BITS_BASEREL_BIG	((unsigned int) 0x08)
383 #define	RELOC_STD_BITS_BASEREL_LITTLE	((unsigned int) 0x10)
384 
385 #define	RELOC_STD_BITS_JMPTABLE_BIG	((unsigned int) 0x04)
386 #define	RELOC_STD_BITS_JMPTABLE_LITTLE	((unsigned int) 0x20)
387 
388 #define	RELOC_STD_BITS_RELATIVE_BIG	((unsigned int) 0x02)
389 #define	RELOC_STD_BITS_RELATIVE_LITTLE	((unsigned int) 0x40)
390 
391 #define	RELOC_STD_SIZE	(BYTES_IN_WORD + 3 + 1)		/**< Bytes per relocation entry */
392 
393 
394 /** EXTENDED RELOCS  */
395 
396 struct reloc_ext_external {
397   tUInt8 r_address[BYTES_IN_WORD];	/**< offset of of data to relocate 	*/
398   tUInt8 r_index[3];	/**< symbol table index of symbol 	*/
399   tUInt8 r_type[1];	/**< relocation type			*/
400   tUInt8 r_addend[BYTES_IN_WORD];	/**< datum addend				*/
401 };
402 
403 #define	RELOC_EXT_BITS_EXTERN_BIG	((unsigned int) 0x80)
404 #define	RELOC_EXT_BITS_EXTERN_LITTLE	((unsigned int) 0x01)
405 
406 #define	RELOC_EXT_BITS_TYPE_BIG		((unsigned int) 0x1F)
407 #define	RELOC_EXT_BITS_TYPE_SH_BIG	0
408 #define	RELOC_EXT_BITS_TYPE_LITTLE	((unsigned int) 0xF8)
409 #define	RELOC_EXT_BITS_TYPE_SH_LITTLE	3
410 
411 /** Bytes per relocation entry */
412 #define	RELOC_EXT_SIZE	(BYTES_IN_WORD + 3 + 1 + BYTES_IN_WORD)
413 
414 enum reloc_type
415 {
416   /** simple relocations */
417   RELOC_8,			/**< data[0:7] = addend + sv 		*/
418   RELOC_16,			/**< data[0:15] = addend + sv 		*/
419   RELOC_32,			/**< data[0:31] = addend + sv 		*/
420   /** pc-rel displacement */
421   RELOC_DISP8,			/**< data[0:7] = addend - pc + sv 	*/
422   RELOC_DISP16,			/**< data[0:15] = addend - pc + sv 	*/
423   RELOC_DISP32,			/**< data[0:31] = addend - pc + sv 	*/
424   /** Special */
425   RELOC_WDISP30,		/**< data[0:29] = (addend + sv - pc)>>2 	*/
426   RELOC_WDISP22,		/**< data[0:21] = (addend + sv - pc)>>2 	*/
427   RELOC_HI22,			/**< data[0:21] = (addend + sv)>>10 	*/
428   RELOC_22,			/**< data[0:21] = (addend + sv) 		*/
429   RELOC_13,			/**< data[0:12] = (addend + sv)		*/
430   RELOC_LO10,			/**< data[0:9] = (addend + sv)		*/
431   RELOC_SFA_BASE,
432   RELOC_SFA_OFF13,
433   /** P.I.C. (base-relative) */
434   RELOC_BASE10,  		/**< Not sure - maybe we can do this the */
435   RELOC_BASE13,			/**< right way now */
436   RELOC_BASE22,
437   /** for some sort of pc-rel P.I.C. (?) */
438   RELOC_PC10,
439   RELOC_PC22,
440   /** P.I.C. jump table */
441   RELOC_JMP_TBL,
442   /** reputedly for shared libraries somehow */
443   RELOC_SEGOFF16,
444   RELOC_GLOB_DAT,
445   RELOC_JMP_SLOT,
446   RELOC_RELATIVE,
447 
448   RELOC_11,
449   RELOC_WDISP2_14,
450   RELOC_WDISP19,
451   RELOC_HHI22,			/**< data[0:21] = (addend + sv) >> 42     */
452   RELOC_HLO10,			/**< data[0:9] = (addend + sv) >> 32      */
453 
454   /** 29K relocation types */
455   RELOC_JUMPTARG,
456   RELOC_CONST,
457   RELOC_CONSTH,
458 
459   /** All the new ones I can think of, for sparc v9 */
460 
461   RELOC_64,			/**< data[0:63] = addend + sv 		*/
462   RELOC_DISP64,			/**< data[0:63] = addend - pc + sv 	*/
463   RELOC_WDISP21,		/**< data[0:20] = (addend + sv - pc)>>2 	*/
464   RELOC_DISP21,			/**< data[0:20] = addend - pc + sv        */
465   RELOC_DISP14,			/**< data[0:13] = addend - pc + sv 	*/
466   /** Q .
467      What are the other ones,
468      Since this is a clean slate, can we throw away the ones we dont
469      understand ? Should we sort the values ? What about using a
470      microcode format like the 68k ?
471      */
472   NO_RELOC
473   };
474 
475 #ifdef __HAVE_PRAGMA_PACK__
476 #pragma pack()
477 #endif
478 
479 #endif				/* __A_OUT_64_H__ */
480