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
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"
30
31namespace llvm {
32namespace objcopy {
33
34using namespace llvm::object;
35
36/// The function executeObjcopyOnBinary does the dispatch based on the format
37/// of the input binary (ELF, MachO or COFF).
38Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
39 object::Binary &In, raw_ostream &Out) {
40 if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(Val: &In)) {
41 Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
42 if (!ELFConfig)
43 return ELFConfig.takeError();
44
45 return elf::executeObjcopyOnBinary(Config: Config.getCommonConfig(), ELFConfig: *ELFConfig,
46 In&: *ELFBinary, Out);
47 }
48 if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(Val: &In)) {
49 Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
50 if (!COFFConfig)
51 return COFFConfig.takeError();
52
53 return coff::executeObjcopyOnBinary(Config: Config.getCommonConfig(), *COFFConfig,
54 In&: *COFFBinary, Out);
55 }
56 if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(Val: &In)) {
57 Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
58 if (!MachOConfig)
59 return MachOConfig.takeError();
60
61 return macho::executeObjcopyOnBinary(Config: Config.getCommonConfig(), MachOConfig: *MachOConfig,
62 In&: *MachOBinary, Out);
63 }
64 if (auto *MachOUniversalBinary =
65 dyn_cast<object::MachOUniversalBinary>(Val: &In)) {
66 return macho::executeObjcopyOnMachOUniversalBinary(
67 Config, In: *MachOUniversalBinary, Out);
68 }
69 if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(Val: &In)) {
70 Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
71 if (!WasmConfig)
72 return WasmConfig.takeError();
73
74 return objcopy::wasm::executeObjcopyOnBinary(Config: Config.getCommonConfig(),
75 *WasmConfig, In&: *WasmBinary, Out);
76 }
77 if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(Val: &In)) {
78 Expected<const XCOFFConfig &> XCOFFConfig = Config.getXCOFFConfig();
79 if (!XCOFFConfig)
80 return XCOFFConfig.takeError();
81
82 return xcoff::executeObjcopyOnBinary(Config: Config.getCommonConfig(), *XCOFFConfig,
83 In&: *XCOFFBinary, Out);
84 }
85 return createStringError(EC: object_error::invalid_file_type,
86 S: "unsupported object file format");
87}
88
89} // end namespace objcopy
90} // end namespace llvm
91