LLVM 23.0.0git
ObjCopy.cpp
Go to the documentation of this file.
1//===- Objcopy.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
23#include "llvm/Object/COFF.h"
26#include "llvm/Object/Error.h"
27#include "llvm/Object/MachO.h"
29#include "llvm/Object/Wasm.h"
31
32using namespace llvm;
33using namespace llvm::object;
34
36 if (const auto *OF = dyn_cast<ObjectFile>(&B))
37 return OF->getFileFormatName();
38 return {};
39}
40
42 StringRef OutPath, StringRef OutFormatName) {
43 if (OutFormatName.empty())
44 OutFormatName = InFormatName;
45 outs() << "copy from '" << InPath << "' [" << InFormatName << "] to '"
46 << OutPath << "' [" << OutFormatName << "]\n";
47}
48
49/// The function executeObjcopyOnBinary does the dispatch based on the format
50/// of the input binary (ELF, MachO or COFF).
52 object::Binary &In, raw_ostream &Out) {
53 if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
55 if (!ELFConfig)
56 return ELFConfig.takeError();
57
59 *ELFBinary, Out);
60 }
61 if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
63 if (!COFFConfig)
64 return COFFConfig.takeError();
65
67 *COFFBinary, Out);
68 }
69 if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
71 if (!MachOConfig)
72 return MachOConfig.takeError();
73
75 *MachOBinary, Out);
76 }
77 if (auto *MachOUniversalBinary =
80 Config, *MachOUniversalBinary, Out);
81 }
82 if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
84 if (!WasmConfig)
85 return WasmConfig.takeError();
86
88 *WasmConfig, *WasmBinary, Out);
89 }
90 if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(&In)) {
92 if (!XCOFFConfig)
93 return XCOFFConfig.takeError();
94
96 *XCOFFBinary, Out);
97 }
98 if (auto *DXContainerBinary = dyn_cast<object::DXContainerObjectFile>(&In)) {
100 Config.getDXContainerConfig();
102 return DXContainerConfig.takeError();
103
105 Config.getCommonConfig(), *DXContainerConfig, *DXContainerBinary, Out);
106 }
108 "unsupported object file format");
109}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
virtual Expected< const XCOFFConfig & > getXCOFFConfig() const =0
virtual Expected< const WasmConfig & > getWasmConfig() const =0
virtual Expected< const ELFConfig & > getELFConfig() const =0
virtual Expected< const COFFConfig & > getCOFFConfig() const =0
virtual Expected< const MachOConfig & > getMachOConfig() const =0
virtual const CommonConfig & getCommonConfig() const =0
virtual Expected< const DXContainerConfig & > getDXContainerConfig() const =0
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI Error executeObjcopyOnBinary(const CommonConfig &Config, const COFFConfig &, object::COFFObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and COFFConfig to In and writes the result into Out.
Error executeObjcopyOnBinary(const CommonConfig &Config, const DXContainerConfig &, object::DXContainerObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and DXContainerConfig to In and writes the result into ...
LLVM_ABI Error executeObjcopyOnBinary(const CommonConfig &Config, const ELFConfig &ELFConfig, object::ELFObjectFileBase &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In and writes the result into Out.
LLVM_ABI Error executeObjcopyOnBinary(const CommonConfig &Config, const MachOConfig &MachOConfig, object::MachOObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and MachOConfig to In and writes the result into Out.
LLVM_ABI Error executeObjcopyOnMachOUniversalBinary(const MultiFormatConfig &Config, const object::MachOUniversalBinary &In, raw_ostream &Out)
Apply the transformations described by Config and MachOConfig to In and writes the result into Out.
LLVM_ABI Error executeObjcopyOnBinary(const CommonConfig &Config, const WasmConfig &, object::WasmObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and WasmConfig to In and writes the result into Out.
Error executeObjcopyOnBinary(const CommonConfig &Config, const XCOFFConfig &, object::XCOFFObjectFile &In, raw_ostream &Out)
Apply the transformations described by Config and XCOFFConfig to In and writes the result into Out.
LLVM_ABI StringRef getObjectFormatName(const object::Binary &B)
Returns the format name of B if it is an ObjectFile, or "" otherwise.
Definition ObjCopy.cpp:35
LLVM_ABI void printCopyMessage(StringRef InPath, StringRef InFormatName, StringRef OutPath, StringRef OutFormatName)
Prints information about the input and output files involved in a copy operation to stdout.
Definition ObjCopy.cpp:41
LLVM_ABI Error executeObjcopyOnBinary(const MultiFormatConfig &Config, object::Binary &In, raw_ostream &Out)
Applies the transformations described by Config to In and writes the result into Out.
Definition ObjCopy.cpp:51
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321