当前位置:网站首页>MC Instruction Decoder
MC Instruction Decoder
2022-06-30 16:41:00 【Hui's Sutra Pavilion】
Instruction Decoder
MC Instruction Decoder Belong to MC layer LLVM Disassembly module in , The main function is to read and write the input binary file as Bytes form , The output corresponds to Target Machine instructions MCInst, Is an indispensable module in disassembly :
MCDisassembler class
MCDisassembler Class is MC Instruction Decoder Improved interface classes in and out , Belong to all target disassembler Of superclass,
This class is located in :llvm\include\llvm\MC\MCDisassembler\MCDisassembler.h file
This class has few interfaces , One of the most important interfaces :getInstruction:
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
ArrayRef<uint8_t> Bytes, uint64_t Address,
raw_ostream &CStream)
This interface converts executable binaries to Bytes As input , Input through disassembly MCInst Machine instructions .
This interface is a virtual function , Every target Machine instructions are different , Writing a new backend requires MCDisassembler Base class , Derive from target Corresponding disassembler, Realization getInstruction Interface .
AMDGPU The derivation relationship is as follows :
AMDGPUDisassembler be located llvm\lib\Target\AMDGPU\Disassembler\AMDGPUDisassembler.h
Disassembler Usage method
disassembler The usage method is usually divided into the following steps :
RegisterMCDisassembler()
When a new backend is added , Need to call RegisterMCDisassembler towards Target Registered in disassemble, AMDGPU Registered at llvm\lib\Target\AMDGPU\Disassemble\AMDGPUDisassembler.cpp:
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUDisassembler() {
TargetRegistry::RegisterMCDisassembler(getTheGCNTarget(),
createAMDGPUDisassembler);
TargetRegistry::RegisterMCSymbolizer(getTheGCNTarget(),
createAMDGPUSymbolizer);
}
RegisterMCDisassembler The main registration corresponds to establish disassamble Callback function createAMDGPUDisassembler.
static void RegisterMCDisassembler(Target &T,
Target::MCDisassemblerCtorTy Fn) {
T.MCDisassemblerCtorFn = Fn;
}
establish disassembler The callback function is registered to MCDisassemblerCtorFn.
createMCDisassembler
createMCDisassembler Create the corresponding MCDisassembler:
MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
MCContext &Ctx) const {
if (!MCDisassemblerCtorFn)
return nullptr;
return MCDisassemblerCtorFn(*this, STI, Ctx);
}
Call registered createAMDGPUDisassemble Callback function , establish AMDGPUDisassembler.
getInstruction
The parsed binary file Text Code segment , Turn into ArrayRef<uint8_t>, Disassembly , for example :
MCInst Inst;
bool Disassembled =
DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
SectionAddr + Index, CommentStream);
AMDGPU getInstruction()
AMDGPU The disassembly implementation is located at llvm\lib\Target\AMDGPU\Disassembler\AMDGPUDisassembler.cpp file :
The implementation process is relatively clear , Disassembly is mainly divided into two parts :
- First, disassemble the binary , Look for all kinds of DecoderTable surface , Find the instruction to which the binary belongs . The characteristic of this step is to check DecoderTable surface ,AMDGPU According to different architecture instructions and instruction types, it is divided into multiple DecoderTable surface , Step by step matching is required .
- After matching , Further processing for some special instructions .
DecoderTable
AMDGPU Back end to support disassembly , Internally, the command is divided into multiple DecoderTable,getInstruction Each... Will be matched and queried in turn DecoderTable Table to match , Exist at present DecoderTable There are mainly :
- DecoderTableGFX10_B64
- DecoderTableDPP864
- DecoderTableDPP64
- DecoderTableSDWA64
- DecoderTableSDWA964
- DecoderTableSDWA1064
- DecoderTableGFX80_UNPACKED64
- DecoderTableGFX832
- DecoderTableAMDGPU32
- DecoderTableGFX932
- DecoderTableGFX90A32
- DecoderTableGFX10_B32
- DecoderTableGFX1032
- DecoderTableGFX90A64
- DecoderTableGFX864
- DecoderTableAMDGPU64
- DecoderTableGFX964
- DecoderTableGFX1064
above DecoderTable Yes TableGen Automatically generate to... During the compilation phase build/lib/Target/AMDGPU/AMDGPUGenDisassemblerTables.inc In file , Generate commands manually :
llvm-tblgen -gen-disassembler./AMDGPU.td -I ../../../include
With DecoderTableGFX932 For example, GFX 32 position DecoderTable surface :
The beginning of each line MCD::OPC_XXXX Is the corresponding status , And determine the significance of subsequent numerical values
For example, the beginning MCD::OPC_ExtractField follow-up 25,7 Jump to the start and end of the match for the next step ,start=25, end=31.
decodeInstruction
decodeInstruction Based on the DecoderTable surface , Disassemble and decode , be located build/lib/Target/AMDGPU/AMDGPUGenDisassemblerTables.inc In file ,
According to the matching table MCD::OPC_XXXX Match operation , until MCD::OPC_Decode The status description is in this DecoderTable Table matching succeeded , otherwise MCD::OPC_Fail Matching failure , Try the next DecoderTable Table to match
Here is an example :
/* 62 */ MCD::OPC_Decode, 236, 127, 46, // Opcode: V_ADDC_CO_U32_e32_gfx9
236,127 Solve the corresponding... For the corresponding opcode, The highest number 8 individual bit Bit flag bit , When the setting represents the opcode Greater than 128 It needs to be split into multiple , Until the highest position is not 1 until , The calculation method is as follows :
(236&~128)+ 127<<7
46 Corresponding DecodeIdx,decodeToMCInst Decide to follow up operand Register parsing operation :
Eventually the machine instructions will be disassembled MCInst.
边栏推荐
- [cve-2019-0193] - Apache Solr dataimport remote command execution analysis
- 【Verilog基础】十进制负数的八进制、十六进制表示
- register_chrdev和cdev_init cdev_add用法区别
- Does flinkcdc have to be a clustered version if the monitored database is Mongo
- 微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
- Cesium-1.72 learning (earth model creation online offline tile)
- How to get the preferential activities for stock account opening? Is online account opening safe?
- Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
- 【活动报名】探秘元宇宙,就差你了!7月2号我在深圳现场等你!
- 招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
猜你喜欢
Li Zexiang, a legendary Chinese professor, is making unicorns in batches
Headhunter 50, 000, I'll go to VC
优惠券种类那么多,先区分清楚再薅羊毛!
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
How the edge computing platform helps the development of the Internet of things
mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT解决办法
Bc1.2 PD protocol
I implement "stack" with C I
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
Hologres共享集群助力淘宝订阅极致精细化运营
随机推荐
Headhunter 50, 000, I'll go to VC
Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
赛芯电子冲刺科创板:拟募资6.2亿 实控人谭健为美国籍
几百行代码实现一个 JSON 解析器
备战数学建模36-时间序列模型2
备战数学建模34-BP神经网络预测2
药品管理系统加数据库,一夜做完,加报告
牛客网:乘积为正数的最长连续子数组
What is XR extended reality and what are the XR cloud streaming platforms
Create statement for Oracle export view
大学生研究生毕业找工作,该选择哪个方向?
Log4j2 advanced use
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
Explain in detail the use of for loop, break and continue in go language
Build cloud native observability capability suitable for organizations
招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
How to get the preferential activities for stock account opening? Is online account opening safe?
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;