xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/elf_memory.3 (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos.\"	$NetBSD: elf_memory.3,v 1.6 2024/03/03 17:37:33 christos Exp $
2e81373b4Schristos.\"
39dd9d0cfSchristos.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
49dd9d0cfSchristos.\"
59dd9d0cfSchristos.\" Redistribution and use in source and binary forms, with or without
69dd9d0cfSchristos.\" modification, are permitted provided that the following conditions
79dd9d0cfSchristos.\" are met:
89dd9d0cfSchristos.\" 1. Redistributions of source code must retain the above copyright
99dd9d0cfSchristos.\"    notice, this list of conditions and the following disclaimer.
109dd9d0cfSchristos.\" 2. Redistributions in binary form must reproduce the above copyright
119dd9d0cfSchristos.\"    notice, this list of conditions and the following disclaimer in the
129dd9d0cfSchristos.\"    documentation and/or other materials provided with the distribution.
139dd9d0cfSchristos.\"
149dd9d0cfSchristos.\" This software is provided by Joseph Koshy ``as is'' and
159dd9d0cfSchristos.\" any express or implied warranties, including, but not limited to, the
169dd9d0cfSchristos.\" implied warranties of merchantability and fitness for a particular purpose
179dd9d0cfSchristos.\" are disclaimed.  in no event shall Joseph Koshy be liable
189dd9d0cfSchristos.\" for any direct, indirect, incidental, special, exemplary, or consequential
199dd9d0cfSchristos.\" damages (including, but not limited to, procurement of substitute goods
209dd9d0cfSchristos.\" or services; loss of use, data, or profits; or business interruption)
219dd9d0cfSchristos.\" however caused and on any theory of liability, whether in contract, strict
229dd9d0cfSchristos.\" liability, or tort (including negligence or otherwise) arising in any way
239dd9d0cfSchristos.\" out of the use of this software, even if advised of the possibility of
249dd9d0cfSchristos.\" such damage.
259dd9d0cfSchristos.\"
263f622274Sjkoshy.\" Id: elf_memory.3 3957 2022-03-12 14:11:52Z jkoshy
279dd9d0cfSchristos.\"
289dd9d0cfSchristos.Dd June 28, 2006
299dd9d0cfSchristos.Dt ELF_MEMORY 3
30c5261b83Sjkoshy.Os
319dd9d0cfSchristos.Sh NAME
329dd9d0cfSchristos.Nm elf_memory
339dd9d0cfSchristos.Nd process an ELF or ar(1) archive mapped into memory
349dd9d0cfSchristos.Sh LIBRARY
359dd9d0cfSchristos.Lb libelf
369dd9d0cfSchristos.Sh SYNOPSIS
379dd9d0cfSchristos.In libelf.h
389dd9d0cfSchristos.Ft "Elf *"
399dd9d0cfSchristos.Fn elf_memory "char *image" "size_t size"
409dd9d0cfSchristos.Sh DESCRIPTION
419dd9d0cfSchristosFunction
429dd9d0cfSchristos.Fn elf_memory
439dd9d0cfSchristosis used to process an ELF file or
449dd9d0cfSchristos.Xr ar 1
459dd9d0cfSchristosarchive whose image is present in memory.
469dd9d0cfSchristos.Pp
479dd9d0cfSchristosArgument
483f622274Sjkoshy.Fa image
499dd9d0cfSchristospoints to the start of the memory image of the file or archive.
509dd9d0cfSchristosArgument
513f622274Sjkoshy.Fa size
529dd9d0cfSchristoscontains the size in bytes of the memory image.
539dd9d0cfSchristos.Pp
549dd9d0cfSchristosThe ELF descriptor is created for reading (i.e., analogous to the
559dd9d0cfSchristosuse of
569dd9d0cfSchristos.Xr elf_begin 3
579dd9d0cfSchristoswith a command argument value of
589dd9d0cfSchristos.Dv ELF_C_READ Ns ).
599dd9d0cfSchristos.Sh RETURN VALUES
609dd9d0cfSchristosFunction
619dd9d0cfSchristos.Fn elf_memory
62c5261b83Sjkoshyreturns a pointer to a new ELF descriptor if successful, or
63c5261b83Sjkoshy.Dv NULL
64c5261b83Sjkoshyif an
659dd9d0cfSchristoserror occurred.
669dd9d0cfSchristos.Pp
679dd9d0cfSchristosThe return value may be queried for the file type using
689dd9d0cfSchristos.Xr elf_kind 3 .
699dd9d0cfSchristos.Sh EXAMPLES
709dd9d0cfSchristosTo read parse an elf file, use:
719dd9d0cfSchristos.Bd -literal -offset indent
729dd9d0cfSchristosint fd;
739dd9d0cfSchristosvoid *p;
749dd9d0cfSchristosstruct stat sb;
759dd9d0cfSchristosElf *e;
769dd9d0cfSchristos\&...
779dd9d0cfSchristosif ((fd = open("./elf-file", O_RDONLY)) < 0 ||
789dd9d0cfSchristos    fstat(fd, &sb) < 0 ||
799dd9d0cfSchristos    (p = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0)) ==
809dd9d0cfSchristos    MAP_FAILED) {
819dd9d0cfSchristos	... handle system error ...
829dd9d0cfSchristos}
839dd9d0cfSchristos
849dd9d0cfSchristosif ((e = elf_memory(p, sb.st_size)) == NULL) {
859dd9d0cfSchristos	... handle elf(3) error ...
869dd9d0cfSchristos}
879dd9d0cfSchristos\&... use ELF descriptor "e" here ...
889dd9d0cfSchristos.Ed
899dd9d0cfSchristos.Sh ERRORS
909dd9d0cfSchristosFunction
919dd9d0cfSchristos.Fn elf_memory
929dd9d0cfSchristoscan fail with the following errors:
939dd9d0cfSchristos.Bl -tag -width "[ELF_E_RESOURCE]"
949dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
95c5261b83SjkoshyA
96c5261b83Sjkoshy.Dv NULL
97c5261b83Sjkoshyvalue was used for argument
983f622274Sjkoshy.Fa image
999dd9d0cfSchristosor the value of argument
1003f622274Sjkoshy.Fa sz
1019dd9d0cfSchristoswas zero.
1029dd9d0cfSchristos.It Bq Er ELF_E_HEADER
1039dd9d0cfSchristosThe header of the ELF object contained an unsupported value in its
1049dd9d0cfSchristos.Va e_ident[EI_CLASS]
1059dd9d0cfSchristosfield.
1069dd9d0cfSchristos.It Bq Er ELF_E_HEADER
1079dd9d0cfSchristosThe header of the ELF object contained an unsupported value in its
1089dd9d0cfSchristos.Va e_ident[EI_DATA]
1099dd9d0cfSchristosfield.
1109dd9d0cfSchristos.It Bq Er ELF_E_RESOURCE
1119dd9d0cfSchristosAn out of memory condition was detected.
1129dd9d0cfSchristos.It Bq Er ELF_E_SEQUENCE
1139dd9d0cfSchristosFunction
1149dd9d0cfSchristos.Fn elf_memory
1159dd9d0cfSchristoswas called before a working version was set using
1169dd9d0cfSchristos.Xr elf_version 3 .
1179dd9d0cfSchristos.It Bq Er ELF_E_VERSION
1189dd9d0cfSchristosThe ELF object referenced by argument
1193f622274Sjkoshy.Fa image
1209dd9d0cfSchristoswas of an unsupported ELF version.
1219dd9d0cfSchristos.El
1229dd9d0cfSchristos.Sh SEE ALSO
1239dd9d0cfSchristos.Xr elf 3 ,
1249dd9d0cfSchristos.Xr elf_begin 3 ,
1259dd9d0cfSchristos.Xr elf_end 3 ,
1269dd9d0cfSchristos.Xr elf_errno 3 ,
1279dd9d0cfSchristos.Xr elf_kind 3 ,
1289dd9d0cfSchristos.Xr gelf 3
129