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" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 11 | #include "CopyConfig.h" |
Alexander Shaposhnikov | f4e75a5 | 2018-10-29 21:22:58 +0000 | [diff] [blame] | 12 | #include "ELF/ELFObjcopy.h" |
Alex Brachet | b349834 | 2019-06-01 07:36:57 +0000 | [diff] [blame] | 13 | #include "COFF/COFFObjcopy.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" |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Error.h" |
| 34 | #include "llvm/Support/ErrorHandling.h" |
| 35 | #include "llvm/Support/ErrorOr.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 36 | #include "llvm/Support/InitLLVM.h" |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Memory.h" |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Path.h" |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Process.h" |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 40 | #include "llvm/Support/StringSaver.h" |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 41 | #include "llvm/Support/WithColor.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
| 43 | #include <algorithm> |
| 44 | #include <cassert> |
| 45 | #include <cstdlib> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 46 | #include <memory> |
| 47 | #include <string> |
| 48 | #include <system_error> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 49 | #include <utility> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 50 | |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 51 | namespace llvm { |
| 52 | namespace objcopy { |
| 53 | |
| 54 | // The name this program was invoked as. |
| 55 | StringRef ToolName; |
| 56 | |
| 57 | LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 58 | WithColor::error(errs(), ToolName) << Message << "\n"; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 59 | exit(1); |
| 60 | } |
| 61 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 62 | LLVM_ATTRIBUTE_NORETURN void error(Error E) { |
| 63 | assert(E); |
| 64 | std::string Buf; |
| 65 | raw_string_ostream OS(Buf); |
| 66 | logAllUnhandledErrors(std::move(E), OS); |
| 67 | OS.flush(); |
| 68 | WithColor::error(errs(), ToolName) << Buf; |
| 69 | exit(1); |
| 70 | } |
| 71 | |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 72 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { |
Eugene Leviant | 317f9e7 | 2019-02-11 09:49:37 +0000 | [diff] [blame] | 73 | assert(EC); |
| 74 | error(createFileError(File, EC)); |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { |
| 78 | assert(E); |
| 79 | std::string Buf; |
| 80 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 81 | logAllUnhandledErrors(std::move(E), OS); |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 82 | OS.flush(); |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 83 | WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 84 | exit(1); |
| 85 | } |
| 86 | |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 87 | ErrorSuccess reportWarning(Error E) { |
| 88 | assert(E); |
Fangrui Song | ebd5029 | 2019-08-20 15:00:07 +0000 | [diff] [blame] | 89 | WithColor::warning(errs(), ToolName) << toString(std::move(E)) << '\n'; |
Alex Brachet | 7747700 | 2019-06-18 00:39:10 +0000 | [diff] [blame] | 90 | return Error::success(); |
| 91 | } |
| 92 | |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 93 | } // end namespace objcopy |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 94 | } // end namespace llvm |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 95 | |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 96 | using namespace llvm; |
| 97 | using namespace llvm::object; |
| 98 | using namespace llvm::objcopy; |
| 99 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 100 | // For regular archives this function simply calls llvm::writeArchive, |
| 101 | // 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] | 102 | static Error deepWriteArchive(StringRef ArcName, |
| 103 | ArrayRef<NewArchiveMember> NewMembers, |
| 104 | bool WriteSymtab, object::Archive::Kind Kind, |
| 105 | bool Deterministic, bool Thin) { |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 106 | if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind, |
| 107 | Deterministic, Thin)) |
| 108 | return createFileError(ArcName, std::move(E)); |
| 109 | |
| 110 | if (!Thin) |
| 111 | return Error::success(); |
| 112 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 113 | for (const NewArchiveMember &Member : NewMembers) { |
| 114 | // Internally, FileBuffer will use the buffer created by |
| 115 | // FileOutputBuffer::create, for regular files (that is the case for |
| 116 | // deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer. |
| 117 | // OnDiskBuffer uses a temporary file and then renames it. So in reality |
| 118 | // there is no inefficiency / duplicated in-memory buffers in this case. For |
| 119 | // now in-memory buffers can not be completely avoided since |
| 120 | // NewArchiveMember still requires them even though writeArchive does not |
| 121 | // write them on disk. |
| 122 | FileBuffer FB(Member.MemberName); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 123 | if (Error E = FB.allocate(Member.Buf->getBufferSize())) |
| 124 | return E; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 125 | std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(), |
| 126 | FB.getBufferStart()); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 127 | if (Error E = FB.commit()) |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 128 | return E; |
| 129 | } |
| 130 | return Error::success(); |
| 131 | } |
| 132 | |
Eugene Leviant | 86b7f86 | 2019-06-13 09:56:14 +0000 | [diff] [blame] | 133 | /// The function executeObjcopyOnIHex does the dispatch based on the format |
| 134 | /// of the output specified by the command line options. |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 135 | static Error executeObjcopyOnIHex(CopyConfig &Config, MemoryBuffer &In, |
Eugene Leviant | 86b7f86 | 2019-06-13 09:56:14 +0000 | [diff] [blame] | 136 | Buffer &Out) { |
| 137 | // TODO: support output formats other than ELF. |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 138 | if (Error E = Config.parseELFConfig()) |
| 139 | return E; |
Eugene Leviant | 86b7f86 | 2019-06-13 09:56:14 +0000 | [diff] [blame] | 140 | return elf::executeObjcopyOnIHex(Config, In, Out); |
| 141 | } |
| 142 | |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 143 | /// The function executeObjcopyOnRawBinary does the dispatch based on the format |
| 144 | /// of the output specified by the command line options. |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 145 | static Error executeObjcopyOnRawBinary(CopyConfig &Config, MemoryBuffer &In, |
| 146 | Buffer &Out) { |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 147 | switch (Config.OutputFormat) { |
| 148 | case FileFormat::ELF: |
| 149 | // FIXME: Currently, we call elf::executeObjcopyOnRawBinary even if the |
| 150 | // output format is binary/ihex or it's not given. This behavior differs from |
| 151 | // GNU objcopy. See https://bugs.llvm.org/show_bug.cgi?id=42171 for details. |
| 152 | case FileFormat::Binary: |
| 153 | case FileFormat::IHex: |
| 154 | case FileFormat::Unspecified: |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 155 | if (Error E = Config.parseELFConfig()) |
| 156 | return E; |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 157 | return elf::executeObjcopyOnRawBinary(Config, In, Out); |
| 158 | } |
| 159 | |
| 160 | llvm_unreachable("unsupported output format"); |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /// The function executeObjcopyOnBinary does the dispatch based on the format |
| 164 | /// of the input binary (ELF, MachO or COFF). |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 165 | static Error executeObjcopyOnBinary(CopyConfig &Config, object::Binary &In, |
| 166 | Buffer &Out) { |
| 167 | if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) { |
| 168 | if (Error E = Config.parseELFConfig()) |
| 169 | return E; |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 170 | return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out); |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 171 | } else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In)) |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 172 | return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 173 | else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In)) |
| 174 | return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out); |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 175 | else |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 176 | return createStringError(object_error::invalid_file_type, |
Alex Brachet | d54d4f9 | 2019-06-14 02:04:02 +0000 | [diff] [blame] | 177 | "unsupported object file format"); |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 180 | static Error executeObjcopyOnArchive(CopyConfig &Config, const Archive &Ar) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 181 | std::vector<NewArchiveMember> NewArchiveMembers; |
| 182 | Error Err = Error::success(); |
| 183 | for (const Archive::Child &Child : Ar.children(Err)) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 184 | Expected<StringRef> ChildNameOrErr = Child.getName(); |
| 185 | if (!ChildNameOrErr) |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 186 | return createFileError(Ar.getFileName(), ChildNameOrErr.takeError()); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 187 | |
James Henderson | 5a2b5ca | 2019-05-08 13:28:58 +0000 | [diff] [blame] | 188 | Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); |
| 189 | if (!ChildOrErr) |
| 190 | return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")", |
| 191 | ChildOrErr.takeError()); |
| 192 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 193 | MemBuffer MB(ChildNameOrErr.get()); |
James Henderson | 5a2b5ca | 2019-05-08 13:28:58 +0000 | [diff] [blame] | 194 | if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MB)) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 195 | return E; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 196 | |
| 197 | Expected<NewArchiveMember> Member = |
Jordan Rupprecht | fc780bb | 2018-11-01 17:36:37 +0000 | [diff] [blame] | 198 | NewArchiveMember::getOldMember(Child, Config.DeterministicArchives); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 199 | if (!Member) |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 200 | return createFileError(Ar.getFileName(), Member.takeError()); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 201 | Member->Buf = MB.releaseMemoryBuffer(); |
| 202 | Member->MemberName = Member->Buf->getBufferIdentifier(); |
| 203 | NewArchiveMembers.push_back(std::move(*Member)); |
| 204 | } |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 205 | if (Err) |
Jordan Rupprecht | 18886db | 2019-02-01 17:08:07 +0000 | [diff] [blame] | 206 | return createFileError(Config.InputFilename, std::move(Err)); |
| 207 | |
| 208 | return deepWriteArchive(Config.OutputFilename, NewArchiveMembers, |
| 209 | Ar.hasSymbolTable(), Ar.kind(), |
| 210 | Config.DeterministicArchives, Ar.isThin()); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Alex Brachet | 7439a9a | 2019-07-04 22:45:27 +0000 | [diff] [blame] | 213 | static Error restoreStatOnFile(StringRef Filename, |
| 214 | const sys::fs::file_status &Stat, |
| 215 | bool PreserveDates) { |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 216 | int FD; |
| 217 | |
Alex Brachet | 7439a9a | 2019-07-04 22:45:27 +0000 | [diff] [blame] | 218 | // Writing to stdout should not be treated as an error here, just |
| 219 | // do not set access/modification times or permissions. |
| 220 | if (Filename == "-") |
| 221 | return Error::success(); |
| 222 | |
Jordan Rupprecht | 7481540 | 2018-08-29 23:21:56 +0000 | [diff] [blame] | 223 | if (auto EC = |
| 224 | sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 225 | return createFileError(Filename, EC); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 226 | |
Alex Brachet | 7439a9a | 2019-07-04 22:45:27 +0000 | [diff] [blame] | 227 | if (PreserveDates) |
| 228 | if (auto EC = sys::fs::setLastAccessAndModificationTime( |
| 229 | FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime())) |
| 230 | return createFileError(Filename, EC); |
| 231 | |
Fangrui Song | 6dc5962 | 2019-07-11 10:17:59 +0000 | [diff] [blame] | 232 | sys::fs::file_status OStat; |
| 233 | if (std::error_code EC = sys::fs::status(FD, OStat)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 234 | return createFileError(Filename, EC); |
Fangrui Song | 6dc5962 | 2019-07-11 10:17:59 +0000 | [diff] [blame] | 235 | if (OStat.type() == sys::fs::file_type::regular_file) |
| 236 | #ifdef _WIN32 |
| 237 | if (auto EC = sys::fs::setPermissions( |
| 238 | Filename, static_cast<sys::fs::perms>(Stat.permissions() & |
| 239 | ~sys::fs::getUmask()))) |
| 240 | #else |
| 241 | if (auto EC = sys::fs::setPermissions( |
| 242 | FD, static_cast<sys::fs::perms>(Stat.permissions() & |
| 243 | ~sys::fs::getUmask()))) |
| 244 | #endif |
| 245 | return createFileError(Filename, EC); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 246 | |
| 247 | if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 248 | return createFileError(Filename, EC); |
| 249 | |
| 250 | return Error::success(); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 253 | /// The function executeObjcopy does the higher level dispatch based on the type |
| 254 | /// of input (raw binary, archive or single object file) and takes care of the |
| 255 | /// format-agnostic modifications, i.e. preserving dates. |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 256 | static Error executeObjcopy(CopyConfig &Config) { |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 257 | sys::fs::file_status Stat; |
Alex Brachet | 7439a9a | 2019-07-04 22:45:27 +0000 | [diff] [blame] | 258 | if (Config.InputFilename != "-") { |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 259 | if (auto EC = sys::fs::status(Config.InputFilename, Stat)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 260 | return createFileError(Config.InputFilename, EC); |
Alex Brachet | 7439a9a | 2019-07-04 22:45:27 +0000 | [diff] [blame] | 261 | } else { |
| 262 | Stat.permissions(static_cast<sys::fs::perms>(0777)); |
| 263 | } |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 264 | |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 265 | using ProcessRawFn = Error (*)(CopyConfig &, MemoryBuffer &, Buffer &); |
Seiya Nuta | ecb60b7 | 2019-07-05 05:28:38 +0000 | [diff] [blame] | 266 | ProcessRawFn ProcessRaw; |
| 267 | switch (Config.InputFormat) { |
| 268 | case FileFormat::Binary: |
| 269 | ProcessRaw = executeObjcopyOnRawBinary; |
| 270 | break; |
| 271 | case FileFormat::IHex: |
| 272 | ProcessRaw = executeObjcopyOnIHex; |
| 273 | break; |
| 274 | default: |
| 275 | ProcessRaw = nullptr; |
| 276 | } |
Eugene Leviant | 86b7f86 | 2019-06-13 09:56:14 +0000 | [diff] [blame] | 277 | |
| 278 | if (ProcessRaw) { |
| 279 | auto BufOrErr = MemoryBuffer::getFileOrSTDIN(Config.InputFilename); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 280 | if (!BufOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 281 | return createFileError(Config.InputFilename, BufOrErr.getError()); |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 282 | FileBuffer FB(Config.OutputFilename); |
Eugene Leviant | 86b7f86 | 2019-06-13 09:56:14 +0000 | [diff] [blame] | 283 | if (Error E = ProcessRaw(Config, *BufOrErr->get(), FB)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 284 | return E; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 285 | } else { |
| 286 | Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr = |
| 287 | createBinary(Config.InputFilename); |
| 288 | if (!BinaryOrErr) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 289 | return createFileError(Config.InputFilename, BinaryOrErr.takeError()); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 290 | |
| 291 | if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) { |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 292 | if (Error E = executeObjcopyOnArchive(Config, *Ar)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 293 | return E; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 294 | } else { |
| 295 | FileBuffer FB(Config.OutputFilename); |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 296 | if (Error E = executeObjcopyOnBinary(Config, |
| 297 | *BinaryOrErr.get().getBinary(), FB)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 298 | return E; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 299 | } |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 300 | } |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 301 | |
Alex Brachet | 7439a9a | 2019-07-04 22:45:27 +0000 | [diff] [blame] | 302 | if (Error E = |
| 303 | restoreStatOnFile(Config.OutputFilename, Stat, Config.PreserveDates)) |
| 304 | return E; |
| 305 | |
| 306 | if (!Config.SplitDWO.empty()) { |
| 307 | Stat.permissions(static_cast<sys::fs::perms>(0666)); |
| 308 | if (Error E = |
| 309 | restoreStatOnFile(Config.SplitDWO, Stat, Config.PreserveDates)) |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 310 | return E; |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 311 | } |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 312 | |
| 313 | return Error::success(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 314 | } |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 315 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame^] | 316 | namespace { |
| 317 | |
| 318 | enum class ToolType { Objcopy, Strip, InstallNameTool }; |
| 319 | |
| 320 | } // anonymous namespace |
| 321 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 322 | int main(int argc, char **argv) { |
| 323 | InitLLVM X(argc, argv); |
| 324 | ToolName = argv[0]; |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame^] | 325 | ToolType Tool = StringSwitch<ToolType>(sys::path::stem(ToolName)) |
| 326 | .EndsWith("strip", ToolType::Strip) |
| 327 | .EndsWith("install-name-tool", ToolType::InstallNameTool) |
| 328 | .EndsWith("install_name_tool", ToolType::InstallNameTool) |
| 329 | .Default(ToolType::Objcopy); |
Michael Pozulp | c45fd0c | 2019-09-14 01:14:43 +0000 | [diff] [blame] | 330 | // Expand response files. |
| 331 | // TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp, |
| 332 | // into a separate function in the CommandLine library and call that function |
| 333 | // here. This is duplicated code. |
| 334 | SmallVector<const char *, 20> NewArgv(argv, argv + argc); |
| 335 | BumpPtrAllocator A; |
| 336 | StringSaver Saver(A); |
| 337 | cl::ExpandResponseFiles(Saver, |
| 338 | Triple(sys::getProcessTriple()).isOSWindows() |
| 339 | ? cl::TokenizeWindowsCommandLine |
| 340 | : cl::TokenizeGNUCommandLine, |
| 341 | NewArgv); |
| 342 | |
| 343 | auto Args = makeArrayRef(NewArgv).drop_front(); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 344 | Expected<DriverConfig> DriverConfig = |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame^] | 345 | (Tool == ToolType::Strip) ? parseStripOptions(Args, reportWarning) |
| 346 | : ((Tool == ToolType::InstallNameTool) |
| 347 | ? parseInstallNameToolOptions(Args) |
| 348 | : parseObjcopyOptions(Args, reportWarning)); |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 349 | if (!DriverConfig) { |
| 350 | logAllUnhandledErrors(DriverConfig.takeError(), |
| 351 | WithColor::error(errs(), ToolName)); |
| 352 | return 1; |
| 353 | } |
Seiya Nuta | c83eefc | 2019-09-24 09:38:23 +0000 | [diff] [blame] | 354 | for (CopyConfig &CopyConfig : DriverConfig->CopyConfigs) { |
Jordan Rupprecht | ad29d29 | 2019-02-21 17:05:19 +0000 | [diff] [blame] | 355 | if (Error E = executeObjcopy(CopyConfig)) { |
| 356 | logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName)); |
| 357 | return 1; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return 0; |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 362 | } |