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/Object/COFF.h"
21 #include "llvm/Object/ELFObjectFile.h"
22 #include "llvm/Object/Error.h"
23 #include "llvm/Object/MachO.h"
24 #include "llvm/Object/MachOUniversal.h"
25 #include "llvm/Object/Wasm.h"
26 #include "llvm/Support/SmallVectorMemoryBuffer.h"
27 
28 namespace llvm {
29 namespace objcopy {
30 
31 using namespace llvm::object;
32 
33 /// The function executeObjcopyOnBinary does the dispatch based on the format
34 /// of the input binary (ELF, MachO or COFF).
35 Error executeObjcopyOnBinary(const MultiFormatConfig &Config,
36                              object::Binary &In, raw_ostream &Out) {
37   if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
38     Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
39     if (!ELFConfig)
40       return ELFConfig.takeError();
41 
42     return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
43                                        *ELFBinary, Out);
44   }
45   if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
46     Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
47     if (!COFFConfig)
48       return COFFConfig.takeError();
49 
50     return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
51                                         *COFFBinary, Out);
52   }
53   if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
54     Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
55     if (!MachOConfig)
56       return MachOConfig.takeError();
57 
58     return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
59                                          *MachOBinary, Out);
60   }
61   if (auto *MachOUniversalBinary =
62           dyn_cast<object::MachOUniversalBinary>(&In)) {
63     return macho::executeObjcopyOnMachOUniversalBinary(
64         Config, *MachOUniversalBinary, Out);
65   }
66   if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
67     Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
68     if (!WasmConfig)
69       return WasmConfig.takeError();
70 
71     return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
72                                                  *WasmConfig, *WasmBinary, Out);
73   }
74   return createStringError(object_error::invalid_file_type,
75                            "unsupported object file format");
76 }
77 
78 } // end namespace objcopy
79 } // end namespace llvm
80