_relocate.c revision 5aefb6555731130ca4fd295960123d71f2d21fe8
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe/*
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * CDDL HEADER START
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * The contents of this file are subject to the terms of the
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * Common Development and Distribution License (the "License").
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * You may not use this file except in compliance with the License.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * or http://www.opensolaris.org/os/licensing.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * See the License for the specific language governing permissions
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * and limitations under the License.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * If applicable, add the following below this CDDL HEADER, with the
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * CDDL HEADER END
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe */
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe/*
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * Use is subject to license terms.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe */
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe#pragma ident "%Z%%M% %I% %E% SMI"
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe#include <string.h>
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe#include "machdep.h"
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe#include "reloc.h"
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe#include "_librtld.h"
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe#include "_elf.h"
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe/*
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * Undo relocations that have been applied to a memory image. Basically this
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * involves copying the original files relocation offset into the new image
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * being created.
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe */
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowevoid
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Loweundo_reloc(void *vrel, uchar_t *oaddr, uchar_t *iaddr, Reloc *reloc)
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe{
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe Rel *rel = vrel;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe /* LINTED */
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe ulong_t *_oaddr = (ulong_t *)oaddr;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe /* LINTED */
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe ulong_t *_iaddr = (ulong_t *)iaddr;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe switch (ELF_R_TYPE(rel->r_info)) {
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe case R_386_NONE:
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe break;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe case R_386_COPY:
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe (void) memset((void *)oaddr, 0, (size_t)reloc->r_size);
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe break;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe case R_386_JMP_SLOT:
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe if (_iaddr)
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *_oaddr = *_iaddr + reloc->r_value;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe else
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *_oaddr = reloc->r_value;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe break;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe default:
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe if (_iaddr)
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *_oaddr = *_iaddr;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe else
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe *_oaddr = 0;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe break;
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe }
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe}
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe/*
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * Copy a relocation record and increment its value. The record must reflect
d2a70789f056fc6c9ce3ab047b52126d80b0e3daRichard Lowe * the new address to which this image is fixed. Note that .got entries
* associated with .plt's must be fixed to the new base address.
*/
void
inc_reloc(void *vnrel, void *vorel, Reloc *reloc, uchar_t *oaddr,
uchar_t *iaddr)
{
Rel *nrel = vnrel;
Rel *orel = vorel;
/* LINTED */
ulong_t *_oaddr = (ulong_t *)oaddr;
/* LINTED */
ulong_t *_iaddr = (ulong_t *)iaddr;
if (ELF_R_TYPE(nrel->r_info) == R_386_JMP_SLOT) {
if (_iaddr)
*_oaddr = *_iaddr + reloc->r_value;
else
*_oaddr = reloc->r_value;
}
*nrel = *orel;
nrel->r_offset += reloc->r_value;
}
/*
* Clear a relocation record. The relocation has been applied to the image and
* thus the relocation must not occur again.
*/
void
clear_reloc(void *vrel)
{
Rel *rel = vrel;
rel->r_offset = 0;
rel->r_info = ELF_R_INFO(0, R_386_NONE);
}
/*
* Apply a relocation to an image being built from an input file. Use the
* runtime linkers routines to do the necessary magic.
*/
void
apply_reloc(void *vrel, Reloc *reloc, const char *name, uchar_t *oaddr,
Rt_map *lmp)
{
Rel *rel = vrel;
Xword type = ELF_R_TYPE(rel->r_info);
Word value = reloc->r_value;
if (type == R_386_JMP_SLOT) {
uintptr_t addr, vaddr;
if (FLAGS(lmp) & FLG_RT_FIXED)
vaddr = 0;
else
vaddr = ADDR(lmp);
addr = (uintptr_t)oaddr - rel->r_offset;
/* LINTED */
elf_plt_write((uintptr_t)addr, vaddr, rel,
(uintptr_t)value, reloc->r_pltndx);
} else if (type == R_386_COPY) {
(void) memcpy((void *)oaddr, (void *)value,
(size_t)reloc->r_size);
} else {
(void) do_reloc(type, oaddr, &value, reloc->r_name, name,
LIST(lmp));
}
}