Skip to content

Commit 8be1d9a

Browse files
committed
[PPC][BOLT] Add minimal PowerPC MCSymbolizer.
Introduce PPCMCSymbolizer. This initial version supports resolving relocations by printing symbolic names and attaching them to instructions. It enables BOLT to disassemble simple PowerPC binaries (e.g., Hello World) with basic symbolic operands.
1 parent 4b547ad commit 8be1d9a

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

bolt/lib/Target/PowerPC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ endif()
1717

1818
add_llvm_library(LLVMBOLTTargetPowerPC
1919
PPCMCPlusBuilder.cpp
20+
PPCMCSymbolizer.cpp
2021

2122
NO_EXPORT
2223
DISABLE_LLVM_LINK_LLVM_DYLIB
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===- bolt/Target/PPC/PPCMCSymbolizer.cpp ----------------------*- C++ -*-===//
2+
//
3+
// Minimal PowerPC Symbolizer for BOLT "Hello World" Programs
4+
//
5+
//===----------------------------------------------------------------------===//
6+
7+
#include "PPCMCSymbolizer.h"
8+
#include "bolt/Core/BinaryFunction.h"
9+
#include "bolt/Core/Relocation.h"
10+
#include "llvm/MC/MCInst.h"
11+
12+
using namespace llvm;
13+
using namespace bolt;
14+
15+
PPCMCSymbolizer::~PPCMCSymbolizer() = default;
16+
17+
bool PPCMCSymbolizer::tryAddingSymbolicOperand(
18+
MCInst &Inst, raw_ostream &CStream, int64_t Value, uint64_t Address,
19+
bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) {
20+
// 1) Normalize to function-relative offset
21+
BinaryContext &BC = Function.getBinaryContext();
22+
MCContext *Ctx = BC.Ctx.get();
23+
const uint64_t InstOffset = Address - Function.getAddress();
24+
25+
// 2) Find relocation at "instruction start + immediate offset"
26+
const Relocation *Rel = Function.getRelocationAt(InstOffset + Offset);
27+
if (!Rel)
28+
return false;
29+
30+
// 3) Build MCExpr = Symbol [+ Addend] and attach as a real operand
31+
const MCSymbol *Sym = Rel->Symbol; // prefer the pointer, not a name string
32+
const MCExpr *Expr = MCSymbolRefExpr::create(Sym, *Ctx);
33+
if (Rel->Addend)
34+
Expr = MCBinaryExpr::createAdd(
35+
Expr, MCConstantExpr::create(Rel->Addend, *Ctx), *Ctx);
36+
37+
Inst.addOperand(MCOperand::createExpr(Expr));
38+
return true;
39+
}
40+
41+
void PPCMCSymbolizer::tryAddingPcLoadReferenceComment(raw_ostream &CStream,
42+
int64_t Value,
43+
uint64_t Address) {
44+
// For "Hello World": no special PC-relative loads, leave empty for now
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===- bolt/Target/PPC/PPCMCSymbolizer.h ------------------------*- C++ -*-===//
2+
//
3+
// Minimal PowerPC Symbolizer for BOLT "Hello World" Programs
4+
//
5+
//===----------------------------------------------------------------------===//
6+
7+
#ifndef BOLT_TARGET_PPC_PPCMCSYMBOLIZER_H
8+
#define BOLT_TARGET_PPC_PPCMCSYMBOLIZER_H
9+
10+
#include "bolt/Core/BinaryFunction.h"
11+
#include "llvm/MC/MCDisassembler/MCSymbolizer.h"
12+
13+
namespace llvm {
14+
namespace bolt {
15+
16+
class PPCMCSymbolizer : public MCSymbolizer {
17+
protected:
18+
BinaryFunction &Function;
19+
20+
public:
21+
PPCMCSymbolizer(BinaryFunction &Function)
22+
: MCSymbolizer(*Function.getBinaryContext().Ctx, nullptr),
23+
Function(Function) {}
24+
25+
PPCMCSymbolizer(const PPCMCSymbolizer &) = delete;
26+
PPCMCSymbolizer &operator=(const PPCMCSymbolizer &) = delete;
27+
virtual ~PPCMCSymbolizer();
28+
29+
/// Minimal: Try to add a symbolic operand if there is a matching relocation
30+
bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &CStream,
31+
int64_t Value, uint64_t Address, bool IsBranch,
32+
uint64_t Offset, uint64_t OpSize,
33+
uint64_t InstSize) override;
34+
35+
void tryAddingPcLoadReferenceComment(raw_ostream &CStream, int64_t Value,
36+
uint64_t Address) override;
37+
};
38+
39+
} // namespace bolt
40+
} // namespace llvm
41+
42+
#endif

0 commit comments

Comments
 (0)