blob: dbdd44cc941267636f2a5306c9087955bd230e62 [file] [log] [blame]
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00001//===- MachOObjcopy.cpp -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth127252b2019-02-11 08:25:19 +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
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MachOObjcopy.h"
10#include "../CopyConfig.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000011#include "MachOReader.h"
12#include "MachOWriter.h"
Seiya Nuta4bc71012019-05-29 22:21:12 +000013#include "llvm/Support/Errc.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000014#include "llvm/Support/Error.h"
15
16namespace llvm {
17namespace objcopy {
18namespace macho {
19
Seiya Nuta4bc71012019-05-29 22:21:12 +000020using namespace object;
Seiya Nuta7f19dd12019-10-28 15:40:37 +090021using SectionPred = std::function<bool(const Section &Sec)>;
22
23static void removeSections(const CopyConfig &Config, Object &Obj) {
24 SectionPred RemovePred = [](const Section &) { return false; };
25
Seiya Nutabc118302019-11-15 12:37:55 +090026 if (!Config.ToRemove.empty()) {
27 RemovePred = [&Config, RemovePred](const Section &Sec) {
28 return Config.ToRemove.matches(Sec.CanonicalName);
29 };
30 }
31
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090032 if (Config.StripAll) {
33 // Remove all debug sections.
34 RemovePred = [RemovePred](const Section &Sec) {
35 if (Sec.Segname == "__DWARF")
36 return true;
37
38 return RemovePred(Sec);
39 };
40 }
41
Seiya Nuta7f19dd12019-10-28 15:40:37 +090042 if (!Config.OnlySection.empty()) {
Seiya Nutabc118302019-11-15 12:37:55 +090043 // Overwrite RemovePred because --only-section takes priority.
44 RemovePred = [&Config](const Section &Sec) {
Seiya Nuta7f19dd12019-10-28 15:40:37 +090045 return !Config.OnlySection.matches(Sec.CanonicalName);
46 };
47 }
48
49 return Obj.removeSections(RemovePred);
50}
Seiya Nuta4bc71012019-05-29 22:21:12 +000051
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090052static void markSymbols(const CopyConfig &Config, Object &Obj) {
53 // Symbols referenced from the indirect symbol table must not be removed.
54 for (IndirectSymbolEntry &ISE : Obj.IndirectSymTable.Symbols)
55 if (ISE.Symbol)
56 (*ISE.Symbol)->Referenced = true;
57}
58
Fangrui Song28a5dc72019-11-13 13:10:15 -080059static void updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
60 for (SymbolEntry &Sym : Obj.SymTable) {
61 auto I = Config.SymbolsToRename.find(Sym.Name);
62 if (I != Config.SymbolsToRename.end())
63 Sym.Name = I->getValue();
64 }
65
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090066 auto RemovePred = [Config](const std::unique_ptr<SymbolEntry> &N) {
67 if (N->Referenced)
68 return false;
69 return Config.StripAll;
70 };
71
72 Obj.SymTable.removeSymbols(RemovePred);
73}
74
Seiya Nuta4bc71012019-05-29 22:21:12 +000075static Error handleArgs(const CopyConfig &Config, Object &Obj) {
76 if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
77 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
78 !Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
79 !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() ||
80 !Config.DumpSection.empty() || !Config.KeepSection.empty() ||
Seiya Nuta7f19dd12019-10-28 15:40:37 +090081 Config.NewSymbolVisibility || !Config.SymbolsToGlobalize.empty() ||
82 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
83 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
Fangrui Song28a5dc72019-11-13 13:10:15 -080084 !Config.SectionsToRename.empty() ||
Seiya Nuta4bc71012019-05-29 22:21:12 +000085 !Config.UnneededSymbolsToRemove.empty() ||
Fangrui Song671fb342019-10-02 12:41:25 +000086 !Config.SetSectionAlignment.empty() || !Config.SetSectionFlags.empty() ||
Seiya Nutabc118302019-11-15 12:37:55 +090087 Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
88 Config.PreserveDates || Config.StripAllGNU || Config.StripDWO ||
89 Config.StripNonAlloc || Config.StripSections || Config.Weaken ||
90 Config.DecompressDebugSections || Config.StripDebug ||
Fangrui Song671fb342019-10-02 12:41:25 +000091 Config.StripNonAlloc || Config.StripSections || Config.StripUnneeded ||
92 Config.DiscardMode != DiscardType::None || !Config.SymbolsToAdd.empty() ||
93 Config.EntryExpr) {
Seiya Nuta4bc71012019-05-29 22:21:12 +000094 return createStringError(llvm::errc::invalid_argument,
95 "option not supported by llvm-objcopy for MachO");
96 }
Alexander Shaposhnikov7d83c292019-11-06 17:04:04 -080097
Seiya Nuta7f19dd12019-10-28 15:40:37 +090098 removeSections(Config, Obj);
Seiya Nuta9bbf2a12019-10-31 13:51:11 +090099
100 // Mark symbols to determine which symbols are still needed.
101 if (Config.StripAll)
102 markSymbols(Config, Obj);
103
Fangrui Song28a5dc72019-11-13 13:10:15 -0800104 updateAndRemoveSymbols(Config, Obj);
Seiya Nuta9bbf2a12019-10-31 13:51:11 +0900105
106 if (Config.StripAll)
107 for (LoadCommand &LC : Obj.LoadCommands)
108 for (Section &Sec : LC.Sections)
109 Sec.Relocations.clear();
110
Seiya Nuta4bc71012019-05-29 22:21:12 +0000111 return Error::success();
112}
113
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000114Error executeObjcopyOnBinary(const CopyConfig &Config,
115 object::MachOObjectFile &In, Buffer &Out) {
116 MachOReader Reader(In);
117 std::unique_ptr<Object> O = Reader.create();
Seiya Nuta4bc71012019-05-29 22:21:12 +0000118 if (!O)
119 return createFileError(
120 Config.InputFilename,
121 createStringError(object_error::parse_failed,
122 "unable to deserialize MachO object"));
123
124 if (Error E = handleArgs(Config, *O))
125 return createFileError(Config.InputFilename, std::move(E));
126
Seiya Nuta552bcb82019-08-19 21:05:31 +0000127 // TODO: Support 16KB pages which are employed in iOS arm64 binaries:
128 // https://github.com/llvm/llvm-project/commit/1bebb2832ee312d3b0316dacff457a7a29435edb
129 const uint64_t PageSize = 4096;
130
131 MachOWriter Writer(*O, In.is64Bit(), In.isLittleEndian(), PageSize, Out);
Seiya Nutab728e532019-06-08 01:22:54 +0000132 if (auto E = Writer.finalize())
133 return E;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000134 return Writer.write();
135}
136
137} // end namespace macho
138} // end namespace objcopy
139} // end namespace llvm