2 * Copyright (c) 1993 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
30 * $FreeBSD: src/libexec/rtld-aout/i386/md.c,v 1.17 1999/08/28 00:10:07 peter Exp $
33 #include <sys/param.h>
37 #include <sys/types.h>
46 #if defined(RTLD) && defined(SUN_COMPAT)
47 #define REL_SIZE(r) (2) /* !!!!! Sun BUG compatible */
49 #define REL_SIZE(r) ((r)->r_length)
53 * Get relocation addend corresponding to relocation record RP
57 md_get_addend(rp, addr)
58 struct relocation_info *rp;
61 switch (REL_SIZE(rp)) {
63 return get_byte(addr);
65 return get_short(addr);
67 return get_long(addr);
69 errx(1, "Unsupported relocation size: %x",
75 * Put RELOCATION at ADDR according to relocation record RP.
78 md_relocate(rp, relocation, addr, relocatable_output)
79 struct relocation_info *rp;
82 int relocatable_output;
84 switch (REL_SIZE(rp)) {
86 put_byte(addr, relocation);
89 put_short(addr, relocation);
92 put_long(addr, relocation);
95 errx(1, "Unsupported relocation size: %x",
101 * Machine dependent part of claim_rrs_reloc().
102 * Set RRS relocation type.
105 md_make_reloc(rp, r, type)
106 struct relocation_info *rp, *r;
109 /* Relocation size */
110 r->r_length = rp->r_length;
115 if (type & RELTYPE_RELATIVE)
118 if (type & RELTYPE_COPY)
125 * Set up a transfer from jmpslot at OFFSET (relative to the PLT table)
126 * to the binder slot (which is at offset 0 of the PLT).
129 md_make_jmpslot(sp, offset, index)
135 * i386 PC-relative "fixed point" is located right after the
136 * instruction it pertains to.
138 u_long fudge = - (sizeof(sp->opcode) + sizeof(sp->addr) + offset);
144 sp->addr[0] = fudge & 0xffff;
145 sp->addr[1] = fudge >> 16;
147 sp->reloc_index = index;
151 * Set up a "direct" transfer (ie. not through the run-time binder) from
152 * jmpslot at OFFSET to ADDR. Used by `ld' when the SYMBOLIC flag is on,
153 * and by `ld.so' after resolving the symbol.
154 * On the i386, we use the JMP instruction which is PC relative, so no
155 * further RRS relocations will be necessary for such a jmpslot.
158 md_fix_jmpslot(sp, offset, addr)
163 u_long fudge = addr - (sizeof(sp->opcode) + sizeof(sp->addr) + offset);
169 sp->addr[0] = fudge & 0xffff;
170 sp->addr[1] = fudge >> 16;
176 * Bind a jmpslot to its target address. TARGET is where the jmpslot
177 * should jump to, and WHERE is a pointer to the jmpslot's address field.
178 * This is called by the dynamic linker when LD_BIND_NOW is set in the
182 md_bind_jmpslot(target, where)
187 (jmpslot_t *) (where - offsetof(jmpslot_t, addr[0]));
189 md_fix_jmpslot(sp, (long) sp, target);
193 * Update the relocation record for a RRS jmpslot.
196 md_make_jmpreloc(rp, r, type)
197 struct relocation_info *rp, *r;
203 * Fix relocation address to point to the correct
204 * location within this jmpslot.
206 r->r_address += sizeof(sp->opcode);
208 /* Relocation size */
211 /* Set relocation type */
213 if (type & RELTYPE_RELATIVE)
219 * Set relocation type for a RRS GOT relocation.
222 md_make_gotreloc(rp, r, type)
223 struct relocation_info *rp, *r;
227 if (type & RELTYPE_RELATIVE)
230 /* Relocation size */
235 * Set relocation type for a RRS copy operation.
238 md_make_cpyreloc(rp, r)
239 struct relocation_info *rp, *r;
241 /* Relocation size */
248 md_set_breakpoint(where, savep)
252 *savep = *(long *)where;
253 *(char *)where = TRAP;
263 * Initialize (output) exec header such that useful values are
264 * obtained from subsequent N_*() macro evaluations.
267 md_init_header(hp, magic, flags)
272 if (oldmagic || magic == QMAGIC)
273 hp->a_midmag = magic;
275 N_SETMAGIC((*hp), magic, MID_I386, flags);
279 hp->a_midmag = magic;
281 N_SETMAGIC_NET((*hp), magic, MID_I386, flags);
283 N_SETMAGIC((*hp), magic, MID_I386, flags);
286 /* TEXT_START depends on the value of outheader.a_entry. */
287 if (!(link_mode & SHAREABLE))
288 hp->a_entry = PAGSIZ;
295 * Byte swap routines for cross-linking.
299 md_swapin_exec_hdr(h)
307 swap_longs((long *)h + skip, sizeof(*h)/sizeof(long) - skip);
311 md_swapout_exec_hdr(h)
314 /* NetBSD: Always leave magic alone */
317 if (N_GETMAGIC(*h) == OMAGIC)
321 swap_longs((long *)h + skip, sizeof(*h)/sizeof(long) - skip);
326 md_swapin_reloc(r, n)
327 struct relocation_info *r;
332 for (; n; n--, r++) {
333 r->r_address = md_swap_long(r->r_address);
334 bits = ((int *)r)[1];
335 r->r_symbolnum = md_swap_long(bits) & 0x00ffffff;
336 r->r_pcrel = (bits & 1);
337 r->r_length = (bits >> 1) & 3;
338 r->r_extern = (bits >> 3) & 1;
339 r->r_baserel = (bits >> 4) & 1;
340 r->r_jmptable = (bits >> 5) & 1;
341 r->r_relative = (bits >> 6) & 1;
343 r->r_copy = (bits >> 7) & 1;
349 md_swapout_reloc(r, n)
350 struct relocation_info *r;
355 for (; n; n--, r++) {
356 r->r_address = md_swap_long(r->r_address);
357 bits = md_swap_long(r->r_symbolnum) & 0xffffff00;
358 bits |= (r->r_pcrel & 1);
359 bits |= (r->r_length & 3) << 1;
360 bits |= (r->r_extern & 1) << 3;
361 bits |= (r->r_baserel & 1) << 4;
362 bits |= (r->r_jmptable & 1) << 5;
363 bits |= (r->r_relative & 1) << 6;
365 bits |= (r->r_copy & 1) << 7;
367 ((int *)r)[1] = bits;
372 md_swapout_jmpslot(j, n)
376 for (; n; n--, j++) {
377 j->opcode = md_swap_short(j->opcode);
378 j->addr[0] = md_swap_short(j->addr[0]);
379 j->addr[1] = md_swap_short(j->addr[1]);
380 j->reloc_index = md_swap_short(j->reloc_index);
384 #endif /* NEED_SWAP */