blob: 60afbf7bb54d80f47f8cde379e07ee47c4c1675a [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 Storsjo78a0b412019-01-19 19:42:41 +000036 if (Config.StripDebug || Config.StripAll || Config.StripAllGNU ||
37 Config.DiscardAll || Config.StripUnneeded) {
38 if (isDebugSection(Sec) &&
39 (Sec.Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0)
40 return true;
41 }
42
Martin Storsjof9e14342019-01-19 19:42:35 +000043 if (is_contained(Config.ToRemove, Sec.Name))
44 return true;
45
46 return false;
47 });
48
Martin Storsjo1868d882019-01-19 19:42:48 +000049 if (Config.OnlyKeepDebug) {
50 // For --only-keep-debug, we keep all other sections, but remove their
51 // content. The VirtualSize field in the section header is kept intact.
52 Obj.truncateSections([](const Section &Sec) {
53 return !isDebugSection(Sec) && Sec.Name != ".buildid" &&
54 ((Sec.Header.Characteristics &
55 (IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA)) != 0);
56 });
57 }
58
Martin Storsjof51f5ea2019-01-15 09:34:55 +000059 // StripAll removes all symbols and thus also removes all relocations.
60 if (Config.StripAll || Config.StripAllGNU)
Martin Storsjof9e14342019-01-19 19:42:35 +000061 for (Section &Sec : Obj.getMutableSections())
Martin Storsjof51f5ea2019-01-15 09:34:55 +000062 Sec.Relocs.clear();
63
Martin Storsjo10b72962019-01-10 21:28:24 +000064 // If we need to do per-symbol removals, initialize the Referenced field.
Martin Storsjofb909202019-01-11 14:13:04 +000065 if (Config.StripUnneeded || Config.DiscardAll ||
66 !Config.SymbolsToRemove.empty())
Martin Storsjo10b72962019-01-10 21:28:24 +000067 if (Error E = Obj.markSymbols())
68 return E;
69
70 // Actually do removals of symbols.
71 Obj.removeSymbols([&](const Symbol &Sym) {
Martin Storsjof51f5ea2019-01-15 09:34:55 +000072 // For StripAll, all relocations have been stripped and we remove all
73 // symbols.
74 if (Config.StripAll || Config.StripAllGNU)
75 return true;
76
Martin Storsjo10b72962019-01-10 21:28:24 +000077 if (is_contained(Config.SymbolsToRemove, Sym.Name)) {
78 // Explicitly removing a referenced symbol is an error.
79 if (Sym.Referenced)
80 reportError(Config.OutputFilename,
81 make_error<StringError>(
82 "not stripping symbol '" + Sym.Name +
83 "' because it is named in a relocation.",
84 llvm::errc::invalid_argument));
85 return true;
86 }
87
Martin Storsjo4b0694b2019-01-14 18:56:47 +000088 if (!Sym.Referenced) {
89 // With --strip-unneeded, GNU objcopy removes all unreferenced local
90 // symbols, and any unreferenced undefined external.
91 if (Config.StripUnneeded &&
92 (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC ||
93 Sym.Sym.SectionNumber == 0))
Martin Storsjofb909202019-01-11 14:13:04 +000094 return true;
Martin Storsjo4b0694b2019-01-14 18:56:47 +000095
Martin Storsjofb909202019-01-11 14:13:04 +000096 // GNU objcopy keeps referenced local symbols and external symbols
97 // if --discard-all is set, similar to what --strip-unneeded does,
98 // but undefined local symbols are kept when --discard-all is set.
Martin Storsjo4b0694b2019-01-14 18:56:47 +000099 if (Config.DiscardAll && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
100 Sym.Sym.SectionNumber != 0)
Martin Storsjofb909202019-01-11 14:13:04 +0000101 return true;
102 }
103
Martin Storsjo10b72962019-01-10 21:28:24 +0000104 return false;
105 });
106 return Error::success();
107}
108
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000109void executeObjcopyOnBinary(const CopyConfig &Config,
Martin Storsjo10042422019-01-19 19:42:23 +0000110 COFFObjectFile &In, Buffer &Out) {
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000111 COFFReader Reader(In);
Martin Storsjo0a5d5b12018-12-30 20:35:43 +0000112 Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create();
113 if (!ObjOrErr)
114 reportError(Config.InputFilename, ObjOrErr.takeError());
115 Object *Obj = ObjOrErr->get();
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000116 assert(Obj && "Unable to deserialize COFF object");
Martin Storsjo10b72962019-01-10 21:28:24 +0000117 if (Error E = handleArgs(Config, *Obj))
118 reportError(Config.InputFilename, std::move(E));
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000119 COFFWriter Writer(*Obj, Out);
Martin Storsjo0a5d5b12018-12-30 20:35:43 +0000120 if (Error E = Writer.write())
121 reportError(Config.OutputFilename, std::move(E));
Martin Storsjoe84a0b52018-12-19 07:24:38 +0000122}
123
124} // end namespace coff
125} // end namespace objcopy
126} // end namespace llvm