Debian Codegraph @

tests/stub-tool/cpp-stub/addr_any.h

raw symbols SCIP
1 #ifndef __ADDR_ANY_H__
2 #define __ADDR_ANY_H__
3
4
5 //linux
6 #include <regex.h>
7 #include <cxxabi.h>
8 //c
9 #include <cinttypes>
10 #include <cstdio>
11 #include <cstdlib>
12
13 //c++
14 #include <string>
15 #include <map>
16 //project
17 #include "elfio.hpp"
18
19
20
21 class AddrAny
22 {
23 public:
24 AddrAny()
25 {
26 m_init = get_exe_pathname(m_fullname);
27 m_baseaddr = 0;
28 }
29 AddrAny(std::string libname)
30 {
31 m_init = get_lib_pathname_and_baseaddr(libname, m_fullname, m_baseaddr);
32 }
33
34 int get_local_func_addr_symtab(std::string func_name_regex_str, std::map<std::string,void*>& result)
35 {
36 return get_func_addr(SHT_SYMTAB, STB_LOCAL, func_name_regex_str, result);
37 }
38 int get_global_func_addr_symtab(std::string func_name_regex_str, std::map<std::string,void*>& result)
39 {
40 return get_func_addr(SHT_SYMTAB, STB_GLOBAL, func_name_regex_str, result);
41 }
42 int get_weak_func_addr_symtab(std::string func_name_regex_str, std::map<std::string,void*>& result)
43 {
44 return get_func_addr(SHT_SYMTAB, STB_WEAK, func_name_regex_str, result);
45 }
46
47 int get_global_func_addr_dynsym( std::string func_name_regex_str, std::map<std::string,void*>& result)
48 {
49 return get_func_addr(SHT_DYNSYM, STB_GLOBAL, func_name_regex_str, result);
50 }
51 int get_weak_func_addr_dynsym(std::string func_name_regex_str, std::map<std::string,void*>& result)
52 {
53 return get_func_addr(SHT_DYNSYM, STB_WEAK, func_name_regex_str, result);
54 }
55
56 private:
57 bool demangle(std::string& s, std::string& name) {
58 int status;
59 char* pname = abi::__cxa_demangle(s.c_str(), 0, 0, &status);
60 if (status != 0)
61 {
62 switch(status)
63 {
64 case -1: name = "memory allocation error"; break;
65 case -2: name = "invalid name given"; break;
66 case -3: name = "internal error: __cxa_demangle: invalid argument"; break;
67 default: name = "unknown error occured"; break;
68 }
69 return false;
70 }
71 name = pname;
72 free(pname);
73 return true;
74 }
75 bool get_exe_pathname( std::string& name)
76 {
77 char line[512];
78 FILE *fp;
79 uintptr_t base_addr;
80 char perm[5];
81 unsigned long offset;
82 int pathname_pos;
83 char *pathname;
84 size_t pathname_len;
85 int match = 0;
86
87 if(NULL == (fp = fopen("/proc/self/maps", "r")))
88 {
89 return false;
90 }
91
92 while(fgets(line, sizeof(line), fp))
93 {
94 if(sscanf(line, "%" PRIxPTR "-%*lx %4s %lx %*x:%*x %*d%n", &base_addr, perm, &offset, &pathname_pos) != 3) continue;
95
96 if(0 != offset) continue;
97
98 //get pathname
99 while(isspace(line[pathname_pos]) && pathname_pos < (int)(sizeof(line) - 1))
100 pathname_pos += 1;
101 if(pathname_pos >= (int)(sizeof(line) - 1)) continue;
102 pathname = line + pathname_pos;
103 pathname_len = strlen(pathname);
104 if(0 == pathname_len) continue;
105 if(pathname[pathname_len - 1] == '\n')
106 {
107 pathname[pathname_len - 1] = '\0';
108 pathname_len -= 1;
109 }
110 if(0 == pathname_len) continue;
111 if('[' == pathname[0]) continue;
112
113 name = pathname;
114 match = 1;
115 break;
116
117 }
118 fclose(fp);
119
120 if(0 == match)
121 {
122 return false;
123 }
124 else
125 {
126 return true;
127 }
128
129 }
130
131 bool get_lib_pathname_and_baseaddr(std::string pathname_regex_str, std::string& name, unsigned long& addr)
132 {
133 char line[512];
134 FILE *fp;
135 uintptr_t base_addr;
136 char perm[5];
137 unsigned long offset;
138 int pathname_pos;
139 char *pathname;
140 size_t pathname_len;
141 int match;
142 regex_t pathname_regex;
143
144 regcomp(&pathname_regex, pathname_regex_str.c_str(), 0);
145
146 if(NULL == (fp = fopen("/proc/self/maps", "r")))
147 {
148 return false;
149 }
150
151 while(fgets(line, sizeof(line), fp))
152 {
153 if(sscanf(line, "%" PRIxPTR "-%*lx %4s %lx %*x:%*x %*d%n", &base_addr, perm, &offset, &pathname_pos) != 3) continue;
154
155 //check permission
156 if(perm[0] != 'r') continue;
157 if(perm[3] != 'p') continue; //do not touch the shared memory
158
159 //check offset
160 //
161 //We are trying to find ELF header in memory.
162 //It can only be found at the beginning of a mapped memory regions
163 //whose offset is 0.
164 if(0 != offset) continue;
165
166 //get pathname
167 while(isspace(line[pathname_pos]) && pathname_pos < (int)(sizeof(line) - 1))
168 pathname_pos += 1;
169 if(pathname_pos >= (int)(sizeof(line) - 1)) continue;
170 pathname = line + pathname_pos;
171 pathname_len = strlen(pathname);
172 if(0 == pathname_len) continue;
173 if(pathname[pathname_len - 1] == '\n')
174 {
175 pathname[pathname_len - 1] = '\0';
176 pathname_len -= 1;
177 }
178 if(0 == pathname_len) continue;
179 if('[' == pathname[0]) continue;
180
181 //check pathname
182 //if we need to hook this elf?
183 match = 0;
184 if(0 == regexec(&pathname_regex, pathname, 0, NULL, 0))
185 {
186 match = 1;
187 name = pathname;
188 addr = (unsigned long)base_addr;
189 break;
190 }
191 if(0 == match) continue;
192
193 }
194 fclose(fp);
195 if(0 == match)
196 {
197 return false;
198 }
199 else
200 {
201 return true;
202 }
203
204 }
205
206 int get_func_addr(unsigned int ttype, unsigned int stype, std::string& func_name_regex_str, std::map<std::string,void*>& result)
207 {
208 // Create an elfio reader
209 ELFIO::elfio reader;
210 int count = 0;
211 regex_t pathname_regex;
212
213 if(!m_init)
214 {
215 return -1;
216 }
217
218 regcomp(&pathname_regex, func_name_regex_str.c_str(), 0);
219 // Load ELF data
220 if(!reader.load(m_fullname.c_str()))
221 {
222 return -1;
223 }
224
225 ELFIO::Elf_Half sec_num = reader.sections.size();
226 for(int i = 0; i < sec_num; ++i)
227 {
228 ELFIO::section* psec = reader.sections[i];
229 // Check section type
230 if(psec->get_type() == ttype)
231 {
232 const ELFIO::symbol_section_accessor symbols( reader, psec );
233 for ( unsigned int j = 0; j < symbols.get_symbols_num(); ++j )
234 {
235 std::string name;
236 std::string name_mangle;
237 ELFIO::Elf64_Addr value;
238 ELFIO::Elf_Xword size;
239 unsigned char bind;
240 unsigned char type;
241 ELFIO::Elf_Half section_index;
242 unsigned char other;
243
244 // Read symbol properties
245 symbols.get_symbol( j, name, value, size, bind, type, section_index, other );
246 if(type == STT_FUNC && bind == stype)
247 {
248 bool ret = demangle(name,name_mangle);
249 if(ret == true)
250 {
251 if (0 == regexec(&pathname_regex, name_mangle.c_str(), 0, NULL, 0))
252 {
253 result.insert ( std::pair<std::string,void *>(name_mangle,(void*)(value + m_baseaddr)));
254 count++;
255 }
256 }
257 else
258 {
259 if (0 == regexec(&pathname_regex, name.c_str(), 0, NULL, 0))
260 {
261 result.insert ( std::pair<std::string,void *>(name,(void*)(value + m_baseaddr)));
262 count++;
263 }
264 }
265 }
266 }
267 break;
268 }
269 }
270
271 return count;
272 }
273 private:
274 bool m_init;
275 std::string m_name;
276 std::string m_fullname;
277 unsigned long m_baseaddr;
278
279 };
280 #endif