-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsectionfs.c
More file actions
96 lines (80 loc) · 2.9 KB
/
sectionfs.c
File metadata and controls
96 lines (80 loc) · 2.9 KB
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
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "sectionfs.h"
#include "fs-structs.h"
#include "log.h"
#include "elfs.h"
#include "sectionfs.h"
#include "defaultfs.h"
#include "symbolfs.h"
extern telf_ctx *ctx;
/* section directory object creation */
telf_status
sectionfs_build(telf_ctx *ctx)
{
telf_status ret;
telf_status rc;
int i;
telf_obj *sections_obj = NULL;
Elf64_Shdr *sh_strtab = ctx->shdr + ctx->ehdr->e_shstrndx;
char *sh_strtab_p = ctx->addr + sh_strtab->sh_offset;
rc = elf_namei(ctx, "/sections", §ions_obj);
if (ELF_SUCCESS != rc) {
ERR("can't find any section entry: %s", elf_status_to_str(rc));
ret = ELF_ENOENT;
goto end;
}
ctx->n_sections = ctx->ehdr->e_shnum;
if (! ctx->n_sections)
return ELF_SUCCESS;
for (i = 0; i < ctx->n_sections; ++i) {
Elf64_Shdr *shdr = ctx->shdr + i;
telf_type type;
char name[128];
char *s_name = sh_strtab_p + shdr->sh_name;
telf_obj *obj = NULL;
if (! *s_name)
/* empty name, use the section address */
sprintf(name, "noname.%p", sh_strtab + i);
else
/* we want to convert '.bss', etc to 'bss', etc*/
sprintf(name, "%s",
'.' == *s_name ? s_name + 1 : s_name);
switch (ctx->shdr[i].sh_type) {
#define MAP(x) case SHT_##x: type = ELF_SECTION_##x; break
MAP(NULL);
MAP(DYNSYM);
MAP(SYMTAB);
MAP(NOBITS);
MAP(PROGBITS);
MAP(DYNAMIC);
MAP(HASH);
MAP(NOTE);
MAP(REL);
MAP(RELA);
MAP(STRTAB);
#undef MAP
default:
ERR("unknown object type: 0x%x", shdr->sh_type);
type = ELF_SECTION_OTHER;
break;
}
obj = elf_obj_new(ctx, name, sections_obj, type, ELF_S_IFDIR);
if (! obj) {
ERR("obj '%s' creation failed", name);
ret = ELF_FAILURE;
goto end;
}
if (SHF_WRITE & shdr->sh_flags)
obj->st.st_mode |= ELF_S_IWUSR;
if (SHF_ALLOC & shdr->sh_flags)
obj->st.st_mode |= ELF_S_IRUSR;
if (SHF_EXECINSTR & shdr->sh_flags)
obj->st.st_mode |= ELF_S_IXUSR;
list_add(sections_obj->entries, obj);
}
ret = ELF_SUCCESS;
end:
return ret;
}