1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* libxelf - section abstraction module
*
* by scut / teso
*/
#ifndef ELF_SECTION_H
#define ELF_SECTION_H
#include <elf.h>
#include <elf_file.h>
/* elf_section structure
* abstract, physically-position-independant section header and content
* it is not virtual-memory position-independant however
*/
typedef struct {
unsigned long int sh_idx;
char * name; /* section name or NULL */
/* here we include an abstracted section header,
* the `sh_offset' element is read only, and contains the original
* value. on write into a new ELF file it is recomputed
*/
Elf32_Shdr Shdr;
unsigned long int data_len; /* original from sh_size */
unsigned char * data; /* data if non-NULL */
unsigned char * data_backup;
} elf_section;
typedef struct {
int elem_count; /* number of elements */
elf_section ** list; /* array of elf_section structure pointers */
} elf_section_list;
/* elf_section_create
*
* create a new elf_section structure
*
* return pointer to new structure
*/
elf_section *
elf_section_create (void);
/* elf_section_destroy
*
* destroy any associated memory with `sect'
*
* return in any case
*/
void
elf_section_destroy (elf_section *sect);
/* elf_section_load
*
* load a new section whose section header is passed through `Shdr'.
*
* return NULL on failure
* return pointer to new elf_section structure on success
*/
elf_section *
elf_section_load (elf_file *elf, unsigned long int sh_idx, Elf32_Shdr *Shdr);
/* elf_section_secalloc
*
* extend all sections that do not cover real content in file to their real
* in-memory size, filling the content with zeroes. this can be used to
* operate in .bss, which does not take up space in the file and is not
* allocated normally. all sections in `slist' are checked for this and
* extended accordingly.
*
* return in any case
*/
void
elf_section_secalloc (elf_section_list *slist);
/* elf_section_name
*
* search for the section name of section `sect' in elf_file `elf'
*
* return NULL on failure
* return pointer to constant string on success
*/
char *
elf_section_name (elf_file *elf, elf_section *sect);
/* elf_section_list_create
*
* create a new section list, which is empty
*
* return pointer to new list
*/
elf_section_list *
elf_section_list_create (void);
/* elf_section_list_destroy
*
* destroy the section list `slist' and all its elements.
*
* return in any case
*/
void
elf_section_list_destroy (elf_section_list *slist);
/* elf_section_list_add
*
* add elf_section `sect' to list `slist'
*
* return in any case
*/
void
elf_section_list_add (elf_section_list *slist, elf_section *sect);
/* elf_section_list_del
*
* remove elf_section `sect' from list `slist'
*
* return 0 in case it was removed
* return 1 in case it was not found
*/
int
elf_section_list_del (elf_section_list *slist, elf_section *sect);
/* elf_section_list_count
*
* return number of elements in section list `slist'
*/
int
elf_section_list_count (elf_section_list *slist);
/* elf_section_list_sort
*
* sort section list `slist' by `slist.list[].Shdr.sh_addr', in ascending
* order
*
* return in any case
*/
void
elf_section_list_sort (elf_section_list *slist);
/* elf_section_list_find_index
*
* find an elf_section structure within the list `slist', that has the
* original sh_idx value of `idx'.
*
* return NULL on failure
* return pointer to structure on success (static structure, do not free!)
*/
elf_section *
elf_section_list_find_index (elf_section_list *slist, unsigned long int idx);
/* elf_section_list_find_type
*
* find an elf_section structure within the list `slist', that has the
* section type (Shdr.sh_type) of `sh_type'. when `old' is non-NULL, the
* search is picked up after this item. this allows walking all sections of a
* type.
*
* return NULL on failure
* return pointer to first structure on success
*/
elf_section *
elf_section_list_find_type (elf_section_list *slist, Elf32_Word sh_type,
elf_section *old);
/* elf_section_list_find_name
*
* find an elf_section structure within the list `slist' with the name `name'.
*
* return found section on success
* return NULL on failure
*/
elf_section *
elf_section_list_find_name (elf_section_list *slist, char *name);
#endif
|