jcs's openbsd hax
openbsd
1.\" Copyright (c) 2006-2008,2018 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_getscn.3,v 1.2 2025/06/10 17:19:45 schwarze Exp $
25.\"
26.Dd September 24, 2018
27.Dt ELF_GETSCN 3
28.Os
29.Sh NAME
30.Nm elf_getscn ,
31.Nm elf_ndxscn ,
32.Nm elf_newscn ,
33.Nm elf_nextscn
34.Nd get/allocate section information for an ELF object
35.Sh SYNOPSIS
36.Lb libelf
37.In libelf.h
38.Ft "Elf_Scn *"
39.Fn elf_getscn "Elf *elf" "size_t index"
40.Ft size_t
41.Fn elf_ndxscn "Elf_Scn *scn"
42.Ft "Elf_Scn *"
43.Fn elf_newscn "Elf *elf"
44.Ft "Elf_Scn *"
45.Fn elf_nextscn "Elf *elf" "Elf_Scn *scn"
46.Sh DESCRIPTION
47These functions are used to iterate through the sections associated
48with an ELF descriptor.
49.Pp
50Function
51.Fn elf_getscn
52will return a section descriptor for the section at index
53.Ar index
54in the object denoted by ELF descriptor
55.Ar elf .
56An error will be signalled if the specified section does not
57exist.
58.Pp
59Function
60.Fn elf_ndxscn
61returns the section table index associated with section descriptor
62.Ar scn .
63.Pp
64Function
65.Fn elf_newscn
66creates a new section and appends it to the list of sections
67associated with descriptor
68.Ar elf .
69The library will automatically increment the
70.Va e_shnum
71field of the ELF header associated with descriptor
72.Ar elf ,
73and will set the
74.Dv ELF_F_DIRTY
75flag on the returned section descriptor.
76For ELF descriptors opened for writing, the ELF library will
77automatically create an empty section at index zero
78.Dv ( SHN_UNDEF )
79on the first call to
80.Fn elf_newscn .
81.Pp
82Function
83.Fn elf_nextscn
84takes a section descriptor
85.Ar scn
86and returns a pointer to the section descriptor at the next higher
87index.
88As a consequence,
89.Fn elf_nextscn
90will never return a pointer to the empty section at index zero
91.Dv ( SHN_UNDEF ) .
92Argument
93.Ar scn
94is allowed to be NULL, in which case this function will return a
95pointer to the section descriptor at index 1.
96If no further sections are present, function
97.Fn elf_nextscn
98will return a NULL pointer.
99.Sh RETURN VALUES
100Functions
101.Fn elf_getscn ,
102.Fn elf_newscn
103and
104.Fn elf_nextscn
105return a valid pointer to a section descriptor if successful, or
106NULL if an error occurs.
107.Pp
108Function
109.Fn elf_ndxscn
110returns a valid section table index if successful, or
111.Dv SHN_UNDEF
112if an error occurs.
113.Sh ERRORS
114These functions may fail with the following errors:
115.Bl -tag -width "[ELF_E_RESOURCE]"
116.It Bq Er ELF_E_ARGUMENT
117Arguments
118.Ar elf
119or
120.Ar scn
121were NULL.
122.It Bq Er ELF_E_ARGUMENT
123Argument
124.Ar index
125exceeded the current number of sections in the ELF object.
126.It Bq Er ELF_E_ARGUMENT
127Argument
128.Ar elf
129was not a descriptor for an ELF file.
130.It Bq Er ELF_E_ARGUMENT
131Section descriptor
132.Ar scn
133was not associated with ELF descriptor
134.Ar elf .
135.It Bq Er ELF_E_CLASS
136Descriptor
137.Ar elf
138was of an unknown ELF class.
139.It Bq Er ELF_E_SECTION
140Argument
141.Ar elf
142specified extended section numbering in the ELF header with the section header at
143index
144.Dv SHN_UNDEF
145not being of type
146.Dv SHT_NULL .
147.El
148.Sh SEE ALSO
149.Xr elf 3 ,
150.Xr elf_flagdata 3 ,
151.Xr elf_flagscn 3 ,
152.Xr elf_getdata 3 ,
153.Xr elf_getshdr 3 ,
154.Xr gelf 3