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
|
/* datahandler.c - burneye2 .rodata/.data handling functions
*
* by scut
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <elf.h>
#include <common.h>
#include <utility.h>
#include <elf_reloc.h>
#include <elf_section.h>
#include <datahandler.h>
data_item *
dh_item_new (void)
{
return (xcalloc (1, sizeof (data_item)));
}
data_item *
dh_item_list_create_bysymreloc (elf_base *base, elf_section *datasec,
elf_rel_list *rel)
{
unsigned int n,
sym_size;
data_item * dh = dh_item_new ();
/* start with a section-sized item that is dangling, then carve the
* used space out of it incrementally
*/
dh->dangling = 1;
dh->offset = 0;
dh->length = datasec->Shdr.sh_size;
/* first carve the 100% known symbol table entries
*/
sym_size = base->symtab->data_len / sizeof (Elf32_Sym);
for (n = 0 ; n < sym_size ; ++n) {
elf_symbol * sym;
sym = elf_sym_fetch_byindex (base, n);
assert (sym != NULL);
if (sym->sec == NULL || sym->sec->sh_idx != datasec->sh_idx) {
free (sym);
continue;
}
fnote ("item: %s\n", sym->name);
if (sym->sent.st_size == 0) {
fnote (" size is zero, skipping.\n");
continue;
}
assert (sym->sent.st_value < datasec->data_len);
dh = dh_carve (dh, sym->sent.st_value, sym->sent.st_size,
&datasec->data[sym->sent.st_value]);
assert (dh != NULL);
}
#if 0
/* search for correct relocation frame in list
*/
while (rel != NULL) {
if (rel->reloc_modify->sh_idx == datasec->sh_idx)
break;
rel = rel->next;
}
for (n = 0 ; n < rel->reloc_count ; ++n) {
}
#endif
return (dh);
}
data_item *
dh_carve (data_item *dh, unsigned int offset, unsigned int length,
unsigned char *data)
{
data_item * ret = dh;
data_item * wlk;
data_item * wlk_last = NULL;
data_item * new;
for (wlk = dh ; wlk != NULL ; wlk = wlk->next) {
/* ignore space already given to someone else or blocks that
* do not cover our range
*/
if (wlk->dangling == 0 ||
(wlk->offset + wlk->length) < (offset + length))
{
wlk_last = wlk;
continue;
}
/* since we know the list is ordered we can safely abort once
* we are past our offset
*/
if (wlk->offset > offset)
break;
/* now we've found the block we have to cut
*/
if (wlk->offset < offset) {
data_item * nbf = dh_item_new ();
nbf->offset = wlk->offset;
nbf->length = offset - wlk->offset;
nbf->dangling = 1;
nbf->data = wlk->data;
nbf->next = wlk;
if (wlk_last == NULL)
ret = nbf;
else
wlk_last->next = nbf;
wlk_last = nbf;
wlk->offset += nbf->length;
wlk->length -= nbf->length;
wlk->data += nbf->length;
}
assert (wlk->offset == offset);
new = dh_item_new ();
new->next = wlk;
new->offset = offset;
new->length = length;
new->data = wlk->data;
wlk->length -= length;
wlk->offset = offset + length;
wlk->data += length;
if (wlk->length == 0) {
new->next = wlk->next;
free (wlk);
}
if (wlk_last == NULL)
ret = new;
else
wlk_last->next = new;
return (ret);
}
return (NULL);
}
|