This repository was archived by the owner on Oct 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathElfSymInfos.cpp
More file actions
152 lines (140 loc) · 3.72 KB
/
Copy pathElfSymInfos.cpp
File metadata and controls
152 lines (140 loc) · 3.72 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
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
#include "ElfSymInfos.h"
ElfSymInfos::ElfSymInfos(string file, bool rdwr, bool show)
{
m_eptrs = new ElfPtrs(file,rdwr,show);
initModInfo();
initSymInfo();
m_show = show;
if(m_show)
{
printModInfo();
printSymInfo();
}
}
ElfSymInfos::~ElfSymInfos()
{
if(m_eptrs) delete m_eptrs;
}
void ElfSymInfos::printModInfo()
{
printf("ModInfo:\n");
for(auto it : m_minfo)
printf("\t%-64s\n",it);
}
void ElfSymInfos::printSymInfo()
{
printf("SymInfo:\n");
printf("\tnum_versions = %lu\n",m_vinfo.size());
for(auto it : m_vinfo)
printf("\tsym name = %-56s crc = 0x%lx\n",it.second->name,it.second->crc);
}
string ElfSymInfos::getVermagic()
{
const char *vermagic = "vermagic=";
for(auto it : m_minfo)
{
if(!strstr(it,vermagic)) continue;
return string(it+strlen(vermagic));
}
return "";
}
map<string, unsigned long> ElfSymInfos::getSymVerInfo()
{
map<string, unsigned long> ret;
for(auto it : m_vinfo)
{
ret.insert(make_pair(it.first,it.second->crc));
}
return ret;
}
map<string, modversion_info *>* ElfSymInfos::getVInfo()
{
return &m_vinfo;
}
list<char *>* ElfSymInfos::getMInfo()
{
return &m_minfo;
}
bool ElfSymInfos::initModInfo()
{
bool ret = false;
do
{
const TableInfo *info = m_eptrs->getTableInfo(".modinfo");
if(!info) break;
if(!info->addr)
{
printf("info.addr is NULL!!!\n");
break;
}
unsigned long size = info->addr->sh_size;
/*
* get_modinfo() calls made before rewrite_section_headers()
* must use sh_offset, as sh_addr isn't set!
*/
char *modinfo = (char *)m_eptrs->elf_hdr + info->addr->sh_offset;
for (char *p = modinfo; p; p = nextString(p, &size)) {
m_minfo.push_back(p);
}
}while(0);
return ret;
}
static __u32 resolve_rel_crc(const __u32 *crc)
{
return *(__u32 *)((char *)crc + *crc);
}
bool ElfSymInfos::initSymInfo()
{
bool ret = false;
do
{
const TableInfo *info = m_eptrs->getTableInfo("__versions");
if(!info) break;
if(!info->addr)
{
printf("info.addr is NULL!!!\n");
break;
}
unsigned int num_versions = info->addr->sh_size / sizeof(struct modversion_info);
modversion_info *versions = (modversion_info *)((char*)m_eptrs->elf_hdr + info->addr->sh_offset);
if(!versions)
{
printf("versions is NULL!!!\n");
break;
}
for(int i = 0; i < num_versions; i++) {
m_vinfo.insert(make_pair(versions[i].name,&(versions[i])));
/* 内核的校验流程 */
// u32 crcval;
// if (strcmp(versions[i].name, symname) != 0)
// continue;
// if (IS_ENABLED(CONFIG_MODULE_REL_CRCS))
// crcval = resolve_rel_crc(crc);
// else
// crcval = *crc;
// if (versions[i].crc == crcval)
// return 1;
// pr_debug("Found checksum %X vs module %lX\n",
// crcval, versions[i].crc);
// goto bad_version;
}
}while(0);
return ret;
}
/* Parse tag=value strings from .modinfo section */
char* ElfSymInfos::nextString(char *str, unsigned long *secsize)
{
/* Skip non-zero chars */
while (str[0]) {
str++;
if ((*secsize)-- <= 1)
return NULL;
}
/* Skip any zero padding. */
while (!str[0]) {
str++;
if ((*secsize)-- <= 1)
return NULL;
}
return str;
}