-
Notifications
You must be signed in to change notification settings - Fork 0
/
programfs.c
212 lines (166 loc) · 4.73 KB
/
programfs.c
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
212
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "log.h"
#include "misc.h"
#include "elfs.h"
#include "defaultfs.h"
#include "programfs.h"
telf_ctx *ctx;
static void
programfs_freecontent(void *data)
{
telf_default_content *content = data;
if (! content)
return;
if (content->buf)
free(content->buf);
free(content);
}
static telf_status
programfs_code_getsize(void *obj_hdl,
size_t *sizep)
{
telf_obj *obj = obj_hdl;
telf_status ret;
char realname[128];
Elf64_Shdr *shdr = NULL;
size_t size;
sprintf(realname, ".%s", obj->parent->name);
shdr = elf_getsectionbyname(ctx, realname);
size = shdr->sh_size;
ret = ELF_SUCCESS;
end:
if (sizep)
*sizep = size;
return ret;
}
static telf_status
programfs_code_setcontent(void *obj_hdl,
char **bufp,
size_t *buf_lenp)
{
telf_obj *obj = obj_hdl;
telf_status ret;
char realname[128];
Elf64_Shdr *shdr = NULL;
char *buf = NULL;
size_t buf_len = 0;
sprintf(realname, ".%s", obj->parent->name);
shdr = elf_getsectionbyname(ctx, realname);
buf_len = shdr->sh_size;
if (buf_len) {
buf = malloc(buf_len);
if (! buf) {
ERR("malloc: %s", strerror(errno));
ret = ELF_ENOMEM;
goto end;
}
memcpy(buf, obj->ctx->addr + shdr->sh_offset, buf_len);
}
ret = ELF_SUCCESS;
end:
if (bufp)
*bufp = buf;
else
free(buf);
if (buf_lenp)
*buf_lenp = buf_len;
return ret;
}
typedef struct {
char *str;
tobj_getsize_func getsize_func;
tobj_setcontent_func setcontent_func;
tobj_freecontent_func freecontent_func;
} telf_fcb;
static telf_fcb programfs_fcb[] = {
{
"code",
programfs_code_getsize,
programfs_code_setcontent,
programfs_freecontent
},
};
static telf_status
programfs_getattr(void *obj_hdl,
telf_stat *stp)
{
telf_obj *obj = obj_hdl;
telf_status ret;
telf_status rc;
telf_stat st;
int i;
elf_obj_lock(obj);
memset(&st, 0, sizeof st);
st.st_mode |= ELF_S_IFREG;
for (i = 0; i < N_ELEMS(programfs_fcb); i++) {
telf_fcb *fcb = programfs_fcb + i;
if (0 == strcmp(obj->name, fcb->str)) {
rc = fcb->getsize_func(obj, &st.st_size);
if (ELF_SUCCESS != rc) {
ERR("can't get size of '%s'", obj->name);
ret = rc;
goto end;
}
break;
}
}
ret = ELF_SUCCESS;
end:
elf_obj_unlock(obj);
if (stp)
*stp = st;
return ret;
}
static void
programfs_override_driver(telf_fs_driver *driver)
{
driver->getattr = programfs_getattr;
}
static void
section_ctor_cb(void *obj_hdl,
void *to_ignore)
{
int i;
telf_obj *obj = obj_hdl;
telf_obj *entry = NULL;
telf_status rc;
if (ELF_SECTION_PROGBITS != obj->type)
return;
for (i = 0; i < N_ELEMS(programfs_fcb); i++) {
telf_fcb *fcb = programfs_fcb + i;
entry = elf_obj_new(obj->ctx, fcb->str, obj,
ELF_SECTION_PROGBITS_CODE,
ELF_S_IFREG);
if (! entry) {
ERR("can't build entry '%s'", fcb->str);
continue;
}
entry->free_func = fcb->freecontent_func;
entry->fill_func = fcb->setcontent_func;
programfs_override_driver(entry->driver);
list_add(obj->entries, entry);
}
}
telf_status
programfs_build(telf_ctx *ctx)
{
telf_obj *obj_sections = NULL;
telf_obj *section = NULL;
telf_status ret;
int rc;
int i;
rc = elf_namei(ctx, "/sections", &obj_sections);
if (ELF_SUCCESS != rc) {
ERR("can't find '/sections' object: %s",
elf_status_to_str(rc));
ret = rc;
goto end;
}
list_map(obj_sections->entries, section_ctor_cb, NULL);
ret = ELF_SUCCESS;
end:
return ret;
}