xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/elf_begin.3 (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos.\"	$NetBSD: elf_begin.3,v 1.6 2024/03/03 17:37:33 christos Exp $
2e81373b4Schristos.\"
39dd9d0cfSchristos.\" Copyright (c) 2006,2008-2011 Joseph Koshy.  All rights reserved.
49dd9d0cfSchristos.\"
59dd9d0cfSchristos.\" Redistribution and use in source and binary forms, with or without
69dd9d0cfSchristos.\" modification, are permitted provided that the following conditions
79dd9d0cfSchristos.\" are met:
89dd9d0cfSchristos.\" 1. Redistributions of source code must retain the above copyright
99dd9d0cfSchristos.\"    notice, this list of conditions and the following disclaimer.
109dd9d0cfSchristos.\" 2. Redistributions in binary form must reproduce the above copyright
119dd9d0cfSchristos.\"    notice, this list of conditions and the following disclaimer in the
129dd9d0cfSchristos.\"    documentation and/or other materials provided with the distribution.
139dd9d0cfSchristos.\"
149dd9d0cfSchristos.\" This software is provided by Joseph Koshy ``as is'' and
159dd9d0cfSchristos.\" any express or implied warranties, including, but not limited to, the
169dd9d0cfSchristos.\" implied warranties of merchantability and fitness for a particular purpose
179dd9d0cfSchristos.\" are disclaimed.  in no event shall Joseph Koshy be liable
189dd9d0cfSchristos.\" for any direct, indirect, incidental, special, exemplary, or consequential
199dd9d0cfSchristos.\" damages (including, but not limited to, procurement of substitute goods
209dd9d0cfSchristos.\" or services; loss of use, data, or profits; or business interruption)
219dd9d0cfSchristos.\" however caused and on any theory of liability, whether in contract, strict
229dd9d0cfSchristos.\" liability, or tort (including negligence or otherwise) arising in any way
239dd9d0cfSchristos.\" out of the use of this software, even if advised of the possibility of
249dd9d0cfSchristos.\" such damage.
259dd9d0cfSchristos.\"
263f622274Sjkoshy.\" Id: elf_begin.3 3952 2022-03-12 09:09:50Z jkoshy
279dd9d0cfSchristos.\"
289dd9d0cfSchristos.Dd December 11, 2011
299dd9d0cfSchristos.Dt ELF_BEGIN 3
30c5261b83Sjkoshy.Os
319dd9d0cfSchristos.Sh NAME
329dd9d0cfSchristos.Nm elf_begin
339dd9d0cfSchristos.Nd open an ELF file or ar(1) archive
349dd9d0cfSchristos.Sh LIBRARY
359dd9d0cfSchristos.Lb libelf
369dd9d0cfSchristos.Sh SYNOPSIS
379dd9d0cfSchristos.In libelf.h
389dd9d0cfSchristos.Ft "Elf *"
399dd9d0cfSchristos.Fn elf_begin "int fd" "Elf_Cmd cmd" "Elf *elf"
409dd9d0cfSchristos.Sh DESCRIPTION
419dd9d0cfSchristosFunction
429dd9d0cfSchristos.Fn elf_begin
439dd9d0cfSchristosis used to open ELF files and
449dd9d0cfSchristos.Xr ar 1
459dd9d0cfSchristosarchives for further processing by other APIs in the
469dd9d0cfSchristos.Xr elf 3
479dd9d0cfSchristoslibrary.
489dd9d0cfSchristosIt is also used to access individual ELF members of an
499dd9d0cfSchristos.Xr ar 1
509dd9d0cfSchristosarchive in combination with the
519dd9d0cfSchristos.Xr elf_next 3
529dd9d0cfSchristosand
539dd9d0cfSchristos.Xr elf_rand 3
549dd9d0cfSchristosAPIs.
559dd9d0cfSchristos.Pp
569dd9d0cfSchristosArgument
573f622274Sjkoshy.Fa fd
589dd9d0cfSchristosis an open file descriptor returned from an
599dd9d0cfSchristos.Xr open 2
609dd9d0cfSchristossystem call.
619dd9d0cfSchristosFunction
629dd9d0cfSchristos.Fn elf_begin
639dd9d0cfSchristosuses argument
643f622274Sjkoshy.Fa fd
659dd9d0cfSchristosfor reading or writing depending on the value of argument
663f622274Sjkoshy.Fa cmd .
679dd9d0cfSchristosArgument
683f622274Sjkoshy.Fa elf
699dd9d0cfSchristosis primarily used for iterating through archives.
709dd9d0cfSchristos.Pp
719dd9d0cfSchristosThe argument
723f622274Sjkoshy.Fa cmd
739dd9d0cfSchristoscan have the following values:
749dd9d0cfSchristos.Bl -tag -width "ELF_C_WRITE"
759dd9d0cfSchristos.It ELF_C_NULL
769dd9d0cfSchristosCauses
779dd9d0cfSchristos.Fn elf_begin
78c5261b83Sjkoshyto return
79c5261b83Sjkoshy.Dv NULL .
809dd9d0cfSchristosArguments
813f622274Sjkoshy.Fa fd
829dd9d0cfSchristosand
833f622274Sjkoshy.Fa elf
849dd9d0cfSchristosare ignored, and no additional error is signalled.
859dd9d0cfSchristos.It ELF_C_READ
869dd9d0cfSchristosThis value is to be when the application wishes to examine (but not
879dd9d0cfSchristosmodify) the contents of the file specified by the arguments
883f622274Sjkoshy.Fa fd
899dd9d0cfSchristosand
903f622274Sjkoshy.Fa elf .
919dd9d0cfSchristosIt can be used for both
929dd9d0cfSchristos.Xr ar 1
939dd9d0cfSchristosarchives and for ELF objects.
949dd9d0cfSchristos.Pp
959dd9d0cfSchristosIf argument
963f622274Sjkoshy.Fa elf
97c5261b83Sjkoshyis
98c5261b83Sjkoshy.Dv NULL ,
99c5261b83Sjkoshythe library will allocate a new ELF descriptor for the file
1009dd9d0cfSchristosbeing processed.
1019dd9d0cfSchristosThe argument
1023f622274Sjkoshy.Fa fd
1039dd9d0cfSchristosshould have been opened for reading.
1049dd9d0cfSchristos.Pp
1059dd9d0cfSchristosIf argument
1063f622274Sjkoshy.Fa elf
107c5261b83Sjkoshyis not
108c5261b83Sjkoshy.Dv NULL ,
109c5261b83Sjkoshyand references a regular ELF file previously opened with
1109dd9d0cfSchristos.Fn elf_begin ,
1119dd9d0cfSchristosthen the activation count for the descriptor referenced by argument
1123f622274Sjkoshy.Fa elf
1139dd9d0cfSchristosis incremented.
1149dd9d0cfSchristosThe value in argument
1153f622274Sjkoshy.Fa fd
1169dd9d0cfSchristosshould match that used to open the descriptor argument
1173f622274Sjkoshy.Fa elf .
1189dd9d0cfSchristos.Pp
1199dd9d0cfSchristosIf argument
1203f622274Sjkoshy.Fa elf
121c5261b83Sjkoshyis not
122c5261b83Sjkoshy.Dv NULL ,
123c5261b83Sjkoshyand references a descriptor for an
1249dd9d0cfSchristos.Xr ar 1
1259dd9d0cfSchristosarchive opened earlier with
1269dd9d0cfSchristos.Fn elf_begin ,
1279dd9d0cfSchristosa descriptor for an element in the archive is returned as
1289dd9d0cfSchristosdescribed in the section
1299dd9d0cfSchristos.Sx "Processing ar(1) archives"
1309dd9d0cfSchristosbelow.
1319dd9d0cfSchristosThe value for argument
1323f622274Sjkoshy.Fa fd
1339dd9d0cfSchristosshould match that used to open the archive earlier.
1349dd9d0cfSchristos.Pp
1359dd9d0cfSchristosIf argument
1363f622274Sjkoshy.Fa elf
137c5261b83Sjkoshyis not
138c5261b83Sjkoshy.Dv NULL ,
139c5261b83Sjkoshyand references an
1409dd9d0cfSchristos.Xr ar 1
1419dd9d0cfSchristosarchive opened earlier with
1429dd9d0cfSchristos.Fn elf_memory ,
1439dd9d0cfSchristosthen the value of the argument
1443f622274Sjkoshy.Fa fd
1459dd9d0cfSchristosis ignored.
1469dd9d0cfSchristos.It Dv ELF_C_RDWR
1479dd9d0cfSchristosThis command is used to prepare an ELF file for reading and writing.
1489dd9d0cfSchristosThis command is not supported for
1499dd9d0cfSchristos.Xr ar 1
1509dd9d0cfSchristosarchives.
1519dd9d0cfSchristos.Pp
1529dd9d0cfSchristosArgument
1533f622274Sjkoshy.Fa fd
1549dd9d0cfSchristosshould have been opened for reading and writing.
1559dd9d0cfSchristosIf argument
1563f622274Sjkoshy.Fa elf
157c5261b83Sjkoshyis
158c5261b83Sjkoshy.Dv NULL ,
159c5261b83Sjkoshythe library will allocate a new ELF descriptor for
1609dd9d0cfSchristosthe file being processed.
1619dd9d0cfSchristosIf the argument
1623f622274Sjkoshy.Fa elf
1639dd9d0cfSchristosis non-null, it should point to a descriptor previously
1649dd9d0cfSchristosallocated with
1659dd9d0cfSchristos.Fn elf_begin
1669dd9d0cfSchristoswith the same values for arguments
1673f622274Sjkoshy.Fa fd
1689dd9d0cfSchristosand
1693f622274Sjkoshy.Fa cmd ;
1709dd9d0cfSchristosin this case the library will increment the activation count for descriptor
1713f622274Sjkoshy.Fa elf
1729dd9d0cfSchristosand return the same descriptor.
1739dd9d0cfSchristos.Pp
1749dd9d0cfSchristosChanges to the in-memory image of the ELF file may be written back to
1759dd9d0cfSchristosdisk using the
1769dd9d0cfSchristos.Xr elf_update 3
1779dd9d0cfSchristosfunction.
1789dd9d0cfSchristos.It Dv ELF_C_WRITE
1799dd9d0cfSchristosThis command is used when the application wishes to create a new ELF
1809dd9d0cfSchristosfile.
1819dd9d0cfSchristosArgument
1823f622274Sjkoshy.Fa fd
1839dd9d0cfSchristosshould have been opened for writing.
1849dd9d0cfSchristosArgument
1853f622274Sjkoshy.Fa elf
1869dd9d0cfSchristosis ignored, and the previous contents of file referenced by argument
1873f622274Sjkoshy.Fa fd
1889dd9d0cfSchristosare overwritten.
1899dd9d0cfSchristos.El
1909dd9d0cfSchristos.Ss Processing ar(1) archives
1919dd9d0cfSchristosAn
1929dd9d0cfSchristos.Xr ar 1
1939dd9d0cfSchristosarchive may be opened in read mode (with argument
1943f622274Sjkoshy.Fa cmd
1959dd9d0cfSchristosset to
1969dd9d0cfSchristos.Dv ELF_C_READ )
1979dd9d0cfSchristosusing
1989dd9d0cfSchristos.Fn elf_begin
1999dd9d0cfSchristosor
2009dd9d0cfSchristos.Fn elf_memory .
2019dd9d0cfSchristosThe returned ELF descriptor can be passed into to
2029dd9d0cfSchristossubsequent calls to
2039dd9d0cfSchristos.Fn elf_begin
2049dd9d0cfSchristosto access individual members of the archive.
2059dd9d0cfSchristos.Pp
2069dd9d0cfSchristosRandom access within an opened archive is possible using
2079dd9d0cfSchristosthe
2089dd9d0cfSchristos.Xr elf_next 3
2099dd9d0cfSchristosand
2109dd9d0cfSchristos.Xr elf_rand 3
2119dd9d0cfSchristosfunctions.
2129dd9d0cfSchristos.Pp
2139dd9d0cfSchristosThe symbol table of the archive may be retrieved
2149dd9d0cfSchristosusing
2159dd9d0cfSchristos.Xr elf_getarsym 3 .
2169dd9d0cfSchristos.Sh RETURN VALUES
217c5261b83SjkoshyThe function returns a pointer to a ELF descriptor if successful, or
218c5261b83Sjkoshy.Dv NULL
2199dd9d0cfSchristosif an error occurred.
2209dd9d0cfSchristos.Sh EXAMPLES
2219dd9d0cfSchristosTo iterate through the members of an
2229dd9d0cfSchristos.Xr ar 1
2239dd9d0cfSchristosarchive, use:
2249dd9d0cfSchristos.Bd -literal -offset indent
2259dd9d0cfSchristosElf_Cmd c;
2269dd9d0cfSchristosElf *ar_e, *elf_e;
2279dd9d0cfSchristos\&...
2289dd9d0cfSchristosc = ELF_C_READ;
2299dd9d0cfSchristosif ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
2309dd9d0cfSchristos	\&... handle error in opening the archive ...
2319dd9d0cfSchristos}
2329dd9d0cfSchristoswhile ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
2339dd9d0cfSchristos	\&... process member referenced by elf_e here ...
2349dd9d0cfSchristos	c = elf_next(elf_e);
2359dd9d0cfSchristos	elf_end(elf_e);
2369dd9d0cfSchristos}
2379dd9d0cfSchristos.Ed
2389dd9d0cfSchristos.Pp
2399dd9d0cfSchristosTo create a new ELF file, use:
2409dd9d0cfSchristos.Bd -literal -offset indent
2419dd9d0cfSchristosint fd;
2429dd9d0cfSchristosElf *e;
2439dd9d0cfSchristos\&...
2449dd9d0cfSchristosif ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
2459dd9d0cfSchristos	\&... handle the error from open(2) ...
2469dd9d0cfSchristos}
2479dd9d0cfSchristosif ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
2489dd9d0cfSchristos	\&... handle the error from elf_begin() ...
2499dd9d0cfSchristos}
2509dd9d0cfSchristos\&... create the ELF image using other elf(3) APIs ...
2519dd9d0cfSchristoself_update(e, ELF_C_WRITE);
2529dd9d0cfSchristoself_end(e);
2539dd9d0cfSchristos.Ed
2549dd9d0cfSchristos.Sh ERRORS
2559dd9d0cfSchristosFunction
2569dd9d0cfSchristos.Fn elf_begin
2579dd9d0cfSchristoscan fail with the following errors:
2589dd9d0cfSchristos.Bl -tag -width "[ELF_E_RESOURCE]"
2599dd9d0cfSchristos.It Bq Er ELF_E_ARCHIVE
2609dd9d0cfSchristosThe archive denoted by argument
2613f622274Sjkoshy.Fa elf
2629dd9d0cfSchristoscould not be parsed.
2639dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2649dd9d0cfSchristosThe value in argument
2653f622274Sjkoshy.Fa cmd
2669dd9d0cfSchristoswas unrecognized.
2679dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2689dd9d0cfSchristosA non-null value for argument
2693f622274Sjkoshy.Fa elf
2709dd9d0cfSchristoswas specified when
2713f622274Sjkoshy.Fa cmd
2729dd9d0cfSchristoswas set to
2739dd9d0cfSchristos.Dv ELF_C_RDWR .
2749dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2759dd9d0cfSchristosThe value of argument
2763f622274Sjkoshy.Fa fd
2779dd9d0cfSchristosdiffers from the one the ELF descriptor
2783f622274Sjkoshy.Fa elf
2799dd9d0cfSchristoswas created with.
2809dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2819dd9d0cfSchristosArgument
2823f622274Sjkoshy.Fa cmd
2839dd9d0cfSchristosdiffers from the value specified when ELF descriptor
2843f622274Sjkoshy.Fa elf
2859dd9d0cfSchristoswas created.
2869dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2879dd9d0cfSchristosAn
2889dd9d0cfSchristos.Xr ar 1
28942bd3019Schristosarchive was opened with
2903f622274Sjkoshy.Fa cmd
2919dd9d0cfSchristosset to
2929dd9d0cfSchristos.Dv ELF_C_RDWR .
2939dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2949dd9d0cfSchristosThe file referenced by argument
2953f622274Sjkoshy.Fa fd
2969dd9d0cfSchristoswas empty.
2979dd9d0cfSchristos.It Bq Er ELF_E_ARGUMENT
2989dd9d0cfSchristosThe underlying file for argument
2993f622274Sjkoshy.Fa fd
3009dd9d0cfSchristoswas of an unsupported type.
3019dd9d0cfSchristos.It Bq Er ELF_E_IO
3029dd9d0cfSchristosThe file descriptor in argument
3033f622274Sjkoshy.Fa fd
3049dd9d0cfSchristoswas invalid.
3059dd9d0cfSchristos.It Bq Er ELF_E_IO
3069dd9d0cfSchristosThe file descriptor in argument
3073f622274Sjkoshy.Fa fd
3089dd9d0cfSchristoscould not be read or written to.
3099dd9d0cfSchristos.It Bq Er ELF_E_RESOURCE
3109dd9d0cfSchristosAn out of memory condition was encountered.
3119dd9d0cfSchristos.It Bq Er ELF_E_SEQUENCE
3129dd9d0cfSchristosFunction
3139dd9d0cfSchristos.Fn elf_begin
3149dd9d0cfSchristoswas called before a working version was established with
3159dd9d0cfSchristos.Xr elf_version 3 .
3169dd9d0cfSchristos.It Bq Er ELF_E_VERSION
3179dd9d0cfSchristosThe ELF object referenced by argument
3183f622274Sjkoshy.Fa fd
3199dd9d0cfSchristoswas of an unsupported ELF version.
3209dd9d0cfSchristos.El
3219dd9d0cfSchristos.Sh SEE ALSO
3229dd9d0cfSchristos.Xr elf 3 ,
3239dd9d0cfSchristos.Xr elf_end 3 ,
3249dd9d0cfSchristos.Xr elf_errno 3 ,
3259dd9d0cfSchristos.Xr elf_memory 3 ,
3269dd9d0cfSchristos.Xr elf_next 3 ,
3279dd9d0cfSchristos.Xr elf_rand 3 ,
3289dd9d0cfSchristos.Xr elf_update 3 ,
3299dd9d0cfSchristos.Xr gelf 3
330