1164190Sjkoshy/*-
2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy
3164190Sjkoshy * All rights reserved.
4164190Sjkoshy *
5164190Sjkoshy * Redistribution and use in source and binary forms, with or without
6164190Sjkoshy * modification, are permitted provided that the following conditions
7164190Sjkoshy * are met:
8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright
9164190Sjkoshy *    notice, this list of conditions and the following disclaimer.
10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
11164190Sjkoshy *    notice, this list of conditions and the following disclaimer in the
12164190Sjkoshy *    documentation and/or other materials provided with the distribution.
13164190Sjkoshy *
14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164190Sjkoshy * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164190Sjkoshy * SUCH DAMAGE.
25164190Sjkoshy */
26164190Sjkoshy
27164190Sjkoshy#include <sys/cdefs.h>
28164190Sjkoshy__FBSDID("$FreeBSD$");
29164190Sjkoshy
30164190Sjkoshy#include <sys/limits.h>
31164190Sjkoshy
32164190Sjkoshy#include <assert.h>
33164190Sjkoshy#include <gelf.h>
34165317Sjkoshy#include <osreldate.h>
35164190Sjkoshy
36164190Sjkoshy#include "_libelf.h"
37164190Sjkoshy
38165317Sjkoshy#if	__FreeBSD_version >= 700025
39165317Sjkoshy
40164190SjkoshyGElf_Move *
41164190Sjkoshygelf_getmove(Elf_Data *d, int ndx, GElf_Move *dst)
42164190Sjkoshy{
43164190Sjkoshy	int ec;
44164190Sjkoshy	Elf *e;
45164190Sjkoshy	Elf_Scn *scn;
46164190Sjkoshy	Elf32_Move *move32;
47164190Sjkoshy	Elf64_Move *move64;
48164190Sjkoshy	size_t msz;
49164190Sjkoshy	uint32_t sh_type;
50164190Sjkoshy
51164190Sjkoshy	if (d == NULL || ndx < 0 || dst == NULL ||
52164190Sjkoshy	    (scn = d->d_scn) == NULL ||
53164190Sjkoshy	    (e = scn->s_elf) == NULL) {
54164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
55164190Sjkoshy		return (NULL);
56164190Sjkoshy	}
57164190Sjkoshy
58164190Sjkoshy	ec = e->e_class;
59164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
60164190Sjkoshy
61164190Sjkoshy	if (ec == ELFCLASS32)
62164190Sjkoshy		sh_type = scn->s_shdr.s_shdr32.sh_type;
63164190Sjkoshy	else
64164190Sjkoshy		sh_type = scn->s_shdr.s_shdr64.sh_type;
65164190Sjkoshy
66164190Sjkoshy	if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) {
67164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
68164190Sjkoshy		return (NULL);
69164190Sjkoshy	}
70164190Sjkoshy
71164190Sjkoshy	msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version);
72164190Sjkoshy
73164190Sjkoshy	assert(msz > 0);
74164190Sjkoshy
75164190Sjkoshy	if (msz * ndx >= d->d_size) {
76164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
77164190Sjkoshy		return (NULL);
78164190Sjkoshy	}
79164190Sjkoshy
80164190Sjkoshy	if (ec == ELFCLASS32) {
81164190Sjkoshy
82164190Sjkoshy		move32 = (Elf32_Move *) d->d_buf + ndx;
83164190Sjkoshy
84164190Sjkoshy		dst->m_value   = move32->m_value;
85164190Sjkoshy		dst->m_info    = (Elf64_Xword) move32->m_info;
86164190Sjkoshy		dst->m_poffset = (Elf64_Xword) move32->m_poffset;
87164190Sjkoshy		dst->m_repeat  = move32->m_repeat;
88164190Sjkoshy		dst->m_stride = move32->m_stride;
89164190Sjkoshy	} else {
90164190Sjkoshy
91164190Sjkoshy		move64 = (Elf64_Move *) d->d_buf + ndx;
92164190Sjkoshy
93164190Sjkoshy		*dst = *move64;
94164190Sjkoshy	}
95164190Sjkoshy
96164190Sjkoshy	return (dst);
97164190Sjkoshy}
98164190Sjkoshy
99164190Sjkoshyint
100164190Sjkoshygelf_update_move(Elf_Data *d, int ndx, GElf_Move *gm)
101164190Sjkoshy{
102164190Sjkoshy	int ec;
103164190Sjkoshy	Elf *e;
104164190Sjkoshy	Elf_Scn *scn;
105164190Sjkoshy	Elf32_Move *move32;
106164190Sjkoshy	Elf64_Move *move64;
107164190Sjkoshy	size_t msz;
108164190Sjkoshy	uint32_t sh_type;
109164190Sjkoshy
110164190Sjkoshy	if (d == NULL || ndx < 0 || gm == NULL ||
111164190Sjkoshy	    (scn = d->d_scn) == NULL ||
112164190Sjkoshy	    (e = scn->s_elf) == NULL) {
113164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
114164190Sjkoshy		return (0);
115164190Sjkoshy	}
116164190Sjkoshy
117164190Sjkoshy	ec = e->e_class;
118164190Sjkoshy	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
119164190Sjkoshy
120164190Sjkoshy	if (ec == ELFCLASS32)
121164190Sjkoshy		sh_type = scn->s_shdr.s_shdr32.sh_type;
122164190Sjkoshy	else
123164190Sjkoshy		sh_type = scn->s_shdr.s_shdr64.sh_type;
124164190Sjkoshy
125164190Sjkoshy	if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) {
126164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
127164190Sjkoshy		return (0);
128164190Sjkoshy	}
129164190Sjkoshy
130164190Sjkoshy	msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version);
131164190Sjkoshy	assert(msz > 0);
132164190Sjkoshy
133164190Sjkoshy	if (msz * ndx >= d->d_size) {
134164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
135164190Sjkoshy		return (0);
136164190Sjkoshy	}
137164190Sjkoshy
138164190Sjkoshy	if (ec == ELFCLASS32) {
139164190Sjkoshy		move32 = (Elf32_Move *) d->d_buf + ndx;
140164190Sjkoshy
141164190Sjkoshy		move32->m_value  = gm->m_value;
142164190Sjkoshy		LIBELF_COPY_U32(move32, gm, m_info);
143164190Sjkoshy		LIBELF_COPY_U32(move32, gm, m_poffset);
144164190Sjkoshy		move32->m_repeat  = gm->m_repeat;
145164190Sjkoshy		move32->m_stride = gm->m_stride;
146164190Sjkoshy
147164190Sjkoshy	} else {
148164190Sjkoshy		move64 = (Elf64_Move *) d->d_buf + ndx;
149164190Sjkoshy
150164190Sjkoshy		*move64 = *gm;
151164190Sjkoshy	}
152164190Sjkoshy
153164190Sjkoshy	return (1);
154164190Sjkoshy}
155165317Sjkoshy
156165317Sjkoshy#endif	/* __FreeBSD_version >= 700025 */
157