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