1 //===- Objcopy.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ObjCopy/ObjCopy.h"
10 #include "llvm/ObjCopy/COFF/COFFConfig.h"
11 #include "llvm/ObjCopy/COFF/COFFObjcopy.h"
12 #include "llvm/ObjCopy/CommonConfig.h"
13 #include "llvm/ObjCopy/ELF/ELFConfig.h"
14 #include "llvm/ObjCopy/ELF/ELFObjcopy.h"
15 #include "llvm/ObjCopy/MachO/MachOConfig.h"
16 #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
17 #include "llvm/ObjCopy/MultiFormatConfig.h"
18 #include "llvm/ObjCopy/wasm/WasmConfig.h"
19 #include "llvm/ObjCopy/wasm/WasmObjcopy.h"
20 #include "llvm/ObjCopy/XCOFF/XCOFFConfig.h"
21 #include "llvm/ObjCopy/XCOFF/XCOFFObjcopy.h"
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Object/ELFObjectFile.h"
24 #include "llvm/Object/Error.h"
25 #include "llvm/Object/MachO.h"
26 #include "llvm/Object/MachOUniversal.h"
27 #include "llvm/Object/Wasm.h"
28 #include "llvm/Object/XCOFFObjectFile.h"
29 #include "llvm/Support/SmallVectorMemoryBuffer.h"
34 using namespace llvm::object
;
36 /// The function executeObjcopyOnBinary does the dispatch based on the format
37 /// of the input binary (ELF, MachO or COFF).
38 Error
executeObjcopyOnBinary(const MultiFormatConfig
&Config
,
39 object::Binary
&In
, raw_ostream
&Out
) {
40 if (auto *ELFBinary
= dyn_cast
<object::ELFObjectFileBase
>(&In
)) {
41 Expected
<const ELFConfig
&> ELFConfig
= Config
.getELFConfig();
43 return ELFConfig
.takeError();
45 return elf::executeObjcopyOnBinary(Config
.getCommonConfig(), *ELFConfig
,
48 if (auto *COFFBinary
= dyn_cast
<object::COFFObjectFile
>(&In
)) {
49 Expected
<const COFFConfig
&> COFFConfig
= Config
.getCOFFConfig();
51 return COFFConfig
.takeError();
53 return coff::executeObjcopyOnBinary(Config
.getCommonConfig(), *COFFConfig
,
56 if (auto *MachOBinary
= dyn_cast
<object::MachOObjectFile
>(&In
)) {
57 Expected
<const MachOConfig
&> MachOConfig
= Config
.getMachOConfig();
59 return MachOConfig
.takeError();
61 return macho::executeObjcopyOnBinary(Config
.getCommonConfig(), *MachOConfig
,
64 if (auto *MachOUniversalBinary
=
65 dyn_cast
<object::MachOUniversalBinary
>(&In
)) {
66 return macho::executeObjcopyOnMachOUniversalBinary(
67 Config
, *MachOUniversalBinary
, Out
);
69 if (auto *WasmBinary
= dyn_cast
<object::WasmObjectFile
>(&In
)) {
70 Expected
<const WasmConfig
&> WasmConfig
= Config
.getWasmConfig();
72 return WasmConfig
.takeError();
74 return objcopy::wasm::executeObjcopyOnBinary(Config
.getCommonConfig(),
75 *WasmConfig
, *WasmBinary
, Out
);
77 if (auto *XCOFFBinary
= dyn_cast
<object::XCOFFObjectFile
>(&In
)) {
78 Expected
<const XCOFFConfig
&> XCOFFConfig
= Config
.getXCOFFConfig();
80 return XCOFFConfig
.takeError();
82 return xcoff::executeObjcopyOnBinary(Config
.getCommonConfig(), *XCOFFConfig
,
85 return createStringError(object_error::invalid_file_type
,
86 "unsupported object file format");
89 } // end namespace objcopy
90 } // end namespace llvm