Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 1 | //===- COFFObjcopy.cpp ----------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "COFFObjcopy.h" |
| 10 | #include "Buffer.h" |
| 11 | #include "CopyConfig.h" |
| 12 | #include "Object.h" |
| 13 | #include "Reader.h" |
| 14 | #include "Writer.h" |
| 15 | #include "llvm-objcopy.h" |
| 16 | |
| 17 | #include "llvm/Object/Binary.h" |
| 18 | #include "llvm/Object/COFF.h" |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Errc.h" |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/JamCRC.h" |
| 21 | #include "llvm/Support/Path.h" |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 22 | #include <cassert> |
| 23 | |
| 24 | namespace llvm { |
| 25 | namespace objcopy { |
| 26 | namespace coff { |
| 27 | |
| 28 | using namespace object; |
| 29 | using namespace COFF; |
| 30 | |
Martin Storsjo | 78a0b41 | 2019-01-19 19:42:41 +0000 | [diff] [blame] | 31 | static bool isDebugSection(const Section &Sec) { |
| 32 | return Sec.Name.startswith(".debug"); |
| 33 | } |
| 34 | |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 35 | static uint64_t getNextRVA(const Object &Obj) { |
| 36 | if (Obj.getSections().empty()) |
| 37 | return 0; |
| 38 | const Section &Last = Obj.getSections().back(); |
| 39 | return alignTo(Last.Header.VirtualAddress + Last.Header.VirtualSize, |
Martin Storsjo | 1be9195 | 2019-01-23 11:54:51 +0000 | [diff] [blame] | 40 | Obj.IsPE ? Obj.PeHeader.SectionAlignment : 1); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static uint32_t getCRC32(StringRef Data) { |
| 44 | JamCRC CRC; |
| 45 | CRC.update(ArrayRef<char>(Data.data(), Data.size())); |
| 46 | // The CRC32 value needs to be complemented because the JamCRC dosn't |
| 47 | // finalize the CRC32 value. It also dosn't negate the initial CRC32 value |
| 48 | // but it starts by default at 0xFFFFFFFF which is the complement of zero. |
| 49 | return ~CRC.getCRC(); |
| 50 | } |
| 51 | |
| 52 | static std::vector<uint8_t> createGnuDebugLinkSectionContents(StringRef File) { |
| 53 | ErrorOr<std::unique_ptr<MemoryBuffer>> LinkTargetOrErr = |
| 54 | MemoryBuffer::getFile(File); |
| 55 | if (!LinkTargetOrErr) |
| 56 | error("'" + File + "': " + LinkTargetOrErr.getError().message()); |
| 57 | auto LinkTarget = std::move(*LinkTargetOrErr); |
| 58 | uint32_t CRC32 = getCRC32(LinkTarget->getBuffer()); |
| 59 | |
| 60 | StringRef FileName = sys::path::filename(File); |
| 61 | size_t CRCPos = alignTo(FileName.size() + 1, 4); |
| 62 | std::vector<uint8_t> Data(CRCPos + 4); |
| 63 | memcpy(Data.data(), FileName.data(), FileName.size()); |
| 64 | support::endian::write32le(Data.data() + CRCPos, CRC32); |
| 65 | return Data; |
| 66 | } |
| 67 | |
| 68 | static void addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) { |
| 69 | uint32_t StartRVA = getNextRVA(Obj); |
| 70 | |
| 71 | std::vector<Section> Sections; |
| 72 | Section Sec; |
| 73 | Sec.setOwnedContents(createGnuDebugLinkSectionContents(DebugLinkFile)); |
| 74 | Sec.Name = ".gnu_debuglink"; |
| 75 | Sec.Header.VirtualSize = Sec.getContents().size(); |
| 76 | Sec.Header.VirtualAddress = StartRVA; |
Martin Storsjo | 1be9195 | 2019-01-23 11:54:51 +0000 | [diff] [blame] | 77 | Sec.Header.SizeOfRawData = alignTo(Sec.Header.VirtualSize, |
| 78 | Obj.IsPE ? Obj.PeHeader.FileAlignment : 1); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 79 | // Sec.Header.PointerToRawData is filled in by the writer. |
| 80 | Sec.Header.PointerToRelocations = 0; |
| 81 | Sec.Header.PointerToLinenumbers = 0; |
| 82 | // Sec.Header.NumberOfRelocations is filled in by the writer. |
| 83 | Sec.Header.NumberOfLinenumbers = 0; |
| 84 | Sec.Header.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 85 | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE; |
| 86 | Sections.push_back(Sec); |
| 87 | Obj.addSections(Sections); |
| 88 | } |
| 89 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 90 | static Error handleArgs(const CopyConfig &Config, Object &Obj) { |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame] | 91 | // Perform the actual section removals. |
| 92 | Obj.removeSections([&Config](const Section &Sec) { |
Martin Storsjo | e830517 | 2019-01-19 19:42:54 +0000 | [diff] [blame] | 93 | // Contrary to --only-keep-debug, --only-section fully removes sections that |
| 94 | // aren't mentioned. |
| 95 | if (!Config.OnlySection.empty() && |
| 96 | !is_contained(Config.OnlySection, Sec.Name)) |
| 97 | return true; |
| 98 | |
Martin Storsjo | 78a0b41 | 2019-01-19 19:42:41 +0000 | [diff] [blame] | 99 | if (Config.StripDebug || Config.StripAll || Config.StripAllGNU || |
| 100 | Config.DiscardAll || Config.StripUnneeded) { |
| 101 | if (isDebugSection(Sec) && |
| 102 | (Sec.Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0) |
| 103 | return true; |
| 104 | } |
| 105 | |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame] | 106 | if (is_contained(Config.ToRemove, Sec.Name)) |
| 107 | return true; |
| 108 | |
| 109 | return false; |
| 110 | }); |
| 111 | |
Martin Storsjo | 1868d88 | 2019-01-19 19:42:48 +0000 | [diff] [blame] | 112 | if (Config.OnlyKeepDebug) { |
| 113 | // For --only-keep-debug, we keep all other sections, but remove their |
| 114 | // content. The VirtualSize field in the section header is kept intact. |
| 115 | Obj.truncateSections([](const Section &Sec) { |
| 116 | return !isDebugSection(Sec) && Sec.Name != ".buildid" && |
| 117 | ((Sec.Header.Characteristics & |
| 118 | (IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA)) != 0); |
| 119 | }); |
| 120 | } |
| 121 | |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 122 | // StripAll removes all symbols and thus also removes all relocations. |
| 123 | if (Config.StripAll || Config.StripAllGNU) |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame] | 124 | for (Section &Sec : Obj.getMutableSections()) |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 125 | Sec.Relocs.clear(); |
| 126 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 127 | // If we need to do per-symbol removals, initialize the Referenced field. |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 128 | if (Config.StripUnneeded || Config.DiscardAll || |
| 129 | !Config.SymbolsToRemove.empty()) |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 130 | if (Error E = Obj.markSymbols()) |
| 131 | return E; |
| 132 | |
| 133 | // Actually do removals of symbols. |
| 134 | Obj.removeSymbols([&](const Symbol &Sym) { |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 135 | // For StripAll, all relocations have been stripped and we remove all |
| 136 | // symbols. |
| 137 | if (Config.StripAll || Config.StripAllGNU) |
| 138 | return true; |
| 139 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 140 | if (is_contained(Config.SymbolsToRemove, Sym.Name)) { |
| 141 | // Explicitly removing a referenced symbol is an error. |
| 142 | if (Sym.Referenced) |
| 143 | reportError(Config.OutputFilename, |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 144 | createStringError(llvm::errc::invalid_argument, |
| 145 | "not stripping symbol '%s' because it is " |
| 146 | "named in a relocation.", |
| 147 | Sym.Name.str().c_str())); |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 151 | if (!Sym.Referenced) { |
| 152 | // With --strip-unneeded, GNU objcopy removes all unreferenced local |
| 153 | // symbols, and any unreferenced undefined external. |
| 154 | if (Config.StripUnneeded && |
| 155 | (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC || |
| 156 | Sym.Sym.SectionNumber == 0)) |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 157 | return true; |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 158 | |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 159 | // GNU objcopy keeps referenced local symbols and external symbols |
| 160 | // if --discard-all is set, similar to what --strip-unneeded does, |
| 161 | // but undefined local symbols are kept when --discard-all is set. |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 162 | if (Config.DiscardAll && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC && |
| 163 | Sym.Sym.SectionNumber != 0) |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 167 | return false; |
| 168 | }); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 169 | |
| 170 | if (!Config.AddGnuDebugLink.empty()) |
| 171 | addGnuDebugLink(Obj, Config.AddGnuDebugLink); |
| 172 | |
Martin Storsjo | 0d19a39 | 2019-01-23 11:54:55 +0000 | [diff] [blame] | 173 | if (!Config.BuildIdLinkDir.empty() || Config.BuildIdLinkInput || |
| 174 | Config.BuildIdLinkOutput || !Config.SplitDWO.empty() || |
| 175 | !Config.SymbolsPrefix.empty() || !Config.AddSection.empty() || |
| 176 | !Config.DumpSection.empty() || !Config.KeepSection.empty() || |
| 177 | !Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() || |
| 178 | !Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() || |
| 179 | !Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() || |
Jordan Rupprecht | c892741 | 2019-01-29 15:05:38 +0000 | [diff] [blame^] | 180 | !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() || |
| 181 | Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden || |
| 182 | Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc || |
| 183 | Config.StripSections || Config.Weaken || Config.DecompressDebugSections) { |
Martin Storsjo | 0d19a39 | 2019-01-23 11:54:55 +0000 | [diff] [blame] | 184 | return createStringError(llvm::errc::invalid_argument, |
| 185 | "Option not supported by llvm-objcopy for COFF"); |
| 186 | } |
| 187 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 188 | return Error::success(); |
| 189 | } |
| 190 | |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 191 | void executeObjcopyOnBinary(const CopyConfig &Config, |
Martin Storsjo | 1004242 | 2019-01-19 19:42:23 +0000 | [diff] [blame] | 192 | COFFObjectFile &In, Buffer &Out) { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 193 | COFFReader Reader(In); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 194 | Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create(); |
| 195 | if (!ObjOrErr) |
| 196 | reportError(Config.InputFilename, ObjOrErr.takeError()); |
| 197 | Object *Obj = ObjOrErr->get(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 198 | assert(Obj && "Unable to deserialize COFF object"); |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 199 | if (Error E = handleArgs(Config, *Obj)) |
| 200 | reportError(Config.InputFilename, std::move(E)); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 201 | COFFWriter Writer(*Obj, Out); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 202 | if (Error E = Writer.write()) |
| 203 | reportError(Config.OutputFilename, std::move(E)); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // end namespace coff |
| 207 | } // end namespace objcopy |
| 208 | } // end namespace llvm |