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/ELF/ELFConfig.h"
13 #include "llvm/ObjCopy/ELF/ELFObjcopy.h"
14 #include "llvm/ObjCopy/MachO/MachOConfig.h"
15 #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
16 #include "llvm/ObjCopy/MultiFormatConfig.h"
17 #include "llvm/ObjCopy/XCOFF/XCOFFConfig.h"
18 #include "llvm/ObjCopy/XCOFF/XCOFFObjcopy.h"
19 #include "llvm/ObjCopy/wasm/WasmConfig.h"
20 #include "llvm/ObjCopy/wasm/WasmObjcopy.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/Object/ELFObjectFile.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Object/MachO.h"
25 #include "llvm/Object/MachOUniversal.h"
26 #include "llvm/Object/Wasm.h"
27 #include "llvm/Object/XCOFFObjectFile.h"
32 using namespace llvm::object
;
34 /// The function executeObjcopyOnBinary does the dispatch based on the format
35 /// of the input binary (ELF, MachO or COFF).
36 Error
executeObjcopyOnBinary(const MultiFormatConfig
&Config
,
37 object::Binary
&In
, raw_ostream
&Out
) {
38 if (auto *ELFBinary
= dyn_cast
<object::ELFObjectFileBase
>(&In
)) {
39 Expected
<const ELFConfig
&> ELFConfig
= Config
.getELFConfig();
41 return ELFConfig
.takeError();
43 return elf::executeObjcopyOnBinary(Config
.getCommonConfig(), *ELFConfig
,
46 if (auto *COFFBinary
= dyn_cast
<object::COFFObjectFile
>(&In
)) {
47 Expected
<const COFFConfig
&> COFFConfig
= Config
.getCOFFConfig();
49 return COFFConfig
.takeError();
51 return coff::executeObjcopyOnBinary(Config
.getCommonConfig(), *COFFConfig
,
54 if (auto *MachOBinary
= dyn_cast
<object::MachOObjectFile
>(&In
)) {
55 Expected
<const MachOConfig
&> MachOConfig
= Config
.getMachOConfig();
57 return MachOConfig
.takeError();
59 return macho::executeObjcopyOnBinary(Config
.getCommonConfig(), *MachOConfig
,
62 if (auto *MachOUniversalBinary
=
63 dyn_cast
<object::MachOUniversalBinary
>(&In
)) {
64 return macho::executeObjcopyOnMachOUniversalBinary(
65 Config
, *MachOUniversalBinary
, Out
);
67 if (auto *WasmBinary
= dyn_cast
<object::WasmObjectFile
>(&In
)) {
68 Expected
<const WasmConfig
&> WasmConfig
= Config
.getWasmConfig();
70 return WasmConfig
.takeError();
72 return objcopy::wasm::executeObjcopyOnBinary(Config
.getCommonConfig(),
73 *WasmConfig
, *WasmBinary
, Out
);
75 if (auto *XCOFFBinary
= dyn_cast
<object::XCOFFObjectFile
>(&In
)) {
76 Expected
<const XCOFFConfig
&> XCOFFConfig
= Config
.getXCOFFConfig();
78 return XCOFFConfig
.takeError();
80 return xcoff::executeObjcopyOnBinary(Config
.getCommonConfig(), *XCOFFConfig
,
83 return createStringError(object_error::invalid_file_type
,
84 "unsupported object file format");
87 } // end namespace objcopy
88 } // end namespace llvm