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" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 13 | #include "Object.h" |
Alexander Shaposhnikov | 8d0b74c | 2018-10-11 22:33:50 +0000 | [diff] [blame] | 14 | |
Jordan Rupprecht | d67c1e1 | 2018-08-01 16:23:22 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/BitmaskEnum.h" |
| 16 | #include "llvm/ADT/Optional.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Jordan Rupprecht | d67c1e1 | 2018-08-01 16:23:22 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ADT/Twine.h" |
| 21 | #include "llvm/BinaryFormat/ELF.h" |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCTargetOptions.h" |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 23 | #include "llvm/Object/Archive.h" |
| 24 | #include "llvm/Object/ArchiveWriter.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 25 | #include "llvm/Object/Binary.h" |
| 26 | #include "llvm/Object/ELFObjectFile.h" |
| 27 | #include "llvm/Object/ELFTypes.h" |
| 28 | #include "llvm/Object/Error.h" |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 29 | #include "llvm/Option/Arg.h" |
| 30 | #include "llvm/Option/ArgList.h" |
| 31 | #include "llvm/Option/Option.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Casting.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Compiler.h" |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Compression.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Error.h" |
| 37 | #include "llvm/Support/ErrorHandling.h" |
| 38 | #include "llvm/Support/ErrorOr.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 39 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 40 | #include "llvm/Support/InitLLVM.h" |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Memory.h" |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Path.h" |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Process.h" |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 44 | #include "llvm/Support/WithColor.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
| 46 | #include <algorithm> |
| 47 | #include <cassert> |
| 48 | #include <cstdlib> |
| 49 | #include <functional> |
| 50 | #include <iterator> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 51 | #include <memory> |
| 52 | #include <string> |
| 53 | #include <system_error> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 54 | #include <utility> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 55 | |
| 56 | using namespace llvm; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 57 | using namespace llvm::objcopy; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 58 | using namespace object; |
| 59 | using namespace ELF; |
| 60 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 61 | using SectionPred = std::function<bool(const SectionBase &Sec)>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 62 | |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 63 | namespace llvm { |
| 64 | namespace objcopy { |
| 65 | |
| 66 | // The name this program was invoked as. |
| 67 | StringRef ToolName; |
| 68 | |
| 69 | LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 70 | WithColor::error(errs(), ToolName) << Message << ".\n"; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 71 | errs().flush(); |
| 72 | exit(1); |
| 73 | } |
| 74 | |
| 75 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { |
| 76 | assert(EC); |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 77 | WithColor::error(errs(), ToolName) |
| 78 | << "'" << File << "': " << EC.message() << ".\n"; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 79 | exit(1); |
| 80 | } |
| 81 | |
| 82 | LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { |
| 83 | assert(E); |
| 84 | std::string Buf; |
| 85 | raw_string_ostream OS(Buf); |
| 86 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 87 | OS.flush(); |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 88 | WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 89 | exit(1); |
| 90 | } |
| 91 | |
| 92 | } // end namespace objcopy |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 93 | } // end namespace llvm |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 94 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 95 | static bool isDebugSection(const SectionBase &Sec) { |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 96 | return StringRef(Sec.Name).startswith(".debug") || |
| 97 | StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index"; |
Fangrui Song | fdfe2a9 | 2018-07-27 22:51:36 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 100 | static bool isDWOSection(const SectionBase &Sec) { |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 101 | return StringRef(Sec.Name).endswith(".dwo"); |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 104 | static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 105 | // We can't remove the section header string table. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 106 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 107 | return false; |
| 108 | // Short of keeping the string table we want to keep everything that is a DWO |
| 109 | // section and remove everything else. |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 110 | return !isDWOSection(Sec); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 113 | static ElfType getOutputElfType(const Binary &Bin) { |
| 114 | // Infer output ELF type from the input ELF object |
| 115 | if (isa<ELFObjectFile<ELF32LE>>(Bin)) |
| 116 | return ELFT_ELF32LE; |
| 117 | if (isa<ELFObjectFile<ELF64LE>>(Bin)) |
| 118 | return ELFT_ELF64LE; |
| 119 | if (isa<ELFObjectFile<ELF32BE>>(Bin)) |
| 120 | return ELFT_ELF32BE; |
| 121 | if (isa<ELFObjectFile<ELF64BE>>(Bin)) |
| 122 | return ELFT_ELF64BE; |
| 123 | llvm_unreachable("Invalid ELFType"); |
| 124 | } |
| 125 | |
| 126 | static ElfType getOutputElfType(const MachineInfo &MI) { |
| 127 | // Infer output ELF type from the binary arch specified |
| 128 | if (MI.Is64Bit) |
| 129 | return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE; |
| 130 | else |
| 131 | return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE; |
| 132 | } |
| 133 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 134 | static std::unique_ptr<Writer> createWriter(const CopyConfig &Config, |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 135 | Object &Obj, Buffer &Buf, |
| 136 | ElfType OutputElfType) { |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 137 | if (Config.OutputFormat == "binary") { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 138 | return llvm::make_unique<BinaryWriter>(Obj, Buf); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 139 | } |
| 140 | // Depending on the initial ELFT and OutputFormat we need a different Writer. |
| 141 | switch (OutputElfType) { |
| 142 | case ELFT_ELF32LE: |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 143 | return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf, |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 144 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 145 | case ELFT_ELF64LE: |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 146 | return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf, |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 147 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 148 | case ELFT_ELF32BE: |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 149 | return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf, |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 150 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 151 | case ELFT_ELF64BE: |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 152 | return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf, |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 153 | !Config.StripSections); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 154 | } |
| 155 | llvm_unreachable("Invalid output format"); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 158 | static void splitDWOToFile(const CopyConfig &Config, const Reader &Reader, |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 159 | StringRef File, ElfType OutputElfType) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 160 | auto DWOFile = Reader.create(); |
| 161 | DWOFile->removeSections( |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 162 | [&](const SectionBase &Sec) { return onlyKeepDWOPred(*DWOFile, Sec); }); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 163 | FileBuffer FB(File); |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 164 | auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 165 | Writer->finalize(); |
| 166 | Writer->write(); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Paul Semel | a42dec7 | 2018-08-09 17:05:21 +0000 | [diff] [blame] | 169 | static Error dumpSectionToFile(StringRef SecName, StringRef Filename, |
| 170 | Object &Obj) { |
| 171 | for (auto &Sec : Obj.sections()) { |
| 172 | if (Sec.Name == SecName) { |
| 173 | if (Sec.OriginalData.size() == 0) |
| 174 | return make_error<StringError>("Can't dump section \"" + SecName + |
| 175 | "\": it has no contents", |
| 176 | object_error::parse_failed); |
| 177 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 178 | FileOutputBuffer::create(Filename, Sec.OriginalData.size()); |
| 179 | if (!BufferOrErr) |
| 180 | return BufferOrErr.takeError(); |
| 181 | std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr); |
| 182 | std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), |
| 183 | Buf->getBufferStart()); |
| 184 | if (Error E = Buf->commit()) |
| 185 | return E; |
| 186 | return Error::success(); |
| 187 | } |
| 188 | } |
| 189 | return make_error<StringError>("Section not found", |
| 190 | object_error::parse_failed); |
| 191 | } |
| 192 | |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 193 | static bool isCompressed(const SectionBase &Section) { |
| 194 | const char *Magic = "ZLIB"; |
| 195 | return StringRef(Section.Name).startswith(".zdebug") || |
| 196 | (Section.OriginalData.size() > strlen(Magic) && |
| 197 | !strncmp(reinterpret_cast<const char *>(Section.OriginalData.data()), |
| 198 | Magic, strlen(Magic))) || |
| 199 | (Section.Flags & ELF::SHF_COMPRESSED); |
| 200 | } |
| 201 | |
| 202 | static bool isCompressable(const SectionBase &Section) { |
| 203 | return !isCompressed(Section) && isDebugSection(Section) && |
| 204 | Section.Name != ".gdb_index"; |
| 205 | } |
| 206 | |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 207 | static void replaceDebugSections( |
| 208 | const CopyConfig &Config, Object &Obj, SectionPred &RemovePred, |
| 209 | function_ref<bool(const SectionBase &)> shouldReplace, |
| 210 | function_ref<SectionBase *(const SectionBase *)> addSection) { |
| 211 | SmallVector<SectionBase *, 13> ToReplace; |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 212 | SmallVector<RelocationSection *, 13> RelocationSections; |
| 213 | for (auto &Sec : Obj.sections()) { |
| 214 | if (RelocationSection *R = dyn_cast<RelocationSection>(&Sec)) { |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 215 | if (shouldReplace(*R->getSection())) |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 216 | RelocationSections.push_back(R); |
| 217 | continue; |
| 218 | } |
| 219 | |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 220 | if (shouldReplace(Sec)) |
| 221 | ToReplace.push_back(&Sec); |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 224 | for (SectionBase *S : ToReplace) { |
| 225 | SectionBase *NewSection = addSection(S); |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 226 | |
| 227 | for (RelocationSection *RS : RelocationSections) { |
| 228 | if (RS->getSection() == S) |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 229 | RS->setSection(NewSection); |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 233 | RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) { |
| 234 | return shouldReplace(Sec) || RemovePred(Sec); |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 235 | }; |
| 236 | } |
| 237 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 238 | // This function handles the high level operations of GNU objcopy including |
| 239 | // handling command line options. It's important to outline certain properties |
| 240 | // we expect to hold of the command line operations. Any operation that "keeps" |
| 241 | // should keep regardless of a remove. Additionally any removal should respect |
| 242 | // any previous removals. Lastly whether or not something is removed shouldn't |
| 243 | // depend a) on the order the options occur in or b) on some opaque priority |
| 244 | // system. The only priority is that keeps/copies overrule removes. |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 245 | static void handleArgs(const CopyConfig &Config, Object &Obj, |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 246 | const Reader &Reader, ElfType OutputElfType) { |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 247 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 248 | if (!Config.SplitDWO.empty()) { |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 249 | splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 250 | } |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 251 | |
| 252 | // TODO: update or remove symbols only if there is an option that affects |
| 253 | // them. |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 254 | if (Obj.SymbolTable) { |
| 255 | Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { |
| 256 | if ((Config.LocalizeHidden && |
| 257 | (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || |
| 258 | (!Config.SymbolsToLocalize.empty() && |
| 259 | is_contained(Config.SymbolsToLocalize, Sym.Name))) |
| 260 | Sym.Binding = STB_LOCAL; |
| 261 | |
Jordan Rupprecht | be8ebcc | 2018-08-17 22:34:48 +0000 | [diff] [blame] | 262 | // Note: these two globalize flags have very similar names but different |
| 263 | // meanings: |
| 264 | // |
| 265 | // --globalize-symbol: promote a symbol to global |
| 266 | // --keep-global-symbol: all symbols except for these should be made local |
| 267 | // |
| 268 | // If --globalize-symbol is specified for a given symbol, it will be |
| 269 | // global in the output file even if it is not included via |
| 270 | // --keep-global-symbol. Because of that, make sure to check |
| 271 | // --globalize-symbol second. |
| 272 | if (!Config.SymbolsToKeepGlobal.empty() && |
| 273 | !is_contained(Config.SymbolsToKeepGlobal, Sym.Name)) |
| 274 | Sym.Binding = STB_LOCAL; |
| 275 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 276 | if (!Config.SymbolsToGlobalize.empty() && |
| 277 | is_contained(Config.SymbolsToGlobalize, Sym.Name)) |
| 278 | Sym.Binding = STB_GLOBAL; |
| 279 | |
| 280 | if (!Config.SymbolsToWeaken.empty() && |
| 281 | is_contained(Config.SymbolsToWeaken, Sym.Name) && |
| 282 | Sym.Binding == STB_GLOBAL) |
| 283 | Sym.Binding = STB_WEAK; |
| 284 | |
| 285 | if (Config.Weaken && Sym.Binding == STB_GLOBAL && |
| 286 | Sym.getShndx() != SHN_UNDEF) |
| 287 | Sym.Binding = STB_WEAK; |
| 288 | |
| 289 | const auto I = Config.SymbolsToRename.find(Sym.Name); |
| 290 | if (I != Config.SymbolsToRename.end()) |
| 291 | Sym.Name = I->getValue(); |
Paul Semel | 7a3dc2c | 2018-08-09 17:49:04 +0000 | [diff] [blame] | 292 | |
| 293 | if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION) |
| 294 | Sym.Name = (Config.SymbolsPrefix + Sym.Name).str(); |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 295 | }); |
| 296 | |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 297 | // The purpose of this loop is to mark symbols referenced by sections |
| 298 | // (like GroupSection or RelocationSection). This way, we know which |
Armando Montanez | fdb732b | 2018-10-10 21:16:57 +0000 | [diff] [blame] | 299 | // symbols are still 'needed' and which are not. |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 300 | if (Config.StripUnneeded) { |
| 301 | for (auto &Section : Obj.sections()) |
| 302 | Section.markSymbols(); |
| 303 | } |
| 304 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 305 | Obj.removeSymbols([&](const Symbol &Sym) { |
Paul Semel | cf51c80 | 2018-05-26 08:10:37 +0000 | [diff] [blame] | 306 | if ((!Config.SymbolsToKeep.empty() && |
| 307 | is_contained(Config.SymbolsToKeep, Sym.Name)) || |
| 308 | (Config.KeepFileSymbols && Sym.Type == STT_FILE)) |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 309 | return false; |
| 310 | |
| 311 | if (Config.DiscardAll && Sym.Binding == STB_LOCAL && |
| 312 | Sym.getShndx() != SHN_UNDEF && Sym.Type != STT_FILE && |
| 313 | Sym.Type != STT_SECTION) |
| 314 | return true; |
| 315 | |
| 316 | if (Config.StripAll || Config.StripAllGNU) |
| 317 | return true; |
| 318 | |
| 319 | if (!Config.SymbolsToRemove.empty() && |
| 320 | is_contained(Config.SymbolsToRemove, Sym.Name)) { |
| 321 | return true; |
| 322 | } |
| 323 | |
Paul Semel | 46201fb | 2018-06-01 16:19:46 +0000 | [diff] [blame] | 324 | if (Config.StripUnneeded && !Sym.Referenced && |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 325 | (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && |
| 326 | Sym.Type != STT_FILE && Sym.Type != STT_SECTION) |
| 327 | return true; |
| 328 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 329 | return false; |
| 330 | }); |
| 331 | } |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 332 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 333 | SectionPred RemovePred = [](const SectionBase &) { return false; }; |
| 334 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 335 | // Removes: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 336 | if (!Config.ToRemove.empty()) { |
| 337 | RemovePred = [&Config](const SectionBase &Sec) { |
Fangrui Song | 0e49ef9 | 2018-08-21 00:13:52 +0000 | [diff] [blame] | 338 | return is_contained(Config.ToRemove, Sec.Name); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 339 | }; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 340 | } |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 341 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 342 | if (Config.StripDWO || !Config.SplitDWO.empty()) |
David Blaikie | 998ff81 | 2017-11-03 20:57:09 +0000 | [diff] [blame] | 343 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 344 | return isDWOSection(Sec) || RemovePred(Sec); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 345 | }; |
| 346 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 347 | if (Config.ExtractDWO) |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 348 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 349 | return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 352 | if (Config.StripAllGNU) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 353 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 354 | if (RemovePred(Sec)) |
| 355 | return true; |
| 356 | if ((Sec.Flags & SHF_ALLOC) != 0) |
| 357 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 358 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 359 | return false; |
Jake Ehrlich | 777fb00 | 2017-12-15 20:17:55 +0000 | [diff] [blame] | 360 | switch (Sec.Type) { |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 361 | case SHT_SYMTAB: |
| 362 | case SHT_REL: |
| 363 | case SHT_RELA: |
| 364 | case SHT_STRTAB: |
| 365 | return true; |
| 366 | } |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 367 | return isDebugSection(Sec); |
Jake Ehrlich | fabddf1 | 2017-11-13 22:02:07 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 370 | if (Config.StripSections) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 371 | RemovePred = [RemovePred](const SectionBase &Sec) { |
| 372 | return RemovePred(Sec) || (Sec.Flags & SHF_ALLOC) == 0; |
| 373 | }; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 376 | if (Config.StripDebug) { |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 377 | RemovePred = [RemovePred](const SectionBase &Sec) { |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 378 | return RemovePred(Sec) || isDebugSection(Sec); |
Jake Ehrlich | 1bfefc1 | 2017-11-13 22:13:08 +0000 | [diff] [blame] | 379 | }; |
| 380 | } |
| 381 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 382 | if (Config.StripNonAlloc) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 383 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 384 | if (RemovePred(Sec)) |
| 385 | return true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 386 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | d56725a | 2017-11-14 18:50:24 +0000 | [diff] [blame] | 387 | return false; |
| 388 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 389 | }; |
| 390 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 391 | if (Config.StripAll) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 392 | RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { |
| 393 | if (RemovePred(Sec)) |
| 394 | return true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 395 | if (&Sec == Obj.SectionNames) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 396 | return false; |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 397 | if (StringRef(Sec.Name).startswith(".gnu.warning")) |
Jake Ehrlich | 6ad72d0 | 2017-11-27 18:56:01 +0000 | [diff] [blame] | 398 | return false; |
| 399 | return (Sec.Flags & SHF_ALLOC) == 0; |
| 400 | }; |
| 401 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 402 | // Explicit copies: |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 403 | if (!Config.OnlyKeep.empty()) { |
| 404 | RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 405 | // Explicitly keep these sections regardless of previous removes. |
Fangrui Song | 0e49ef9 | 2018-08-21 00:13:52 +0000 | [diff] [blame] | 406 | if (is_contained(Config.OnlyKeep, Sec.Name)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 407 | return false; |
| 408 | |
| 409 | // Allow all implicit removes. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 410 | if (RemovePred(Sec)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 411 | return true; |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 412 | |
| 413 | // Keep special sections. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 414 | if (Obj.SectionNames == &Sec) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 415 | return false; |
Stephen Hines | e6e75bf | 2018-07-26 20:05:31 +0000 | [diff] [blame] | 416 | if (Obj.SymbolTable == &Sec || |
| 417 | (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 418 | return false; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 419 | |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 420 | // Remove everything else. |
| 421 | return true; |
| 422 | }; |
| 423 | } |
| 424 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 425 | if (!Config.Keep.empty()) { |
| 426 | RemovePred = [Config, RemovePred](const SectionBase &Sec) { |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 427 | // Explicitly keep these sections regardless of previous removes. |
Fangrui Song | 0e49ef9 | 2018-08-21 00:13:52 +0000 | [diff] [blame] | 428 | if (is_contained(Config.Keep, Sec.Name)) |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 429 | return false; |
| 430 | // Otherwise defer to RemovePred. |
| 431 | return RemovePred(Sec); |
| 432 | }; |
| 433 | } |
| 434 | |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 435 | // This has to be the last predicate assignment. |
| 436 | // If the option --keep-symbol has been specified |
Alexander Shaposhnikov | c7277e6 | 2018-05-23 20:39:52 +0000 | [diff] [blame] | 437 | // and at least one of those symbols is present |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 438 | // (equivalently, the updated symbol table is not empty) |
| 439 | // the symbol table and the string table should not be removed. |
Paul Semel | cf51c80 | 2018-05-26 08:10:37 +0000 | [diff] [blame] | 440 | if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && |
Stephen Hines | e6e75bf | 2018-07-26 20:05:31 +0000 | [diff] [blame] | 441 | Obj.SymbolTable && !Obj.SymbolTable->empty()) { |
Alexander Shaposhnikov | 6e7814c | 2018-05-22 18:24:07 +0000 | [diff] [blame] | 442 | RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { |
| 443 | if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) |
| 444 | return false; |
| 445 | return RemovePred(Sec); |
| 446 | }; |
| 447 | } |
| 448 | |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 449 | if (Config.CompressionType != DebugCompressionType::None) |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 450 | replaceDebugSections(Config, Obj, RemovePred, isCompressable, |
| 451 | [&Config, &Obj](const SectionBase *S) { |
| 452 | return &Obj.addSection<CompressedSection>( |
| 453 | *S, Config.CompressionType); |
| 454 | }); |
| 455 | else if (Config.DecompressDebugSections) |
| 456 | replaceDebugSections( |
| 457 | Config, Obj, RemovePred, |
| 458 | [](const SectionBase &S) { return isa<CompressedSection>(&S); }, |
| 459 | [&Obj](const SectionBase *S) { |
| 460 | auto CS = cast<CompressedSection>(S); |
| 461 | return &Obj.addSection<DecompressedSection>(*CS); |
| 462 | }); |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 463 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 464 | Obj.removeSections(RemovePred); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 465 | |
Jordan Rupprecht | db2036e | 2018-07-20 19:54:24 +0000 | [diff] [blame] | 466 | if (!Config.SectionsToRename.empty()) { |
| 467 | for (auto &Sec : Obj.sections()) { |
| 468 | const auto Iter = Config.SectionsToRename.find(Sec.Name); |
Jordan Rupprecht | d67c1e1 | 2018-08-01 16:23:22 +0000 | [diff] [blame] | 469 | if (Iter != Config.SectionsToRename.end()) { |
| 470 | const SectionRename &SR = Iter->second; |
| 471 | Sec.Name = SR.NewName; |
| 472 | if (SR.NewFlags.hasValue()) { |
| 473 | // Preserve some flags which should not be dropped when setting flags. |
| 474 | // Also, preserve anything OS/processor dependant. |
| 475 | const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE | |
| 476 | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER | |
| 477 | ELF::SHF_MASKOS | ELF::SHF_MASKPROC | |
| 478 | ELF::SHF_TLS | ELF::SHF_INFO_LINK; |
| 479 | Sec.Flags = (Sec.Flags & PreserveMask) | |
| 480 | (SR.NewFlags.getValue() & ~PreserveMask); |
| 481 | } |
| 482 | } |
Jordan Rupprecht | db2036e | 2018-07-20 19:54:24 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 486 | if (!Config.AddSection.empty()) { |
| 487 | for (const auto &Flag : Config.AddSection) { |
| 488 | auto SecPair = Flag.split("="); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 489 | auto SecName = SecPair.first; |
| 490 | auto File = SecPair.second; |
| 491 | auto BufOrErr = MemoryBuffer::getFile(File); |
| 492 | if (!BufOrErr) |
| 493 | reportError(File, BufOrErr.getError()); |
| 494 | auto Buf = std::move(*BufOrErr); |
| 495 | auto BufPtr = reinterpret_cast<const uint8_t *>(Buf->getBufferStart()); |
| 496 | auto BufSize = Buf->getBufferSize(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 497 | Obj.addSection<OwnedDataSection>(SecName, |
| 498 | ArrayRef<uint8_t>(BufPtr, BufSize)); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Paul Semel | a42dec7 | 2018-08-09 17:05:21 +0000 | [diff] [blame] | 502 | if (!Config.DumpSection.empty()) { |
| 503 | for (const auto &Flag : Config.DumpSection) { |
| 504 | std::pair<StringRef, StringRef> SecPair = Flag.split("="); |
| 505 | StringRef SecName = SecPair.first; |
| 506 | StringRef File = SecPair.second; |
| 507 | if (Error E = dumpSectionToFile(SecName, File, Obj)) |
| 508 | reportError(Config.InputFilename, std::move(E)); |
| 509 | } |
| 510 | } |
| 511 | |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 512 | if (!Config.AddGnuDebugLink.empty()) |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 513 | Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 514 | } |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 515 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 516 | static void executeElfObjcopyOnBinary(const CopyConfig &Config, Reader &Reader, |
| 517 | Buffer &Out, ElfType OutputElfType) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 518 | std::unique_ptr<Object> Obj = Reader.create(); |
| 519 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 520 | handleArgs(Config, *Obj, Reader, OutputElfType); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 521 | |
| 522 | std::unique_ptr<Writer> Writer = |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 523 | createWriter(Config, *Obj, Out, OutputElfType); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 524 | Writer->finalize(); |
| 525 | Writer->write(); |
| 526 | } |
| 527 | |
| 528 | // For regular archives this function simply calls llvm::writeArchive, |
| 529 | // 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] | 530 | static Error deepWriteArchive(StringRef ArcName, |
| 531 | ArrayRef<NewArchiveMember> NewMembers, |
| 532 | bool WriteSymtab, object::Archive::Kind Kind, |
| 533 | bool Deterministic, bool Thin) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 534 | Error E = |
| 535 | writeArchive(ArcName, NewMembers, WriteSymtab, Kind, Deterministic, Thin); |
| 536 | if (!Thin || E) |
| 537 | return E; |
| 538 | for (const NewArchiveMember &Member : NewMembers) { |
| 539 | // Internally, FileBuffer will use the buffer created by |
| 540 | // FileOutputBuffer::create, for regular files (that is the case for |
| 541 | // deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer. |
| 542 | // OnDiskBuffer uses a temporary file and then renames it. So in reality |
| 543 | // there is no inefficiency / duplicated in-memory buffers in this case. For |
| 544 | // now in-memory buffers can not be completely avoided since |
| 545 | // NewArchiveMember still requires them even though writeArchive does not |
| 546 | // write them on disk. |
| 547 | FileBuffer FB(Member.MemberName); |
| 548 | FB.allocate(Member.Buf->getBufferSize()); |
| 549 | std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(), |
| 550 | FB.getBufferStart()); |
| 551 | if (auto E = FB.commit()) |
| 552 | return E; |
| 553 | } |
| 554 | return Error::success(); |
| 555 | } |
| 556 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 557 | static void executeElfObjcopyOnArchive(const CopyConfig &Config, |
Puyan Lotfi | 97604b4 | 2018-08-02 18:16:52 +0000 | [diff] [blame] | 558 | const Archive &Ar) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 559 | std::vector<NewArchiveMember> NewArchiveMembers; |
| 560 | Error Err = Error::success(); |
| 561 | for (const Archive::Child &Child : Ar.children(Err)) { |
| 562 | Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); |
| 563 | if (!ChildOrErr) |
| 564 | reportError(Ar.getFileName(), ChildOrErr.takeError()); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 565 | Binary *Bin = ChildOrErr->get(); |
| 566 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 567 | Expected<StringRef> ChildNameOrErr = Child.getName(); |
| 568 | if (!ChildNameOrErr) |
| 569 | reportError(Ar.getFileName(), ChildNameOrErr.takeError()); |
| 570 | |
| 571 | MemBuffer MB(ChildNameOrErr.get()); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 572 | ELFReader Reader(Bin); |
| 573 | executeElfObjcopyOnBinary(Config, Reader, MB, getOutputElfType(*Bin)); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 574 | |
| 575 | Expected<NewArchiveMember> Member = |
| 576 | NewArchiveMember::getOldMember(Child, true); |
| 577 | if (!Member) |
| 578 | reportError(Ar.getFileName(), Member.takeError()); |
| 579 | Member->Buf = MB.releaseMemoryBuffer(); |
| 580 | Member->MemberName = Member->Buf->getBufferIdentifier(); |
| 581 | NewArchiveMembers.push_back(std::move(*Member)); |
| 582 | } |
| 583 | |
| 584 | if (Err) |
| 585 | reportError(Config.InputFilename, std::move(Err)); |
| 586 | if (Error E = |
| 587 | deepWriteArchive(Config.OutputFilename, NewArchiveMembers, |
| 588 | Ar.hasSymbolTable(), Ar.kind(), true, Ar.isThin())) |
| 589 | reportError(Config.OutputFilename, std::move(E)); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 592 | static void restoreDateOnFile(StringRef Filename, |
| 593 | const sys::fs::file_status &Stat) { |
| 594 | int FD; |
| 595 | |
Jordan Rupprecht | 7481540 | 2018-08-29 23:21:56 +0000 | [diff] [blame] | 596 | if (auto EC = |
| 597 | sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting)) |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 598 | reportError(Filename, EC); |
| 599 | |
| 600 | if (auto EC = sys::fs::setLastAccessAndModificationTime( |
| 601 | FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime())) |
| 602 | reportError(Filename, EC); |
| 603 | |
| 604 | if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD)) |
| 605 | reportError(Filename, EC); |
| 606 | } |
| 607 | |
Jordan Rupprecht | 6b57539 | 2018-08-13 21:30:27 +0000 | [diff] [blame] | 608 | static void executeElfObjcopy(const CopyConfig &Config) { |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 609 | sys::fs::file_status Stat; |
| 610 | if (Config.PreserveDates) |
| 611 | if (auto EC = sys::fs::status(Config.InputFilename, Stat)) |
| 612 | reportError(Config.InputFilename, EC); |
| 613 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 614 | if (Config.InputFormat == "binary") { |
| 615 | auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename); |
| 616 | if (!BufOrErr) |
| 617 | reportError(Config.InputFilename, BufOrErr.getError()); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 618 | |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 619 | FileBuffer FB(Config.OutputFilename); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 620 | BinaryReader Reader(Config.BinaryArch, BufOrErr->get()); |
| 621 | executeElfObjcopyOnBinary(Config, Reader, FB, |
| 622 | getOutputElfType(Config.BinaryArch)); |
| 623 | } else { |
| 624 | Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr = |
| 625 | createBinary(Config.InputFilename); |
| 626 | if (!BinaryOrErr) |
| 627 | reportError(Config.InputFilename, BinaryOrErr.takeError()); |
| 628 | |
| 629 | if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) { |
| 630 | executeElfObjcopyOnArchive(Config, *Ar); |
| 631 | } else { |
| 632 | FileBuffer FB(Config.OutputFilename); |
| 633 | Binary *Bin = BinaryOrErr.get().getBinary(); |
| 634 | ELFReader Reader(Bin); |
| 635 | executeElfObjcopyOnBinary(Config, Reader, FB, getOutputElfType(*Bin)); |
| 636 | } |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 637 | } |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 638 | |
Jordan Rupprecht | d1767dc | 2018-08-16 18:29:40 +0000 | [diff] [blame] | 639 | if (Config.PreserveDates) { |
| 640 | restoreDateOnFile(Config.OutputFilename, Stat); |
| 641 | if (!Config.SplitDWO.empty()) |
| 642 | restoreDateOnFile(Config.SplitDWO, Stat); |
| 643 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 644 | } |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 645 | |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 646 | int main(int argc, char **argv) { |
| 647 | InitLLVM X(argc, argv); |
| 648 | ToolName = argv[0]; |
Jordan Rupprecht | 591d889 | 2018-09-05 13:10:03 +0000 | [diff] [blame] | 649 | DriverConfig DriverConfig; |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 650 | if (sys::path::stem(ToolName).endswith_lower("strip")) |
Jordan Rupprecht | 591d889 | 2018-09-05 13:10:03 +0000 | [diff] [blame] | 651 | DriverConfig = parseStripOptions(makeArrayRef(argv + 1, argc)); |
Alexander Shaposhnikov | cca6998 | 2018-05-07 19:32:09 +0000 | [diff] [blame] | 652 | else |
Jordan Rupprecht | 591d889 | 2018-09-05 13:10:03 +0000 | [diff] [blame] | 653 | DriverConfig = parseObjcopyOptions(makeArrayRef(argv + 1, argc)); |
| 654 | for (const CopyConfig &CopyConfig : DriverConfig.CopyConfigs) |
| 655 | executeElfObjcopy(CopyConfig); |
Alexander Shaposhnikov | d688479 | 2018-04-24 05:43:32 +0000 | [diff] [blame] | 656 | } |