1260684Skaiw/*-
2260684Skaiw * Copyright (c) 2006,2008-2011 Joseph Koshy
3260684Skaiw * All rights reserved.
4260684Skaiw *
5260684Skaiw * Redistribution and use in source and binary forms, with or without
6260684Skaiw * modification, are permitted provided that the following conditions
7260684Skaiw * are met:
8260684Skaiw * 1. Redistributions of source code must retain the above copyright
9260684Skaiw *    notice, this list of conditions and the following disclaimer.
10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright
11260684Skaiw *    notice, this list of conditions and the following disclaimer in the
12260684Skaiw *    documentation and/or other materials provided with the distribution.
13260684Skaiw *
14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17260684Skaiw * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24260684Skaiw * SUCH DAMAGE.
25260684Skaiw */
26260684Skaiw
27260684Skaiw#include <libelf.h>
28260684Skaiw
29260684Skaiw#include "_libelf.h"
30260684Skaiw
31260684SkaiwELFTC_VCSID("$Id: elf_begin.c 2364 2011-12-28 17:55:25Z jkoshy $");
32260684Skaiw
33260684SkaiwElf *
34260684Skaiwelf_begin(int fd, Elf_Cmd c, Elf *a)
35260684Skaiw{
36260684Skaiw	Elf *e;
37260684Skaiw
38260684Skaiw	e = NULL;
39260684Skaiw
40260684Skaiw	if (LIBELF_PRIVATE(version) == EV_NONE) {
41260684Skaiw		LIBELF_SET_ERROR(SEQUENCE, 0);
42260684Skaiw		return (NULL);
43260684Skaiw	}
44260684Skaiw
45260684Skaiw	switch (c) {
46260684Skaiw	case ELF_C_NULL:
47260684Skaiw		return (NULL);
48260684Skaiw
49260684Skaiw	case ELF_C_WRITE:
50260684Skaiw		/*
51260684Skaiw		 * The ELF_C_WRITE command is required to ignore the
52260684Skaiw		 * descriptor passed in.
53260684Skaiw		 */
54260684Skaiw		a = NULL;
55260684Skaiw		break;
56260684Skaiw
57260684Skaiw	case ELF_C_RDWR:
58260684Skaiw		if (a != NULL) { /* not allowed for ar(1) archives. */
59260684Skaiw			LIBELF_SET_ERROR(ARGUMENT, 0);
60260684Skaiw			return (NULL);
61260684Skaiw		}
62260684Skaiw		/*FALLTHROUGH*/
63260684Skaiw	case ELF_C_READ:
64260684Skaiw		/*
65260684Skaiw		 * Descriptor `a' could be for a regular ELF file, or
66260684Skaiw		 * for an ar(1) archive.  If descriptor `a' was opened
67260684Skaiw		 * using a valid file descriptor, we need to check if
68260684Skaiw		 * the passed in `fd' value matches the original one.
69260684Skaiw		 */
70260684Skaiw		if (a &&
71260684Skaiw		    ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) {
72260684Skaiw			LIBELF_SET_ERROR(ARGUMENT, 0);
73260684Skaiw			return (NULL);
74260684Skaiw		}
75260684Skaiw		break;
76260684Skaiw
77260684Skaiw	default:
78260684Skaiw		LIBELF_SET_ERROR(ARGUMENT, 0);
79260684Skaiw		return (NULL);
80260684Skaiw
81260684Skaiw	}
82260684Skaiw
83260684Skaiw	if (a == NULL)
84260684Skaiw		e = _libelf_open_object(fd, c, 1);
85260684Skaiw	else if (a->e_kind == ELF_K_AR)
86260684Skaiw		e = _libelf_ar_open_member(a->e_fd, c, a);
87260684Skaiw	else
88260684Skaiw		(e = a)->e_activations++;
89260684Skaiw
90260684Skaiw	return (e);
91260684Skaiw}
92