blob: d9a190d1a2ad182aa0b0b0ea05d9333d9c8dc2e6 [file] [log] [blame]
Alexey Lapshinf75da0c2022-02-11 21:42:40 +03001//===- 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"
Alexey Lapshinf75da0c2022-02-11 21:42:40 +030012#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"
esmeyi61835d12022-02-28 04:59:46 -050017#include "llvm/ObjCopy/XCOFF/XCOFFConfig.h"
18#include "llvm/ObjCopy/XCOFF/XCOFFObjcopy.h"
Kazu Hirata54dad9e2024-11-17 08:39:13 -080019#include "llvm/ObjCopy/wasm/WasmConfig.h"
20#include "llvm/ObjCopy/wasm/WasmObjcopy.h"
Alexey Lapshinf75da0c2022-02-11 21:42:40 +030021#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"
esmeyi61835d12022-02-28 04:59:46 -050027#include "llvm/Object/XCOFFObjectFile.h"
Alexey Lapshinf75da0c2022-02-11 21:42:40 +030028
Fangrui Song80c61de2025-05-11 22:51:18 -070029using namespace llvm;
Alexey Lapshinf75da0c2022-02-11 21:42:40 +030030using namespace llvm::object;
31
32/// The function executeObjcopyOnBinary does the dispatch based on the format
33/// of the input binary (ELF, MachO or COFF).
Fangrui Song80c61de2025-05-11 22:51:18 -070034Error objcopy::executeObjcopyOnBinary(const MultiFormatConfig &Config,
35 object::Binary &In, raw_ostream &Out) {
Alexey Lapshinf75da0c2022-02-11 21:42:40 +030036 if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
37 Expected<const ELFConfig &> ELFConfig = Config.getELFConfig();
38 if (!ELFConfig)
39 return ELFConfig.takeError();
40
41 return elf::executeObjcopyOnBinary(Config.getCommonConfig(), *ELFConfig,
42 *ELFBinary, Out);
43 }
44 if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) {
45 Expected<const COFFConfig &> COFFConfig = Config.getCOFFConfig();
46 if (!COFFConfig)
47 return COFFConfig.takeError();
48
49 return coff::executeObjcopyOnBinary(Config.getCommonConfig(), *COFFConfig,
50 *COFFBinary, Out);
51 }
52 if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) {
53 Expected<const MachOConfig &> MachOConfig = Config.getMachOConfig();
54 if (!MachOConfig)
55 return MachOConfig.takeError();
56
57 return macho::executeObjcopyOnBinary(Config.getCommonConfig(), *MachOConfig,
58 *MachOBinary, Out);
59 }
60 if (auto *MachOUniversalBinary =
61 dyn_cast<object::MachOUniversalBinary>(&In)) {
62 return macho::executeObjcopyOnMachOUniversalBinary(
63 Config, *MachOUniversalBinary, Out);
64 }
65 if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In)) {
66 Expected<const WasmConfig &> WasmConfig = Config.getWasmConfig();
67 if (!WasmConfig)
68 return WasmConfig.takeError();
69
70 return objcopy::wasm::executeObjcopyOnBinary(Config.getCommonConfig(),
71 *WasmConfig, *WasmBinary, Out);
72 }
esmeyi61835d12022-02-28 04:59:46 -050073 if (auto *XCOFFBinary = dyn_cast<object::XCOFFObjectFile>(&In)) {
74 Expected<const XCOFFConfig &> XCOFFConfig = Config.getXCOFFConfig();
75 if (!XCOFFConfig)
76 return XCOFFConfig.takeError();
77
78 return xcoff::executeObjcopyOnBinary(Config.getCommonConfig(), *XCOFFConfig,
79 *XCOFFBinary, Out);
80 }
Alexey Lapshinf75da0c2022-02-11 21:42:40 +030081 return createStringError(object_error::invalid_file_type,
82 "unsupported object file format");
83}