117
* strtbl_add - add a string to a string table.
119
* @str: string to add
120
* @tbl: current string table. NULL = allocate new table
122
* Allocates an array of pointers to strings.
123
* The strings themselves are not actually kept in the table.
125
* Returns reallocated and updated string table. NULL = out of memory.
127
* Implementation note: The string table is designed to be lighter-weight
128
* and faster than a more conventional linked list that stores the strings
129
* in the list elements, as it does far fewer malloc/realloc calls
130
* and avoids copying entirely.
132
struct string_table *strtbl_add(const char *str, struct string_table *tbl)
135
const char max = 100;
136
tbl = malloc(sizeof(*tbl) + sizeof(char *) * max);
142
if (tbl->cnt >= tbl->max) {
144
tbl = realloc(tbl, sizeof(*tbl) + sizeof(char *) * tbl->max);
148
tbl->str[tbl->cnt] = str;
155
* strtbl_free - string table destructor
157
void strtbl_free(struct string_table *tbl)
117
163
* Get the basename in a pathname.
118
164
* Unlike the standard implementation, this does not copy the string.
157
203
return (char) *((uint32_t*)("\1\0\0\2"));
161
* Check ELF file header.
163
int elf_ident(void *file, unsigned long fsize, int *conv)
165
/* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
166
unsigned char *ident = file;
168
if (fsize < EI_CLASS || memcmp(file, ELFMAG, SELFMAG) != 0)
169
return -ENOEXEC; /* Not an ELF object */
170
if (ident[EI_DATA] == 0 || ident[EI_DATA] > 2)
171
return -EINVAL; /* Unknown endianness */
174
*conv = native_endianness() != ident[EI_DATA];
175
return ident[EI_CLASS];
178
#define PERBIT(x) x##32
179
#define ElfPERBIT(x) Elf32_##x
180
#define ELFPERBIT(x) ELF32_##x
181
#include "elf_core.c"
186
#define PERBIT(x) x##64
187
#define ElfPERBIT(x) Elf64_##x
188
#define ELFPERBIT(x) ELF64_##x
189
#include "elf_core.c"
191
void *get_section(void *file, unsigned long filesize,
192
const char *secname, unsigned long *secsize)
196
switch (elf_ident(file, filesize, &conv)) {
198
return get_section32(file, filesize, secname, secsize, conv);
200
return get_section64(file, filesize, secname, secsize, conv);