1 //===- llvm-objcopy.cpp ---------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Buffer.h"
10 #include "COFF/COFFObjcopy.h"
11 #include "CopyConfig.h"
12 #include "ELF/ELFObjcopy.h"
13 #include "MachO/MachOObjcopy.h"
14 #include "wasm/WasmObjcopy.h"
15
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/ArchiveWriter.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/ELFObjectFile.h"
25 #include "llvm/Object/ELFTypes.h"
26 #include "llvm/Object/Error.h"
27 #include "llvm/Object/MachO.h"
28 #include "llvm/Object/MachOUniversal.h"
29 #include "llvm/Object/Wasm.h"
30 #include "llvm/Option/Arg.h"
31 #include "llvm/Option/ArgList.h"
32 #include "llvm/Option/Option.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/ErrorOr.h"
38 #include "llvm/Support/Host.h"
39 #include "llvm/Support/InitLLVM.h"
40 #include "llvm/Support/Memory.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/Process.h"
43 #include "llvm/Support/StringSaver.h"
44 #include "llvm/Support/WithColor.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include <algorithm>
47 #include <cassert>
48 #include <cstdlib>
49 #include <memory>
50 #include <string>
51 #include <system_error>
52 #include <utility>
53
54 namespace llvm {
55 namespace objcopy {
56
57 // The name this program was invoked as.
58 StringRef ToolName;
59
reportWarning(Error E)60 ErrorSuccess reportWarning(Error E) {
61 assert(E);
62 WithColor::warning(errs(), ToolName) << toString(std::move(E)) << '\n';
63 return Error::success();
64 }
65
getDriverConfig(ArrayRef<const char * > Args)66 static Expected<DriverConfig> getDriverConfig(ArrayRef<const char *> Args) {
67 StringRef Stem = sys::path::stem(ToolName);
68 auto Is = [=](StringRef Tool) {
69 // We need to recognize the following filenames:
70 //
71 // llvm-objcopy -> objcopy
72 // strip-10.exe -> strip
73 // powerpc64-unknown-freebsd13-objcopy -> objcopy
74 // llvm-install-name-tool -> install-name-tool
75 auto I = Stem.rfind_lower(Tool);
76 return I != StringRef::npos &&
77 (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
78 };
79
80 if (Is("bitcode-strip") || Is("bitcode_strip"))
81 return parseBitcodeStripOptions(Args);
82 else if (Is("strip"))
83 return parseStripOptions(Args, reportWarning);
84 else if (Is("install-name-tool") || Is("install_name_tool"))
85 return parseInstallNameToolOptions(Args);
86 else
87 return parseObjcopyOptions(Args, reportWarning);
88 }
89
90 } // end namespace objcopy
91 } // end namespace llvm
92
93 using namespace llvm;
94 using namespace llvm::object;
95 using namespace llvm::objcopy;
96
97 // For regular archives this function simply calls llvm::writeArchive,
98 // For thin archives it writes the archive file itself as well as its members.
deepWriteArchive(StringRef ArcName,ArrayRef<NewArchiveMember> NewMembers,bool WriteSymtab,object::Archive::Kind Kind,bool Deterministic,bool Thin)99 static Error deepWriteArchive(StringRef ArcName,
100 ArrayRef<NewArchiveMember> NewMembers,
101 bool WriteSymtab, object::Archive::Kind Kind,
102 bool Deterministic, bool Thin) {
103 if (Error E = writeArchive(ArcName, NewMembers, WriteSymtab, Kind,
104 Deterministic, Thin))
105 return createFileError(ArcName, std::move(E));
106
107 if (!Thin)
108 return Error::success();
109
110 for (const NewArchiveMember &Member : NewMembers) {
111 // Internally, FileBuffer will use the buffer created by
112 // FileOutputBuffer::create, for regular files (that is the case for
113 // deepWriteArchive) FileOutputBuffer::create will return OnDiskBuffer.
114 // OnDiskBuffer uses a temporary file and then renames it. So in reality
115 // there is no inefficiency / duplicated in-memory buffers in this case. For
116 // now in-memory buffers can not be completely avoided since
117 // NewArchiveMember still requires them even though writeArchive does not
118 // write them on disk.
119 FileBuffer FB(Member.MemberName);
120 if (Error E = FB.allocate(Member.Buf->getBufferSize()))
121 return E;
122 std::copy(Member.Buf->getBufferStart(), Member.Buf->getBufferEnd(),
123 FB.getBufferStart());
124 if (Error E = FB.commit())
125 return E;
126 }
127 return Error::success();
128 }
129
130 /// The function executeObjcopyOnIHex does the dispatch based on the format
131 /// of the output specified by the command line options.
executeObjcopyOnIHex(CopyConfig & Config,MemoryBuffer & In,Buffer & Out)132 static Error executeObjcopyOnIHex(CopyConfig &Config, MemoryBuffer &In,
133 Buffer &Out) {
134 // TODO: support output formats other than ELF.
135 if (Error E = Config.parseELFConfig())
136 return E;
137 return elf::executeObjcopyOnIHex(Config, In, Out);
138 }
139
140 /// The function executeObjcopyOnRawBinary does the dispatch based on the format
141 /// of the output specified by the command line options.
executeObjcopyOnRawBinary(CopyConfig & Config,MemoryBuffer & In,Buffer & Out)142 static Error executeObjcopyOnRawBinary(CopyConfig &Config, MemoryBuffer &In,
143 Buffer &Out) {
144 switch (Config.OutputFormat) {
145 case FileFormat::ELF:
146 // FIXME: Currently, we call elf::executeObjcopyOnRawBinary even if the
147 // output format is binary/ihex or it's not given. This behavior differs from
148 // GNU objcopy. See https://bugs.llvm.org/show_bug.cgi?id=42171 for details.
149 case FileFormat::Binary:
150 case FileFormat::IHex:
151 case FileFormat::Unspecified:
152 if (Error E = Config.parseELFConfig())
153 return E;
154 return elf::executeObjcopyOnRawBinary(Config, In, Out);
155 }
156
157 llvm_unreachable("unsupported output format");
158 }
159
160 /// The function executeObjcopyOnBinary does the dispatch based on the format
161 /// of the input binary (ELF, MachO or COFF).
executeObjcopyOnBinary(CopyConfig & Config,object::Binary & In,Buffer & Out)162 static Error executeObjcopyOnBinary(CopyConfig &Config, object::Binary &In,
163 Buffer &Out) {
164 if (auto *ELFBinary = dyn_cast<object::ELFObjectFileBase>(&In)) {
165 if (Error E = Config.parseELFConfig())
166 return E;
167 return elf::executeObjcopyOnBinary(Config, *ELFBinary, Out);
168 } else if (auto *COFFBinary = dyn_cast<object::COFFObjectFile>(&In))
169 return coff::executeObjcopyOnBinary(Config, *COFFBinary, Out);
170 else if (auto *MachOBinary = dyn_cast<object::MachOObjectFile>(&In))
171 return macho::executeObjcopyOnBinary(Config, *MachOBinary, Out);
172 else if (auto *MachOUniversalBinary =
173 dyn_cast<object::MachOUniversalBinary>(&In))
174 return macho::executeObjcopyOnMachOUniversalBinary(
175 Config, *MachOUniversalBinary, Out);
176 else if (auto *WasmBinary = dyn_cast<object::WasmObjectFile>(&In))
177 return objcopy::wasm::executeObjcopyOnBinary(Config, *WasmBinary, Out);
178 else
179 return createStringError(object_error::invalid_file_type,
180 "unsupported object file format");
181 }
182
183 namespace llvm {
184 namespace objcopy {
185
186 Expected<std::vector<NewArchiveMember>>
createNewArchiveMembers(CopyConfig & Config,const Archive & Ar)187 createNewArchiveMembers(CopyConfig &Config, const Archive &Ar) {
188 std::vector<NewArchiveMember> NewArchiveMembers;
189 Error Err = Error::success();
190 for (const Archive::Child &Child : Ar.children(Err)) {
191 Expected<StringRef> ChildNameOrErr = Child.getName();
192 if (!ChildNameOrErr)
193 return createFileError(Ar.getFileName(), ChildNameOrErr.takeError());
194
195 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
196 if (!ChildOrErr)
197 return createFileError(Ar.getFileName() + "(" + *ChildNameOrErr + ")",
198 ChildOrErr.takeError());
199
200 MemBuffer MB(ChildNameOrErr.get());
201 if (Error E = executeObjcopyOnBinary(Config, *ChildOrErr->get(), MB))
202 return std::move(E);
203
204 Expected<NewArchiveMember> Member =
205 NewArchiveMember::getOldMember(Child, Config.DeterministicArchives);
206 if (!Member)
207 return createFileError(Ar.getFileName(), Member.takeError());
208 Member->Buf = MB.releaseMemoryBuffer();
209 Member->MemberName = Member->Buf->getBufferIdentifier();
210 NewArchiveMembers.push_back(std::move(*Member));
211 }
212 if (Err)
213 return createFileError(Config.InputFilename, std::move(Err));
214 return std::move(NewArchiveMembers);
215 }
216
217 } // end namespace objcopy
218 } // end namespace llvm
219
executeObjcopyOnArchive(CopyConfig & Config,const object::Archive & Ar)220 static Error executeObjcopyOnArchive(CopyConfig &Config,
221 const object::Archive &Ar) {
222 Expected<std::vector<NewArchiveMember>> NewArchiveMembersOrErr =
223 createNewArchiveMembers(Config, Ar);
224 if (!NewArchiveMembersOrErr)
225 return NewArchiveMembersOrErr.takeError();
226 return deepWriteArchive(Config.OutputFilename, *NewArchiveMembersOrErr,
227 Ar.hasSymbolTable(), Ar.kind(),
228 Config.DeterministicArchives, Ar.isThin());
229 }
230
restoreStatOnFile(StringRef Filename,const sys::fs::file_status & Stat,bool PreserveDates)231 static Error restoreStatOnFile(StringRef Filename,
232 const sys::fs::file_status &Stat,
233 bool PreserveDates) {
234 int FD;
235
236 // Writing to stdout should not be treated as an error here, just
237 // do not set access/modification times or permissions.
238 if (Filename == "-")
239 return Error::success();
240
241 if (auto EC =
242 sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
243 return createFileError(Filename, EC);
244
245 if (PreserveDates)
246 if (auto EC = sys::fs::setLastAccessAndModificationTime(
247 FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
248 return createFileError(Filename, EC);
249
250 sys::fs::file_status OStat;
251 if (std::error_code EC = sys::fs::status(FD, OStat))
252 return createFileError(Filename, EC);
253 if (OStat.type() == sys::fs::file_type::regular_file)
254 #ifdef _WIN32
255 if (auto EC = sys::fs::setPermissions(
256 Filename, static_cast<sys::fs::perms>(Stat.permissions() &
257 ~sys::fs::getUmask())))
258 #else
259 if (auto EC = sys::fs::setPermissions(
260 FD, static_cast<sys::fs::perms>(Stat.permissions() &
261 ~sys::fs::getUmask())))
262 #endif
263 return createFileError(Filename, EC);
264
265 if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
266 return createFileError(Filename, EC);
267
268 return Error::success();
269 }
270
271 /// The function executeObjcopy does the higher level dispatch based on the type
272 /// of input (raw binary, archive or single object file) and takes care of the
273 /// format-agnostic modifications, i.e. preserving dates.
executeObjcopy(CopyConfig & Config)274 static Error executeObjcopy(CopyConfig &Config) {
275 sys::fs::file_status Stat;
276 if (Config.InputFilename != "-") {
277 if (auto EC = sys::fs::status(Config.InputFilename, Stat))
278 return createFileError(Config.InputFilename, EC);
279 } else {
280 Stat.permissions(static_cast<sys::fs::perms>(0777));
281 }
282
283 using ProcessRawFn = Error (*)(CopyConfig &, MemoryBuffer &, Buffer &);
284 ProcessRawFn ProcessRaw;
285 switch (Config.InputFormat) {
286 case FileFormat::Binary:
287 ProcessRaw = executeObjcopyOnRawBinary;
288 break;
289 case FileFormat::IHex:
290 ProcessRaw = executeObjcopyOnIHex;
291 break;
292 default:
293 ProcessRaw = nullptr;
294 }
295
296 if (ProcessRaw) {
297 auto BufOrErr = MemoryBuffer::getFileOrSTDIN(Config.InputFilename);
298 if (!BufOrErr)
299 return createFileError(Config.InputFilename, BufOrErr.getError());
300 FileBuffer FB(Config.OutputFilename);
301 if (Error E = ProcessRaw(Config, *BufOrErr->get(), FB))
302 return E;
303 } else {
304 Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
305 createBinary(Config.InputFilename);
306 if (!BinaryOrErr)
307 return createFileError(Config.InputFilename, BinaryOrErr.takeError());
308
309 if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
310 if (Error E = executeObjcopyOnArchive(Config, *Ar))
311 return E;
312 } else {
313 FileBuffer FB(Config.OutputFilename);
314 if (Error E = executeObjcopyOnBinary(Config,
315 *BinaryOrErr.get().getBinary(), FB))
316 return E;
317 }
318 }
319
320 if (Error E =
321 restoreStatOnFile(Config.OutputFilename, Stat, Config.PreserveDates))
322 return E;
323
324 if (!Config.SplitDWO.empty()) {
325 Stat.permissions(static_cast<sys::fs::perms>(0666));
326 if (Error E =
327 restoreStatOnFile(Config.SplitDWO, Stat, Config.PreserveDates))
328 return E;
329 }
330
331 return Error::success();
332 }
333
334 namespace {
335
336 } // anonymous namespace
337
main(int argc,char ** argv)338 int main(int argc, char **argv) {
339 InitLLVM X(argc, argv);
340 ToolName = argv[0];
341
342 // Expand response files.
343 // TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp,
344 // into a separate function in the CommandLine library and call that function
345 // here. This is duplicated code.
346 SmallVector<const char *, 20> NewArgv(argv, argv + argc);
347 BumpPtrAllocator A;
348 StringSaver Saver(A);
349 cl::ExpandResponseFiles(Saver,
350 Triple(sys::getProcessTriple()).isOSWindows()
351 ? cl::TokenizeWindowsCommandLine
352 : cl::TokenizeGNUCommandLine,
353 NewArgv);
354
355 auto Args = makeArrayRef(NewArgv).drop_front();
356 Expected<DriverConfig> DriverConfig = getDriverConfig(Args);
357
358 if (!DriverConfig) {
359 logAllUnhandledErrors(DriverConfig.takeError(),
360 WithColor::error(errs(), ToolName));
361 return 1;
362 }
363 for (CopyConfig &CopyConfig : DriverConfig->CopyConfigs) {
364 if (Error E = executeObjcopy(CopyConfig)) {
365 logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
366 return 1;
367 }
368 }
369
370 return 0;
371 }
372