xref: /openbsd-src/lib/libelf/elf_getident.c (revision e219834f801e78888637194a4612bc3229c35e9c)
1a1b5ec25Sjsg /*-
2a1b5ec25Sjsg  * Copyright (c) 2006,2008 Joseph Koshy
3a1b5ec25Sjsg  * All rights reserved.
4a1b5ec25Sjsg  *
5a1b5ec25Sjsg  * Redistribution and use in source and binary forms, with or without
6a1b5ec25Sjsg  * modification, are permitted provided that the following conditions
7a1b5ec25Sjsg  * are met:
8a1b5ec25Sjsg  * 1. Redistributions of source code must retain the above copyright
9a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer.
10a1b5ec25Sjsg  * 2. Redistributions in binary form must reproduce the above copyright
11a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer in the
12a1b5ec25Sjsg  *    documentation and/or other materials provided with the distribution.
13a1b5ec25Sjsg  *
14a1b5ec25Sjsg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a1b5ec25Sjsg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a1b5ec25Sjsg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a1b5ec25Sjsg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a1b5ec25Sjsg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a1b5ec25Sjsg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a1b5ec25Sjsg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a1b5ec25Sjsg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a1b5ec25Sjsg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a1b5ec25Sjsg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a1b5ec25Sjsg  * SUCH DAMAGE.
25a1b5ec25Sjsg  */
26a1b5ec25Sjsg 
27a1b5ec25Sjsg #include <ar.h>
28a1b5ec25Sjsg #include <assert.h>
29a1b5ec25Sjsg #include <libelf.h>
30a1b5ec25Sjsg 
31a1b5ec25Sjsg #include "_libelf.h"
32a1b5ec25Sjsg 
33*e219834fSjsg ELFTC_VCSID("$Id: elf_getident.c,v 1.2 2019/03/19 02:31:35 jsg Exp $");
34a1b5ec25Sjsg 
35a1b5ec25Sjsg char *
elf_getident(Elf * e,size_t * sz)36a1b5ec25Sjsg elf_getident(Elf *e, size_t *sz)
37a1b5ec25Sjsg {
38a1b5ec25Sjsg 
39a1b5ec25Sjsg 	if (e == NULL) {
40a1b5ec25Sjsg 		LIBELF_SET_ERROR(ARGUMENT, 0);
41a1b5ec25Sjsg 		goto error;
42a1b5ec25Sjsg 	}
43a1b5ec25Sjsg 
44a1b5ec25Sjsg 	if (e->e_cmd == ELF_C_WRITE && e->e_rawfile == NULL) {
45a1b5ec25Sjsg 		LIBELF_SET_ERROR(SEQUENCE, 0);
46a1b5ec25Sjsg 		goto error;
47a1b5ec25Sjsg 	}
48a1b5ec25Sjsg 
49a1b5ec25Sjsg 	assert(e->e_kind != ELF_K_AR || e->e_cmd == ELF_C_READ);
50a1b5ec25Sjsg 
51a1b5ec25Sjsg 	if (sz) {
52a1b5ec25Sjsg 		if (e->e_kind == ELF_K_AR)
53a1b5ec25Sjsg 			*sz = SARMAG;
54a1b5ec25Sjsg 		else if (e->e_kind == ELF_K_ELF)
55a1b5ec25Sjsg 			*sz = EI_NIDENT;
56a1b5ec25Sjsg 		else
57*e219834fSjsg 			*sz = (size_t) e->e_rawsize;
58a1b5ec25Sjsg 	}
59a1b5ec25Sjsg 
60a1b5ec25Sjsg 	return ((char *) e->e_rawfile);
61a1b5ec25Sjsg 
62a1b5ec25Sjsg  error:
63a1b5ec25Sjsg 	if (sz)
64a1b5ec25Sjsg 		*sz = 0;
65a1b5ec25Sjsg 	return (NULL);
66a1b5ec25Sjsg }
67