1*0a6a1f1dSLionel Sambuc.\" $NetBSD: elf_memory.3,v 1.2 2014/03/09 16:58:04 christos Exp $ 2*0a6a1f1dSLionel Sambuc.\" 3*0a6a1f1dSLionel Sambuc.\" Copyright (c) 2006,2008 Joseph Koshy. All rights reserved. 4*0a6a1f1dSLionel Sambuc.\" 5*0a6a1f1dSLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 6*0a6a1f1dSLionel Sambuc.\" modification, are permitted provided that the following conditions 7*0a6a1f1dSLionel Sambuc.\" are met: 8*0a6a1f1dSLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 9*0a6a1f1dSLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 10*0a6a1f1dSLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 11*0a6a1f1dSLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 12*0a6a1f1dSLionel Sambuc.\" documentation and/or other materials provided with the distribution. 13*0a6a1f1dSLionel Sambuc.\" 14*0a6a1f1dSLionel Sambuc.\" This software is provided by Joseph Koshy ``as is'' and 15*0a6a1f1dSLionel Sambuc.\" any express or implied warranties, including, but not limited to, the 16*0a6a1f1dSLionel Sambuc.\" implied warranties of merchantability and fitness for a particular purpose 17*0a6a1f1dSLionel Sambuc.\" are disclaimed. in no event shall Joseph Koshy be liable 18*0a6a1f1dSLionel Sambuc.\" for any direct, indirect, incidental, special, exemplary, or consequential 19*0a6a1f1dSLionel Sambuc.\" damages (including, but not limited to, procurement of substitute goods 20*0a6a1f1dSLionel Sambuc.\" or services; loss of use, data, or profits; or business interruption) 21*0a6a1f1dSLionel Sambuc.\" however caused and on any theory of liability, whether in contract, strict 22*0a6a1f1dSLionel Sambuc.\" liability, or tort (including negligence or otherwise) arising in any way 23*0a6a1f1dSLionel Sambuc.\" out of the use of this software, even if advised of the possibility of 24*0a6a1f1dSLionel Sambuc.\" such damage. 25*0a6a1f1dSLionel Sambuc.\" 26*0a6a1f1dSLionel Sambuc.\" Id: elf_memory.3 2314 2011-12-11 06:19:51Z jkoshy 27*0a6a1f1dSLionel Sambuc.\" 28*0a6a1f1dSLionel Sambuc.Dd June 28, 2006 29*0a6a1f1dSLionel Sambuc.Os 30*0a6a1f1dSLionel Sambuc.Dt ELF_MEMORY 3 31*0a6a1f1dSLionel Sambuc.Sh NAME 32*0a6a1f1dSLionel Sambuc.Nm elf_memory 33*0a6a1f1dSLionel Sambuc.Nd process an ELF or ar(1) archive mapped into memory 34*0a6a1f1dSLionel Sambuc.Sh LIBRARY 35*0a6a1f1dSLionel Sambuc.Lb libelf 36*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS 37*0a6a1f1dSLionel Sambuc.In libelf.h 38*0a6a1f1dSLionel Sambuc.Ft "Elf *" 39*0a6a1f1dSLionel Sambuc.Fn elf_memory "char *image" "size_t size" 40*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION 41*0a6a1f1dSLionel SambucFunction 42*0a6a1f1dSLionel Sambuc.Fn elf_memory 43*0a6a1f1dSLionel Sambucis used to process an ELF file or 44*0a6a1f1dSLionel Sambuc.Xr ar 1 45*0a6a1f1dSLionel Sambucarchive whose image is present in memory. 46*0a6a1f1dSLionel Sambuc.Pp 47*0a6a1f1dSLionel SambucArgument 48*0a6a1f1dSLionel Sambuc.Ar image 49*0a6a1f1dSLionel Sambucpoints to the start of the memory image of the file or archive. 50*0a6a1f1dSLionel SambucArgument 51*0a6a1f1dSLionel Sambuc.Ar size 52*0a6a1f1dSLionel Sambuccontains the size in bytes of the memory image. 53*0a6a1f1dSLionel Sambuc.Pp 54*0a6a1f1dSLionel SambucThe ELF descriptor is created for reading (i.e., analogous to the 55*0a6a1f1dSLionel Sambucuse of 56*0a6a1f1dSLionel Sambuc.Xr elf_begin 3 57*0a6a1f1dSLionel Sambucwith a command argument value of 58*0a6a1f1dSLionel Sambuc.Dv ELF_C_READ Ns ). 59*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES 60*0a6a1f1dSLionel SambucFunction 61*0a6a1f1dSLionel Sambuc.Fn elf_memory 62*0a6a1f1dSLionel Sambucreturns a pointer to a new ELF descriptor if successful, or NULL if an 63*0a6a1f1dSLionel Sambucerror occurred. 64*0a6a1f1dSLionel Sambuc.Pp 65*0a6a1f1dSLionel SambucThe return value may be queried for the file type using 66*0a6a1f1dSLionel Sambuc.Xr elf_kind 3 . 67*0a6a1f1dSLionel Sambuc.Sh EXAMPLES 68*0a6a1f1dSLionel SambucTo read parse an elf file, use: 69*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 70*0a6a1f1dSLionel Sambucint fd; 71*0a6a1f1dSLionel Sambucvoid *p; 72*0a6a1f1dSLionel Sambucstruct stat sb; 73*0a6a1f1dSLionel SambucElf *e; 74*0a6a1f1dSLionel Sambuc\&... 75*0a6a1f1dSLionel Sambucif ((fd = open("./elf-file", O_RDONLY)) < 0 || 76*0a6a1f1dSLionel Sambuc fstat(fd, &sb) < 0 || 77*0a6a1f1dSLionel Sambuc (p = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0)) == 78*0a6a1f1dSLionel Sambuc MAP_FAILED) { 79*0a6a1f1dSLionel Sambuc ... handle system error ... 80*0a6a1f1dSLionel Sambuc} 81*0a6a1f1dSLionel Sambuc 82*0a6a1f1dSLionel Sambucif ((e = elf_memory(p, sb.st_size)) == NULL) { 83*0a6a1f1dSLionel Sambuc ... handle elf(3) error ... 84*0a6a1f1dSLionel Sambuc} 85*0a6a1f1dSLionel Sambuc\&... use ELF descriptor "e" here ... 86*0a6a1f1dSLionel Sambuc.Ed 87*0a6a1f1dSLionel Sambuc.Sh ERRORS 88*0a6a1f1dSLionel SambucFunction 89*0a6a1f1dSLionel Sambuc.Fn elf_memory 90*0a6a1f1dSLionel Sambuccan fail with the following errors: 91*0a6a1f1dSLionel Sambuc.Bl -tag -width "[ELF_E_RESOURCE]" 92*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 93*0a6a1f1dSLionel SambucA NULL value was used for argument 94*0a6a1f1dSLionel Sambuc.Ar image 95*0a6a1f1dSLionel Sambucor the value of argument 96*0a6a1f1dSLionel Sambuc.Ar sz 97*0a6a1f1dSLionel Sambucwas zero. 98*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_HEADER 99*0a6a1f1dSLionel SambucThe header of the ELF object contained an unsupported value in its 100*0a6a1f1dSLionel Sambuc.Va e_ident[EI_CLASS] 101*0a6a1f1dSLionel Sambucfield. 102*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_HEADER 103*0a6a1f1dSLionel SambucThe header of the ELF object contained an unsupported value in its 104*0a6a1f1dSLionel Sambuc.Va e_ident[EI_DATA] 105*0a6a1f1dSLionel Sambucfield. 106*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_RESOURCE 107*0a6a1f1dSLionel SambucAn out of memory condition was detected. 108*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_SEQUENCE 109*0a6a1f1dSLionel SambucFunction 110*0a6a1f1dSLionel Sambuc.Fn elf_memory 111*0a6a1f1dSLionel Sambucwas called before a working version was set using 112*0a6a1f1dSLionel Sambuc.Xr elf_version 3 . 113*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_VERSION 114*0a6a1f1dSLionel SambucThe ELF object referenced by argument 115*0a6a1f1dSLionel Sambuc.Ar image 116*0a6a1f1dSLionel Sambucwas of an unsupported ELF version. 117*0a6a1f1dSLionel Sambuc.El 118*0a6a1f1dSLionel Sambuc.Sh SEE ALSO 119*0a6a1f1dSLionel Sambuc.Xr elf 3 , 120*0a6a1f1dSLionel Sambuc.Xr elf_begin 3 , 121*0a6a1f1dSLionel Sambuc.Xr elf_end 3 , 122*0a6a1f1dSLionel Sambuc.Xr elf_errno 3 , 123*0a6a1f1dSLionel Sambuc.Xr elf_kind 3 , 124*0a6a1f1dSLionel Sambuc.Xr gelf 3 125