1*a1b5ec25Sjsg /*-
2*a1b5ec25Sjsg * Copyright (c) 2006,2008-2011 Joseph Koshy
3*a1b5ec25Sjsg * All rights reserved.
4*a1b5ec25Sjsg *
5*a1b5ec25Sjsg * Redistribution and use in source and binary forms, with or without
6*a1b5ec25Sjsg * modification, are permitted provided that the following conditions
7*a1b5ec25Sjsg * are met:
8*a1b5ec25Sjsg * 1. Redistributions of source code must retain the above copyright
9*a1b5ec25Sjsg * notice, this list of conditions and the following disclaimer.
10*a1b5ec25Sjsg * 2. Redistributions in binary form must reproduce the above copyright
11*a1b5ec25Sjsg * notice, this list of conditions and the following disclaimer in the
12*a1b5ec25Sjsg * documentation and/or other materials provided with the distribution.
13*a1b5ec25Sjsg *
14*a1b5ec25Sjsg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*a1b5ec25Sjsg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*a1b5ec25Sjsg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*a1b5ec25Sjsg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*a1b5ec25Sjsg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*a1b5ec25Sjsg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*a1b5ec25Sjsg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*a1b5ec25Sjsg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*a1b5ec25Sjsg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*a1b5ec25Sjsg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*a1b5ec25Sjsg * SUCH DAMAGE.
25*a1b5ec25Sjsg */
26*a1b5ec25Sjsg
27*a1b5ec25Sjsg #include <libelf.h>
28*a1b5ec25Sjsg
29*a1b5ec25Sjsg #include "_libelf.h"
30*a1b5ec25Sjsg
31*a1b5ec25Sjsg ELFTC_VCSID("$Id: elf_begin.c,v 1.1 2019/02/01 05:27:37 jsg Exp $");
32*a1b5ec25Sjsg
33*a1b5ec25Sjsg Elf *
elf_begin(int fd,Elf_Cmd c,Elf * a)34*a1b5ec25Sjsg elf_begin(int fd, Elf_Cmd c, Elf *a)
35*a1b5ec25Sjsg {
36*a1b5ec25Sjsg Elf *e;
37*a1b5ec25Sjsg
38*a1b5ec25Sjsg e = NULL;
39*a1b5ec25Sjsg
40*a1b5ec25Sjsg if (LIBELF_PRIVATE(version) == EV_NONE) {
41*a1b5ec25Sjsg LIBELF_SET_ERROR(SEQUENCE, 0);
42*a1b5ec25Sjsg return (NULL);
43*a1b5ec25Sjsg }
44*a1b5ec25Sjsg
45*a1b5ec25Sjsg switch (c) {
46*a1b5ec25Sjsg case ELF_C_NULL:
47*a1b5ec25Sjsg return (NULL);
48*a1b5ec25Sjsg
49*a1b5ec25Sjsg case ELF_C_WRITE:
50*a1b5ec25Sjsg /*
51*a1b5ec25Sjsg * The ELF_C_WRITE command is required to ignore the
52*a1b5ec25Sjsg * descriptor passed in.
53*a1b5ec25Sjsg */
54*a1b5ec25Sjsg a = NULL;
55*a1b5ec25Sjsg break;
56*a1b5ec25Sjsg
57*a1b5ec25Sjsg case ELF_C_RDWR:
58*a1b5ec25Sjsg if (a != NULL) { /* not allowed for ar(1) archives. */
59*a1b5ec25Sjsg LIBELF_SET_ERROR(ARGUMENT, 0);
60*a1b5ec25Sjsg return (NULL);
61*a1b5ec25Sjsg }
62*a1b5ec25Sjsg /*FALLTHROUGH*/
63*a1b5ec25Sjsg case ELF_C_READ:
64*a1b5ec25Sjsg /*
65*a1b5ec25Sjsg * Descriptor `a' could be for a regular ELF file, or
66*a1b5ec25Sjsg * for an ar(1) archive. If descriptor `a' was opened
67*a1b5ec25Sjsg * using a valid file descriptor, we need to check if
68*a1b5ec25Sjsg * the passed in `fd' value matches the original one.
69*a1b5ec25Sjsg */
70*a1b5ec25Sjsg if (a &&
71*a1b5ec25Sjsg ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) {
72*a1b5ec25Sjsg LIBELF_SET_ERROR(ARGUMENT, 0);
73*a1b5ec25Sjsg return (NULL);
74*a1b5ec25Sjsg }
75*a1b5ec25Sjsg break;
76*a1b5ec25Sjsg
77*a1b5ec25Sjsg default:
78*a1b5ec25Sjsg LIBELF_SET_ERROR(ARGUMENT, 0);
79*a1b5ec25Sjsg return (NULL);
80*a1b5ec25Sjsg
81*a1b5ec25Sjsg }
82*a1b5ec25Sjsg
83*a1b5ec25Sjsg if (a == NULL)
84*a1b5ec25Sjsg e = _libelf_open_object(fd, c, 1);
85*a1b5ec25Sjsg else if (a->e_kind == ELF_K_AR)
86*a1b5ec25Sjsg e = _libelf_ar_open_member(a->e_fd, c, a);
87*a1b5ec25Sjsg else
88*a1b5ec25Sjsg (e = a)->e_activations++;
89*a1b5ec25Sjsg
90*a1b5ec25Sjsg return (e);
91*a1b5ec25Sjsg }
92