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