blob: be25bd5ee4391867986591afcb9d2ad6f4b65ce5 [file] [log] [blame]
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +00001//===- ELFObjcopy.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
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "ELFObjcopy.h"
10#include "Buffer.h"
11#include "CopyConfig.h"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000012#include "Object.h"
Jake Ehrlich8ad77792018-12-03 19:49:23 +000013#include "llvm-objcopy.h"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000014
15#include "llvm/ADT/BitmaskEnum.h"
James Hendersonfa11fb32019-05-08 09:49:35 +000016#include "llvm/ADT/DenseSet.h"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000017#include "llvm/ADT/Optional.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/BinaryFormat/ELF.h"
23#include "llvm/MC/MCTargetOptions.h"
24#include "llvm/Object/Binary.h"
25#include "llvm/Object/ELFObjectFile.h"
26#include "llvm/Object/ELFTypes.h"
27#include "llvm/Object/Error.h"
28#include "llvm/Option/Option.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/Compression.h"
Jake Ehrlich8ad77792018-12-03 19:49:23 +000031#include "llvm/Support/Errc.h"
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +000032#include "llvm/Support/Error.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/ErrorOr.h"
35#include "llvm/Support/Memory.h"
36#include "llvm/Support/Path.h"
37#include "llvm/Support/raw_ostream.h"
38#include <algorithm>
39#include <cassert>
40#include <cstdlib>
41#include <functional>
42#include <iterator>
43#include <memory>
44#include <string>
45#include <system_error>
46#include <utility>
47
48namespace llvm {
49namespace objcopy {
50namespace elf {
51
52using namespace object;
53using namespace ELF;
54using SectionPred = std::function<bool(const SectionBase &Sec)>;
55
56static bool isDebugSection(const SectionBase &Sec) {
57 return StringRef(Sec.Name).startswith(".debug") ||
58 StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index";
59}
60
61static bool isDWOSection(const SectionBase &Sec) {
62 return StringRef(Sec.Name).endswith(".dwo");
63}
64
65static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
66 // We can't remove the section header string table.
67 if (&Sec == Obj.SectionNames)
68 return false;
69 // Short of keeping the string table we want to keep everything that is a DWO
70 // section and remove everything else.
71 return !isDWOSection(Sec);
72}
73
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +000074uint64_t getNewShfFlags(SectionFlag AllFlags) {
75 uint64_t NewFlags = 0;
76 if (AllFlags & SectionFlag::SecAlloc)
77 NewFlags |= ELF::SHF_ALLOC;
78 if (!(AllFlags & SectionFlag::SecReadonly))
79 NewFlags |= ELF::SHF_WRITE;
80 if (AllFlags & SectionFlag::SecCode)
81 NewFlags |= ELF::SHF_EXECINSTR;
82 if (AllFlags & SectionFlag::SecMerge)
83 NewFlags |= ELF::SHF_MERGE;
84 if (AllFlags & SectionFlag::SecStrings)
85 NewFlags |= ELF::SHF_STRINGS;
86 return NewFlags;
87}
88
Jordan Rupprecht017deaf2019-04-02 16:49:56 +000089static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags,
Jordan Rupprechtc8927412019-01-29 15:05:38 +000090 uint64_t NewFlags) {
91 // Preserve some flags which should not be dropped when setting flags.
92 // Also, preserve anything OS/processor dependant.
93 const uint64_t PreserveMask = ELF::SHF_COMPRESSED | ELF::SHF_EXCLUDE |
94 ELF::SHF_GROUP | ELF::SHF_LINK_ORDER |
95 ELF::SHF_MASKOS | ELF::SHF_MASKPROC |
96 ELF::SHF_TLS | ELF::SHF_INFO_LINK;
97 return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
98}
99
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000100static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) {
101 Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(Flags));
102
Fangrui Songaa1f2c52019-05-01 00:39:31 +0000103 // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule
104 // may promote more non-ALLOC sections than GNU objcopy, but it is fine as
105 // non-ALLOC SHT_NOBITS sections do not make much sense.
106 if (Sec.Type == SHT_NOBITS &&
107 (!(Sec.Flags & ELF::SHF_ALLOC) ||
108 Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000109 Sec.Type = SHT_PROGBITS;
110}
111
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000112static ElfType getOutputElfType(const Binary &Bin) {
113 // Infer output ELF type from the input ELF object
114 if (isa<ELFObjectFile<ELF32LE>>(Bin))
115 return ELFT_ELF32LE;
116 if (isa<ELFObjectFile<ELF64LE>>(Bin))
117 return ELFT_ELF64LE;
118 if (isa<ELFObjectFile<ELF32BE>>(Bin))
119 return ELFT_ELF32BE;
120 if (isa<ELFObjectFile<ELF64BE>>(Bin))
121 return ELFT_ELF64BE;
122 llvm_unreachable("Invalid ELFType");
123}
124
125static ElfType getOutputElfType(const MachineInfo &MI) {
126 // Infer output ELF type from the binary arch specified
127 if (MI.Is64Bit)
128 return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
129 else
130 return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
131}
132
133static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
134 Object &Obj, Buffer &Buf,
135 ElfType OutputElfType) {
136 if (Config.OutputFormat == "binary") {
137 return llvm::make_unique<BinaryWriter>(Obj, Buf);
138 }
139 // Depending on the initial ELFT and OutputFormat we need a different Writer.
140 switch (OutputElfType) {
141 case ELFT_ELF32LE:
142 return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
143 !Config.StripSections);
144 case ELFT_ELF64LE:
145 return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
146 !Config.StripSections);
147 case ELFT_ELF32BE:
148 return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
149 !Config.StripSections);
150 case ELFT_ELF64BE:
151 return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
152 !Config.StripSections);
153 }
154 llvm_unreachable("Invalid output format");
155}
156
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000157template <class ELFT>
158static Expected<ArrayRef<uint8_t>>
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000159findBuildID(const CopyConfig &Config, const object::ELFFile<ELFT> &In) {
160 auto PhdrsOrErr = In.program_headers();
161 if (auto Err = PhdrsOrErr.takeError())
162 return createFileError(Config.InputFilename, std::move(Err));
163
164 for (const auto &Phdr : *PhdrsOrErr) {
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000165 if (Phdr.p_type != PT_NOTE)
166 continue;
167 Error Err = Error::success();
David Blaikieba005aa2018-12-11 00:09:06 +0000168 for (const auto &Note : In.notes(Phdr, Err))
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000169 if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
170 return Note.getDesc();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000171 if (Err)
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000172 return createFileError(Config.InputFilename, std::move(Err));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000173 }
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000174
175 return createFileError(
176 Config.InputFilename,
177 createStringError(llvm::errc::invalid_argument,
178 "could not find build ID"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000179}
180
181static Expected<ArrayRef<uint8_t>>
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000182findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) {
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000183 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000184 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000185 else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000186 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000187 else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000188 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000189 else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000190 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000191
192 llvm_unreachable("Bad file format");
193}
194
Jake Ehrlich5049c342019-03-18 20:35:18 +0000195template <class... Ts>
James Henderson5316a0d2019-05-22 13:23:26 +0000196static Error makeStringError(std::error_code EC, const Twine &Msg, Ts &&... Args) {
Jake Ehrlich5049c342019-03-18 20:35:18 +0000197 std::string FullMsg = (EC.message() + ": " + Msg).str();
198 return createStringError(EC, FullMsg.c_str(), std::forward<Ts>(Args)...);
199}
200
201#define MODEL_8 "%%%%%%%%"
202#define MODEL_16 MODEL_8 MODEL_8
203#define MODEL_32 (MODEL_16 MODEL_16)
204
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000205static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
206 StringRef Suffix,
207 ArrayRef<uint8_t> BuildIdBytes) {
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000208 SmallString<128> Path = Config.BuildIdLinkDir;
209 sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true));
210 if (auto EC = sys::fs::create_directories(Path))
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000211 return createFileError(
212 Path.str(),
Jake Ehrlich5049c342019-03-18 20:35:18 +0000213 makeStringError(EC, "cannot create build ID link directory"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000214
215 sys::path::append(Path,
216 llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true));
217 Path += Suffix;
Jake Ehrlich5049c342019-03-18 20:35:18 +0000218 SmallString<128> TmpPath;
219 // create_hard_link races so we need to link to a temporary path but
220 // we want to make sure that we choose a filename that does not exist.
221 // By using 32 model characters we get 128-bits of entropy. It is
222 // unlikely that this string has ever existed before much less exists
223 // on this disk or in the current working directory.
224 // Additionally we prepend the original Path for debugging but also
225 // because it ensures that we're linking within a directory on the same
226 // partition on the same device which is critical. It has the added
227 // win of yet further decreasing the odds of a conflict.
228 sys::fs::createUniquePath(Twine(Path) + "-" + MODEL_32 + ".tmp", TmpPath,
229 /*MakeAbsolute*/ false);
230 if (auto EC = sys::fs::create_hard_link(ToLink, TmpPath)) {
231 Path.push_back('\0');
James Henderson5316a0d2019-05-22 13:23:26 +0000232 return makeStringError(EC, "cannot link '%s' to '%s'", ToLink.data(),
233 Path.data());
Jake Ehrlich5049c342019-03-18 20:35:18 +0000234 }
235 // We then atomically rename the link into place which will just move the
236 // link. If rename fails something is more seriously wrong so just return
237 // an error.
238 if (auto EC = sys::fs::rename(TmpPath, Path)) {
239 Path.push_back('\0');
James Henderson5316a0d2019-05-22 13:23:26 +0000240 return makeStringError(EC, "cannot link '%s' to '%s'", ToLink.data(),
241 Path.data());
Jake Ehrlich5049c342019-03-18 20:35:18 +0000242 }
243 // If `Path` was already a hard-link to the same underlying file then the
244 // temp file will be left so we need to remove it. Remove will not cause
245 // an error by default if the file is already gone so just blindly remove
246 // it rather than checking.
247 if (auto EC = sys::fs::remove(TmpPath)) {
248 TmpPath.push_back('\0');
James Henderson5316a0d2019-05-22 13:23:26 +0000249 return makeStringError(EC, "could not remove '%s'", TmpPath.data());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000250 }
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000251 return Error::success();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000252}
253
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000254static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
255 StringRef File, ElfType OutputElfType) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000256 auto DWOFile = Reader.create();
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000257 auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) {
258 return onlyKeepDWOPred(*DWOFile, Sec);
259 };
James Henderson5316a0d2019-05-22 13:23:26 +0000260 if (Error E = DWOFile->removeSections(Config.AllowBrokenLinks,
James Henderson66a9d0f2019-04-18 09:13:30 +0000261 OnlyKeepDWOPred))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000262 return E;
James Hendersonc040d5d2019-03-22 10:21:09 +0000263 if (Config.OutputArch) {
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000264 DWOFile->Machine = Config.OutputArch.getValue().EMachine;
James Hendersonc040d5d2019-03-22 10:21:09 +0000265 DWOFile->OSABI = Config.OutputArch.getValue().OSABI;
266 }
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000267 FileBuffer FB(File);
268 auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000269 if (Error E = Writer->finalize())
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000270 return E;
271 return Writer->write();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000272}
273
274static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
275 Object &Obj) {
276 for (auto &Sec : Obj.sections()) {
277 if (Sec.Name == SecName) {
Jordan Rupprecht16a0de22018-12-20 00:57:06 +0000278 if (Sec.OriginalData.empty())
James Henderson5316a0d2019-05-22 13:23:26 +0000279 return createStringError(object_error::parse_failed,
280 "cannot dump section '%s': it has no contents",
281 SecName.str().c_str());
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000282 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
283 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
284 if (!BufferOrErr)
285 return BufferOrErr.takeError();
286 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
287 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
288 Buf->getBufferStart());
289 if (Error E = Buf->commit())
290 return E;
291 return Error::success();
292 }
293 }
James Henderson5316a0d2019-05-22 13:23:26 +0000294 return createStringError(object_error::parse_failed, "section '%s' not found",
295 SecName.str().c_str());
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000296}
297
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000298static bool isCompressable(const SectionBase &Section) {
George Rimarade3c702019-03-05 13:07:43 +0000299 return !(Section.Flags & ELF::SHF_COMPRESSED) &&
300 StringRef(Section.Name).startswith(".debug");
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000301}
302
303static void replaceDebugSections(
Fangrui Song3dfc3fb2019-03-15 10:27:28 +0000304 Object &Obj, SectionPred &RemovePred,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000305 function_ref<bool(const SectionBase &)> shouldReplace,
306 function_ref<SectionBase *(const SectionBase *)> addSection) {
George Rimard8a5c6c2019-03-11 11:01:24 +0000307 // Build a list of the debug sections we are going to replace.
308 // We can't call `addSection` while iterating over sections,
309 // because it would mutate the sections array.
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000310 SmallVector<SectionBase *, 13> ToReplace;
George Rimard8a5c6c2019-03-11 11:01:24 +0000311 for (auto &Sec : Obj.sections())
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000312 if (shouldReplace(Sec))
313 ToReplace.push_back(&Sec);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000314
George Rimard8a5c6c2019-03-11 11:01:24 +0000315 // Build a mapping from original section to a new one.
316 DenseMap<SectionBase *, SectionBase *> FromTo;
317 for (SectionBase *S : ToReplace)
318 FromTo[S] = addSection(S);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000319
George Rimard8a5c6c2019-03-11 11:01:24 +0000320 // Now we want to update the target sections of relocation
321 // sections. Also we will update the relocations themselves
322 // to update the symbol references.
323 for (auto &Sec : Obj.sections())
324 Sec.replaceSectionReferences(FromTo);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000325
326 RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
327 return shouldReplace(Sec) || RemovePred(Sec);
328 };
329}
330
Eugene Leviant2db10622019-02-13 07:34:54 +0000331static bool isUnneededSymbol(const Symbol &Sym) {
332 return !Sym.Referenced &&
333 (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
Eugene Leviantec767b02019-05-21 09:09:33 +0000334 Sym.Type != STT_SECTION;
Eugene Leviant2db10622019-02-13 07:34:54 +0000335}
336
George Rimare6963be2019-03-25 12:34:25 +0000337static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000338 // TODO: update or remove symbols only if there is an option that affects
339 // them.
George Rimare6963be2019-03-25 12:34:25 +0000340 if (!Obj.SymbolTable)
341 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000342
George Rimare6963be2019-03-25 12:34:25 +0000343 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
344 // Common and undefined symbols don't make sense as local symbols, and can
345 // even cause crashes if we localize those, so skip them.
346 if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
347 ((Config.LocalizeHidden &&
348 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
349 is_contained(Config.SymbolsToLocalize, Sym.Name)))
350 Sym.Binding = STB_LOCAL;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000351
George Rimare6963be2019-03-25 12:34:25 +0000352 // Note: these two globalize flags have very similar names but different
353 // meanings:
354 //
355 // --globalize-symbol: promote a symbol to global
356 // --keep-global-symbol: all symbols except for these should be made local
357 //
358 // If --globalize-symbol is specified for a given symbol, it will be
359 // global in the output file even if it is not included via
360 // --keep-global-symbol. Because of that, make sure to check
361 // --globalize-symbol second.
362 if (!Config.SymbolsToKeepGlobal.empty() &&
363 !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) &&
364 Sym.getShndx() != SHN_UNDEF)
365 Sym.Binding = STB_LOCAL;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000366
George Rimare6963be2019-03-25 12:34:25 +0000367 if (is_contained(Config.SymbolsToGlobalize, Sym.Name) &&
368 Sym.getShndx() != SHN_UNDEF)
369 Sym.Binding = STB_GLOBAL;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000370
George Rimare6963be2019-03-25 12:34:25 +0000371 if (is_contained(Config.SymbolsToWeaken, Sym.Name) &&
372 Sym.Binding == STB_GLOBAL)
373 Sym.Binding = STB_WEAK;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000374
George Rimare6963be2019-03-25 12:34:25 +0000375 if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
376 Sym.getShndx() != SHN_UNDEF)
377 Sym.Binding = STB_WEAK;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000378
George Rimare6963be2019-03-25 12:34:25 +0000379 const auto I = Config.SymbolsToRename.find(Sym.Name);
380 if (I != Config.SymbolsToRename.end())
381 Sym.Name = I->getValue();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000382
George Rimare6963be2019-03-25 12:34:25 +0000383 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
384 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
385 });
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000386
George Rimare6963be2019-03-25 12:34:25 +0000387 // The purpose of this loop is to mark symbols referenced by sections
388 // (like GroupSection or RelocationSection). This way, we know which
389 // symbols are still 'needed' and which are not.
George Rimarc1cc8d02019-05-24 15:04:50 +0000390 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
391 !Config.OnlySection.empty()) {
George Rimare6963be2019-03-25 12:34:25 +0000392 for (auto &Section : Obj.sections())
393 Section.markSymbols();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000394 }
395
George Rimare6963be2019-03-25 12:34:25 +0000396 auto RemoveSymbolsPred = [&](const Symbol &Sym) {
397 if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
398 (Config.KeepFileSymbols && Sym.Type == STT_FILE))
399 return false;
400
401 if ((Config.DiscardMode == DiscardType::All ||
402 (Config.DiscardMode == DiscardType::Locals &&
403 StringRef(Sym.Name).startswith(".L"))) &&
404 Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
405 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
406 return true;
407
408 if (Config.StripAll || Config.StripAllGNU)
409 return true;
410
411 if (is_contained(Config.SymbolsToRemove, Sym.Name))
412 return true;
413
414 if ((Config.StripUnneeded ||
415 is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) &&
416 isUnneededSymbol(Sym))
417 return true;
418
George Rimarc1cc8d02019-05-24 15:04:50 +0000419 // We want to remove undefined symbols if all references have been stripped.
420 if (!Config.OnlySection.empty() && !Sym.Referenced &&
421 Sym.getShndx() == SHN_UNDEF)
422 return true;
423
George Rimare6963be2019-03-25 12:34:25 +0000424 return false;
425 };
426
427 return Obj.removeSymbols(RemoveSymbolsPred);
428}
429
430static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000431 SectionPred RemovePred = [](const SectionBase &) { return false; };
432
433 // Removes:
434 if (!Config.ToRemove.empty()) {
435 RemovePred = [&Config](const SectionBase &Sec) {
436 return is_contained(Config.ToRemove, Sec.Name);
437 };
438 }
439
440 if (Config.StripDWO || !Config.SplitDWO.empty())
441 RemovePred = [RemovePred](const SectionBase &Sec) {
442 return isDWOSection(Sec) || RemovePred(Sec);
443 };
444
445 if (Config.ExtractDWO)
446 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
447 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
448 };
449
450 if (Config.StripAllGNU)
451 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
452 if (RemovePred(Sec))
453 return true;
454 if ((Sec.Flags & SHF_ALLOC) != 0)
455 return false;
456 if (&Sec == Obj.SectionNames)
457 return false;
458 switch (Sec.Type) {
459 case SHT_SYMTAB:
460 case SHT_REL:
461 case SHT_RELA:
462 case SHT_STRTAB:
463 return true;
464 }
465 return isDebugSection(Sec);
466 };
467
468 if (Config.StripSections) {
469 RemovePred = [RemovePred](const SectionBase &Sec) {
James Hendersonb5de5e22019-03-14 11:47:41 +0000470 return RemovePred(Sec) || Sec.ParentSegment == nullptr;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000471 };
472 }
473
474 if (Config.StripDebug) {
475 RemovePred = [RemovePred](const SectionBase &Sec) {
476 return RemovePred(Sec) || isDebugSection(Sec);
477 };
478 }
479
480 if (Config.StripNonAlloc)
481 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
482 if (RemovePred(Sec))
483 return true;
484 if (&Sec == Obj.SectionNames)
485 return false;
James Hendersonb5de5e22019-03-14 11:47:41 +0000486 return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000487 };
488
489 if (Config.StripAll)
490 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
491 if (RemovePred(Sec))
492 return true;
493 if (&Sec == Obj.SectionNames)
494 return false;
495 if (StringRef(Sec.Name).startswith(".gnu.warning"))
496 return false;
James Hendersonb5de5e22019-03-14 11:47:41 +0000497 if (Sec.ParentSegment != nullptr)
498 return false;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000499 return (Sec.Flags & SHF_ALLOC) == 0;
500 };
501
502 // Explicit copies:
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000503 if (!Config.OnlySection.empty()) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000504 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
505 // Explicitly keep these sections regardless of previous removes.
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000506 if (is_contained(Config.OnlySection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000507 return false;
508
509 // Allow all implicit removes.
510 if (RemovePred(Sec))
511 return true;
512
513 // Keep special sections.
514 if (Obj.SectionNames == &Sec)
515 return false;
516 if (Obj.SymbolTable == &Sec ||
517 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
518 return false;
519
520 // Remove everything else.
521 return true;
522 };
523 }
524
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000525 if (!Config.KeepSection.empty()) {
Fangrui Songe9f34b02018-11-12 23:46:22 +0000526 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000527 // Explicitly keep these sections regardless of previous removes.
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000528 if (is_contained(Config.KeepSection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000529 return false;
530 // Otherwise defer to RemovePred.
531 return RemovePred(Sec);
532 };
533 }
534
535 // This has to be the last predicate assignment.
536 // If the option --keep-symbol has been specified
537 // and at least one of those symbols is present
538 // (equivalently, the updated symbol table is not empty)
539 // the symbol table and the string table should not be removed.
540 if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
541 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
542 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
543 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
544 return false;
545 return RemovePred(Sec);
546 };
547 }
548
549 if (Config.CompressionType != DebugCompressionType::None)
James Henderson5316a0d2019-05-22 13:23:26 +0000550 replaceDebugSections(Obj, RemovePred, isCompressable,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000551 [&Config, &Obj](const SectionBase *S) {
552 return &Obj.addSection<CompressedSection>(
James Henderson5316a0d2019-05-22 13:23:26 +0000553 *S, Config.CompressionType);
554 });
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000555 else if (Config.DecompressDebugSections)
556 replaceDebugSections(
Fangrui Song3dfc3fb2019-03-15 10:27:28 +0000557 Obj, RemovePred,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000558 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
559 [&Obj](const SectionBase *S) {
560 auto CS = cast<CompressedSection>(S);
561 return &Obj.addSection<DecompressedSection>(*CS);
562 });
563
James Henderson66a9d0f2019-04-18 09:13:30 +0000564 return Obj.removeSections(Config.AllowBrokenLinks, RemovePred);
George Rimare6963be2019-03-25 12:34:25 +0000565}
566
567// This function handles the high level operations of GNU objcopy including
568// handling command line options. It's important to outline certain properties
569// we expect to hold of the command line operations. Any operation that "keeps"
570// should keep regardless of a remove. Additionally any removal should respect
571// any previous removals. Lastly whether or not something is removed shouldn't
572// depend a) on the order the options occur in or b) on some opaque priority
573// system. The only priority is that keeps/copies overrule removes.
574static Error handleArgs(const CopyConfig &Config, Object &Obj,
575 const Reader &Reader, ElfType OutputElfType) {
576
577 if (!Config.SplitDWO.empty())
578 if (Error E =
579 splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType))
580 return E;
581
582 if (Config.OutputArch) {
583 Obj.Machine = Config.OutputArch.getValue().EMachine;
584 Obj.OSABI = Config.OutputArch.getValue().OSABI;
585 }
586
George Rimar279898b2019-03-26 18:42:15 +0000587 // It is important to remove the sections first. For example, we want to
588 // remove the relocation sections before removing the symbols. That allows
589 // us to avoid reporting the inappropriate errors about removing symbols
590 // named in relocations.
591 if (Error E = replaceAndRemoveSections(Config, Obj))
George Rimare6963be2019-03-25 12:34:25 +0000592 return E;
593
George Rimar279898b2019-03-26 18:42:15 +0000594 if (Error E = updateAndRemoveSymbols(Config, Obj))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000595 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000596
James Hendersonfa11fb32019-05-08 09:49:35 +0000597 if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) {
598 DenseSet<SectionBase *> PrefixedSections;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000599 for (auto &Sec : Obj.sections()) {
600 const auto Iter = Config.SectionsToRename.find(Sec.Name);
601 if (Iter != Config.SectionsToRename.end()) {
602 const SectionRename &SR = Iter->second;
603 Sec.Name = SR.NewName;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000604 if (SR.NewFlags.hasValue())
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000605 setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000606 }
James Hendersonfa11fb32019-05-08 09:49:35 +0000607
608 // Add a prefix to allocated sections and their relocation sections. This
609 // should be done after renaming the section by Config.SectionToRename to
610 // imitate the GNU objcopy behavior.
611 if (!Config.AllocSectionsPrefix.empty()) {
612 if (Sec.Flags & SHF_ALLOC) {
613 Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
614 PrefixedSections.insert(&Sec);
615
James Henderson5316a0d2019-05-22 13:23:26 +0000616 // Rename relocation sections associated to the allocated sections.
617 // For example, if we rename .text to .prefix.text, we also rename
618 // .rel.text to .rel.prefix.text.
619 //
620 // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
621 // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
622 // .rela.prefix.plt since GNU objcopy does so.
James Hendersonfa11fb32019-05-08 09:49:35 +0000623 } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
624 auto *TargetSec = RelocSec->getSection();
625 if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
626 StringRef prefix;
627 switch (Sec.Type) {
628 case SHT_REL:
629 prefix = ".rel";
630 break;
631 case SHT_RELA:
632 prefix = ".rela";
633 break;
634 default:
635 continue;
636 }
637
638 // If the relocation section comes *after* the target section, we
639 // don't add Config.AllocSectionsPrefix because we've already added
640 // the prefix to TargetSec->Name. Otherwise, if the relocation
641 // section comes *before* the target section, we add the prefix.
642 if (PrefixedSections.count(TargetSec)) {
643 Sec.Name = (prefix + TargetSec->Name).str();
644 } else {
645 const auto Iter = Config.SectionsToRename.find(TargetSec->Name);
646 if (Iter != Config.SectionsToRename.end()) {
647 // Both `--rename-section` and `--prefix-alloc-sections` are
648 // given but the target section is not yet renamed.
649 Sec.Name =
650 (prefix + Config.AllocSectionsPrefix + Iter->second.NewName)
651 .str();
652 } else {
653 Sec.Name =
654 (prefix + Config.AllocSectionsPrefix + TargetSec->Name)
655 .str();
656 }
657 }
658 }
659 }
660 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000661 }
662 }
663
664 if (!Config.SetSectionFlags.empty()) {
665 for (auto &Sec : Obj.sections()) {
666 const auto Iter = Config.SetSectionFlags.find(Sec.Name);
667 if (Iter != Config.SetSectionFlags.end()) {
668 const SectionFlagsUpdate &SFU = Iter->second;
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000669 setSectionFlagsAndType(Sec, SFU.NewFlags);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000670 }
671 }
672 }
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000673
Eugene Leviantc76671b2019-03-12 12:41:06 +0000674 for (const auto &Flag : Config.AddSection) {
675 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
676 StringRef SecName = SecPair.first;
677 StringRef File = SecPair.second;
678 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
679 MemoryBuffer::getFile(File);
680 if (!BufOrErr)
681 return createFileError(File, errorCodeToError(BufOrErr.getError()));
682 std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
683 ArrayRef<uint8_t> Data(
684 reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
685 Buf->getBufferSize());
686 OwnedDataSection &NewSection =
687 Obj.addSection<OwnedDataSection>(SecName, Data);
688 if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
689 NewSection.Type = SHT_NOTE;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000690 }
691
Eugene Leviantc76671b2019-03-12 12:41:06 +0000692 for (const auto &Flag : Config.DumpSection) {
693 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
694 StringRef SecName = SecPair.first;
695 StringRef File = SecPair.second;
696 if (Error E = dumpSectionToFile(SecName, File, Obj))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000697 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000698 }
699
700 if (!Config.AddGnuDebugLink.empty())
James Henderson9df38832019-05-14 10:59:04 +0000701 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink,
702 Config.GnuDebugLinkCRC32);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000703
Eugene Leviant51c1f642019-02-25 14:12:41 +0000704 for (const NewSymbolInfo &SI : Config.SymbolsToAdd) {
705 SectionBase *Sec = Obj.findSection(SI.SectionName);
706 uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value;
Simon Pilgrim65706cf2019-02-27 10:19:53 +0000707 Obj.SymbolTable->addSymbol(
708 SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility,
709 Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
Eugene Leviant51c1f642019-02-25 14:12:41 +0000710 }
711
Eugene Leviant53350d02019-02-26 09:24:22 +0000712 if (Config.EntryExpr)
713 Obj.Entry = Config.EntryExpr(Obj.Entry);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000714 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000715}
716
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000717Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
718 Buffer &Out) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000719 BinaryReader Reader(Config.BinaryArch, &In);
720 std::unique_ptr<Object> Obj = Reader.create();
721
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000722 // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
723 // (-B<arch>).
724 const ElfType OutputElfType = getOutputElfType(
725 Config.OutputArch ? Config.OutputArch.getValue() : Config.BinaryArch);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000726 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
727 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000728 std::unique_ptr<Writer> Writer =
729 createWriter(Config, *Obj, Out, OutputElfType);
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000730 if (Error E = Writer->finalize())
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000731 return E;
732 return Writer->write();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000733}
734
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000735Error executeObjcopyOnBinary(const CopyConfig &Config,
736 object::ELFObjectFileBase &In, Buffer &Out) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000737 ELFReader Reader(&In);
738 std::unique_ptr<Object> Obj = Reader.create();
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000739 // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
740 const ElfType OutputElfType =
741 Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
742 : getOutputElfType(In);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000743 ArrayRef<uint8_t> BuildIdBytes;
744
745 if (!Config.BuildIdLinkDir.empty()) {
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000746 auto BuildIdBytesOrErr = findBuildID(Config, In);
747 if (auto E = BuildIdBytesOrErr.takeError())
748 return E;
749 BuildIdBytes = *BuildIdBytesOrErr;
750
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000751 if (BuildIdBytes.size() < 2)
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000752 return createFileError(
753 Config.InputFilename,
754 createStringError(object_error::parse_failed,
James Henderson5316a0d2019-05-22 13:23:26 +0000755 "build ID is smaller than two bytes"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000756 }
757
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000758 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput)
759 if (Error E =
760 linkToBuildIdDir(Config, Config.InputFilename,
761 Config.BuildIdLinkInput.getValue(), BuildIdBytes))
762 return E;
763
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000764 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000765 return createFileError(Config.InputFilename, std::move(E));
766
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000767 std::unique_ptr<Writer> Writer =
768 createWriter(Config, *Obj, Out, OutputElfType);
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000769 if (Error E = Writer->finalize())
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000770 return createFileError(Config.InputFilename, std::move(E));
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000771 if (Error E = Writer->write())
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000772 return E;
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000773 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
774 if (Error E =
775 linkToBuildIdDir(Config, Config.OutputFilename,
776 Config.BuildIdLinkOutput.getValue(), BuildIdBytes))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000777 return createFileError(Config.OutputFilename, std::move(E));
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000778
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000779 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000780}
781
782} // end namespace elf
783} // end namespace objcopy
784} // end namespace llvm