xref: /freebsd/contrib/llvm-project/llvm/lib/ObjCopy/ObjCopy.cpp (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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 
31 namespace llvm {
32 namespace objcopy {
33 
34 using namespace llvm::object;
35 
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();
42     if (!ELFConfig)
43       return ELFConfig.takeError();
44 
45     return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
46                                        *ELFBinary, Out);
47   }
48   if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
49     Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
50     if (!COFFConfig)
51       return COFFConfig.takeError();
52 
53     return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
54                                         *COFFBinary, Out);
55   }
56   if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
57     Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
58     if (!MachOConfig)
59       return MachOConfig.takeError();
60 
61     return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
62                                          *MachOBinary, Out);
63   }
64   if (auto *MachOUniversalBinary =
65           dyn_cast<object::MachOUniversalBinary>(&In)) {
66     return macho::executeObjcopyOnMachOUniversalBinary(
67         Config, *MachOUniversalBinary, Out);
68   }
69   if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
70     Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
71     if (!WasmConfig)
72       return WasmConfig.takeError();
73 
74     return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
75                                                  *WasmConfig, *WasmBinary, Out);
76   }
77   if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(&In)) {
78     Expected<const XCOFFConfig &> XCOFFConfig = Config.getXCOFFConfig();
79     if (!XCOFFConfig)
80       return XCOFFConfig.takeError();
81 
82     return xcoff::executeObjcopyOnBinary(Config.getCommonConfig(), *XCOFFConfig,
83                                          *XCOFFBinary, Out);
84   }
85   return createStringError(object_error::invalid_file_type,
86                            "unsupported object file format");
87 }
88 
89 } // end namespace objcopy
90 } // end namespace llvm
91