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 | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 20 | #include <cassert> |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace objcopy { |
| 24 | namespace coff { |
| 25 | |
| 26 | using namespace object; |
| 27 | using namespace COFF; |
| 28 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 29 | static Error handleArgs(const CopyConfig &Config, Object &Obj) { |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame^] | 30 | // Perform the actual section removals. |
| 31 | Obj.removeSections([&Config](const Section &Sec) { |
| 32 | if (is_contained(Config.ToRemove, Sec.Name)) |
| 33 | return true; |
| 34 | |
| 35 | return false; |
| 36 | }); |
| 37 | |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 38 | // StripAll removes all symbols and thus also removes all relocations. |
| 39 | if (Config.StripAll || Config.StripAllGNU) |
Martin Storsjo | f9e1434 | 2019-01-19 19:42:35 +0000 | [diff] [blame^] | 40 | for (Section &Sec : Obj.getMutableSections()) |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 41 | Sec.Relocs.clear(); |
| 42 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 43 | // If we need to do per-symbol removals, initialize the Referenced field. |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 44 | if (Config.StripUnneeded || Config.DiscardAll || |
| 45 | !Config.SymbolsToRemove.empty()) |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 46 | if (Error E = Obj.markSymbols()) |
| 47 | return E; |
| 48 | |
| 49 | // Actually do removals of symbols. |
| 50 | Obj.removeSymbols([&](const Symbol &Sym) { |
Martin Storsjo | f51f5ea | 2019-01-15 09:34:55 +0000 | [diff] [blame] | 51 | // For StripAll, all relocations have been stripped and we remove all |
| 52 | // symbols. |
| 53 | if (Config.StripAll || Config.StripAllGNU) |
| 54 | return true; |
| 55 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 56 | if (is_contained(Config.SymbolsToRemove, Sym.Name)) { |
| 57 | // Explicitly removing a referenced symbol is an error. |
| 58 | if (Sym.Referenced) |
| 59 | reportError(Config.OutputFilename, |
| 60 | make_error<StringError>( |
| 61 | "not stripping symbol '" + Sym.Name + |
| 62 | "' because it is named in a relocation.", |
| 63 | llvm::errc::invalid_argument)); |
| 64 | return true; |
| 65 | } |
| 66 | |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 67 | if (!Sym.Referenced) { |
| 68 | // With --strip-unneeded, GNU objcopy removes all unreferenced local |
| 69 | // symbols, and any unreferenced undefined external. |
| 70 | if (Config.StripUnneeded && |
| 71 | (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC || |
| 72 | Sym.Sym.SectionNumber == 0)) |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 73 | return true; |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 74 | |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 75 | // GNU objcopy keeps referenced local symbols and external symbols |
| 76 | // if --discard-all is set, similar to what --strip-unneeded does, |
| 77 | // but undefined local symbols are kept when --discard-all is set. |
Martin Storsjo | 4b0694b | 2019-01-14 18:56:47 +0000 | [diff] [blame] | 78 | if (Config.DiscardAll && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC && |
| 79 | Sym.Sym.SectionNumber != 0) |
Martin Storsjo | fb90920 | 2019-01-11 14:13:04 +0000 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 83 | return false; |
| 84 | }); |
| 85 | return Error::success(); |
| 86 | } |
| 87 | |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 88 | void executeObjcopyOnBinary(const CopyConfig &Config, |
Martin Storsjo | 1004242 | 2019-01-19 19:42:23 +0000 | [diff] [blame] | 89 | COFFObjectFile &In, Buffer &Out) { |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 90 | COFFReader Reader(In); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 91 | Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create(); |
| 92 | if (!ObjOrErr) |
| 93 | reportError(Config.InputFilename, ObjOrErr.takeError()); |
| 94 | Object *Obj = ObjOrErr->get(); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 95 | assert(Obj && "Unable to deserialize COFF object"); |
Martin Storsjo | 10b7296 | 2019-01-10 21:28:24 +0000 | [diff] [blame] | 96 | if (Error E = handleArgs(Config, *Obj)) |
| 97 | reportError(Config.InputFilename, std::move(E)); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 98 | COFFWriter Writer(*Obj, Out); |
Martin Storsjo | 0a5d5b1 | 2018-12-30 20:35:43 +0000 | [diff] [blame] | 99 | if (Error E = Writer.write()) |
| 100 | reportError(Config.OutputFilename, std::move(E)); |
Martin Storsjo | e84a0b5 | 2018-12-19 07:24:38 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // end namespace coff |
| 104 | } // end namespace objcopy |
| 105 | } // end namespace llvm |