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