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