xref: /freebsd/contrib/elftoolchain/libdwarf/dwarf_get_section_bytes.3 (revision ae500c1ff8974130f7f2692772cf288b90349e0d)
12de3b87aSKai Wang.\" Copyright (c) 2011 Kai Wang
22de3b87aSKai Wang.\" All rights reserved.
32de3b87aSKai Wang.\"
42de3b87aSKai Wang.\" Redistribution and use in source and binary forms, with or without
52de3b87aSKai Wang.\" modification, are permitted provided that the following conditions
62de3b87aSKai Wang.\" are met:
72de3b87aSKai Wang.\" 1. Redistributions of source code must retain the above copyright
82de3b87aSKai Wang.\"    notice, this list of conditions and the following disclaimer.
92de3b87aSKai Wang.\" 2. Redistributions in binary form must reproduce the above copyright
102de3b87aSKai Wang.\"    notice, this list of conditions and the following disclaimer in the
112de3b87aSKai Wang.\"    documentation and/or other materials provided with the distribution.
122de3b87aSKai Wang.\"
132de3b87aSKai Wang.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
142de3b87aSKai Wang.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
152de3b87aSKai Wang.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
162de3b87aSKai Wang.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
172de3b87aSKai Wang.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
182de3b87aSKai Wang.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
192de3b87aSKai Wang.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
202de3b87aSKai Wang.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
212de3b87aSKai Wang.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
222de3b87aSKai Wang.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
232de3b87aSKai Wang.\" SUCH DAMAGE.
242de3b87aSKai Wang.\"
25*ae500c1fSEd Maste.\" $Id: dwarf_get_section_bytes.3 3644 2018-10-15 19:55:01Z jkoshy $
262de3b87aSKai Wang.\"
272de3b87aSKai Wang.Dd August 26, 2011
282de3b87aSKai Wang.Dt DWARF_GET_SECTION_BYTES 3
29*ae500c1fSEd Maste.Os
302de3b87aSKai Wang.Sh NAME
312de3b87aSKai Wang.Nm dwarf_get_section_bytes
322de3b87aSKai Wang.Nd retrieve ELF section byte streams
332de3b87aSKai Wang.Sh LIBRARY
342de3b87aSKai Wang.Lb libdwarf
352de3b87aSKai Wang.Sh SYNOPSIS
362de3b87aSKai Wang.In libdwarf.h
372de3b87aSKai Wang.Ft Dwarf_Ptr
382de3b87aSKai Wang.Fo dwarf_get_section_bytes
392de3b87aSKai Wang.Fa "Dwarf_P_Debug dbg"
402de3b87aSKai Wang.Fa "Dwarf_Signed dwarf_section"
412de3b87aSKai Wang.Fa "Dwarf_Signed *elf_section_index"
422de3b87aSKai Wang.Fa "Dwarf_Unsigned *length"
432de3b87aSKai Wang.Fa "Dwarf_Error *err"
442de3b87aSKai Wang.Fc
452de3b87aSKai Wang.Sh DESCRIPTION
462de3b87aSKai WangFunction
472de3b87aSKai Wang.Fn dwarf_get_section_bytes
482de3b87aSKai Wangreturns the ELF section byte streams generated by a prior call
492de3b87aSKai Wangto function
502de3b87aSKai Wang.Xr dwarf_transform_to_disk_form 3 .
512de3b87aSKai Wang.Pp
522de3b87aSKai WangEach call to function
532de3b87aSKai Wang.Fn dwarf_get_section_bytes
542de3b87aSKai Wangwill return the byte stream for one ELF section.
552de3b87aSKai WangThe first call to this function will always return the first ELF
562de3b87aSKai Wangsection, and the subsequent calls will return the rest of sections
572de3b87aSKai Wangin the order when they were generated, until the last one.
582de3b87aSKai WangThe total number of sections generated is returned by the function
592de3b87aSKai Wang.Xr dwarf_transform_to_disk_form 3 .
602de3b87aSKai Wang.Pp
612de3b87aSKai WangArgument
622de3b87aSKai Wang.Ar dbg
632de3b87aSKai Wangshould reference a DWARF producer instance allocated using the
642de3b87aSKai Wangfunctions
652de3b87aSKai Wang.Xr dwarf_producer_init 3
662de3b87aSKai Wangor
672de3b87aSKai Wang.Xr dwarf_producer_init_b 3 .
682de3b87aSKai Wang.Pp
692de3b87aSKai WangArgument
702de3b87aSKai Wang.Ar dwarf_section
712de3b87aSKai Wangis currently ignored.
722de3b87aSKai Wang.Pp
732de3b87aSKai WangArgument
742de3b87aSKai Wang.Ar elf_section_index
752de3b87aSKai Wangshould point to a location which will be set to the section index value
762de3b87aSKai Wangof the returned ELF section.
772de3b87aSKai Wang.Pp
782de3b87aSKai WangArgument
792de3b87aSKai Wang.Ar length
802de3b87aSKai Wangshould point to a location which will hold the length in bytes of the
812de3b87aSKai Wangreturned ELF section.
822de3b87aSKai Wang.Pp
832de3b87aSKai WangIf argument
842de3b87aSKai Wang.Ar err
852de3b87aSKai Wangis not NULL, it will be used to store error information in case of an
862de3b87aSKai Wangerror.
872de3b87aSKai Wang.Ss Memory Management
882de3b87aSKai WangThe memory areas used for the returned ELF section byte streams should
892de3b87aSKai Wangbe freed using the function
902de3b87aSKai Wang.Fn dwarf_producer_finish .
912de3b87aSKai Wang.Sh RETURN VALUES
922de3b87aSKai WangOn success, function
932de3b87aSKai Wang.Fn dwarf_get_section_bytes
942de3b87aSKai Wangreturns a pointer to a ELF section byte stream.
952de3b87aSKai WangIn case of an error, function
962de3b87aSKai Wang.Fn dwarf_get_section_bytes
972de3b87aSKai Wangwill return NULL and set the argument
982de3b87aSKai Wang.Ar err .
992de3b87aSKai Wang.Sh EXAMPLES
1002de3b87aSKai WangTo generate and retrieve ELF section byte streams, use:
1012de3b87aSKai Wang.Bd -literal -offset indent
1022de3b87aSKai WangDwarf_P_Debug dbg;
1032de3b87aSKai WangDwarf_Signed count, i, sec_index;
1042de3b87aSKai WangDwarf_Unsigned len;
1052de3b87aSKai WangDwarf_Ptr bytes;
1062de3b87aSKai WangDwarf_Error de;
1072de3b87aSKai Wang
1082de3b87aSKai Wang/* ... Assume that `dbg' refers to a DWARF producer instance,
1092de3b87aSKai Wang * and that application code has added DWARF debugging
1102de3b87aSKai Wang * information to the producer instance. ...
1112de3b87aSKai Wang */
1122de3b87aSKai Wangif ((count = dwarf_transform_to_disk_form(dbg, &de)) ==
1132de3b87aSKai Wang    DW_DLV_NOCOUNT) {
1142de3b87aSKai Wang	warnx("dwarf_transform_to_disk_form failed: %s",
1152de3b87aSKai Wang	    dwarf_errmsg(-1));
1162de3b87aSKai Wang	return;
1172de3b87aSKai Wang}
1182de3b87aSKai Wang
1192de3b87aSKai Wang/* Retrieve section data. */
1202de3b87aSKai Wangfor (i = 0; i < count; i++) {
1212de3b87aSKai Wang	bytes = dwarf_get_section_bytes(dbg, i, &sec_index, &len,
1222de3b87aSKai Wang	    &de);
1232de3b87aSKai Wang	if (bytes == NULL) {
1242de3b87aSKai Wang		warnx("dwarf_get_section_bytes failed: %s",
1252de3b87aSKai Wang		    dwarf_errmsg(-1));
1262de3b87aSKai Wang		continue;
1272de3b87aSKai Wang	}
1282de3b87aSKai Wang	/* ... use the returned byte stream ... */
1292de3b87aSKai Wang}
1302de3b87aSKai Wang
1312de3b87aSKai Wang/* Release resources. */
1322de3b87aSKai Wangdwarf_producer_finish(dbg, &de);
1332de3b87aSKai Wang.Ed
134*ae500c1fSEd Maste.Sh ERRORS
135*ae500c1fSEd MasteFunction
136*ae500c1fSEd Maste.Fn dwarf_get_section_bytes
137*ae500c1fSEd Mastecan fail with:
138*ae500c1fSEd Maste.Bl -tag -width ".Bq Er DW_DLE_NO_ENTRY"
139*ae500c1fSEd Maste.It Bq Er DW_DLE_ARGUMENT
140*ae500c1fSEd MasteOne of the arguments
141*ae500c1fSEd Maste.Ar dbg ,
142*ae500c1fSEd Maste.Ar elf_section_index ,
143*ae500c1fSEd Masteor
144*ae500c1fSEd Maste.Ar length
145*ae500c1fSEd Mastewas NULL.
146*ae500c1fSEd Maste.It Bq Er DW_DLE_NO_ENTRY
147*ae500c1fSEd MasteThere were no more ELF sections to retrieve, or the function was
148*ae500c1fSEd Mastecalled before a call to
149*ae500c1fSEd Maste.Xr dwarf_transform_to_disk_form 3 .
150*ae500c1fSEd Maste.El
1512de3b87aSKai Wang.Sh SEE ALSO
1522de3b87aSKai Wang.Xr dwarf 3 ,
1532de3b87aSKai Wang.Xr dwarf_producer_finish 3 ,
1542de3b87aSKai Wang.Xr dwarf_producer_init 3 ,
1552de3b87aSKai Wang.Xr dwarf_producer_init_b 3 ,
156*ae500c1fSEd Maste.Xr dwarf_reset_section_bytes 3 ,
1572de3b87aSKai Wang.Xr dwarf_transform_to_disk_form 3
158