xref: /freebsd/contrib/elftoolchain/libelf/libelf_memory.c (revision d003e0d7fe0d3a9b4b2c5835bb3f0f6faf3ab538)
12de3b87aSKai Wang /*-
22de3b87aSKai Wang  * Copyright (c) 2011 Joseph Koshy
32de3b87aSKai Wang  * All rights reserved.
42de3b87aSKai Wang  *
52de3b87aSKai Wang  * Redistribution and use in source and binary forms, with or without
62de3b87aSKai Wang  * modification, are permitted provided that the following conditions
72de3b87aSKai Wang  * are met:
82de3b87aSKai Wang  * 1. Redistributions of source code must retain the above copyright
92de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer.
102de3b87aSKai Wang  * 2. Redistributions in binary form must reproduce the above copyright
112de3b87aSKai Wang  *    notice, this list of conditions and the following disclaimer in the
122de3b87aSKai Wang  *    documentation and/or other materials provided with the distribution.
132de3b87aSKai Wang  *
142de3b87aSKai Wang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152de3b87aSKai Wang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162de3b87aSKai Wang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172de3b87aSKai Wang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182de3b87aSKai Wang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192de3b87aSKai Wang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202de3b87aSKai Wang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212de3b87aSKai Wang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222de3b87aSKai Wang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232de3b87aSKai Wang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242de3b87aSKai Wang  * SUCH DAMAGE.
252de3b87aSKai Wang  */
262de3b87aSKai Wang 
272de3b87aSKai Wang #include <ar.h>
282de3b87aSKai Wang #include <assert.h>
292de3b87aSKai Wang #include <string.h>
302de3b87aSKai Wang #include <libelf.h>
312de3b87aSKai Wang 
322de3b87aSKai Wang #include "_libelf.h"
332de3b87aSKai Wang 
34*d003e0d7SEd Maste ELFTC_VCSID("$Id: libelf_memory.c 3738 2019-05-05 21:49:06Z jkoshy $");
352de3b87aSKai Wang 
362de3b87aSKai Wang /*
372de3b87aSKai Wang  * Create an ELF descriptor for a memory image, optionally reporting
382de3b87aSKai Wang  * parse errors.
392de3b87aSKai Wang  */
402de3b87aSKai Wang 
412de3b87aSKai Wang Elf *
_libelf_memory(unsigned char * image,size_t sz,int reporterror)42cf781b2eSEd Maste _libelf_memory(unsigned char *image, size_t sz, int reporterror)
432de3b87aSKai Wang {
442de3b87aSKai Wang 	Elf *e;
452de3b87aSKai Wang 	int e_class;
462de3b87aSKai Wang 	enum Elf_Error error;
472de3b87aSKai Wang 	unsigned int e_byteorder, e_version;
482de3b87aSKai Wang 
492de3b87aSKai Wang 	assert(image != NULL);
502de3b87aSKai Wang 	assert(sz > 0);
512de3b87aSKai Wang 
522de3b87aSKai Wang 	if ((e = _libelf_allocate_elf()) == NULL)
532de3b87aSKai Wang 		return (NULL);
542de3b87aSKai Wang 
552de3b87aSKai Wang 	e->e_cmd = ELF_C_READ;
562de3b87aSKai Wang 	e->e_rawfile = image;
57*d003e0d7SEd Maste 	e->e_rawsize = (off_t) sz;
582de3b87aSKai Wang 
592de3b87aSKai Wang #undef	LIBELF_IS_ELF
602de3b87aSKai Wang #define	LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && 		\
612de3b87aSKai Wang 	(P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 &&	\
622de3b87aSKai Wang 	(P)[EI_MAG3] == ELFMAG3)
632de3b87aSKai Wang 
642de3b87aSKai Wang 	if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) {
652de3b87aSKai Wang 		e_byteorder = image[EI_DATA];
662de3b87aSKai Wang 		e_class     = image[EI_CLASS];
672de3b87aSKai Wang 		e_version   = image[EI_VERSION];
682de3b87aSKai Wang 
692de3b87aSKai Wang 		error = ELF_E_NONE;
702de3b87aSKai Wang 
712de3b87aSKai Wang 		if (e_version > EV_CURRENT)
722de3b87aSKai Wang 			error = ELF_E_VERSION;
732de3b87aSKai Wang 		else if ((e_byteorder != ELFDATA2LSB && e_byteorder !=
742de3b87aSKai Wang  		    ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class !=
752de3b87aSKai Wang 		    ELFCLASS64))
762de3b87aSKai Wang 			error = ELF_E_HEADER;
772de3b87aSKai Wang 
782de3b87aSKai Wang 		if (error != ELF_E_NONE) {
792de3b87aSKai Wang 			if (reporterror) {
802de3b87aSKai Wang 				LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0);
81*d003e0d7SEd Maste 				_libelf_release_elf(e);
822de3b87aSKai Wang 				return (NULL);
832de3b87aSKai Wang 			}
842de3b87aSKai Wang 		} else {
852de3b87aSKai Wang 			_libelf_init_elf(e, ELF_K_ELF);
862de3b87aSKai Wang 
872de3b87aSKai Wang 			e->e_byteorder = e_byteorder;
882de3b87aSKai Wang 			e->e_class = e_class;
892de3b87aSKai Wang 			e->e_version = e_version;
902de3b87aSKai Wang 		}
912de3b87aSKai Wang 	} else if (sz >= SARMAG &&
92cf781b2eSEd Maste 	    strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0)
932de3b87aSKai Wang 		return (_libelf_ar_open(e, reporterror));
942de3b87aSKai Wang 
952de3b87aSKai Wang 	return (e);
962de3b87aSKai Wang }
97