xref: /netbsd-src/external/bsd/elftoolchain/dist/libdwarf/dwarf_get_section_bytes.3 (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos.\"	$NetBSD: dwarf_get_section_bytes.3,v 1.6 2024/03/03 17:37:31 christos Exp $
2e81373b4Schristos.\"
39dd9d0cfSchristos.\" Copyright (c) 2011 Kai Wang
49dd9d0cfSchristos.\" All rights reserved.
59dd9d0cfSchristos.\"
69dd9d0cfSchristos.\" Redistribution and use in source and binary forms, with or without
79dd9d0cfSchristos.\" modification, are permitted provided that the following conditions
89dd9d0cfSchristos.\" are met:
99dd9d0cfSchristos.\" 1. Redistributions of source code must retain the above copyright
109dd9d0cfSchristos.\"    notice, this list of conditions and the following disclaimer.
119dd9d0cfSchristos.\" 2. Redistributions in binary form must reproduce the above copyright
129dd9d0cfSchristos.\"    notice, this list of conditions and the following disclaimer in the
139dd9d0cfSchristos.\"    documentation and/or other materials provided with the distribution.
149dd9d0cfSchristos.\"
159dd9d0cfSchristos.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
169dd9d0cfSchristos.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
179dd9d0cfSchristos.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
189dd9d0cfSchristos.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199dd9d0cfSchristos.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
209dd9d0cfSchristos.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
219dd9d0cfSchristos.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
229dd9d0cfSchristos.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
239dd9d0cfSchristos.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249dd9d0cfSchristos.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
259dd9d0cfSchristos.\" SUCH DAMAGE.
269dd9d0cfSchristos.\"
27cdbf1fe1Sjkoshy.\" Id: dwarf_get_section_bytes.3 3963 2022-03-12 16:07:32Z jkoshy
289dd9d0cfSchristos.\"
299dd9d0cfSchristos.Dd August 26, 2011
309dd9d0cfSchristos.Dt DWARF_GET_SECTION_BYTES 3
31604b0842Sjkoshy.Os
329dd9d0cfSchristos.Sh NAME
339dd9d0cfSchristos.Nm dwarf_get_section_bytes
349dd9d0cfSchristos.Nd retrieve ELF section byte streams
359dd9d0cfSchristos.Sh LIBRARY
369dd9d0cfSchristos.Lb libdwarf
379dd9d0cfSchristos.Sh SYNOPSIS
389dd9d0cfSchristos.In libdwarf.h
399dd9d0cfSchristos.Ft Dwarf_Ptr
409dd9d0cfSchristos.Fo dwarf_get_section_bytes
419dd9d0cfSchristos.Fa "Dwarf_P_Debug dbg"
429dd9d0cfSchristos.Fa "Dwarf_Signed dwarf_section"
439dd9d0cfSchristos.Fa "Dwarf_Signed *elf_section_index"
449dd9d0cfSchristos.Fa "Dwarf_Unsigned *length"
459dd9d0cfSchristos.Fa "Dwarf_Error *err"
469dd9d0cfSchristos.Fc
479dd9d0cfSchristos.Sh DESCRIPTION
489dd9d0cfSchristosFunction
499dd9d0cfSchristos.Fn dwarf_get_section_bytes
509dd9d0cfSchristosreturns the ELF section byte streams generated by a prior call
519dd9d0cfSchristosto function
529dd9d0cfSchristos.Xr dwarf_transform_to_disk_form 3 .
539dd9d0cfSchristos.Pp
549dd9d0cfSchristosEach call to function
559dd9d0cfSchristos.Fn dwarf_get_section_bytes
569dd9d0cfSchristoswill return the byte stream for one ELF section.
579dd9d0cfSchristosThe first call to this function will always return the first ELF
589dd9d0cfSchristossection, and the subsequent calls will return the rest of sections
599dd9d0cfSchristosin the order when they were generated, until the last one.
609dd9d0cfSchristosThe total number of sections generated is returned by the function
619dd9d0cfSchristos.Xr dwarf_transform_to_disk_form 3 .
629dd9d0cfSchristos.Pp
639dd9d0cfSchristosArgument
64cdbf1fe1Sjkoshy.Fa dbg
659dd9d0cfSchristosshould reference a DWARF producer instance allocated using the
669dd9d0cfSchristosfunctions
679dd9d0cfSchristos.Xr dwarf_producer_init 3
689dd9d0cfSchristosor
699dd9d0cfSchristos.Xr dwarf_producer_init_b 3 .
709dd9d0cfSchristos.Pp
719dd9d0cfSchristosArgument
72cdbf1fe1Sjkoshy.Fa dwarf_section
739dd9d0cfSchristosis currently ignored.
749dd9d0cfSchristos.Pp
759dd9d0cfSchristosArgument
76cdbf1fe1Sjkoshy.Fa elf_section_index
779dd9d0cfSchristosshould point to a location which will be set to the section index value
789dd9d0cfSchristosof the returned ELF section.
799dd9d0cfSchristos.Pp
809dd9d0cfSchristosArgument
81cdbf1fe1Sjkoshy.Fa length
829dd9d0cfSchristosshould point to a location which will hold the length in bytes of the
839dd9d0cfSchristosreturned ELF section.
849dd9d0cfSchristos.Pp
859dd9d0cfSchristosIf argument
86cdbf1fe1Sjkoshy.Fa err
87cdbf1fe1Sjkoshyis not
88cdbf1fe1Sjkoshy.Dv NULL ,
89cdbf1fe1Sjkoshyit will be used to store error information in case of an error.
909dd9d0cfSchristos.Ss Memory Management
919dd9d0cfSchristosThe memory areas used for the returned ELF section byte streams should
929dd9d0cfSchristosbe freed using the function
939dd9d0cfSchristos.Fn dwarf_producer_finish .
949dd9d0cfSchristos.Sh RETURN VALUES
959dd9d0cfSchristosOn success, function
969dd9d0cfSchristos.Fn dwarf_get_section_bytes
979dd9d0cfSchristosreturns a pointer to a ELF section byte stream.
989dd9d0cfSchristosIn case of an error, function
999dd9d0cfSchristos.Fn dwarf_get_section_bytes
100cdbf1fe1Sjkoshywill return
101cdbf1fe1Sjkoshy.Dv NULL
102cdbf1fe1Sjkoshyand set the argument
103cdbf1fe1Sjkoshy.Fa err .
1049dd9d0cfSchristos.Sh EXAMPLES
1059dd9d0cfSchristosTo generate and retrieve ELF section byte streams, use:
1069dd9d0cfSchristos.Bd -literal -offset indent
1079dd9d0cfSchristosDwarf_P_Debug dbg;
1089dd9d0cfSchristosDwarf_Signed count, i, sec_index;
1099dd9d0cfSchristosDwarf_Unsigned len;
1109dd9d0cfSchristosDwarf_Ptr bytes;
1119dd9d0cfSchristosDwarf_Error de;
1129dd9d0cfSchristos
1139dd9d0cfSchristos/* ... Assume that `dbg' refers to a DWARF producer instance,
1149dd9d0cfSchristos * and that application code has added DWARF debugging
1159dd9d0cfSchristos * information to the producer instance. ...
1169dd9d0cfSchristos */
1179dd9d0cfSchristosif ((count = dwarf_transform_to_disk_form(dbg, &de)) ==
1189dd9d0cfSchristos    DW_DLV_NOCOUNT) {
1199dd9d0cfSchristos	warnx("dwarf_transform_to_disk_form failed: %s",
1209dd9d0cfSchristos	    dwarf_errmsg(-1));
1219dd9d0cfSchristos	return;
1229dd9d0cfSchristos}
1239dd9d0cfSchristos
1249dd9d0cfSchristos/* Retrieve section data. */
1259dd9d0cfSchristosfor (i = 0; i < count; i++) {
1269dd9d0cfSchristos	bytes = dwarf_get_section_bytes(dbg, i, &sec_index, &len,
1279dd9d0cfSchristos	    &de);
1289dd9d0cfSchristos	if (bytes == NULL) {
1299dd9d0cfSchristos		warnx("dwarf_get_section_bytes failed: %s",
1309dd9d0cfSchristos		    dwarf_errmsg(-1));
1319dd9d0cfSchristos		continue;
1329dd9d0cfSchristos	}
1339dd9d0cfSchristos	/* ... use the returned byte stream ... */
1349dd9d0cfSchristos}
1359dd9d0cfSchristos
1369dd9d0cfSchristos/* Release resources. */
1379dd9d0cfSchristosdwarf_producer_finish(dbg, &de);
1389dd9d0cfSchristos.Ed
139604b0842Sjkoshy.Sh ERRORS
140604b0842SjkoshyFunction
141604b0842Sjkoshy.Fn dwarf_get_section_bytes
142604b0842Sjkoshycan fail with:
143604b0842Sjkoshy.Bl -tag -width ".Bq Er DW_DLE_NO_ENTRY"
144604b0842Sjkoshy.It Bq Er DW_DLE_ARGUMENT
145604b0842SjkoshyOne of the arguments
146cdbf1fe1Sjkoshy.Fa dbg ,
147cdbf1fe1Sjkoshy.Fa elf_section_index ,
148604b0842Sjkoshyor
149cdbf1fe1Sjkoshy.Fa length
150cdbf1fe1Sjkoshywas
151cdbf1fe1Sjkoshy.Dv NULL .
152604b0842Sjkoshy.It Bq Er DW_DLE_NO_ENTRY
153604b0842SjkoshyThere were no more ELF sections to retrieve, or the function was
154604b0842Sjkoshycalled before a call to
155604b0842Sjkoshy.Xr dwarf_transform_to_disk_form 3 .
156604b0842Sjkoshy.El
1579dd9d0cfSchristos.Sh SEE ALSO
1589dd9d0cfSchristos.Xr dwarf 3 ,
1599dd9d0cfSchristos.Xr dwarf_producer_finish 3 ,
1609dd9d0cfSchristos.Xr dwarf_producer_init 3 ,
1619dd9d0cfSchristos.Xr dwarf_producer_init_b 3 ,
162604b0842Sjkoshy.Xr dwarf_reset_section_bytes 3 ,
1639dd9d0cfSchristos.Xr dwarf_transform_to_disk_form 3
164