blob: b2e750d15f03f0881eeb3ad83bee9539aa45a1fa [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.
390 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty()) {
391 for (auto &Section : Obj.sections())
392 Section.markSymbols();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000393 }
394
George Rimare6963be2019-03-25 12:34:25 +0000395 auto RemoveSymbolsPred = [&](const Symbol &Sym) {
396 if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
397 (Config.KeepFileSymbols && Sym.Type == STT_FILE))
398 return false;
399
400 if ((Config.DiscardMode == DiscardType::All ||
401 (Config.DiscardMode == DiscardType::Locals &&
402 StringRef(Sym.Name).startswith(".L"))) &&
403 Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
404 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
405 return true;
406
407 if (Config.StripAll || Config.StripAllGNU)
408 return true;
409
410 if (is_contained(Config.SymbolsToRemove, Sym.Name))
411 return true;
412
413 if ((Config.StripUnneeded ||
414 is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) &&
415 isUnneededSymbol(Sym))
416 return true;
417
418 return false;
419 };
420
421 return Obj.removeSymbols(RemoveSymbolsPred);
422}
423
424static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000425 SectionPred RemovePred = [](const SectionBase &) { return false; };
426
427 // Removes:
428 if (!Config.ToRemove.empty()) {
429 RemovePred = [&Config](const SectionBase &Sec) {
430 return is_contained(Config.ToRemove, Sec.Name);
431 };
432 }
433
434 if (Config.StripDWO || !Config.SplitDWO.empty())
435 RemovePred = [RemovePred](const SectionBase &Sec) {
436 return isDWOSection(Sec) || RemovePred(Sec);
437 };
438
439 if (Config.ExtractDWO)
440 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
441 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
442 };
443
444 if (Config.StripAllGNU)
445 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
446 if (RemovePred(Sec))
447 return true;
448 if ((Sec.Flags & SHF_ALLOC) != 0)
449 return false;
450 if (&Sec == Obj.SectionNames)
451 return false;
452 switch (Sec.Type) {
453 case SHT_SYMTAB:
454 case SHT_REL:
455 case SHT_RELA:
456 case SHT_STRTAB:
457 return true;
458 }
459 return isDebugSection(Sec);
460 };
461
462 if (Config.StripSections) {
463 RemovePred = [RemovePred](const SectionBase &Sec) {
James Hendersonb5de5e22019-03-14 11:47:41 +0000464 return RemovePred(Sec) || Sec.ParentSegment == nullptr;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000465 };
466 }
467
468 if (Config.StripDebug) {
469 RemovePred = [RemovePred](const SectionBase &Sec) {
470 return RemovePred(Sec) || isDebugSection(Sec);
471 };
472 }
473
474 if (Config.StripNonAlloc)
475 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
476 if (RemovePred(Sec))
477 return true;
478 if (&Sec == Obj.SectionNames)
479 return false;
James Hendersonb5de5e22019-03-14 11:47:41 +0000480 return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000481 };
482
483 if (Config.StripAll)
484 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
485 if (RemovePred(Sec))
486 return true;
487 if (&Sec == Obj.SectionNames)
488 return false;
489 if (StringRef(Sec.Name).startswith(".gnu.warning"))
490 return false;
James Hendersonb5de5e22019-03-14 11:47:41 +0000491 if (Sec.ParentSegment != nullptr)
492 return false;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000493 return (Sec.Flags & SHF_ALLOC) == 0;
494 };
495
496 // Explicit copies:
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000497 if (!Config.OnlySection.empty()) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000498 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
499 // Explicitly keep these sections regardless of previous removes.
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000500 if (is_contained(Config.OnlySection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000501 return false;
502
503 // Allow all implicit removes.
504 if (RemovePred(Sec))
505 return true;
506
507 // Keep special sections.
508 if (Obj.SectionNames == &Sec)
509 return false;
510 if (Obj.SymbolTable == &Sec ||
511 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
512 return false;
513
514 // Remove everything else.
515 return true;
516 };
517 }
518
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000519 if (!Config.KeepSection.empty()) {
Fangrui Songe9f34b02018-11-12 23:46:22 +0000520 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000521 // Explicitly keep these sections regardless of previous removes.
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000522 if (is_contained(Config.KeepSection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000523 return false;
524 // Otherwise defer to RemovePred.
525 return RemovePred(Sec);
526 };
527 }
528
529 // This has to be the last predicate assignment.
530 // If the option --keep-symbol has been specified
531 // and at least one of those symbols is present
532 // (equivalently, the updated symbol table is not empty)
533 // the symbol table and the string table should not be removed.
534 if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
535 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
536 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
537 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
538 return false;
539 return RemovePred(Sec);
540 };
541 }
542
543 if (Config.CompressionType != DebugCompressionType::None)
James Henderson5316a0d2019-05-22 13:23:26 +0000544 replaceDebugSections(Obj, RemovePred, isCompressable,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000545 [&Config, &Obj](const SectionBase *S) {
546 return &Obj.addSection<CompressedSection>(
James Henderson5316a0d2019-05-22 13:23:26 +0000547 *S, Config.CompressionType);
548 });
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000549 else if (Config.DecompressDebugSections)
550 replaceDebugSections(
Fangrui Song3dfc3fb2019-03-15 10:27:28 +0000551 Obj, RemovePred,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000552 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
553 [&Obj](const SectionBase *S) {
554 auto CS = cast<CompressedSection>(S);
555 return &Obj.addSection<DecompressedSection>(*CS);
556 });
557
James Henderson66a9d0f2019-04-18 09:13:30 +0000558 return Obj.removeSections(Config.AllowBrokenLinks, RemovePred);
George Rimare6963be2019-03-25 12:34:25 +0000559}
560
561// This function handles the high level operations of GNU objcopy including
562// handling command line options. It's important to outline certain properties
563// we expect to hold of the command line operations. Any operation that "keeps"
564// should keep regardless of a remove. Additionally any removal should respect
565// any previous removals. Lastly whether or not something is removed shouldn't
566// depend a) on the order the options occur in or b) on some opaque priority
567// system. The only priority is that keeps/copies overrule removes.
568static Error handleArgs(const CopyConfig &Config, Object &Obj,
569 const Reader &Reader, ElfType OutputElfType) {
570
571 if (!Config.SplitDWO.empty())
572 if (Error E =
573 splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType))
574 return E;
575
576 if (Config.OutputArch) {
577 Obj.Machine = Config.OutputArch.getValue().EMachine;
578 Obj.OSABI = Config.OutputArch.getValue().OSABI;
579 }
580
George Rimar279898b2019-03-26 18:42:15 +0000581 // It is important to remove the sections first. For example, we want to
582 // remove the relocation sections before removing the symbols. That allows
583 // us to avoid reporting the inappropriate errors about removing symbols
584 // named in relocations.
585 if (Error E = replaceAndRemoveSections(Config, Obj))
George Rimare6963be2019-03-25 12:34:25 +0000586 return E;
587
George Rimar279898b2019-03-26 18:42:15 +0000588 if (Error E = updateAndRemoveSymbols(Config, Obj))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000589 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000590
James Hendersonfa11fb32019-05-08 09:49:35 +0000591 if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) {
592 DenseSet<SectionBase *> PrefixedSections;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000593 for (auto &Sec : Obj.sections()) {
594 const auto Iter = Config.SectionsToRename.find(Sec.Name);
595 if (Iter != Config.SectionsToRename.end()) {
596 const SectionRename &SR = Iter->second;
597 Sec.Name = SR.NewName;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000598 if (SR.NewFlags.hasValue())
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000599 setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000600 }
James Hendersonfa11fb32019-05-08 09:49:35 +0000601
602 // Add a prefix to allocated sections and their relocation sections. This
603 // should be done after renaming the section by Config.SectionToRename to
604 // imitate the GNU objcopy behavior.
605 if (!Config.AllocSectionsPrefix.empty()) {
606 if (Sec.Flags & SHF_ALLOC) {
607 Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
608 PrefixedSections.insert(&Sec);
609
James Henderson5316a0d2019-05-22 13:23:26 +0000610 // Rename relocation sections associated to the allocated sections.
611 // For example, if we rename .text to .prefix.text, we also rename
612 // .rel.text to .rel.prefix.text.
613 //
614 // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
615 // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
616 // .rela.prefix.plt since GNU objcopy does so.
James Hendersonfa11fb32019-05-08 09:49:35 +0000617 } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
618 auto *TargetSec = RelocSec->getSection();
619 if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
620 StringRef prefix;
621 switch (Sec.Type) {
622 case SHT_REL:
623 prefix = ".rel";
624 break;
625 case SHT_RELA:
626 prefix = ".rela";
627 break;
628 default:
629 continue;
630 }
631
632 // If the relocation section comes *after* the target section, we
633 // don't add Config.AllocSectionsPrefix because we've already added
634 // the prefix to TargetSec->Name. Otherwise, if the relocation
635 // section comes *before* the target section, we add the prefix.
636 if (PrefixedSections.count(TargetSec)) {
637 Sec.Name = (prefix + TargetSec->Name).str();
638 } else {
639 const auto Iter = Config.SectionsToRename.find(TargetSec->Name);
640 if (Iter != Config.SectionsToRename.end()) {
641 // Both `--rename-section` and `--prefix-alloc-sections` are
642 // given but the target section is not yet renamed.
643 Sec.Name =
644 (prefix + Config.AllocSectionsPrefix + Iter->second.NewName)
645 .str();
646 } else {
647 Sec.Name =
648 (prefix + Config.AllocSectionsPrefix + TargetSec->Name)
649 .str();
650 }
651 }
652 }
653 }
654 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000655 }
656 }
657
658 if (!Config.SetSectionFlags.empty()) {
659 for (auto &Sec : Obj.sections()) {
660 const auto Iter = Config.SetSectionFlags.find(Sec.Name);
661 if (Iter != Config.SetSectionFlags.end()) {
662 const SectionFlagsUpdate &SFU = Iter->second;
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000663 setSectionFlagsAndType(Sec, SFU.NewFlags);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000664 }
665 }
666 }
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000667
Eugene Leviantc76671b2019-03-12 12:41:06 +0000668 for (const auto &Flag : Config.AddSection) {
669 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
670 StringRef SecName = SecPair.first;
671 StringRef File = SecPair.second;
672 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
673 MemoryBuffer::getFile(File);
674 if (!BufOrErr)
675 return createFileError(File, errorCodeToError(BufOrErr.getError()));
676 std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
677 ArrayRef<uint8_t> Data(
678 reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
679 Buf->getBufferSize());
680 OwnedDataSection &NewSection =
681 Obj.addSection<OwnedDataSection>(SecName, Data);
682 if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
683 NewSection.Type = SHT_NOTE;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000684 }
685
Eugene Leviantc76671b2019-03-12 12:41:06 +0000686 for (const auto &Flag : Config.DumpSection) {
687 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
688 StringRef SecName = SecPair.first;
689 StringRef File = SecPair.second;
690 if (Error E = dumpSectionToFile(SecName, File, Obj))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000691 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000692 }
693
694 if (!Config.AddGnuDebugLink.empty())
James Henderson9df38832019-05-14 10:59:04 +0000695 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink,
696 Config.GnuDebugLinkCRC32);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000697
Eugene Leviant51c1f642019-02-25 14:12:41 +0000698 for (const NewSymbolInfo &SI : Config.SymbolsToAdd) {
699 SectionBase *Sec = Obj.findSection(SI.SectionName);
700 uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value;
Simon Pilgrim65706cf2019-02-27 10:19:53 +0000701 Obj.SymbolTable->addSymbol(
702 SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility,
703 Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
Eugene Leviant51c1f642019-02-25 14:12:41 +0000704 }
705
Eugene Leviant53350d02019-02-26 09:24:22 +0000706 if (Config.EntryExpr)
707 Obj.Entry = Config.EntryExpr(Obj.Entry);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000708 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000709}
710
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000711Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
712 Buffer &Out) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000713 BinaryReader Reader(Config.BinaryArch, &In);
714 std::unique_ptr<Object> Obj = Reader.create();
715
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000716 // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
717 // (-B<arch>).
718 const ElfType OutputElfType = getOutputElfType(
719 Config.OutputArch ? Config.OutputArch.getValue() : Config.BinaryArch);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000720 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
721 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000722 std::unique_ptr<Writer> Writer =
723 createWriter(Config, *Obj, Out, OutputElfType);
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000724 if (Error E = Writer->finalize())
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000725 return E;
726 return Writer->write();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000727}
728
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000729Error executeObjcopyOnBinary(const CopyConfig &Config,
730 object::ELFObjectFileBase &In, Buffer &Out) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000731 ELFReader Reader(&In);
732 std::unique_ptr<Object> Obj = Reader.create();
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000733 // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
734 const ElfType OutputElfType =
735 Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
736 : getOutputElfType(In);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000737 ArrayRef<uint8_t> BuildIdBytes;
738
739 if (!Config.BuildIdLinkDir.empty()) {
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000740 auto BuildIdBytesOrErr = findBuildID(Config, In);
741 if (auto E = BuildIdBytesOrErr.takeError())
742 return E;
743 BuildIdBytes = *BuildIdBytesOrErr;
744
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000745 if (BuildIdBytes.size() < 2)
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000746 return createFileError(
747 Config.InputFilename,
748 createStringError(object_error::parse_failed,
James Henderson5316a0d2019-05-22 13:23:26 +0000749 "build ID is smaller than two bytes"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000750 }
751
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000752 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput)
753 if (Error E =
754 linkToBuildIdDir(Config, Config.InputFilename,
755 Config.BuildIdLinkInput.getValue(), BuildIdBytes))
756 return E;
757
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000758 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000759 return createFileError(Config.InputFilename, std::move(E));
760
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000761 std::unique_ptr<Writer> Writer =
762 createWriter(Config, *Obj, Out, OutputElfType);
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000763 if (Error E = Writer->finalize())
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000764 return createFileError(Config.InputFilename, std::move(E));
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000765 if (Error E = Writer->write())
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000766 return E;
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000767 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
768 if (Error E =
769 linkToBuildIdDir(Config, Config.OutputFilename,
770 Config.BuildIdLinkOutput.getValue(), BuildIdBytes))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000771 return createFileError(Config.OutputFilename, std::move(E));
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000772
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000773 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000774}
775
776} // end namespace elf
777} // end namespace objcopy
778} // end namespace llvm