1260684Skaiw/*- 2260684Skaiw * Copyright (c) 2011 Joseph Koshy 3260684Skaiw * All rights reserved. 4260684Skaiw * 5260684Skaiw * Redistribution and use in source and binary forms, with or without 6260684Skaiw * modification, are permitted provided that the following conditions 7260684Skaiw * are met: 8260684Skaiw * 1. Redistributions of source code must retain the above copyright 9260684Skaiw * notice, this list of conditions and the following disclaimer. 10260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright 11260684Skaiw * notice, this list of conditions and the following disclaimer in the 12260684Skaiw * documentation and/or other materials provided with the distribution. 13260684Skaiw * 14260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15260684Skaiw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16260684Skaiw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17260684Skaiw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18260684Skaiw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19260684Skaiw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20260684Skaiw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21260684Skaiw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22260684Skaiw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23260684Skaiw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24260684Skaiw * SUCH DAMAGE. 25260684Skaiw */ 26260684Skaiw 27260684Skaiw#include <ar.h> 28260684Skaiw#include <assert.h> 29260684Skaiw#include <string.h> 30260684Skaiw#include <libelf.h> 31260684Skaiw 32260684Skaiw#include "_libelf.h" 33260684Skaiw 34276371SemasteELFTC_VCSID("$Id: libelf_memory.c 3013 2014-03-23 06:16:59Z jkoshy $"); 35260684Skaiw 36260684Skaiw/* 37260684Skaiw * Create an ELF descriptor for a memory image, optionally reporting 38260684Skaiw * parse errors. 39260684Skaiw */ 40260684Skaiw 41260684SkaiwElf * 42276371Semaste_libelf_memory(unsigned char *image, size_t sz, int reporterror) 43260684Skaiw{ 44260684Skaiw Elf *e; 45260684Skaiw int e_class; 46260684Skaiw enum Elf_Error error; 47260684Skaiw unsigned int e_byteorder, e_version; 48260684Skaiw 49260684Skaiw assert(image != NULL); 50260684Skaiw assert(sz > 0); 51260684Skaiw 52260684Skaiw if ((e = _libelf_allocate_elf()) == NULL) 53260684Skaiw return (NULL); 54260684Skaiw 55260684Skaiw e->e_cmd = ELF_C_READ; 56260684Skaiw e->e_rawfile = image; 57260684Skaiw e->e_rawsize = sz; 58260684Skaiw 59260684Skaiw#undef LIBELF_IS_ELF 60260684Skaiw#define LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && \ 61260684Skaiw (P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 && \ 62260684Skaiw (P)[EI_MAG3] == ELFMAG3) 63260684Skaiw 64260684Skaiw if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) { 65260684Skaiw e_byteorder = image[EI_DATA]; 66260684Skaiw e_class = image[EI_CLASS]; 67260684Skaiw e_version = image[EI_VERSION]; 68260684Skaiw 69260684Skaiw error = ELF_E_NONE; 70260684Skaiw 71260684Skaiw if (e_version > EV_CURRENT) 72260684Skaiw error = ELF_E_VERSION; 73260684Skaiw else if ((e_byteorder != ELFDATA2LSB && e_byteorder != 74260684Skaiw ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class != 75260684Skaiw ELFCLASS64)) 76260684Skaiw error = ELF_E_HEADER; 77260684Skaiw 78260684Skaiw if (error != ELF_E_NONE) { 79260684Skaiw if (reporterror) { 80260684Skaiw LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0); 81260684Skaiw (void) _libelf_release_elf(e); 82260684Skaiw return (NULL); 83260684Skaiw } 84260684Skaiw } else { 85260684Skaiw _libelf_init_elf(e, ELF_K_ELF); 86260684Skaiw 87260684Skaiw e->e_byteorder = e_byteorder; 88260684Skaiw e->e_class = e_class; 89260684Skaiw e->e_version = e_version; 90260684Skaiw } 91260684Skaiw } else if (sz >= SARMAG && 92276371Semaste strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0) 93260684Skaiw return (_libelf_ar_open(e, reporterror)); 94260684Skaiw 95260684Skaiw return (e); 96260684Skaiw} 97