Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1 | //===- llvm-objcopy.cpp ---------------------------------------------------===// |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 8 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 9 | #include "llvm-objcopy.h" |
Alexander Shaposhnikov | 3d4c4ac | 2018-10-16 05:40:18 +0000 | [diff] [blame] | 10 | #include "Buffer.h" |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 11 | #include "COFF/COFFObjcopy.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 12 | #include "CopyConfig.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 13 | #include "ELF/ELFObjcopy.h" |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 14 | #include "MachO/MachOObjcopy.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 15 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Jordan Rupprecht | d67c1e1 | 2018-08-01 16:23:22 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 20 | #include "llvm/Object/Archive.h" |
| 21 | #include "llvm/Object/ArchiveWriter.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 22 | #include "llvm/Object/Binary.h" |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 23 | #include "llvm/Object/COFF.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 24 | #include "llvm/Object/ELFObjectFile.h" |
| 25 | #include "llvm/Object/ELFTypes.h" |
| 26 | #include "llvm/Object/Error.h" |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 27 | #include "llvm/Object/MachO.h" |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 28 | #include "llvm/Option/Arg.h" |
| 29 | #include "llvm/Option/ArgList.h" |
| 30 | #include "llvm/Option/Option.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Casting.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Error.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/ErrorOr.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/InitLLVM.h" |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Memory.h" |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Path.h" |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Process.h" |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 39 | #include "llvm/Support/WithColor.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
| 41 | #include <algorithm> |
| 42 | #include <cassert> |
| 43 | #include <cstdlib> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 44 | #include <memory> |
| 45 | #include <string> |
| 46 | #include <system_error> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 47 | #include <utility> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 48 | |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 49 | namespace llvm { |
| 50 | namespace objcopy { |
| 51 | |
| 52 | // The name this program was invoked as. |
| 53 | StringRef ToolName; |
| 54 | |
| 55 | LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 56 | WithColor::error(errs(), ToolName) << Message << ".\n"; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 57 | errs().flush(); |
| 58 | exit(1); |
| 59 | } |
| 60 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 61 | LLVM_ATTRIBUTE_NORETURN void error(Error E) { |
| 62 | assert(E); |
| 63 | std::string Buf; |
| 64 | raw_string_ostream OS(Buf); |
| 65 | logAllUnhandledErrors(std::move(E), OS); |
| 66 | OS.flush(); |
| 67 | WithColor::error(errs(), ToolName) << Buf; |
| 68 | exit(1); |
| 69 | } |
| 70 | |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 71 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { |
Eugene Leviant | 317f9e7 | 2019-02-11 09:49:37 +0000 | [diff] [blame] | 72 | assert(EC); |
| 73 | error(createFileError(File, EC)); |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { |
| 77 | assert(E); |
| 78 | std::string Buf; |
| 79 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 80 | logAllUnhandledErrors(std::move(E), OS); |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 81 | OS.flush(); |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 82 | WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 83 | exit(1); |
| 84 | } |
| 85 | |
| 86 | } // end namespace objcopy |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 87 | } // end namespace llvm |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 88 | |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 89 | using namespace llvm; |
| 90 | using namespace llvm::object; |
| 91 | using namespace llvm::objcopy; |
| 92 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 93 | // For regular archives this function simply calls llvm::writeArchive, |
| 94 | // For thin archives it writes the archive file itself as well as its members. |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 95 | static Error deepWriteArchive(StringRef ArcName, |
| 96 | ArrayRef<NewArchiveMember> NewMembers, |
| 97 | bool WriteSymtab, object::Archive::Kind Kind, |
| 98 | bool Deterministic, bool Thin) { |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 99 | if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind, |
| 100 | Deterministic, Thin)) |
| 101 | return createFileError(ArcName, std::move(E)); |
| 102 | |
| 103 | if (!Thin) |
| 104 | return Error::success(); |
| 105 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 106 | for (const NewArchiveMember &Member : NewMembers) { |
| 107 | // Internally, FileBuffer will use the buffer created by |
| 108 | // FileOutputBuffer::create, for regular files (that is the case for |
| 109 | // deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer. |
| 110 | // OnDiskBuffer uses a temporary file and then renames it. So in reality |
| 111 | // there is no inefficiency / duplicated in-memory buffers in this case. For |
| 112 | // now in-memory buffers can not be completely avoided since |
| 113 | // NewArchiveMember still requires them even though writeArchive does not |
| 114 | // write them on disk. |
| 115 | FileBuffer FB(Member.MemberName); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 116 | if (Error E = FB.allocate(Member.Buf->getBufferSize())) |
| 117 | return E; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 118 | std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(), |
| 119 | FB.getBufferStart()); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 120 | if (Error E = FB.commit()) |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 121 | return E; |
| 122 | } |
| 123 | return Error::success(); |
| 124 | } |
| 125 | |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 126 | /// The function executeObjcopyOnRawBinary does the dispatch based on the format |
| 127 | /// of the output specified by the command line options. |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 128 | static Error executeObjcopyOnRawBinary(const CopyConfig &Config, |
| 129 | MemoryBuffer &In, Buffer &Out) { |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 130 | // TODO: llvm-objcopy should parse CopyConfig.OutputFormat to recognize |
| 131 | // formats other than ELF / "binary" and invoke |
| 132 | // elf::executeObjcopyOnRawBinary, macho::executeObjcopyOnRawBinary or |
| 133 | // coff::executeObjcopyOnRawBinary accordingly. |
| 134 | return elf::executeObjcopyOnRawBinary(Config, In, Out); |
| 135 | } |
| 136 | |
| 137 | /// The function executeObjcopyOnBinary does the dispatch based on the format |
| 138 | /// of the input binary (ELF, MachO or COFF). |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 139 | static Error executeObjcopyOnBinary(const CopyConfig &Config, |
| 140 | object::Binary &In, Buffer &Out) { |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 141 | if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) |
| 142 | return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 143 | else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) |
| 144 | return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 145 | else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) |
| 146 | return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out); |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 147 | else |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 148 | return createStringError(object_error::invalid_file_type, |
| 149 | "Unsupported object file format"); |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 152 | static Error executeObjcopyOnArchive(const CopyConfig &Config, |
| 153 | const Archive &Ar) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 154 | std::vector<NewArchiveMember> NewArchiveMembers; |
| 155 | Error Err = Error::success(); |
| 156 | for (const Archive::Child &Child : Ar.children(Err)) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 157 | Expected<StringRef> ChildNameOrErr = Child.getName(); |
| 158 | if (!ChildNameOrErr) |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 159 | return createFileError(Ar.getFileName(), ChildNameOrErr.takeError()); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 160 | |
James Henderson | 5a2b5ca | 2019-05-08 13:28:58 +0000 | [diff] [blame] | 161 | Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); |
| 162 | if (!ChildOrErr) |
| 163 | return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")", |
| 164 | ChildOrErr.takeError()); |
| 165 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 166 | MemBuffer MB(ChildNameOrErr.get()); |
James Henderson | 5a2b5ca | 2019-05-08 13:28:58 +0000 | [diff] [blame] | 167 | if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MB)) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 168 | return E; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 169 | |
| 170 | Expected<NewArchiveMember> Member = |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 171 | NewArchiveMember::getOldMember(Child, Config.DeterministicArchives); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 172 | if (!Member) |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 173 | return createFileError(Ar.getFileName(), Member.takeError()); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 174 | Member->Buf = MB.releaseMemoryBuffer(); |
| 175 | Member->MemberName = Member->Buf->getBufferIdentifier(); |
| 176 | NewArchiveMembers.push_back(std::move(*Member)); |
| 177 | } |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 178 | if (Err) |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 179 | return createFileError(Config.InputFilename, std::move(Err)); |
| 180 | |
| 181 | return deepWriteArchive(Config.OutputFilename, NewArchiveMembers, |
| 182 | Ar.hasSymbolTable(), Ar.kind(), |
| 183 | Config.DeterministicArchives, Ar.isThin()); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 186 | static Error restoreDateOnFile(StringRef Filename, |
| 187 | const sys::fs::file_status &Stat) { |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 188 | int FD; |
| 189 | |
Jordan Rupprecht | 7481540 | 2018-08-29 23:21:56 +0000 | [diff] [blame] | 190 | if (auto EC = |
| 191 | sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 192 | return createFileError(Filename, EC); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 193 | |
| 194 | if (auto EC = sys::fs::setLastAccessAndModificationTime( |
| 195 | FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime())) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 196 | return createFileError(Filename, EC); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 197 | |
| 198 | if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 199 | return createFileError(Filename, EC); |
| 200 | |
| 201 | return Error::success(); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 204 | /// The function executeObjcopy does the higher level dispatch based on the type |
| 205 | /// of input (raw binary, archive or single object file) and takes care of the |
| 206 | /// format-agnostic modifications, i.e. preserving dates. |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 207 | static Error executeObjcopy(const CopyConfig &Config) { |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 208 | sys::fs::file_status Stat; |
| 209 | if (Config.PreserveDates) |
| 210 | if (auto EC = sys::fs::status(Config.InputFilename, Stat)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 211 | return createFileError(Config.InputFilename, EC); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 212 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 213 | if (Config.InputFormat == "binary") { |
| 214 | auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename); |
| 215 | if (!BufOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 216 | return createFileError(Config.InputFilename, BufOrErr.getError()); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 217 | FileBuffer FB(Config.OutputFilename); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 218 | if (Error E = executeObjcopyOnRawBinary(Config, *BufOrErr->get(), FB)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 219 | return E; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 220 | } else { |
| 221 | Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr = |
| 222 | createBinary(Config.InputFilename); |
| 223 | if (!BinaryOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 224 | return createFileError(Config.InputFilename, BinaryOrErr.takeError()); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 225 | |
| 226 | if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) { |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 227 | if (Error E = executeObjcopyOnArchive(Config, *Ar)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 228 | return E; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 229 | } else { |
| 230 | FileBuffer FB(Config.OutputFilename); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 231 | if (Error E = executeObjcopyOnBinary(Config, |
| 232 | *BinaryOrErr.get().getBinary(), FB)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 233 | return E; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 234 | } |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 235 | } |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 236 | |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 237 | if (Config.PreserveDates) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 238 | if (Error E = restoreDateOnFile(Config.OutputFilename, Stat)) |
| 239 | return E; |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 240 | if (!Config.SplitDWO.empty()) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 241 | if (Error E = restoreDateOnFile(Config.SplitDWO, Stat)) |
| 242 | return E; |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 243 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 244 | |
| 245 | return Error::success(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 246 | } |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 247 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 248 | int main(int argc, char **argv) { |
| 249 | InitLLVM X(argc, argv); |
| 250 | ToolName = argv[0]; |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 251 | bool IsStrip = sys::path::stem(ToolName).contains("strip"); |
| 252 | Expected<DriverConfig> DriverConfig = |
| 253 | IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc)) |
| 254 | : parseObjcopyOptions(makeArrayRef(argv + 1, argc)); |
| 255 | if (!DriverConfig) { |
| 256 | logAllUnhandledErrors(DriverConfig.takeError(), |
| 257 | WithColor::error(errs(), ToolName)); |
| 258 | return 1; |
| 259 | } |
| 260 | for (const CopyConfig &CopyConfig : DriverConfig->CopyConfigs) { |
| 261 | if (Error E = executeObjcopy(CopyConfig)) { |
| 262 | logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName)); |
| 263 | return 1; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return 0; |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 268 | } |