blob: 437dccbd3d5f338adba05064d8fdedeb80dea296 [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 Storsjo10b72962019-01-10 21:28:24 +000029static Error handleArgs(const CopyConfig &Config, Object &Obj) {
Martin Storsjof51f5ea2019-01-15 09:34:55 +000030 // StripAll removes all symbols and thus also removes all relocations.
31 if (Config.StripAll || Config.StripAllGNU)
32 for (Section &Sec : Obj.Sections)
33 Sec.Relocs.clear();
34
Martin Storsjo10b72962019-01-10 21:28:24 +000035 // If we need to do per-symbol removals, initialize the Referenced field.
Martin Storsjofb909202019-01-11 14:13:04 +000036 if (Config.StripUnneeded || Config.DiscardAll ||
37 !Config.SymbolsToRemove.empty())
Martin Storsjo10b72962019-01-10 21:28:24 +000038 if (Error E = Obj.markSymbols())
39 return E;
40
41 // Actually do removals of symbols.
42 Obj.removeSymbols([&](const Symbol &Sym) {
Martin Storsjof51f5ea2019-01-15 09:34:55 +000043 // For StripAll, all relocations have been stripped and we remove all
44 // symbols.
45 if (Config.StripAll || Config.StripAllGNU)
46 return true;
47
Martin Storsjo10b72962019-01-10 21:28:24 +000048 if (is_contained(Config.SymbolsToRemove, Sym.Name)) {
49 // Explicitly removing a referenced symbol is an error.
50 if (Sym.Referenced)
51 reportError(Config.OutputFilename,
52 make_error<StringError>(
53 "not stripping symbol '" + Sym.Name +
54 "' because it is named in a relocation.",
55 llvm::errc::invalid_argument));
56 return true;
57 }
58
Martin Storsjo4b0694b2019-01-14 18:56:47 +000059 if (!Sym.Referenced) {
60 // With --strip-unneeded, GNU objcopy removes all unreferenced local
61 // symbols, and any unreferenced undefined external.
62 if (Config.StripUnneeded &&
63 (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC ||
64 Sym.Sym.SectionNumber == 0))
Martin Storsjofb909202019-01-11 14:13:04 +000065 return true;
Martin Storsjo4b0694b2019-01-14 18:56:47 +000066
Martin Storsjofb909202019-01-11 14:13:04 +000067 // GNU objcopy keeps referenced local symbols and external symbols
68 // if --discard-all is set, similar to what --strip-unneeded does,
69 // but undefined local symbols are kept when --discard-all is set.
Martin Storsjo4b0694b2019-01-14 18:56:47 +000070 if (Config.DiscardAll && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
71 Sym.Sym.SectionNumber != 0)
Martin Storsjofb909202019-01-11 14:13:04 +000072 return true;
73 }
74
Martin Storsjo10b72962019-01-10 21:28:24 +000075 return false;
76 });
77 return Error::success();
78}
79
Martin Storsjoe84a0b52018-12-19 07:24:38 +000080void executeObjcopyOnBinary(const CopyConfig &Config,
Martin Storsjo10042422019-01-19 19:42:23 +000081 COFFObjectFile &In, Buffer &Out) {
Martin Storsjoe84a0b52018-12-19 07:24:38 +000082 COFFReader Reader(In);
Martin Storsjo0a5d5b12018-12-30 20:35:43 +000083 Expected<std::unique_ptr<Object>> ObjOrErr = Reader.create();
84 if (!ObjOrErr)
85 reportError(Config.InputFilename, ObjOrErr.takeError());
86 Object *Obj = ObjOrErr->get();
Martin Storsjoe84a0b52018-12-19 07:24:38 +000087 assert(Obj && "Unable to deserialize COFF object");
Martin Storsjo10b72962019-01-10 21:28:24 +000088 if (Error E = handleArgs(Config, *Obj))
89 reportError(Config.InputFilename, std::move(E));
Martin Storsjoe84a0b52018-12-19 07:24:38 +000090 COFFWriter Writer(*Obj, Out);
Martin Storsjo0a5d5b12018-12-30 20:35:43 +000091 if (Error E = Writer.write())
92 reportError(Config.OutputFilename, std::move(E));
Martin Storsjoe84a0b52018-12-19 07:24:38 +000093}
94
95} // end namespace coff
96} // end namespace objcopy
97} // end namespace llvm