12f09f445SMaksim Panchenko //===- bolt/RuntimeLibs/RuntimeLibrary.cpp - Runtime Library --------------===//
2a34c753fSRafael Auler //
3a34c753fSRafael Auler // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a34c753fSRafael Auler // See https://llvm.org/LICENSE.txt for license information.
5a34c753fSRafael Auler // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a34c753fSRafael Auler //
7a34c753fSRafael Auler //===----------------------------------------------------------------------===//
8a34c753fSRafael Auler //
92f09f445SMaksim Panchenko // This file implements the RuntimeLibrary class.
102f09f445SMaksim Panchenko //
11a34c753fSRafael Auler //===----------------------------------------------------------------------===//
12a34c753fSRafael Auler
13a34c753fSRafael Auler #include "bolt/RuntimeLibs/RuntimeLibrary.h"
14a34c753fSRafael Auler #include "bolt/Utils/Utils.h"
15a34c753fSRafael Auler #include "llvm/BinaryFormat/Magic.h"
16a34c753fSRafael Auler #include "llvm/ExecutionEngine/RuntimeDyld.h"
17a34c753fSRafael Auler #include "llvm/Object/Archive.h"
18a34c753fSRafael Auler #include "llvm/Support/Path.h"
19a34c753fSRafael Auler
20a34c753fSRafael Auler #define DEBUG_TYPE "bolt-rtlib"
21a34c753fSRafael Auler
22a34c753fSRafael Auler using namespace llvm;
23a34c753fSRafael Auler using namespace bolt;
24a34c753fSRafael Auler
anchor()25a34c753fSRafael Auler void RuntimeLibrary::anchor() {}
26a34c753fSRafael Auler
getLibPath(StringRef ToolPath,StringRef LibFileName)27a34c753fSRafael Auler std::string RuntimeLibrary::getLibPath(StringRef ToolPath,
28a34c753fSRafael Auler StringRef LibFileName) {
29a34c753fSRafael Auler StringRef Dir = llvm::sys::path::parent_path(ToolPath);
30a34c753fSRafael Auler SmallString<128> LibPath = llvm::sys::path::parent_path(Dir);
31a34c753fSRafael Auler llvm::sys::path::append(LibPath, "lib");
32a34c753fSRafael Auler if (!llvm::sys::fs::exists(LibPath)) {
33a34c753fSRafael Auler // In some cases we install bolt binary into one level deeper in bin/,
34a34c753fSRafael Auler // we need to go back one more level to find lib directory.
3540c2e0faSMaksim Panchenko LibPath = llvm::sys::path::parent_path(llvm::sys::path::parent_path(Dir));
36a34c753fSRafael Auler llvm::sys::path::append(LibPath, "lib");
37a34c753fSRafael Auler }
38a34c753fSRafael Auler llvm::sys::path::append(LibPath, LibFileName);
39a34c753fSRafael Auler if (!llvm::sys::fs::exists(LibPath)) {
40a34c753fSRafael Auler errs() << "BOLT-ERROR: library not found: " << LibPath << "\n";
41a34c753fSRafael Auler exit(1);
42a34c753fSRafael Auler }
43a34c753fSRafael Auler return std::string(LibPath.str());
44a34c753fSRafael Auler }
45a34c753fSRafael Auler
loadLibrary(StringRef LibPath,RuntimeDyld & RTDyld)46a34c753fSRafael Auler void RuntimeLibrary::loadLibrary(StringRef LibPath, RuntimeDyld &RTDyld) {
47a34c753fSRafael Auler ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf =
48b392ec69SRafael Auler MemoryBuffer::getFile(LibPath, false, false);
49a34c753fSRafael Auler check_error(MaybeBuf.getError(), LibPath);
50a34c753fSRafael Auler std::unique_ptr<MemoryBuffer> B = std::move(MaybeBuf.get());
51a34c753fSRafael Auler file_magic Magic = identify_magic(B->getBuffer());
52a34c753fSRafael Auler
53a34c753fSRafael Auler if (Magic == file_magic::archive) {
54a34c753fSRafael Auler Error Err = Error::success();
55a34c753fSRafael Auler object::Archive Archive(B.get()->getMemBufferRef(), Err);
56a34c753fSRafael Auler for (const object::Archive::Child &C : Archive.children(Err)) {
57a34c753fSRafael Auler std::unique_ptr<object::Binary> Bin = cantFail(C.getAsBinary());
58*883bf0e8SAmir Ayupov if (object::ObjectFile *Obj = dyn_cast<object::ObjectFile>(&*Bin))
59a34c753fSRafael Auler RTDyld.loadObject(*Obj);
60a34c753fSRafael Auler }
61a34c753fSRafael Auler check_error(std::move(Err), B->getBufferIdentifier());
62a34c753fSRafael Auler } else if (Magic == file_magic::elf_relocatable ||
63a34c753fSRafael Auler Magic == file_magic::elf_shared_object) {
64a34c753fSRafael Auler std::unique_ptr<object::ObjectFile> Obj = cantFail(
65a34c753fSRafael Auler object::ObjectFile::createObjectFile(B.get()->getMemBufferRef()),
66a34c753fSRafael Auler "error creating in-memory object");
67a34c753fSRafael Auler RTDyld.loadObject(*Obj);
68a34c753fSRafael Auler } else {
69a34c753fSRafael Auler errs() << "BOLT-ERROR: unrecognized library format: " << LibPath << "\n";
70a34c753fSRafael Auler exit(1);
71a34c753fSRafael Auler }
72a34c753fSRafael Auler }
73