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