1 //===-- llvm-rtdyld.cpp - MCJIT Testing Tool ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a testing tool for use with the MC-JIT LLVM components.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/Object/MachOObject.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/Memory.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/system_error.h"
24 using namespace llvm::object
;
26 static cl::opt
<std::string
>
27 InputFile(cl::Positional
, cl::desc("<input file>"), cl::init("-"));
33 static cl::opt
<ActionType
>
34 Action(cl::desc("Action to perform:"),
36 cl::values(clEnumValN(AC_Execute
, "execute",
37 "Load, link, and execute the inputs."),
42 static const char *ProgramName
;
44 static void Message(const char *Type
, const Twine
&Msg
) {
45 errs() << ProgramName
<< ": " << Type
<< ": " << Msg
<< "\n";
48 static int Error(const Twine
&Msg
) {
49 Message("error", Msg
);
55 loadSegment32(const MachOObject
*Obj
,
56 sys::MemoryBlock
&Data
,
57 const MachOObject::LoadCommandInfo
*SegmentLCI
,
58 const InMemoryStruct
<macho::SymtabLoadCommand
> &SymtabLC
,
59 StringMap
<void*> &SymbolTable
) {
60 InMemoryStruct
<macho::SegmentLoadCommand
> Segment32LC
;
61 Obj
->ReadSegmentLoadCommand(*SegmentLCI
, Segment32LC
);
63 return Error("unable to load segment load command");
65 // Map the segment into memory.
67 Data
= sys::Memory::AllocateRWX(Segment32LC
->VMSize
, 0, &ErrorStr
);
69 return Error("unable to allocate memory block: '" + ErrorStr
+ "'");
70 memcpy(Data
.base(), Obj
->getData(Segment32LC
->FileOffset
,
71 Segment32LC
->FileSize
).data(),
72 Segment32LC
->FileSize
);
73 memset((char*)Data
.base() + Segment32LC
->FileSize
, 0,
74 Segment32LC
->VMSize
- Segment32LC
->FileSize
);
76 // Bind the section indices to address.
77 void **SectionBases
= new void*[Segment32LC
->NumSections
];
78 for (unsigned i
= 0; i
!= Segment32LC
->NumSections
; ++i
) {
79 InMemoryStruct
<macho::Section
> Sect
;
80 Obj
->ReadSection(*SegmentLCI
, i
, Sect
);
82 return Error("unable to load section: '" + Twine(i
) + "'");
84 // FIXME: We don't support relocations yet.
85 if (Sect
->NumRelocationTableEntries
!= 0)
86 return Error("not yet implemented: relocations!");
88 // FIXME: Improve check.
89 if (Sect
->Flags
!= 0x80000400)
90 return Error("unsupported section type!");
92 SectionBases
[i
] = (char*) Data
.base() + Sect
->Address
;
95 // Bind all the symbols to address.
96 for (unsigned i
= 0; i
!= SymtabLC
->NumSymbolTableEntries
; ++i
) {
97 InMemoryStruct
<macho::SymbolTableEntry
> STE
;
98 Obj
->ReadSymbolTableEntry(SymtabLC
->SymbolTableOffset
, i
, STE
);
100 return Error("unable to read symbol: '" + Twine(i
) + "'");
101 if (STE
->SectionIndex
== 0)
102 return Error("unexpected undefined symbol!");
104 unsigned Index
= STE
->SectionIndex
- 1;
105 if (Index
>= Segment32LC
->NumSections
)
106 return Error("invalid section index for symbol: '" + Twine() + "'");
108 // Get the symbol name.
109 StringRef Name
= Obj
->getStringAtIndex(STE
->StringIndex
);
111 // Get the section base address.
112 void *SectionBase
= SectionBases
[Index
];
114 // Get the symbol address.
115 void *Address
= (char*) SectionBase
+ STE
->Value
;
117 // FIXME: Check the symbol type and flags.
118 if (STE
->Type
!= 0xF)
119 return Error("unexpected symbol type!");
120 if (STE
->Flags
!= 0x0)
121 return Error("unexpected symbol type!");
123 SymbolTable
[Name
] = Address
;
131 loadSegment64(const MachOObject
*Obj
,
132 sys::MemoryBlock
&Data
,
133 const MachOObject::LoadCommandInfo
*SegmentLCI
,
134 const InMemoryStruct
<macho::SymtabLoadCommand
> &SymtabLC
,
135 StringMap
<void*> &SymbolTable
) {
136 InMemoryStruct
<macho::Segment64LoadCommand
> Segment64LC
;
137 Obj
->ReadSegment64LoadCommand(*SegmentLCI
, Segment64LC
);
139 return Error("unable to load segment load command");
141 // Map the segment into memory.
142 std::string ErrorStr
;
143 Data
= sys::Memory::AllocateRWX(Segment64LC
->VMSize
, 0, &ErrorStr
);
145 return Error("unable to allocate memory block: '" + ErrorStr
+ "'");
146 memcpy(Data
.base(), Obj
->getData(Segment64LC
->FileOffset
,
147 Segment64LC
->FileSize
).data(),
148 Segment64LC
->FileSize
);
149 memset((char*)Data
.base() + Segment64LC
->FileSize
, 0,
150 Segment64LC
->VMSize
- Segment64LC
->FileSize
);
152 // Bind the section indices to address.
153 void **SectionBases
= new void*[Segment64LC
->NumSections
];
154 for (unsigned i
= 0; i
!= Segment64LC
->NumSections
; ++i
) {
155 InMemoryStruct
<macho::Section64
> Sect
;
156 Obj
->ReadSection64(*SegmentLCI
, i
, Sect
);
158 return Error("unable to load section: '" + Twine(i
) + "'");
160 // FIXME: We don't support relocations yet.
161 if (Sect
->NumRelocationTableEntries
!= 0)
162 return Error("not yet implemented: relocations!");
164 // FIXME: Improve check.
165 if (Sect
->Flags
!= 0x80000400)
166 return Error("unsupported section type!");
168 SectionBases
[i
] = (char*) Data
.base() + Sect
->Address
;
171 // Bind all the symbols to address.
172 for (unsigned i
= 0; i
!= SymtabLC
->NumSymbolTableEntries
; ++i
) {
173 InMemoryStruct
<macho::Symbol64TableEntry
> STE
;
174 Obj
->ReadSymbol64TableEntry(SymtabLC
->SymbolTableOffset
, i
, STE
);
176 return Error("unable to read symbol: '" + Twine(i
) + "'");
177 if (STE
->SectionIndex
== 0)
178 return Error("unexpected undefined symbol!");
180 unsigned Index
= STE
->SectionIndex
- 1;
181 if (Index
>= Segment64LC
->NumSections
)
182 return Error("invalid section index for symbol: '" + Twine() + "'");
184 // Get the symbol name.
185 StringRef Name
= Obj
->getStringAtIndex(STE
->StringIndex
);
187 // Get the section base address.
188 void *SectionBase
= SectionBases
[Index
];
190 // Get the symbol address.
191 void *Address
= (char*) SectionBase
+ STE
->Value
;
193 // FIXME: Check the symbol type and flags.
194 if (STE
->Type
!= 0xF)
195 return Error("unexpected symbol type!");
196 if (STE
->Flags
!= 0x0)
197 return Error("unexpected symbol type!");
199 SymbolTable
[Name
] = Address
;
206 static int executeInput() {
207 // Load the input memory buffer.
208 OwningPtr
<MemoryBuffer
> InputBuffer
;
209 if (error_code ec
= MemoryBuffer::getFileOrSTDIN(InputFile
, InputBuffer
))
210 return Error("unable to read input: '" + ec
.message() + "'");
212 // Load the Mach-O wrapper object.
213 std::string ErrorStr
;
214 OwningPtr
<MachOObject
> Obj(
215 MachOObject::LoadFromBuffer(InputBuffer
.take(), &ErrorStr
));
217 return Error("unable to load object: '" + ErrorStr
+ "'");
219 // Validate that the load commands match what we expect.
220 const MachOObject::LoadCommandInfo
*SegmentLCI
= 0, *SymtabLCI
= 0,
222 for (unsigned i
= 0; i
!= Obj
->getHeader().NumLoadCommands
; ++i
) {
223 const MachOObject::LoadCommandInfo
&LCI
= Obj
->getLoadCommandInfo(i
);
224 switch (LCI
.Command
.Type
) {
225 case macho::LCT_Segment
:
226 case macho::LCT_Segment64
:
228 return Error("unexpected input object (multiple segments)");
231 case macho::LCT_Symtab
:
233 return Error("unexpected input object (multiple symbol tables)");
236 case macho::LCT_Dysymtab
:
238 return Error("unexpected input object (multiple symbol tables)");
242 return Error("unexpected input object (unexpected load command");
247 return Error("no symbol table found in object");
249 return Error("no symbol table found in object");
251 // Read and register the symbol table data.
252 InMemoryStruct
<macho::SymtabLoadCommand
> SymtabLC
;
253 Obj
->ReadSymtabLoadCommand(*SymtabLCI
, SymtabLC
);
255 return Error("unable to load symbol table load command");
256 Obj
->RegisterStringTable(*SymtabLC
);
258 // Read the dynamic link-edit information, if present (not present in static
261 InMemoryStruct
<macho::DysymtabLoadCommand
> DysymtabLC
;
262 Obj
->ReadDysymtabLoadCommand(*DysymtabLCI
, DysymtabLC
);
264 return Error("unable to load dynamic link-exit load command");
266 // FIXME: We don't support anything interesting yet.
267 if (DysymtabLC
->LocalSymbolsIndex
!= 0)
268 return Error("NOT YET IMPLEMENTED: local symbol entries");
269 if (DysymtabLC
->ExternalSymbolsIndex
!= 0)
270 return Error("NOT YET IMPLEMENTED: non-external symbol entries");
271 if (DysymtabLC
->UndefinedSymbolsIndex
!= SymtabLC
->NumSymbolTableEntries
)
272 return Error("NOT YET IMPLEMENTED: undefined symbol entries");
275 // Load the segment load command.
276 sys::MemoryBlock Data
;
277 StringMap
<void*> SymbolTable
;
278 if (SegmentLCI
->Command
.Type
== macho::LCT_Segment
) {
279 if (loadSegment32(Obj
.get(), Data
, SegmentLCI
, SymtabLC
, SymbolTable
))
282 if (loadSegment64(Obj
.get(), Data
, SegmentLCI
, SymtabLC
, SymbolTable
))
286 // Get the address of "_main".
287 StringMap
<void*>::iterator it
= SymbolTable
.find("_main");
288 if (it
== SymbolTable
.end())
289 return Error("no definition for '_main'");
291 // Invalidate the instruction cache.
292 sys::Memory::InvalidateInstructionCache(Data
.base(), Data
.size());
294 // Make sure the memory is executable.
295 if (!sys::Memory::setExecutable(Data
, &ErrorStr
))
296 return Error("unable to mark function executable: '" + ErrorStr
+ "'");
298 // Dispatch to _main().
299 void *MainAddress
= it
->second
;
300 errs() << "loaded '_main' at: " << MainAddress
<< "\n";
302 int (*Main
)(int, const char**) =
303 (int(*)(int,const char**)) uintptr_t(MainAddress
);
304 const char **Argv
= new const char*[2];
305 Argv
[0] = InputFile
.c_str();
307 return Main(1, Argv
);
310 int main(int argc
, char **argv
) {
311 ProgramName
= argv
[0];
312 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
314 cl::ParseCommandLineOptions(argc
, argv
, "llvm MC-JIT tool\n");
319 return executeInput();