xref: /freebsd-src/contrib/elftoolchain/libelftc/elftc_string_table_create.3 (revision a85fe12e361c14018a24f0f7714663b490206c7f)
1*a85fe12eSEd Maste.\" Copyright (c) 2012-2013 Joseph Koshy.
2*a85fe12eSEd Maste.\" All rights reserved.
3*a85fe12eSEd Maste.\"
4*a85fe12eSEd Maste.\" Redistribution and use in source and binary forms, with or without
5*a85fe12eSEd Maste.\" modification, are permitted provided that the following conditions
6*a85fe12eSEd Maste.\" are met:
7*a85fe12eSEd Maste.\" 1. Redistributions of source code must retain the above copyright
8*a85fe12eSEd Maste.\"    notice, this list of conditions and the following disclaimer.
9*a85fe12eSEd Maste.\" 2. Redistributions in binary form must reproduce the above copyright
10*a85fe12eSEd Maste.\"    notice, this list of conditions and the following disclaimer in the
11*a85fe12eSEd Maste.\"    documentation and/or other materials provided with the distribution.
12*a85fe12eSEd Maste.\"
13*a85fe12eSEd Maste.\" This software is provided by Joseph Koshy ``as is'' and
14*a85fe12eSEd Maste.\" any express or implied warranties, including, but not limited to, the
15*a85fe12eSEd Maste.\" implied warranties of merchantability and fitness for a particular purpose
16*a85fe12eSEd Maste.\" are disclaimed.  in no event shall Joseph Koshy be liable
17*a85fe12eSEd Maste.\" for any direct, indirect, incidental, special, exemplary, or consequential
18*a85fe12eSEd Maste.\" damages (including, but not limited to, procurement of substitute goods
19*a85fe12eSEd Maste.\" or services; loss of use, data, or profits; or business interruption)
20*a85fe12eSEd Maste.\" however caused and on any theory of liability, whether in contract, strict
21*a85fe12eSEd Maste.\" liability, or tort (including negligence or otherwise) arising in any way
22*a85fe12eSEd Maste.\" out of the use of this software, even if advised of the possibility of
23*a85fe12eSEd Maste.\" such damage.
24*a85fe12eSEd Maste.\"
25*a85fe12eSEd Maste.\" $Id: elftc_string_table_create.3 2866 2013-01-06 03:20:14Z jkoshy $
26*a85fe12eSEd Maste.\"
27*a85fe12eSEd Maste.Dd January 5, 2013
28*a85fe12eSEd Maste.Os
29*a85fe12eSEd Maste.Dt ELFTC_STRING_TABLE_CREATE 3
30*a85fe12eSEd Maste.Sh NAME
31*a85fe12eSEd Maste.Nm elftc_string_table_create ,
32*a85fe12eSEd Maste.Nm elftc_string_table_destroy ,
33*a85fe12eSEd Maste.Nm elftc_string_table_from_section ,
34*a85fe12eSEd Maste.Nm elftc_string_table_image ,
35*a85fe12eSEd Maste.Nm elftc_string_table_insert ,
36*a85fe12eSEd Maste.Nm elftc_string_table_lookup ,
37*a85fe12eSEd Maste.Nm elftc_string_table_remove ,
38*a85fe12eSEd Maste.Nm elftc_string_table_to_string
39*a85fe12eSEd Maste.Nd convenience routines for handling ELF string tables
40*a85fe12eSEd Maste.Sh SYNOPSIS
41*a85fe12eSEd Maste.In libelftc.h
42*a85fe12eSEd Maste.Ft "Elftc_String_Table *"
43*a85fe12eSEd Maste.Fn elftc_string_table_create "int sizehint"
44*a85fe12eSEd Maste.Ft int
45*a85fe12eSEd Maste.Fn elftc_string_table_destroy "Elftc_String_Table *table"
46*a85fe12eSEd Maste.Ft "Elftc_String_Table *"
47*a85fe12eSEd Maste.Fn elftc_string_table_from_section "Elf_Scn *scn" "int sizehint"
48*a85fe12eSEd Maste.Ft "const char *"
49*a85fe12eSEd Maste.Fo elftc_string_table_image
50*a85fe12eSEd Maste.Fa "Elftc_String_Table *table"
51*a85fe12eSEd Maste.Fa "size_t *size"
52*a85fe12eSEd Maste.Fc
53*a85fe12eSEd Maste.Ft size_t
54*a85fe12eSEd Maste.Fo elftc_string_table_insert
55*a85fe12eSEd Maste.Fa "Elftc_String_Table *table"
56*a85fe12eSEd Maste.Fa "const char *string"
57*a85fe12eSEd Maste.Fc
58*a85fe12eSEd Maste.Ft size_t
59*a85fe12eSEd Maste.Fo elftc_string_table_lookup
60*a85fe12eSEd Maste.Fa "Elftc_String_Table *table"
61*a85fe12eSEd Maste.Fa "const char *string"
62*a85fe12eSEd Maste.Fc
63*a85fe12eSEd Maste.Ft int
64*a85fe12eSEd Maste.Fo elftc_string_table_remove
65*a85fe12eSEd Maste.Fa "Elftc_String_Table *table"
66*a85fe12eSEd Maste.Fa "const char *string"
67*a85fe12eSEd Maste.Fc
68*a85fe12eSEd Maste.Ft "const char *"
69*a85fe12eSEd Maste.Fo elftc_string_table_to_string
70*a85fe12eSEd Maste.Fa "Elftc_String_Table *table"
71*a85fe12eSEd Maste.Fa "size_t offset"
72*a85fe12eSEd Maste.Fc
73*a85fe12eSEd Maste.Sh DESCRIPTION
74*a85fe12eSEd MasteThis manual page documents convenience routines for handling ELF
75*a85fe12eSEd Mastestring tables.
76*a85fe12eSEd Maste.Pp
77*a85fe12eSEd MasteFunction
78*a85fe12eSEd Maste.Fn elftc_string_table_create
79*a85fe12eSEd Mastecreates a new, empty string table.
80*a85fe12eSEd MasteThe argument
81*a85fe12eSEd Maste.Ar sizehint
82*a85fe12eSEd Masteprovides a hint about the expected number of bytes of string data in
83*a85fe12eSEd Mastethe table.
84*a85fe12eSEd MasteIf the argument
85*a85fe12eSEd Maste.Ar sizehint
86*a85fe12eSEd Masteis zero, an implementation-defined default will be used instead.
87*a85fe12eSEd Maste.Pp
88*a85fe12eSEd MasteFunction
89*a85fe12eSEd Maste.Fn elftc_string_table_destroy
90*a85fe12eSEd Mastedestroys the previously allocated string table specified by
91*a85fe12eSEd Masteargument
92*a85fe12eSEd Maste.Ar table ,
93*a85fe12eSEd Masteand frees the internal resources allocated for it.
94*a85fe12eSEd Maste.Pp
95*a85fe12eSEd MasteFunction
96*a85fe12eSEd Maste.Fn elftc_string_table_from_section
97*a85fe12eSEd Mastecreates a new string table and initializes it based on the
98*a85fe12eSEd Mastecontents of the section specified by argument
99*a85fe12eSEd Maste.Ar scn .
100*a85fe12eSEd MasteThis section must be of type
101*a85fe12eSEd Maste.Dv SHT_STRTAB .
102*a85fe12eSEd MasteThe argument
103*a85fe12eSEd Maste.Ar sizehint
104*a85fe12eSEd Masteprovides a hint about expected number of bytes of string data in the
105*a85fe12eSEd Mastetable.
106*a85fe12eSEd MasteIf the value of
107*a85fe12eSEd Maste.Ar sizehint
108*a85fe12eSEd Masteis zero, an implementation-default will be used instead.
109*a85fe12eSEd Maste.Pp
110*a85fe12eSEd MasteFunction
111*a85fe12eSEd Maste.Fn elftc_string_table_image
112*a85fe12eSEd Mastereturns a pointer to the ELF representation of the contents of the
113*a85fe12eSEd Mastestring table specified by argument
114*a85fe12eSEd Maste.Ar table .
115*a85fe12eSEd MasteIf argument
116*a85fe12eSEd Maste.Ar size
117*a85fe12eSEd Masteis not NULL, the size of the ELF representation of the string table is
118*a85fe12eSEd Mastestored in the location pointed to by argument
119*a85fe12eSEd Maste.Ar size .
120*a85fe12eSEd MasteThe function
121*a85fe12eSEd Maste.Fn elftc_string_table_image
122*a85fe12eSEd Mastewill compact the string table if the table contains deleted strings.
123*a85fe12eSEd MasteThe string offsets returned by prior calls to
124*a85fe12eSEd Maste.Fn elftc_string_table_insert
125*a85fe12eSEd Masteand
126*a85fe12eSEd Maste.Fn elftc_string_table_lookup
127*a85fe12eSEd Masteshould be treated as invalid after a call to this function.
128*a85fe12eSEd Maste.Pp
129*a85fe12eSEd MasteFunction
130*a85fe12eSEd Maste.Fn elftc_string_table_insert
131*a85fe12eSEd Masteinserts the NUL-terminated string pointed to by argument
132*a85fe12eSEd Maste.Ar string
133*a85fe12eSEd Masteinto the string table specified by argument
134*a85fe12eSEd Maste.Ar table ,
135*a85fe12eSEd Masteand returns an offset value usable in ELF data structures.
136*a85fe12eSEd MasteMultiple insertions of the same content will return the same offset.
137*a85fe12eSEd MasteThe offset returned will remain valid until the next call to
138*a85fe12eSEd Maste.Fn elftc_string_table_image .
139*a85fe12eSEd Maste.Pp
140*a85fe12eSEd MasteFunction
141*a85fe12eSEd Maste.Fn elftc_string_table_lookup
142*a85fe12eSEd Mastelooks up the string referenced by argument
143*a85fe12eSEd Maste.Ar string
144*a85fe12eSEd Mastein the string table specified by argument
145*a85fe12eSEd Maste.Ar table ,
146*a85fe12eSEd Masteand if found, returns the offset associated with the string.
147*a85fe12eSEd MasteThe returned offset will be valid till the next call to function
148*a85fe12eSEd Maste.Fn elftc_string_table_image .
149*a85fe12eSEd Maste.Pp
150*a85fe12eSEd MasteFunction
151*a85fe12eSEd Maste.Fn elftc_string_table_remove
152*a85fe12eSEd Masteremoves the string pointed by argument
153*a85fe12eSEd Maste.Ar string
154*a85fe12eSEd Mastefrom the string table referenced by argument
155*a85fe12eSEd Maste.Ar table ,
156*a85fe12eSEd Masteif it is present in the string table.
157*a85fe12eSEd Maste.Pp
158*a85fe12eSEd MasteFunction
159*a85fe12eSEd Maste.Fn elftc_string_table_to_string
160*a85fe12eSEd Mastereturns a pointer to the NUL-terminated string residing at argument
161*a85fe12eSEd Maste.Ar offset
162*a85fe12eSEd Mastein the string table specified by argument
163*a85fe12eSEd Maste.Ar table .
164*a85fe12eSEd MasteThe value of argument
165*a85fe12eSEd Maste.Ar offset
166*a85fe12eSEd Masteshould be one returned by a prior call to
167*a85fe12eSEd Maste.Fn elftc_string_table_insert
168*a85fe12eSEd Masteor
169*a85fe12eSEd Maste.Fn elftc_string_table_lookup .
170*a85fe12eSEd MasteThe returned pointer will remain valid until the next call to
171*a85fe12eSEd Maste.Fn elftc_string_table_insert
172*a85fe12eSEd Masteor
173*a85fe12eSEd Maste.Fn elftc_string_table_image .
174*a85fe12eSEd Maste.Ss Memory Management
175*a85fe12eSEd MasteThe
176*a85fe12eSEd Maste.Lb libelftc
177*a85fe12eSEd Mastelibrary manages its own memory allocations.
178*a85fe12eSEd MasteThe application should not free the pointers returned by the string
179*a85fe12eSEd Mastetable functions.
180*a85fe12eSEd Maste.El
181*a85fe12eSEd Maste.Sh IMPLEMENTATION NOTES
182*a85fe12eSEd MasteThe current implementation is optimized for the case where strings are
183*a85fe12eSEd Masteadded to a string table, but rarely removed from it.
184*a85fe12eSEd Maste.Pp
185*a85fe12eSEd MasteThe functions
186*a85fe12eSEd Maste.Fn elftc_string_table_insert ,
187*a85fe12eSEd Maste.Fn elftc_string_table_lookup ,
188*a85fe12eSEd Maste.Fn elftc_string_table_remove
189*a85fe12eSEd Masteand
190*a85fe12eSEd Maste.Fn elftc_string_table_to_string
191*a85fe12eSEd Mastehave O(1) asymptotic behavior.
192*a85fe12eSEd MasteThe function
193*a85fe12eSEd Maste.Fn elftc_string_table_image
194*a85fe12eSEd Mastecan have O(size) asymptotic behavior, where
195*a85fe12eSEd Maste.Ar size
196*a85fe12eSEd Mastedenotes the size of the string table.
197*a85fe12eSEd Maste.Sh RETURN VALUES
198*a85fe12eSEd MasteFunctions
199*a85fe12eSEd Maste.Fn elftc_string_table_create
200*a85fe12eSEd Masteand
201*a85fe12eSEd Maste.Fn elftc_string_table_from_section
202*a85fe12eSEd Mastereturn a valid pointer to an opaque structure of type
203*a85fe12eSEd Maste.Vt Elftc_String_Table
204*a85fe12eSEd Masteon success, or NULL in case of an error.
205*a85fe12eSEd Maste.Pp
206*a85fe12eSEd MasteThe function
207*a85fe12eSEd Maste.Fn elftc_string_table_image
208*a85fe12eSEd Mastereturns a pointer to an in-memory representation of an ELF string
209*a85fe12eSEd Mastetable on success, or NULL in case of an error.
210*a85fe12eSEd Maste.Pp
211*a85fe12eSEd MasteFunctions
212*a85fe12eSEd Maste.Fn elftc_string_table_insert
213*a85fe12eSEd Masteand
214*a85fe12eSEd Maste.Fn elftc_string_table_lookup
215*a85fe12eSEd Mastereturn a non-zero offset on success, or zero in case of an error.
216*a85fe12eSEd Maste.Pp
217*a85fe12eSEd MasteFunction
218*a85fe12eSEd Maste.Fn elftc_string_table_remove
219*a85fe12eSEd Mastereturns a positive value on success, or zero in case of an error.
220*a85fe12eSEd Maste.Pp
221*a85fe12eSEd MasteFunction
222*a85fe12eSEd Maste.Fn elftc_string_table_to_string
223*a85fe12eSEd Mastereturns a valid pointer on success, or NULL in case of an error.
224*a85fe12eSEd Maste.Sh SEE ALSO
225*a85fe12eSEd Maste.Xr dwarf 3 ,
226*a85fe12eSEd Maste.Xr elf 3 ,
227*a85fe12eSEd Maste.Xr elftc 3
228