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 | |
Sergey Dmitriev | cdeaac5 | 2019-07-26 17:06:41 +0000 | [diff] [blame] | 68 | // Adds named section with given contents to the object. |
| 69 | static void addSection(Object &Obj, StringRef Name, ArrayRef<uint8_t> Contents, |
| 70 | uint32_t Characteristics) { |
| 71 | bool NeedVA = Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | |
| 72 | IMAGE_SCN_MEM_WRITE); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 73 | |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 74 | Section Sec; |
Sergey Dmitriev | cdeaac5 | 2019-07-26 17:06:41 +0000 | [diff] [blame] | 75 | Sec.setOwnedContents(Contents); |
| 76 | Sec.Name = Name; |
| 77 | Sec.Header.VirtualSize = NeedVA ? Sec.getContents().size() : 0u; |
| 78 | Sec.Header.VirtualAddress = NeedVA ? getNextRVA(Obj) : 0u; |
| 79 | Sec.Header.SizeOfRawData = |
| 80 | NeedVA ? alignTo(Sec.Header.VirtualSize, |
| 81 | Obj.IsPE ? Obj.PeHeader.FileAlignment : 1) |
| 82 | : Sec.getContents().size(); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 83 | // Sec.Header.PointerToRawData is filled in by the writer. |
| 84 | Sec.Header.PointerToRelocations = 0; |
| 85 | Sec.Header.PointerToLinenumbers = 0; |
| 86 | // Sec.Header.NumberOfRelocations is filled in by the writer. |
| 87 | Sec.Header.NumberOfLinenumbers = 0; |
Sergey Dmitriev | cdeaac5 | 2019-07-26 17:06:41 +0000 | [diff] [blame] | 88 | Sec.Header.Characteristics = Characteristics; |
| 89 | |
| 90 | Obj.addSections(Sec); |
| 91 | } |
| 92 | |
| 93 | static void addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) { |
| 94 | std::vector<uint8_t> Contents = |
| 95 | createGnuDebugLinkSectionContents(DebugLinkFile); |
| 96 | addSection(Obj, ".gnu_debuglink", Contents, |
| 97 | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | |
| 98 | IMAGE_SCN_MEM_DISCARDABLE); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 101 | static Error handleArgs(const CopyConfig &Config, Object &Obj) { |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame] | 102 | // Perform the actual section removals. |
| 103 | Obj.removeSections([&Config](const Section &Sec) { |
Martin Storsjo | e830517 | 2019-01-19 19:42:54 +0000 | [diff] [blame] | 104 | // Contrary to --only-keep-debug, --only-section fully removes sections that |
| 105 | // aren't mentioned. |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame^] | 106 | if (!Config.OnlySection.empty() && !Config.OnlySection.matches(Sec.Name)) |
Martin Storsjo | e830517 | 2019-01-19 19:42:54 +0000 | [diff] [blame] | 107 | return true; |
| 108 | |
Martin Storsjo | 78a0b41 | 2019-01-19 19:42:41 +0000 | [diff] [blame] | 109 | if (Config.StripDebug || Config.StripAll || Config.StripAllGNU || |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 110 | Config.DiscardMode == DiscardType::All || Config.StripUnneeded) { |
Martin Storsjo | 78a0b41 | 2019-01-19 19:42:41 +0000 | [diff] [blame] | 111 | if (isDebugSection(Sec) && |
| 112 | (Sec.Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0) |
| 113 | return true; |
| 114 | } |
| 115 | |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame^] | 116 | if (Config.ToRemove.matches(Sec.Name)) |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame] | 117 | return true; |
| 118 | |
| 119 | return false; |
| 120 | }); |
| 121 | |
Martin Storsjo | 1868d88 | 2019-01-19 19:42:48 +0000 | [diff] [blame] | 122 | if (Config.OnlyKeepDebug) { |
| 123 | // For --only-keep-debug, we keep all other sections, but remove their |
| 124 | // content. The VirtualSize field in the section header is kept intact. |
| 125 | Obj.truncateSections([](const Section &Sec) { |
| 126 | return !isDebugSection(Sec) && Sec.Name != ".buildid" && |
| 127 | ((Sec.Header.Characteristics & |
| 128 | (IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA)) != 0); |
| 129 | }); |
| 130 | } |
| 131 | |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 132 | // StripAll removes all symbols and thus also removes all relocations. |
| 133 | if (Config.StripAll || Config.StripAllGNU) |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame] | 134 | for (Section &Sec : Obj.getMutableSections()) |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 135 | Sec.Relocs.clear(); |
| 136 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 137 | // If we need to do per-symbol removals, initialize the Referenced field. |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 138 | if (Config.StripUnneeded || Config.DiscardMode == DiscardType::All || |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 139 | !Config.SymbolsToRemove.empty()) |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 140 | if (Error E = Obj.markSymbols()) |
| 141 | return E; |
| 142 | |
| 143 | // Actually do removals of symbols. |
| 144 | Obj.removeSymbols([&](const Symbol &Sym) { |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 145 | // For StripAll, all relocations have been stripped and we remove all |
| 146 | // symbols. |
| 147 | if (Config.StripAll || Config.StripAllGNU) |
| 148 | return true; |
| 149 | |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame^] | 150 | if (Config.SymbolsToRemove.matches(Sym.Name)) { |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 151 | // Explicitly removing a referenced symbol is an error. |
| 152 | if (Sym.Referenced) |
| 153 | reportError(Config.OutputFilename, |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 154 | createStringError(llvm::errc::invalid_argument, |
| 155 | "not stripping symbol '%s' because it is " |
James Henderson | 5316a0d | 2019-05-22 13:23:26 +0000 | [diff] [blame] | 156 | "named in a relocation", |
Martin Storsjo | 8010c6be | 2019-01-22 10:57:59 +0000 | [diff] [blame] | 157 | Sym.Name.str().c_str())); |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 161 | if (!Sym.Referenced) { |
| 162 | // With --strip-unneeded, GNU objcopy removes all unreferenced local |
| 163 | // symbols, and any unreferenced undefined external. |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 164 | // With --strip-unneeded-symbol we strip only specific unreferenced |
| 165 | // local symbol instead of removing all of such. |
| 166 | if (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC || |
| 167 | Sym.Sym.SectionNumber == 0) |
| 168 | if (Config.StripUnneeded || |
Jordan Rupprecht | 6c6dd6a | 2019-08-22 19:17:50 +0000 | [diff] [blame^] | 169 | Config.UnneededSymbolsToRemove.matches(Sym.Name)) |
Eugene Leviant | 2db1062 | 2019-02-13 07:34:54 +0000 | [diff] [blame] | 170 | return true; |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 171 | |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 172 | // GNU objcopy keeps referenced local symbols and external symbols |
| 173 | // if --discard-all is set, similar to what --strip-unneeded does, |
| 174 | // but undefined local symbols are kept when --discard-all is set. |
Jordan Rupprecht | d0f7bcf | 2019-01-30 14:58:13 +0000 | [diff] [blame] | 175 | if (Config.DiscardMode == DiscardType::All && |
| 176 | Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC && |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 177 | Sym.Sym.SectionNumber != 0) |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 181 | return false; |
| 182 | }); |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 183 | |
Sergey Dmitriev | cdeaac5 | 2019-07-26 17:06:41 +0000 | [diff] [blame] | 184 | for (const auto &Flag : Config.AddSection) { |
| 185 | StringRef SecName, FileName; |
| 186 | std::tie(SecName, FileName) = Flag.split("="); |
| 187 | |
Sergey Dmitriev | cdeaac5 | 2019-07-26 17:06:41 +0000 | [diff] [blame] | 188 | auto BufOrErr = MemoryBuffer::getFile(FileName); |
| 189 | if (!BufOrErr) |
| 190 | return createFileError(FileName, errorCodeToError(BufOrErr.getError())); |
| 191 | auto Buf = std::move(*BufOrErr); |
| 192 | |
| 193 | addSection( |
| 194 | Obj, SecName, |
| 195 | makeArrayRef(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()), |
| 196 | Buf->getBufferSize()), |
| 197 | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_1BYTES); |
| 198 | } |
| 199 | |
Martin Storsjo | 12b6b80 | 2019-01-23 08:25:28 +0000 | [diff] [blame] | 200 | if (!Config.AddGnuDebugLink.empty()) |
| 201 | addGnuDebugLink(Obj, Config.AddGnuDebugLink); |
| 202 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 203 | if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() || |
| 204 | Config.BuildIdLinkInput || Config.BuildIdLinkOutput || |
| 205 | !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() || |
Sergey Dmitriev | cdeaac5 | 2019-07-26 17:06:41 +0000 | [diff] [blame] | 206 | !Config.AllocSectionsPrefix.empty() || !Config.DumpSection.empty() || |
| 207 | !Config.KeepSection.empty() || !Config.SymbolsToGlobalize.empty() || |
| 208 | !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() || |
| 209 | !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() || |
| 210 | !Config.SectionsToRename.empty() || !Config.SetSectionFlags.empty() || |
| 211 | !Config.SymbolsToRename.empty() || Config.ExtractDWO || |
| 212 | Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates || |
| 213 | Config.StripDWO || Config.StripNonAlloc || Config.StripSections || |
| 214 | Config.Weaken || Config.DecompressDebugSections || |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 215 | Config.DiscardMode == DiscardType::Locals || |
Eugene Leviant | 53350d0 | 2019-02-26 09:24:22 +0000 | [diff] [blame] | 216 | !Config.SymbolsToAdd.empty() || Config.EntryExpr) { |
Martin Storsjo | 0d19a39 | 2019-01-23 11:54:55 +0000 | [diff] [blame] | 217 | return createStringError(llvm::errc::invalid_argument, |
James Henderson | 5316a0d | 2019-05-22 13:23:26 +0000 | [diff] [blame] | 218 | "option not supported by llvm-objcopy for COFF"); |
Martin Storsjo | 0d19a39 | 2019-01-23 11:54:55 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 221 | return Error::success(); |
| 222 | } |
| 223 | |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 224 | Error executeObjcopyOnBinary(const CopyConfig &Config, COFFObjectFile &In, |
| 225 | Buffer &Out) { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 226 | COFFReader Reader(In); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 227 | Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create(); |
| 228 | if (!ObjOrErr) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 229 | return createFileError(Config.InputFilename, ObjOrErr.takeError()); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 230 | Object *Obj = ObjOrErr->get(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 231 | assert(Obj && "Unable to deserialize COFF object"); |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 232 | if (Error E = handleArgs(Config, *Obj)) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 233 | return createFileError(Config.InputFilename, std::move(E)); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 234 | COFFWriter Writer(*Obj, Out); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 235 | if (Error E = Writer.write()) |
Jordan Rupprecht | 307deab | 2019-01-30 14:36:53 +0000 | [diff] [blame] | 236 | return createFileError(Config.OutputFilename, std::move(E)); |
| 237 | return Error::success(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | } // end namespace coff |
| 241 | } // end namespace objcopy |
| 242 | } // end namespace llvm |