xref: /dflybsd-src/contrib/elftoolchain/libelf/libelf_memory.c (revision 91deece701e3d2bfb30869db2dd6a3c0d67cfae0)
1*f8fb3368SJohn Marino /*-
2*f8fb3368SJohn Marino  * Copyright (c) 2011 Joseph Koshy
3*f8fb3368SJohn Marino  * All rights reserved.
4*f8fb3368SJohn Marino  *
5*f8fb3368SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*f8fb3368SJohn Marino  * modification, are permitted provided that the following conditions
7*f8fb3368SJohn Marino  * are met:
8*f8fb3368SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*f8fb3368SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*f8fb3368SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*f8fb3368SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*f8fb3368SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*f8fb3368SJohn Marino  *
14*f8fb3368SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*f8fb3368SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*f8fb3368SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*f8fb3368SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*f8fb3368SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*f8fb3368SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*f8fb3368SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*f8fb3368SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*f8fb3368SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*f8fb3368SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*f8fb3368SJohn Marino  * SUCH DAMAGE.
25*f8fb3368SJohn Marino  */
26*f8fb3368SJohn Marino 
27*f8fb3368SJohn Marino #include <ar.h>
28*f8fb3368SJohn Marino #include <assert.h>
29*f8fb3368SJohn Marino #include <string.h>
30*f8fb3368SJohn Marino #include <libelf.h>
31*f8fb3368SJohn Marino 
32*f8fb3368SJohn Marino #include "_libelf.h"
33*f8fb3368SJohn Marino 
34*f8fb3368SJohn Marino ELFTC_VCSID("$Id: libelf_memory.c 3013 2014-03-23 06:16:59Z jkoshy $");
35*f8fb3368SJohn Marino 
36*f8fb3368SJohn Marino /*
37*f8fb3368SJohn Marino  * Create an ELF descriptor for a memory image, optionally reporting
38*f8fb3368SJohn Marino  * parse errors.
39*f8fb3368SJohn Marino  */
40*f8fb3368SJohn Marino 
41*f8fb3368SJohn Marino Elf *
_libelf_memory(unsigned char * image,size_t sz,int reporterror)42*f8fb3368SJohn Marino _libelf_memory(unsigned char *image, size_t sz, int reporterror)
43*f8fb3368SJohn Marino {
44*f8fb3368SJohn Marino 	Elf *e;
45*f8fb3368SJohn Marino 	int e_class;
46*f8fb3368SJohn Marino 	enum Elf_Error error;
47*f8fb3368SJohn Marino 	unsigned int e_byteorder, e_version;
48*f8fb3368SJohn Marino 
49*f8fb3368SJohn Marino 	assert(image != NULL);
50*f8fb3368SJohn Marino 	assert(sz > 0);
51*f8fb3368SJohn Marino 
52*f8fb3368SJohn Marino 	if ((e = _libelf_allocate_elf()) == NULL)
53*f8fb3368SJohn Marino 		return (NULL);
54*f8fb3368SJohn Marino 
55*f8fb3368SJohn Marino 	e->e_cmd = ELF_C_READ;
56*f8fb3368SJohn Marino 	e->e_rawfile = image;
57*f8fb3368SJohn Marino 	e->e_rawsize = sz;
58*f8fb3368SJohn Marino 
59*f8fb3368SJohn Marino #undef	LIBELF_IS_ELF
60*f8fb3368SJohn Marino #define	LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && 		\
61*f8fb3368SJohn Marino 	(P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 &&	\
62*f8fb3368SJohn Marino 	(P)[EI_MAG3] == ELFMAG3)
63*f8fb3368SJohn Marino 
64*f8fb3368SJohn Marino 	if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) {
65*f8fb3368SJohn Marino 		e_byteorder = image[EI_DATA];
66*f8fb3368SJohn Marino 		e_class     = image[EI_CLASS];
67*f8fb3368SJohn Marino 		e_version   = image[EI_VERSION];
68*f8fb3368SJohn Marino 
69*f8fb3368SJohn Marino 		error = ELF_E_NONE;
70*f8fb3368SJohn Marino 
71*f8fb3368SJohn Marino 		if (e_version > EV_CURRENT)
72*f8fb3368SJohn Marino 			error = ELF_E_VERSION;
73*f8fb3368SJohn Marino 		else if ((e_byteorder != ELFDATA2LSB && e_byteorder !=
74*f8fb3368SJohn Marino  		    ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class !=
75*f8fb3368SJohn Marino 		    ELFCLASS64))
76*f8fb3368SJohn Marino 			error = ELF_E_HEADER;
77*f8fb3368SJohn Marino 
78*f8fb3368SJohn Marino 		if (error != ELF_E_NONE) {
79*f8fb3368SJohn Marino 			if (reporterror) {
80*f8fb3368SJohn Marino 				LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0);
81*f8fb3368SJohn Marino 				(void) _libelf_release_elf(e);
82*f8fb3368SJohn Marino 				return (NULL);
83*f8fb3368SJohn Marino 			}
84*f8fb3368SJohn Marino 		} else {
85*f8fb3368SJohn Marino 			_libelf_init_elf(e, ELF_K_ELF);
86*f8fb3368SJohn Marino 
87*f8fb3368SJohn Marino 			e->e_byteorder = e_byteorder;
88*f8fb3368SJohn Marino 			e->e_class = e_class;
89*f8fb3368SJohn Marino 			e->e_version = e_version;
90*f8fb3368SJohn Marino 		}
91*f8fb3368SJohn Marino 	} else if (sz >= SARMAG &&
92*f8fb3368SJohn Marino 	    strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0)
93*f8fb3368SJohn Marino 		return (_libelf_ar_open(e, reporterror));
94*f8fb3368SJohn Marino 
95*f8fb3368SJohn Marino 	return (e);
96*f8fb3368SJohn Marino }
97