|
| 1 | +//===-------- clang-offload-extract/ClangOffloadExtract.cpp ---------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// \file |
| 10 | +/// Implementation of the clang-offload-extract tool which allows extracting |
| 11 | +/// target images from linked fat offload binaries. For locating target images |
| 12 | +/// in the binary it uses information from the .tgtimg section which is added to |
| 13 | +/// the image by the clang-offload-wrapper tool. This section contains <address, |
| 14 | +/// size> pairs for all embedded target images. |
| 15 | +/// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#include "clang/Basic/Version.h" |
| 19 | +#include "llvm/ADT/ArrayRef.h" |
| 20 | +#include "llvm/Object/COFF.h" |
| 21 | +#include "llvm/Object/ELFObjectFile.h" |
| 22 | +#include "llvm/Object/ObjectFile.h" |
| 23 | +#include "llvm/Support/CommandLine.h" |
| 24 | +#include "llvm/Support/Errc.h" |
| 25 | +#include "llvm/Support/Signals.h" |
| 26 | +#include "llvm/Support/WithColor.h" |
| 27 | +#include "llvm/Support/raw_ostream.h" |
| 28 | + |
| 29 | +#define IMAGE_INFO_SECTION_NAME ".tgtimg" |
| 30 | + |
| 31 | +using namespace llvm; |
| 32 | +using namespace llvm::object; |
| 33 | + |
| 34 | +static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); |
| 35 | + |
| 36 | +// Mark all our options with this category, everything else (except for -version |
| 37 | +// and -help) will be hidden. |
| 38 | +static cl::OptionCategory |
| 39 | + ClangOffloadExtractCategory("clang-offload-extract options"); |
| 40 | + |
| 41 | +static cl::opt<std::string> OutputPrefix( |
| 42 | + "output", cl::Required, |
| 43 | + cl::desc("Specifies prefix for the output file(s). Output file name\n" |
| 44 | + "is composed from this prefix and the sequential number\n" |
| 45 | + "of extracted image appended to the prefix."), |
| 46 | + cl::cat(ClangOffloadExtractCategory)); |
| 47 | + |
| 48 | +static cl::opt<std::string> Input(cl::Positional, cl::Required, |
| 49 | + cl::desc("<input file>"), |
| 50 | + cl::cat(ClangOffloadExtractCategory)); |
| 51 | + |
| 52 | +/// Path to the current binary. |
| 53 | +static std::string ToolPath; |
| 54 | + |
| 55 | +static void reportError(Error E) { |
| 56 | + logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolPath)); |
| 57 | +} |
| 58 | + |
| 59 | +int main(int argc, const char **argv) { |
| 60 | + sys::PrintStackTraceOnErrorSignal(argv[0]); |
| 61 | + ToolPath = argv[0]; |
| 62 | + |
| 63 | + cl::HideUnrelatedOptions(ClangOffloadExtractCategory); |
| 64 | + cl::SetVersionPrinter([](raw_ostream &OS) { |
| 65 | + OS << clang::getClangToolFullVersion("clang-offload-extract") << '\n'; |
| 66 | + }); |
| 67 | + cl::ParseCommandLineOptions(argc, argv, |
| 68 | + "A tool for extracting target images from the " |
| 69 | + "linked fat offload binary."); |
| 70 | + |
| 71 | + if (Help) { |
| 72 | + cl::PrintHelpMessage(); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + // Read input file. It should have one of the supported object file formats. |
| 77 | + Expected<OwningBinary<ObjectFile>> ObjectOrErr = |
| 78 | + ObjectFile::createObjectFile(Input); |
| 79 | + if (!ObjectOrErr) { |
| 80 | + reportError(ObjectOrErr.takeError()); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + ObjectFile *Binary = ObjectOrErr->getBinary(); |
| 85 | + |
| 86 | + // Do we plan to support 32-bit offload binaries? |
| 87 | + if (!(isa<ELF64LEObjectFile>(Binary) || isa<COFFObjectFile>(Binary)) || |
| 88 | + Binary->getBytesInAddress() != sizeof(void *)) { |
| 89 | + reportError( |
| 90 | + createStringError(errc::invalid_argument, |
| 91 | + "only 64-bit ELF or COFF inputs are supported")); |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
| 95 | + unsigned FileNum = 0; |
| 96 | + |
| 97 | + for (SectionRef Section : Binary->sections()) { |
| 98 | + // Look for the .tgtimg section in the binary. |
| 99 | + Expected<StringRef> NameOrErr = Section.getName(); |
| 100 | + if (!NameOrErr) { |
| 101 | + reportError(NameOrErr.takeError()); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + if (*NameOrErr != IMAGE_INFO_SECTION_NAME) |
| 105 | + continue; |
| 106 | + |
| 107 | + // This is the section we are looking for. |
| 108 | + Expected<StringRef> DataOrErr = Section.getContents(); |
| 109 | + if (!DataOrErr) { |
| 110 | + reportError(DataOrErr.takeError()); |
| 111 | + return 1; |
| 112 | + } |
| 113 | + |
| 114 | + // This section contains concatenated <address, size> pairs describing |
| 115 | + // target images that are stored in the binary. Loop over these descriptors |
| 116 | + // and extract each target image. |
| 117 | + struct ImgInfoTy { |
| 118 | + uintptr_t Addr; |
| 119 | + uintptr_t Size; |
| 120 | + }; |
| 121 | + |
| 122 | + auto ImgInfo = makeArrayRef<ImgInfoTy>( |
| 123 | + reinterpret_cast<const ImgInfoTy *>(DataOrErr->data()), |
| 124 | + DataOrErr->size() / sizeof(ImgInfoTy)); |
| 125 | + |
| 126 | + for (auto &Img : ImgInfo) { |
| 127 | + // Find section which contains this image. |
| 128 | + // TODO: can use more efficient algorithm than linear search. For example |
| 129 | + // sections and images could be sorted by address then one pass performed |
| 130 | + // through both at the same time. |
| 131 | + auto ImgSec = find_if(Binary->sections(), [&Img](SectionRef Sec) { |
| 132 | + if (!Sec.isData()) |
| 133 | + return false; |
| 134 | + if (Img.Addr < Sec.getAddress() || |
| 135 | + Img.Addr + Img.Size > Sec.getAddress() + Sec.getSize()) |
| 136 | + return false; |
| 137 | + return true; |
| 138 | + }); |
| 139 | + if (ImgSec == Binary->section_end()) { |
| 140 | + reportError(createStringError( |
| 141 | + inconvertibleErrorCode(), |
| 142 | + "cannot find section containing <0x%lx, 0x%lx> target image", |
| 143 | + Img.Addr, Img.Size)); |
| 144 | + return 1; |
| 145 | + } |
| 146 | + |
| 147 | + Expected<StringRef> SecDataOrErr = ImgSec->getContents(); |
| 148 | + if (!SecDataOrErr) { |
| 149 | + reportError(SecDataOrErr.takeError()); |
| 150 | + return 1; |
| 151 | + } |
| 152 | + |
| 153 | + // Output file name is composed from the name prefix provided by the user |
| 154 | + // and the image number which is appended to the prefix. |
| 155 | + std::string FileName = OutputPrefix + "." + std::to_string(FileNum++); |
| 156 | + |
| 157 | + // Tell user that we are saving an image. |
| 158 | + outs() << "Saving target image to \"" << FileName << "\"\n"; |
| 159 | + |
| 160 | + // And write image data to the output. |
| 161 | + std::error_code EC; |
| 162 | + raw_fd_ostream OS(FileName, EC); |
| 163 | + if (EC) { |
| 164 | + reportError(createFileError(FileName, EC)); |
| 165 | + return 1; |
| 166 | + } |
| 167 | + |
| 168 | + OS << SecDataOrErr->substr(Img.Addr - ImgSec->getAddress(), Img.Size); |
| 169 | + if (OS.has_error()) { |
| 170 | + reportError(createFileError(FileName, OS.error())); |
| 171 | + return 1; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Binary is not expected to have more than one .tgtimg section. |
| 176 | + break; |
| 177 | + } |
| 178 | + return 0; |
| 179 | +} |
0 commit comments