summaryrefslogtreecommitdiff
path: root/other/burneye2/elf/elf_segment.h
blob: d09834d88d6ec828784bcb9b77d718180f18a02f (plain)
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
/* libxelf - segment abstraction module
 *
 * by scut / teso
 */

#ifndef	ELF_SEGMENT_H
#define	ELF_SEGMENT_H

#include <elf.h>

#include <elf_file.h>
#include <elf_section.h>


/* elf_segment structure
 *
 * this is a program header segment, which contains a variable number of
 * sections, the program header and extra information. executeables are
 * build of a number of elf_segment structures
 */
typedef struct {
	unsigned long int	ph_idx;	/* original index of header table */

	/* here we include an abstracted segment header,
	 * the `p_offset' value is the original value from the elf file
	 * this structure was read from, but it is read-only, and rebuild
	 * on creation of a new ELF file
	 */
	Elf32_Phdr		Phdr;

	/* sections that lie within this segment
	 */
	elf_section_list *	slist;
} elf_segment;


typedef struct {
	int		elem_count;	/* number of elements */

	elf_segment **	list;	/* array of elf_segment structure pointers */
} elf_segment_list;


/* elf_segment_create
 *
 * create a new elf_segment structure
 *
 * return pointer to new structure
 */

elf_segment *
elf_segment_create (void);


/* elf_segment_destroy
 *
 * free all resources within `seg' structure, including the section list
 *
 * return in any case
 */

void
elf_segment_destroy (elf_segment *seg);


/* elf_segment_load
 *
 * load a program segment described by the program header `Phdr', with
 * original program header table index `ph_idx' from elf_file `elf'.
 *
 * return NULL on failure
 * return pointer to new elf_segment structure on success
 */
elf_segment *
elf_segment_load (elf_file *elf, unsigned long int ph_idx, Elf32_Phdr *Phdr);


/* elf_segment_addsections
 *
 * add elf_section_list `slist' into the segment `seg', where the virtual
 * address mappings are fitting within the segments description headers'
 * virtual address. remove matching sections from `slist'.
 *
 * return number of added sections
 */

int
elf_segment_addsections (elf_segment *seg, elf_section_list *slist);


/* elf_segment_store
 *
 * store an elf segment `seg' with all its sections to a properly aligned file
 * `fp'
 *
 * return in any case
 */

void
elf_segment_store (FILE *fp, elf_segment *seg);


/* elf_segment_emptyhead
 *
 * count the number of bytes from the segments `seg' virtual start address
 * to the first real occupied byte by its first contained section
 * assume sections within segment are ordered.
 *
 * return distance on success
 * return -1 on failure
 */

long int
elf_segment_emptyhead (elf_segment *seg);


/* elf_segment_list_create
 *
 * create a new elf_segment_list structure
 *
 * return pointer to new structure
 */

elf_segment_list *
elf_segment_list_create (void);


/* elf_segment_list_destroy
 *
 * destroy the segment list `seglist' and all its elements, including any
 * sections associated with those segments
 *
 * return in any case
 */

void
elf_segment_list_destroy (elf_segment_list *seglist);


/* elf_segment_list_count
 *
 * return the number of segments within the segment list `seglist'
 */

int
elf_segment_list_count (elf_segment_list *seglist);


/* elf_segment_list_add
 *
 * add an elf_section structure `seg' to the segment list `seglist'
 *
 * return in any case
 */

void
elf_segment_list_add (elf_segment_list *seglist, elf_segment *seg);


/* elf_segment_list_del
 *
 * delete segment `seg' from the segment list `seglist'
 *
 * return 0 in case it was removed
 * return 1 in case it was not found
 */

int
elf_segment_list_del (elf_segment_list *seglist, elf_segment *seg);


/* elf_segment_list_print
 *
 * print a elf_segment_list `seglist' in a short "one-segment-one-line"
 * form
 *
 * return in any case
 */

void
elf_segment_list_print (elf_segment_list *seglist);


#endif