forked from AndroidAppsUsedByMyself/SKRoot-linuxKernelRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmAsmHelper_Linux.h
More file actions
94 lines (78 loc) · 2.38 KB
/
Copy pathArmAsmHelper_Linux.h
File metadata and controls
94 lines (78 loc) · 2.38 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
#ifndef ARM_ASM_HELPER_H_
#define ARM_ASM_HELPER_H_
#include <string>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
std::string AsmToBytes(const std::string& strArm64Asm) {
// 获取汇编文本
// 获取自身运行目录
char szFileName[1024] = { 0 };
readlink("/proc/self/exe", szFileName, sizeof(szFileName)-1);
std::string strMyPath = szFileName;
strMyPath = strMyPath.substr(0, strMyPath.find_last_of('/') + 1);
// 写出 input.s 文件
std::ofstream inputFile;
inputFile.open(strMyPath + "input.s", std::ios_base::out | std::ios_base::trunc);
inputFile << ".arch armv8-a\n";
inputFile << ".text\n";
inputFile << ".global _start\n";
inputFile << "_start:\n";
inputFile << strArm64Asm;
inputFile.close();
// ARM64
std::system(std::string("rm -f " + strMyPath + "output.txt").c_str());
std::string cmd = "aarch64-linux-gnu-as -o " + strMyPath + "output.o " + strMyPath + "input.s";
std::system(cmd.c_str());
cmd = "aarch64-linux-gnu-objcopy -O binary " + strMyPath + "output.o " + strMyPath + "output.bin";
std::system(cmd.c_str());
// 读取 output.bin 文件
std::ifstream in(strMyPath + "output.bin", std::ios::binary);
std::stringstream ssOutput;
if (in) // 有该文件
{
char buffer[1024] = {0};
while (in.read(buffer, sizeof(buffer)))
{
ssOutput << std::hex;
for(int i = 0; i < sizeof(buffer); i++) {
ssOutput << ((buffer[i] >> 4) & 0xf) << (buffer[i] & 0xf);
}
}
if (in.gcount() > 0) {
ssOutput << std::hex;
for(int i = 0; i < in.gcount(); i++) {
ssOutput << ((buffer[i] >> 4) & 0xf) << (buffer[i] & 0xf);
}
}
in.close();
}
std::system(std::string("rm -f " + strMyPath + "input.s").c_str());
std::system(std::string("rm -f " + strMyPath + "output.o").c_str());
std::system(std::string("rm -f " + strMyPath + "output.bin").c_str());
return ssOutput.str();
}
const char HEX[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
/* Convert byte array to hex string. */
std::string bytesToHexString(const unsigned char* input, size_t length) {
std::string str;
str.reserve(length << 1);
for (size_t i = 0; i < length; ++i) {
int t = input[i];
int a = t / 16;
int b = t % 16;
str.append(1, HEX[a]);
str.append(1, HEX[b]);
}
return str;
}
#endif /* ARM_ASM_HELPER_H_ */