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 <ar.h>
31164190Sjkoshy#include <libelf.h>
32164190Sjkoshy
33164190Sjkoshy#include "_libelf.h"
34164190Sjkoshy
35210345Skaiwstatic int
36210345Skaiw_libelf_getshdrstrndx(Elf *e, size_t *strndx)
37164190Sjkoshy{
38164190Sjkoshy	void *eh;
39164190Sjkoshy	int ec;
40164190Sjkoshy
41164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF ||
42165535Sjkoshy	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
43164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
44210345Skaiw		return (-1);
45164190Sjkoshy	}
46164190Sjkoshy
47165535Sjkoshy	if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
48210345Skaiw		return (-1);
49165535Sjkoshy
50165535Sjkoshy	*strndx = e->e_u.e_elf.e_strndx;
51165535Sjkoshy
52210345Skaiw	return (0);
53164190Sjkoshy}
54164190Sjkoshy
55164190Sjkoshyint
56210345Skaiwelf_getshdrstrndx(Elf *e, size_t *strndx)
57210345Skaiw{
58210345Skaiw	return (_libelf_getshdrstrndx(e, strndx));
59210345Skaiw}
60210345Skaiw
61210345Skaiwint
62210345Skaiwelf_getshstrndx(Elf *e, size_t *strndx)	/* Deprecated API. */
63210345Skaiw{
64210345Skaiw	return (_libelf_getshdrstrndx(e, strndx) >= 0);
65210345Skaiw}
66210345Skaiw
67210345Skaiwint
68164190Sjkoshyelf_setshstrndx(Elf *e, size_t strndx)
69164190Sjkoshy{
70164190Sjkoshy	void *eh;
71164190Sjkoshy	int ec;
72164190Sjkoshy
73164190Sjkoshy	if (e == NULL || e->e_kind != ELF_K_ELF ||
74164190Sjkoshy	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
75164190Sjkoshy	    ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) {
76164190Sjkoshy		LIBELF_SET_ERROR(ARGUMENT, 0);
77164190Sjkoshy		return (0);
78164190Sjkoshy	}
79164190Sjkoshy
80164190Sjkoshy	return (_libelf_setshstrndx(e, eh, ec, strndx));
81164190Sjkoshy}
82