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