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