blob: 8d8f53d13d87fb6e4281f1b4a07295d92d005878 [file] [log] [blame]
Martin Storsjoe84a0b52018-12-19 07:24:38 +00001//===- COFFObjcopy.cpp ----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Storsjoe84a0b52018-12-19 07:24:38 +00006//
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 Storsjo10b72962019-01-10 21:28:24 +000019#include "llvm/Support/Errc.h"
Martin Storsjoe84a0b52018-12-19 07:24:38 +000020#include <cassert>
21
22namespace llvm {
23namespace objcopy {
24namespace coff {
25
26using namespace object;
27using namespace COFF;
28
Martin Storsjo78a0b412019-01-19 19:42:41 +000029static bool isDebugSection(const Section &Sec) {
30 return Sec.Name.startswith(".debug");
31}
32
Martin Storsjo10b72962019-01-10 21:28:24 +000033static Error handleArgs(const CopyConfig &Config, Object &Obj) {
Martin Storsjof9e14342019-01-19 19:42:35 +000034 // Perform the actual section removals.
35 Obj.removeSections([&Config](const Section &Sec) {
Martin Storsjoe8305172019-01-19 19:42:54 +000036 // Contrary to --only-keep-debug, --only-section fully removes sections that
37 // aren't mentioned.
38 if (!Config.OnlySection.empty() &&
39 !is_contained(Config.OnlySection, Sec.Name))
40 return true;
41
Martin Storsjo78a0b412019-01-19 19:42:41 +000042 if (Config.StripDebug || Config.StripAll || Config.StripAllGNU ||
43 Config.DiscardAll || Config.StripUnneeded) {
44 if (isDebugSection(Sec) &&
45 (Sec.Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0)
46 return true;
47 }
48
Martin Storsjof9e14342019-01-19 19:42:35 +000049 if (is_contained(Config.ToRemove, Sec.Name))
50 return true;
51
52 return false;
53 });
54
Martin Storsjo1868d882019-01-19 19:42:48 +000055 if (Config.OnlyKeepDebug) {
56 // For --only-keep-debug, we keep all other sections, but remove their
57 // content. The VirtualSize field in the section header is kept intact.
58 Obj.truncateSections([](const Section &Sec) {
59 return !isDebugSection(Sec) && Sec.Name != ".buildid" &&
60 ((Sec.Header.Characteristics &
61 (IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA)) != 0);
62 });
63 }
64
Martin Storsjof51f5ea2019-01-15 09:34:55 +000065 // StripAll removes all symbols and thus also removes all relocations.
66 if (Config.StripAll || Config.StripAllGNU)
Martin Storsjof9e14342019-01-19 19:42:35 +000067 for (Section &Sec : Obj.getMutableSections())
Martin Storsjof51f5ea2019-01-15 09:34:55 +000068 Sec.Relocs.clear();
69
Martin Storsjo10b72962019-01-10 21:28:24 +000070 // If we need to do per-symbol removals, initialize the Referenced field.
Martin Storsjofb909202019-01-11 14:13:04 +000071 if (Config.StripUnneeded || Config.DiscardAll ||
72 !Config.SymbolsToRemove.empty())
Martin Storsjo10b72962019-01-10 21:28:24 +000073 if (Error E = Obj.markSymbols())
74 return E;
75
76 // Actually do removals of symbols.
77 Obj.removeSymbols([&](const Symbol &Sym) {
Martin Storsjof51f5ea2019-01-15 09:34:55 +000078 // For StripAll, all relocations have been stripped and we remove all
79 // symbols.
80 if (Config.StripAll || Config.StripAllGNU)
81 return true;
82
Martin Storsjo10b72962019-01-10 21:28:24 +000083 if (is_contained(Config.SymbolsToRemove, Sym.Name)) {
84 // Explicitly removing a referenced symbol is an error.
85 if (Sym.Referenced)
86 reportError(Config.OutputFilename,
Martin Storsjo8010c6be2019-01-22 10:57:59 +000087 createStringError(llvm::errc::invalid_argument,
88 "not stripping symbol '%s' because it is "
89 "named in a relocation.",
90 Sym.Name.str().c_str()));
Martin Storsjo10b72962019-01-10 21:28:24 +000091 return true;
92 }
93
Martin Storsjo4b0694b2019-01-14 18:56:47 +000094 if (!Sym.Referenced) {
95 // With --strip-unneeded, GNU objcopy removes all unreferenced local
96 // symbols, and any unreferenced undefined external.
97 if (Config.StripUnneeded &&
98 (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC ||
99 Sym.Sym.SectionNumber == 0))
Martin Storsjofb909202019-01-11 14:13:04 +0000100 return true;
Martin Storsjo4b0694b2019-01-14 18:56:47 +0000101
Martin Storsjofb909202019-01-11 14:13:04 +0000102 // GNU objcopy keeps referenced local symbols and external symbols
103 // if --discard-all is set, similar to what --strip-unneeded does,
104 // but undefined local symbols are kept when --discard-all is set.
Martin Storsjo4b0694b2019-01-14 18:56:47 +0000105 if (Config.DiscardAll && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
106 Sym.Sym.SectionNumber != 0)
Martin Storsjofb909202019-01-11 14:13:04 +0000107 return true;
108 }
109
Martin Storsjo10b72962019-01-10 21:28:24 +0000110 return false;
111 });
112 return Error::success();
113}
114
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000115void executeObjcopyOnBinary(const CopyConfig &Config,
Martin Storsjo10042422019-01-19 19:42:23 +0000116 COFFObjectFile &In, Buffer &Out) {
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000117 COFFReader Reader(In);
Martin Storsjo0a5d5b12018-12-30 20:35:43 +0000118 Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create();
119 if (!ObjOrErr)
120 reportError(Config.InputFilename, ObjOrErr.takeError());
121 Object *Obj = ObjOrErr->get();
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000122 assert(Obj && "Unable to deserialize COFF object");
Martin Storsjo10b72962019-01-10 21:28:24 +0000123 if (Error E = handleArgs(Config, *Obj))
124 reportError(Config.InputFilename, std::move(E));
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000125 COFFWriter Writer(*Obj, Out);
Martin Storsjo0a5d5b12018-12-30 20:35:43 +0000126 if (Error E = Writer.write())
127 reportError(Config.OutputFilename, std::move(E));
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000128}
129
130} // end namespace coff
131} // end namespace objcopy
132} // end namespace llvm