2013-08-29 04:57:05 +04:00
|
|
|
//===-- lldb-moduleimport-test.cpp - LLDB moduleimport tester -------------===//
|
|
|
|
//
|
|
|
|
// This source file is part of the Swift.org open source project
|
|
|
|
//
|
|
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
|
|
//
|
|
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program simulates LLDB importing modules from the __apple_ast
|
|
|
|
// section in Mach-O files. We use it to test for regressions in the
|
|
|
|
// deserialization API.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "swift/Frontend/Frontend.h"
|
|
|
|
#include "swift/Serialization/SerializedModuleLoader.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2013-09-03 22:09:53 +04:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-08-29 04:57:05 +04:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2013-08-29 22:59:14 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-08-29 04:57:05 +04:00
|
|
|
#include <mach-o/loader.h>
|
2013-08-30 00:06:25 +04:00
|
|
|
#include <stdint.h>
|
2013-08-29 04:57:05 +04:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
InputNames(llvm::cl::Positional, llvm::cl::desc("compiled_swift_file1.o ..."),
|
2013-08-29 22:46:56 +04:00
|
|
|
llvm::cl::OneOrMore);
|
2013-08-29 04:57:05 +04:00
|
|
|
|
|
|
|
#ifndef SWIFT_MODULES_SDK
|
|
|
|
#define SWIFT_MODULES_SDK ""
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static llvm::cl::opt<std::string>
|
|
|
|
SDK("sdk", llvm::cl::desc("path to the SDK to build against"),
|
|
|
|
llvm::cl::init(SWIFT_MODULES_SDK));
|
|
|
|
|
|
|
|
void anchorForGetMainExecutable() {}
|
|
|
|
|
2013-09-03 22:09:53 +04:00
|
|
|
|
2013-08-29 04:57:05 +04:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
|
|
|
llvm::PrettyStackTraceProgram ST(argc, argv);
|
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv);
|
|
|
|
|
|
|
|
// Create a Swift compiler.
|
2013-09-03 22:09:53 +04:00
|
|
|
llvm::SmallVector<std::string, 4> modules;
|
2013-08-29 04:57:05 +04:00
|
|
|
swift::CompilerInstance CI;
|
|
|
|
swift::CompilerInvocation Invocation;
|
|
|
|
|
|
|
|
Invocation.setMainExecutablePath(
|
|
|
|
llvm::sys::fs::getMainExecutable(argv[0],
|
|
|
|
reinterpret_cast<void *>(&anchorForGetMainExecutable)));
|
|
|
|
|
|
|
|
Invocation.setSDKPath(SDK);
|
|
|
|
Invocation.setTargetTriple(llvm::sys::getDefaultTargetTriple());
|
|
|
|
Invocation.setModuleName("lldbtest");
|
|
|
|
|
|
|
|
if (CI.setup(Invocation))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Fetch the serialized module bitstreams from the Mach-O files and
|
|
|
|
// register them with the module loader.
|
|
|
|
for (std::string name : InputNames) {
|
|
|
|
// We assume Macho-O 64 bit.
|
|
|
|
std::ifstream macho(name);
|
2013-09-03 22:09:53 +04:00
|
|
|
if (!macho.good()) {
|
|
|
|
llvm::outs() << "Cannot read from " << name << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-08-29 04:57:05 +04:00
|
|
|
struct mach_header_64 h;
|
|
|
|
macho.read((char*)&h, sizeof(h));
|
|
|
|
assert(h.magic == MH_MAGIC_64);
|
|
|
|
// Load command.
|
|
|
|
for (uint32_t i = 0; i < h.ncmds; ++i) {
|
|
|
|
struct load_command lc;
|
|
|
|
macho.read((char*)&lc, sizeof(lc));
|
|
|
|
// Segment command.
|
|
|
|
if (lc.cmd == LC_SEGMENT_64) {
|
|
|
|
macho.seekg(-sizeof(lc), macho.cur);
|
|
|
|
struct segment_command_64 sc;
|
|
|
|
macho.read((char*)&sc, sizeof(sc));
|
|
|
|
// Sections.
|
|
|
|
for (uint32_t j = 0; j < sc.nsects; ++j) {
|
|
|
|
struct section_64 section;
|
|
|
|
macho.read((char*)§ion, sizeof(section));
|
|
|
|
auto sectname = "__apple_ast";
|
|
|
|
if (strncmp(section.sectname, sectname, strlen(sectname)) == 0) {
|
2013-09-03 22:09:53 +04:00
|
|
|
macho.seekg(section.offset, macho.beg);
|
|
|
|
assert(macho.good());
|
|
|
|
|
|
|
|
// Pass the __apple_AST section to the module loader.
|
|
|
|
auto data = llvm::MemoryBuffer::getNewMemBuffer(section.size, name);
|
|
|
|
macho.read(const_cast<char *>(data->getBufferStart()), section.size);
|
|
|
|
if (!CI.getSerializedModuleLoader()->addASTSection
|
|
|
|
(std::unique_ptr<llvm::MemoryBuffer>(data), modules))
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
for (auto path : modules)
|
|
|
|
llvm::outs() << "Loaded module " << path << " from " << name
|
|
|
|
<< "\n";
|
2013-08-29 04:57:05 +04:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 22:46:56 +04:00
|
|
|
} else
|
|
|
|
macho.seekg(lc.cmdsize-sizeof(lc), macho.cur);
|
2013-08-29 04:57:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to import all modules we found.
|
|
|
|
for (auto path : modules) {
|
2013-08-29 22:59:14 +04:00
|
|
|
llvm::outs() << "Importing " << path << "... ";
|
2013-08-29 04:57:05 +04:00
|
|
|
|
|
|
|
#ifdef SWIFT_SUPPORTS_SUBMODULES
|
|
|
|
std::vector<std::pair<swift::Identifier, swift::SourceLoc> > AccessPath;
|
|
|
|
for (auto i = llvm::sys::path::begin(path);
|
2013-08-29 22:46:56 +04:00
|
|
|
i != llvm::sys::path::end(path); ++i)
|
2013-08-29 04:57:05 +04:00
|
|
|
if (!llvm::sys::path::is_separator((*i)[0]))
|
|
|
|
AccessPath.push_back({ CI.getASTContext().getIdentifier(*i),
|
|
|
|
swift::SourceLoc() });
|
|
|
|
#else
|
|
|
|
std::vector<std::pair<swift::Identifier, swift::SourceLoc> > AccessPath;
|
|
|
|
AccessPath.push_back({ CI.getASTContext().getIdentifier(path),
|
|
|
|
swift::SourceLoc() });
|
|
|
|
#endif
|
|
|
|
|
2013-09-03 22:09:53 +04:00
|
|
|
auto Module = CI.getASTContext().getModule(AccessPath);
|
2013-08-29 04:57:05 +04:00
|
|
|
if (!Module) {
|
2013-08-29 22:59:14 +04:00
|
|
|
llvm::errs() << "FAIL!\n";
|
2013-08-29 04:57:05 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2013-08-29 22:59:14 +04:00
|
|
|
llvm::outs() << "ok!\n";
|
2013-08-29 04:57:05 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|