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