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