Initial import from FreeBSD RELENG_4:
[dragonfly.git] / libexec / rtld-aout / i386 / md.h
1 /*
2  * Copyright (c) 1993 Paul Kranenburg
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Paul Kranenburg.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/libexec/rtld-aout/i386/md.h,v 1.18 1999/08/28 00:10:08 peter Exp $
31  */
32
33 #ifndef __MD_H__
34 #define __MD_H__
35
36 #if defined(CROSS_LINKER) && defined(XHOST) && XHOST==sparc
37 #define NEED_SWAP
38 #endif
39
40 #define MAX_ALIGNMENT           (sizeof (long))
41
42 #ifdef NetBSD
43 #define PAGSIZ                  __LDPGSZ
44 #else
45 #define PAGSIZ                  4096
46 #endif
47
48 #if defined(NetBSD) || defined(CROSS_LINKER)
49
50 #define N_SET_FLAG(ex,f)        (oldmagic || N_GETMAGIC(ex)==QMAGIC ? (0) : \
51                                         N_SETMAGIC(ex,                  \
52                                                    N_GETMAGIC(ex),      \
53                                                    MID_MACHINE,         \
54                                                    N_GETFLAG(ex)|(f)))
55
56 #define N_IS_DYNAMIC(ex)        ((N_GETFLAG(ex) & EX_DYNAMIC))
57
58 #define N_BADMID(ex) \
59         (N_GETMID(ex) != 0 && N_GETMID(ex) != MID_MACHINE)
60
61 #endif
62
63 /*
64  * FreeBSD does it differently
65  */
66 #ifdef __FreeBSD__
67 #define N_SET_FLAG(ex,f)        (oldmagic ? (0) :                       \
68                                   (netzmagic == 0 ?                     \
69                                         N_SETMAGIC(ex,                  \
70                                                    N_GETMAGIC(ex),      \
71                                                    MID_MACHINE,         \
72                                                    N_GETFLAG(ex)|(f)) : \
73                                         N_SETMAGIC_NET(ex,              \
74                                                    N_GETMAGIC_NET(ex),  \
75                                                    MID_MACHINE,         \
76                                                    N_GETFLAG_NET(ex)|(f)) ))
77
78 #define N_IS_DYNAMIC(ex)        ((N_GETMAGIC_NET(ex) == ZMAGIC) ?       \
79                                 ((N_GETFLAG_NET(ex) & EX_DYNAMIC)) :    \
80                                 ((N_GETFLAG(ex) & EX_DYNAMIC) ))
81 #define N_BADMID(ex) 0
82 #endif
83
84 /*
85  * Should be handled by a.out.h ?
86  */
87 #define N_ADJUST(ex)            (((ex).a_entry < PAGSIZ) ? -PAGSIZ : 0)
88 #define TEXT_START(ex)          (N_TXTADDR(ex) + N_ADJUST(ex))
89 #define DATA_START(ex)          (N_DATADDR(ex) + N_ADJUST(ex))
90
91 #define RELOC_STATICS_THROUGH_GOT_P(r)  (0)
92 #define JMPSLOT_NEEDS_RELOC             (0)
93
94 #define md_got_reloc(r)                 (0)
95
96 #define md_get_rt_segment_addend(r,a)   md_get_addend(r,a)
97
98 #define RELOC_INIT_SEGMENT_RELOC(r)     ((r)->r_length = 2)
99
100 /* Width of a Global Offset Table entry */
101 #define GOT_ENTRY_SIZE  4
102 typedef long    got_t;
103
104 typedef struct jmpslot {
105         u_short opcode;
106         u_short addr[2];
107         u_short reloc_index;
108 #define JMPSLOT_RELOC_MASK              0xffff
109 } jmpslot_t;
110
111 #define NOP     0x90
112 #define CALL    0xe890          /* NOP + CALL opcode */
113 #define JUMP    0xe990          /* NOP + JMP opcode */
114 #define TRAP    0xcc            /* INT 3 */
115
116 /*
117  * Byte swap defs for cross linking
118  */
119
120 #if !defined(NEED_SWAP)
121
122 #define md_swapin_exec_hdr(h)
123 #define md_swapout_exec_hdr(h)
124 #define md_swapin_symbols(s,n)
125 #define md_swapout_symbols(s,n)
126 #define md_swapin_zsymbols(s,n)
127 #define md_swapout_zsymbols(s,n)
128 #define md_swapin_reloc(r,n)
129 #define md_swapout_reloc(r,n)
130 #define md_swapin__dynamic(l)
131 #define md_swapout__dynamic(l)
132 #define md_swapin_section_dispatch_table(l)
133 #define md_swapout_section_dispatch_table(l)
134 #define md_swapin_so_debug(d)
135 #define md_swapout_so_debug(d)
136 #define md_swapin_rrs_hash(f,n)
137 #define md_swapout_rrs_hash(f,n)
138 #define md_swapin_sod(l,n)
139 #define md_swapout_sod(l,n)
140 #define md_swapout_jmpslot(j,n)
141 #define md_swapout_got(g,n)
142 #define md_swapin_ranlib_hdr(h,n)
143 #define md_swapout_ranlib_hdr(h,n)
144
145 #endif /* NEED_SWAP */
146
147 #ifdef CROSS_LINKER
148
149 #define get_byte(p)     ( ((unsigned char *)(p))[0] )
150
151 #define get_short(p)    ( ( ((unsigned char *)(p))[0] << 8) | \
152                           ( ((unsigned char *)(p))[1]     )   \
153                         )
154
155 #define get_long(p)     ( ( ((unsigned char *)(p))[0] << 24) | \
156                           ( ((unsigned char *)(p))[1] << 16) | \
157                           ( ((unsigned char *)(p))[2] << 8 ) | \
158                           ( ((unsigned char *)(p))[3]      )   \
159                         )
160
161 #define put_byte(p, v)  { ((unsigned char *)(p))[0] = ((unsigned long)(v)); }
162
163 #define put_short(p, v) { ((unsigned char *)(p))[0] =                   \
164                                 ((((unsigned long)(v)) >> 8) & 0xff);   \
165                           ((unsigned char *)(p))[1] =                   \
166                                 ((((unsigned long)(v))     ) & 0xff); }
167
168 #define put_long(p, v)  { ((unsigned char *)(p))[0] =                   \
169                                 ((((unsigned long)(v)) >> 24) & 0xff);  \
170                           ((unsigned char *)(p))[1] =                   \
171                                 ((((unsigned long)(v)) >> 16) & 0xff);  \
172                           ((unsigned char *)(p))[2] =                   \
173                                 ((((unsigned long)(v)) >>  8) & 0xff);  \
174                           ((unsigned char *)(p))[3] =                   \
175                                 ((((unsigned long)(v))      ) & 0xff); }
176
177 #ifdef NEED_SWAP
178
179 /* Define IO byte swapping routines */
180
181 void    md_swapin_exec_hdr __P((struct exec *));
182 void    md_swapout_exec_hdr __P((struct exec *));
183 void    md_swapin_reloc __P((struct relocation_info *, int));
184 void    md_swapout_reloc __P((struct relocation_info *, int));
185 void    md_swapout_jmpslot __P((jmpslot_t *, int));
186
187 #define md_swapin_symbols(s,n)                  swap_symbols(s,n)
188 #define md_swapout_symbols(s,n)                 swap_symbols(s,n)
189 #define md_swapin_zsymbols(s,n)                 swap_zsymbols(s,n)
190 #define md_swapout_zsymbols(s,n)                swap_zsymbols(s,n)
191 #define md_swapin__dynamic(l)                   swap__dynamic(l)
192 #define md_swapout__dynamic(l)                  swap__dynamic(l)
193 #define md_swapin_section_dispatch_table(l)     swap_section_dispatch_table(l)
194 #define md_swapout_section_dispatch_table(l)    swap_section_dispatch_table(l)
195 #define md_swapin_so_debug(d)                   swap_so_debug(d)
196 #define md_swapout_so_debug(d)                  swap_so_debug(d)
197 #define md_swapin_rrs_hash(f,n)                 swap_rrs_hash(f,n)
198 #define md_swapout_rrs_hash(f,n)                swap_rrs_hash(f,n)
199 #define md_swapin_sod(l,n)                      swapin_sod(l,n)
200 #define md_swapout_sod(l,n)                     swapout_sod(l,n)
201 #define md_swapout_got(g,n)                     swap_longs((long*)(g),n)
202 #define md_swapin_ranlib_hdr(h,n)               swap_ranlib_hdr(h,n)
203 #define md_swapout_ranlib_hdr(h,n)              swap_ranlib_hdr(h,n)
204
205 #define md_swap_short(x) ( (((x) >> 8) & 0xff) | (((x) & 0xff) << 8) )
206
207 #define md_swap_long(x) ( (((x) >> 24) & 0xff    ) | (((x) >> 8 ) & 0xff00   ) | \
208                         (((x) << 8 ) & 0xff0000) | (((x) << 24) & 0xff000000))
209
210 #else   /* We need not swap, but must pay attention to alignment: */
211
212 #define md_swap_short(x)        (x)
213 #define md_swap_long(x)         (x)
214
215 #endif /* NEED_SWAP */
216
217 #else   /* Not a cross linker: use native */
218
219 #define md_swap_short(x)                (x)
220 #define md_swap_long(x)                 (x)
221
222 #define get_byte(where)                 (*(char *)(where))
223 #define get_short(where)                (*(short *)(where))
224 #define get_long(where)                 (*(long *)(where))
225
226 #define put_byte(where,what)            (*(char *)(where) = (what))
227 #define put_short(where,what)           (*(short *)(where) = (what))
228 #define put_long(where,what)            (*(long *)(where) = (what))
229
230 #endif /* CROSS_LINKER */
231
232 void    md_init_header __P((struct exec *, int, int));
233 long    md_get_addend __P((struct relocation_info *, unsigned char *));
234 void    md_relocate __P((struct relocation_info *, long, unsigned char *, int));
235 void    md_make_jmpslot __P((jmpslot_t *, long, long));
236 void    md_fix_jmpslot __P((jmpslot_t *, long, u_long));
237 void    md_bind_jmpslot __P((u_long, caddr_t));
238 int     md_make_reloc __P((struct relocation_info *, struct relocation_info *, int));
239 void    md_make_jmpreloc __P((struct relocation_info *, struct relocation_info *, int));
240 void    md_make_gotreloc __P((struct relocation_info *, struct relocation_info *, int));
241 void    md_make_copyreloc __P((struct relocation_info *, struct relocation_info *));
242 void    md_set_breakpoint __P((long, long *));
243
244
245 #endif /* __MD_H__ */