blob: efb8f0582ec3459cef6bb0560969a7e65831a8bb [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
Eugene Levianta6fb1832019-05-29 11:37:16 +0000133static std::unique_ptr<Writer> createELFWriter(const CopyConfig &Config,
134 Object &Obj, Buffer &Buf,
135 ElfType OutputElfType) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000136 // Depending on the initial ELFT and OutputFormat we need a different Writer.
137 switch (OutputElfType) {
138 case ELFT_ELF32LE:
139 return llvm::make_unique<ELFWriter<ELF32LE>>(Obj, Buf,
140 !Config.StripSections);
141 case ELFT_ELF64LE:
142 return llvm::make_unique<ELFWriter<ELF64LE>>(Obj, Buf,
143 !Config.StripSections);
144 case ELFT_ELF32BE:
145 return llvm::make_unique<ELFWriter<ELF32BE>>(Obj, Buf,
146 !Config.StripSections);
147 case ELFT_ELF64BE:
148 return llvm::make_unique<ELFWriter<ELF64BE>>(Obj, Buf,
149 !Config.StripSections);
150 }
151 llvm_unreachable("Invalid output format");
152}
153
Eugene Levianta6fb1832019-05-29 11:37:16 +0000154static std::unique_ptr<Writer> createWriter(const CopyConfig &Config,
155 Object &Obj, Buffer &Buf,
156 ElfType OutputElfType) {
157 using Functor = std::function<std::unique_ptr<Writer>()>;
158 return StringSwitch<Functor>(Config.OutputFormat)
159 .Case("binary", [&] { return llvm::make_unique<BinaryWriter>(Obj, Buf); })
160 .Case("ihex", [&] { return llvm::make_unique<IHexWriter>(Obj, Buf); })
161 .Default(
162 [&] { return createELFWriter(Config, Obj, Buf, OutputElfType); })();
163}
164
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000165template <class ELFT>
166static Expected<ArrayRef<uint8_t>>
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000167findBuildID(const CopyConfig &Config, const object::ELFFile<ELFT> &In) {
168 auto PhdrsOrErr = In.program_headers();
169 if (auto Err = PhdrsOrErr.takeError())
170 return createFileError(Config.InputFilename, std::move(Err));
171
172 for (const auto &Phdr : *PhdrsOrErr) {
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000173 if (Phdr.p_type != PT_NOTE)
174 continue;
175 Error Err = Error::success();
David Blaikieba005aa2018-12-11 00:09:06 +0000176 for (const auto &Note : In.notes(Phdr, Err))
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000177 if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU)
178 return Note.getDesc();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000179 if (Err)
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000180 return createFileError(Config.InputFilename, std::move(Err));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000181 }
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000182
183 return createFileError(
184 Config.InputFilename,
185 createStringError(llvm::errc::invalid_argument,
186 "could not find build ID"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000187}
188
189static Expected<ArrayRef<uint8_t>>
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000190findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) {
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000191 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000192 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000193 else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000194 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000195 else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000196 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000197 else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(&In))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000198 return findBuildID(Config, *O->getELFFile());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000199
200 llvm_unreachable("Bad file format");
201}
202
Jake Ehrlich5049c342019-03-18 20:35:18 +0000203template <class... Ts>
James Henderson5316a0d2019-05-22 13:23:26 +0000204static Error makeStringError(std::error_code EC, const Twine &Msg, Ts &&... Args) {
Jake Ehrlich5049c342019-03-18 20:35:18 +0000205 std::string FullMsg = (EC.message() + ": " + Msg).str();
206 return createStringError(EC, FullMsg.c_str(), std::forward<Ts>(Args)...);
207}
208
209#define MODEL_8 "%%%%%%%%"
210#define MODEL_16 MODEL_8 MODEL_8
211#define MODEL_32 (MODEL_16 MODEL_16)
212
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000213static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink,
214 StringRef Suffix,
215 ArrayRef<uint8_t> BuildIdBytes) {
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000216 SmallString<128> Path = Config.BuildIdLinkDir;
217 sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true));
218 if (auto EC = sys::fs::create_directories(Path))
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000219 return createFileError(
220 Path.str(),
Jake Ehrlich5049c342019-03-18 20:35:18 +0000221 makeStringError(EC, "cannot create build ID link directory"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000222
223 sys::path::append(Path,
224 llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true));
225 Path += Suffix;
Jake Ehrlich5049c342019-03-18 20:35:18 +0000226 SmallString<128> TmpPath;
227 // create_hard_link races so we need to link to a temporary path but
228 // we want to make sure that we choose a filename that does not exist.
229 // By using 32 model characters we get 128-bits of entropy. It is
230 // unlikely that this string has ever existed before much less exists
231 // on this disk or in the current working directory.
232 // Additionally we prepend the original Path for debugging but also
233 // because it ensures that we're linking within a directory on the same
234 // partition on the same device which is critical. It has the added
235 // win of yet further decreasing the odds of a conflict.
236 sys::fs::createUniquePath(Twine(Path) + "-" + MODEL_32 + ".tmp", TmpPath,
237 /*MakeAbsolute*/ false);
238 if (auto EC = sys::fs::create_hard_link(ToLink, TmpPath)) {
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 // We then atomically rename the link into place which will just move the
244 // link. If rename fails something is more seriously wrong so just return
245 // an error.
246 if (auto EC = sys::fs::rename(TmpPath, Path)) {
247 Path.push_back('\0');
James Henderson5316a0d2019-05-22 13:23:26 +0000248 return makeStringError(EC, "cannot link '%s' to '%s'", ToLink.data(),
249 Path.data());
Jake Ehrlich5049c342019-03-18 20:35:18 +0000250 }
251 // If `Path` was already a hard-link to the same underlying file then the
252 // temp file will be left so we need to remove it. Remove will not cause
253 // an error by default if the file is already gone so just blindly remove
254 // it rather than checking.
255 if (auto EC = sys::fs::remove(TmpPath)) {
256 TmpPath.push_back('\0');
James Henderson5316a0d2019-05-22 13:23:26 +0000257 return makeStringError(EC, "could not remove '%s'", TmpPath.data());
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000258 }
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000259 return Error::success();
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000260}
261
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000262static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader,
263 StringRef File, ElfType OutputElfType) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000264 auto DWOFile = Reader.create();
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000265 auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) {
266 return onlyKeepDWOPred(*DWOFile, Sec);
267 };
James Henderson5316a0d2019-05-22 13:23:26 +0000268 if (Error E = DWOFile->removeSections(Config.AllowBrokenLinks,
James Henderson66a9d0f2019-04-18 09:13:30 +0000269 OnlyKeepDWOPred))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000270 return E;
James Hendersonc040d5d2019-03-22 10:21:09 +0000271 if (Config.OutputArch) {
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000272 DWOFile->Machine = Config.OutputArch.getValue().EMachine;
James Hendersonc040d5d2019-03-22 10:21:09 +0000273 DWOFile->OSABI = Config.OutputArch.getValue().OSABI;
274 }
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000275 FileBuffer FB(File);
276 auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType);
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000277 if (Error E = Writer->finalize())
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000278 return E;
279 return Writer->write();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000280}
281
282static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
283 Object &Obj) {
284 for (auto &Sec : Obj.sections()) {
285 if (Sec.Name == SecName) {
Jordan Rupprecht16a0de22018-12-20 00:57:06 +0000286 if (Sec.OriginalData.empty())
James Henderson5316a0d2019-05-22 13:23:26 +0000287 return createStringError(object_error::parse_failed,
288 "cannot dump section '%s': it has no contents",
289 SecName.str().c_str());
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000290 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
291 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
292 if (!BufferOrErr)
293 return BufferOrErr.takeError();
294 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
295 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
296 Buf->getBufferStart());
297 if (Error E = Buf->commit())
298 return E;
299 return Error::success();
300 }
301 }
James Henderson5316a0d2019-05-22 13:23:26 +0000302 return createStringError(object_error::parse_failed, "section '%s' not found",
303 SecName.str().c_str());
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000304}
305
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000306static bool isCompressable(const SectionBase &Section) {
George Rimarade3c702019-03-05 13:07:43 +0000307 return !(Section.Flags & ELF::SHF_COMPRESSED) &&
308 StringRef(Section.Name).startswith(".debug");
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000309}
310
311static void replaceDebugSections(
Fangrui Song3dfc3fb2019-03-15 10:27:28 +0000312 Object &Obj, SectionPred &RemovePred,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000313 function_ref<bool(const SectionBase &)> shouldReplace,
314 function_ref<SectionBase *(const SectionBase *)> addSection) {
George Rimard8a5c6c2019-03-11 11:01:24 +0000315 // Build a list of the debug sections we are going to replace.
316 // We can't call `addSection` while iterating over sections,
317 // because it would mutate the sections array.
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000318 SmallVector<SectionBase *, 13> ToReplace;
George Rimard8a5c6c2019-03-11 11:01:24 +0000319 for (auto &Sec : Obj.sections())
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000320 if (shouldReplace(Sec))
321 ToReplace.push_back(&Sec);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000322
George Rimard8a5c6c2019-03-11 11:01:24 +0000323 // Build a mapping from original section to a new one.
324 DenseMap<SectionBase *, SectionBase *> FromTo;
325 for (SectionBase *S : ToReplace)
326 FromTo[S] = addSection(S);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000327
George Rimard8a5c6c2019-03-11 11:01:24 +0000328 // Now we want to update the target sections of relocation
329 // sections. Also we will update the relocations themselves
330 // to update the symbol references.
331 for (auto &Sec : Obj.sections())
332 Sec.replaceSectionReferences(FromTo);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000333
334 RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) {
335 return shouldReplace(Sec) || RemovePred(Sec);
336 };
337}
338
Eugene Leviant2db10622019-02-13 07:34:54 +0000339static bool isUnneededSymbol(const Symbol &Sym) {
340 return !Sym.Referenced &&
341 (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
Eugene Leviantec767b02019-05-21 09:09:33 +0000342 Sym.Type != STT_SECTION;
Eugene Leviant2db10622019-02-13 07:34:54 +0000343}
344
George Rimare6963be2019-03-25 12:34:25 +0000345static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000346 // TODO: update or remove symbols only if there is an option that affects
347 // them.
George Rimare6963be2019-03-25 12:34:25 +0000348 if (!Obj.SymbolTable)
349 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000350
George Rimare6963be2019-03-25 12:34:25 +0000351 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
352 // Common and undefined symbols don't make sense as local symbols, and can
353 // even cause crashes if we localize those, so skip them.
354 if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
355 ((Config.LocalizeHidden &&
356 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
357 is_contained(Config.SymbolsToLocalize, Sym.Name)))
358 Sym.Binding = STB_LOCAL;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000359
George Rimare6963be2019-03-25 12:34:25 +0000360 // Note: these two globalize flags have very similar names but different
361 // meanings:
362 //
363 // --globalize-symbol: promote a symbol to global
364 // --keep-global-symbol: all symbols except for these should be made local
365 //
366 // If --globalize-symbol is specified for a given symbol, it will be
367 // global in the output file even if it is not included via
368 // --keep-global-symbol. Because of that, make sure to check
369 // --globalize-symbol second.
370 if (!Config.SymbolsToKeepGlobal.empty() &&
371 !is_contained(Config.SymbolsToKeepGlobal, Sym.Name) &&
372 Sym.getShndx() != SHN_UNDEF)
373 Sym.Binding = STB_LOCAL;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000374
George Rimare6963be2019-03-25 12:34:25 +0000375 if (is_contained(Config.SymbolsToGlobalize, Sym.Name) &&
376 Sym.getShndx() != SHN_UNDEF)
377 Sym.Binding = STB_GLOBAL;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000378
George Rimare6963be2019-03-25 12:34:25 +0000379 if (is_contained(Config.SymbolsToWeaken, Sym.Name) &&
380 Sym.Binding == STB_GLOBAL)
381 Sym.Binding = STB_WEAK;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000382
George Rimare6963be2019-03-25 12:34:25 +0000383 if (Config.Weaken && Sym.Binding == STB_GLOBAL &&
384 Sym.getShndx() != SHN_UNDEF)
385 Sym.Binding = STB_WEAK;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000386
George Rimare6963be2019-03-25 12:34:25 +0000387 const auto I = Config.SymbolsToRename.find(Sym.Name);
388 if (I != Config.SymbolsToRename.end())
389 Sym.Name = I->getValue();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000390
George Rimare6963be2019-03-25 12:34:25 +0000391 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
392 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
393 });
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000394
George Rimare6963be2019-03-25 12:34:25 +0000395 // The purpose of this loop is to mark symbols referenced by sections
396 // (like GroupSection or RelocationSection). This way, we know which
397 // symbols are still 'needed' and which are not.
George Rimarc1cc8d02019-05-24 15:04:50 +0000398 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
399 !Config.OnlySection.empty()) {
George Rimare6963be2019-03-25 12:34:25 +0000400 for (auto &Section : Obj.sections())
401 Section.markSymbols();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000402 }
403
George Rimare6963be2019-03-25 12:34:25 +0000404 auto RemoveSymbolsPred = [&](const Symbol &Sym) {
405 if (is_contained(Config.SymbolsToKeep, Sym.Name) ||
406 (Config.KeepFileSymbols && Sym.Type == STT_FILE))
407 return false;
408
409 if ((Config.DiscardMode == DiscardType::All ||
410 (Config.DiscardMode == DiscardType::Locals &&
411 StringRef(Sym.Name).startswith(".L"))) &&
412 Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
413 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
414 return true;
415
416 if (Config.StripAll || Config.StripAllGNU)
417 return true;
418
419 if (is_contained(Config.SymbolsToRemove, Sym.Name))
420 return true;
421
422 if ((Config.StripUnneeded ||
423 is_contained(Config.UnneededSymbolsToRemove, Sym.Name)) &&
424 isUnneededSymbol(Sym))
425 return true;
426
George Rimarc1cc8d02019-05-24 15:04:50 +0000427 // We want to remove undefined symbols if all references have been stripped.
428 if (!Config.OnlySection.empty() && !Sym.Referenced &&
429 Sym.getShndx() == SHN_UNDEF)
430 return true;
431
George Rimare6963be2019-03-25 12:34:25 +0000432 return false;
433 };
434
435 return Obj.removeSymbols(RemoveSymbolsPred);
436}
437
438static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000439 SectionPred RemovePred = [](const SectionBase &) { return false; };
440
441 // Removes:
442 if (!Config.ToRemove.empty()) {
443 RemovePred = [&Config](const SectionBase &Sec) {
444 return is_contained(Config.ToRemove, Sec.Name);
445 };
446 }
447
448 if (Config.StripDWO || !Config.SplitDWO.empty())
449 RemovePred = [RemovePred](const SectionBase &Sec) {
450 return isDWOSection(Sec) || RemovePred(Sec);
451 };
452
453 if (Config.ExtractDWO)
454 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
455 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
456 };
457
458 if (Config.StripAllGNU)
459 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
460 if (RemovePred(Sec))
461 return true;
462 if ((Sec.Flags & SHF_ALLOC) != 0)
463 return false;
464 if (&Sec == Obj.SectionNames)
465 return false;
466 switch (Sec.Type) {
467 case SHT_SYMTAB:
468 case SHT_REL:
469 case SHT_RELA:
470 case SHT_STRTAB:
471 return true;
472 }
473 return isDebugSection(Sec);
474 };
475
476 if (Config.StripSections) {
477 RemovePred = [RemovePred](const SectionBase &Sec) {
James Hendersonb5de5e22019-03-14 11:47:41 +0000478 return RemovePred(Sec) || Sec.ParentSegment == nullptr;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000479 };
480 }
481
482 if (Config.StripDebug) {
483 RemovePred = [RemovePred](const SectionBase &Sec) {
484 return RemovePred(Sec) || isDebugSection(Sec);
485 };
486 }
487
488 if (Config.StripNonAlloc)
489 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
490 if (RemovePred(Sec))
491 return true;
492 if (&Sec == Obj.SectionNames)
493 return false;
James Hendersonb5de5e22019-03-14 11:47:41 +0000494 return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000495 };
496
497 if (Config.StripAll)
498 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
499 if (RemovePred(Sec))
500 return true;
501 if (&Sec == Obj.SectionNames)
502 return false;
503 if (StringRef(Sec.Name).startswith(".gnu.warning"))
504 return false;
James Hendersonb5de5e22019-03-14 11:47:41 +0000505 if (Sec.ParentSegment != nullptr)
506 return false;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000507 return (Sec.Flags & SHF_ALLOC) == 0;
508 };
509
510 // Explicit copies:
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000511 if (!Config.OnlySection.empty()) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000512 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
513 // Explicitly keep these sections regardless of previous removes.
Jake Ehrlich85985ed2018-12-06 02:03:53 +0000514 if (is_contained(Config.OnlySection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000515 return false;
516
517 // Allow all implicit removes.
518 if (RemovePred(Sec))
519 return true;
520
521 // Keep special sections.
522 if (Obj.SectionNames == &Sec)
523 return false;
524 if (Obj.SymbolTable == &Sec ||
525 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
526 return false;
527
528 // Remove everything else.
529 return true;
530 };
531 }
532
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000533 if (!Config.KeepSection.empty()) {
Fangrui Songe9f34b02018-11-12 23:46:22 +0000534 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000535 // Explicitly keep these sections regardless of previous removes.
Jordan Rupprechtc5bae782018-11-13 19:32:27 +0000536 if (is_contained(Config.KeepSection, Sec.Name))
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000537 return false;
538 // Otherwise defer to RemovePred.
539 return RemovePred(Sec);
540 };
541 }
542
543 // This has to be the last predicate assignment.
544 // If the option --keep-symbol has been specified
545 // and at least one of those symbols is present
546 // (equivalently, the updated symbol table is not empty)
547 // the symbol table and the string table should not be removed.
548 if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) &&
549 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
550 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
551 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
552 return false;
553 return RemovePred(Sec);
554 };
555 }
556
557 if (Config.CompressionType != DebugCompressionType::None)
James Henderson5316a0d2019-05-22 13:23:26 +0000558 replaceDebugSections(Obj, RemovePred, isCompressable,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000559 [&Config, &Obj](const SectionBase *S) {
560 return &Obj.addSection<CompressedSection>(
James Henderson5316a0d2019-05-22 13:23:26 +0000561 *S, Config.CompressionType);
562 });
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000563 else if (Config.DecompressDebugSections)
564 replaceDebugSections(
Fangrui Song3dfc3fb2019-03-15 10:27:28 +0000565 Obj, RemovePred,
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000566 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
567 [&Obj](const SectionBase *S) {
568 auto CS = cast<CompressedSection>(S);
569 return &Obj.addSection<DecompressedSection>(*CS);
570 });
571
James Henderson66a9d0f2019-04-18 09:13:30 +0000572 return Obj.removeSections(Config.AllowBrokenLinks, RemovePred);
George Rimare6963be2019-03-25 12:34:25 +0000573}
574
575// This function handles the high level operations of GNU objcopy including
576// handling command line options. It's important to outline certain properties
577// we expect to hold of the command line operations. Any operation that "keeps"
578// should keep regardless of a remove. Additionally any removal should respect
579// any previous removals. Lastly whether or not something is removed shouldn't
580// depend a) on the order the options occur in or b) on some opaque priority
581// system. The only priority is that keeps/copies overrule removes.
582static Error handleArgs(const CopyConfig &Config, Object &Obj,
583 const Reader &Reader, ElfType OutputElfType) {
584
585 if (!Config.SplitDWO.empty())
586 if (Error E =
587 splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType))
588 return E;
589
590 if (Config.OutputArch) {
591 Obj.Machine = Config.OutputArch.getValue().EMachine;
592 Obj.OSABI = Config.OutputArch.getValue().OSABI;
593 }
594
George Rimar279898b2019-03-26 18:42:15 +0000595 // It is important to remove the sections first. For example, we want to
596 // remove the relocation sections before removing the symbols. That allows
597 // us to avoid reporting the inappropriate errors about removing symbols
598 // named in relocations.
599 if (Error E = replaceAndRemoveSections(Config, Obj))
George Rimare6963be2019-03-25 12:34:25 +0000600 return E;
601
George Rimar279898b2019-03-26 18:42:15 +0000602 if (Error E = updateAndRemoveSymbols(Config, Obj))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000603 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000604
James Hendersonfa11fb32019-05-08 09:49:35 +0000605 if (!Config.SectionsToRename.empty() || !Config.AllocSectionsPrefix.empty()) {
606 DenseSet<SectionBase *> PrefixedSections;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000607 for (auto &Sec : Obj.sections()) {
608 const auto Iter = Config.SectionsToRename.find(Sec.Name);
609 if (Iter != Config.SectionsToRename.end()) {
610 const SectionRename &SR = Iter->second;
611 Sec.Name = SR.NewName;
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000612 if (SR.NewFlags.hasValue())
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000613 setSectionFlagsAndType(Sec, SR.NewFlags.getValue());
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000614 }
James Hendersonfa11fb32019-05-08 09:49:35 +0000615
616 // Add a prefix to allocated sections and their relocation sections. This
617 // should be done after renaming the section by Config.SectionToRename to
618 // imitate the GNU objcopy behavior.
619 if (!Config.AllocSectionsPrefix.empty()) {
620 if (Sec.Flags & SHF_ALLOC) {
621 Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
622 PrefixedSections.insert(&Sec);
623
James Henderson5316a0d2019-05-22 13:23:26 +0000624 // Rename relocation sections associated to the allocated sections.
625 // For example, if we rename .text to .prefix.text, we also rename
626 // .rel.text to .rel.prefix.text.
627 //
628 // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
629 // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
630 // .rela.prefix.plt since GNU objcopy does so.
James Hendersonfa11fb32019-05-08 09:49:35 +0000631 } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
632 auto *TargetSec = RelocSec->getSection();
633 if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
634 StringRef prefix;
635 switch (Sec.Type) {
636 case SHT_REL:
637 prefix = ".rel";
638 break;
639 case SHT_RELA:
640 prefix = ".rela";
641 break;
642 default:
643 continue;
644 }
645
646 // If the relocation section comes *after* the target section, we
647 // don't add Config.AllocSectionsPrefix because we've already added
648 // the prefix to TargetSec->Name. Otherwise, if the relocation
649 // section comes *before* the target section, we add the prefix.
650 if (PrefixedSections.count(TargetSec)) {
651 Sec.Name = (prefix + TargetSec->Name).str();
652 } else {
653 const auto Iter = Config.SectionsToRename.find(TargetSec->Name);
654 if (Iter != Config.SectionsToRename.end()) {
655 // Both `--rename-section` and `--prefix-alloc-sections` are
656 // given but the target section is not yet renamed.
657 Sec.Name =
658 (prefix + Config.AllocSectionsPrefix + Iter->second.NewName)
659 .str();
660 } else {
661 Sec.Name =
662 (prefix + Config.AllocSectionsPrefix + TargetSec->Name)
663 .str();
664 }
665 }
666 }
667 }
668 }
Jordan Rupprechtc8927412019-01-29 15:05:38 +0000669 }
670 }
671
672 if (!Config.SetSectionFlags.empty()) {
673 for (auto &Sec : Obj.sections()) {
674 const auto Iter = Config.SetSectionFlags.find(Sec.Name);
675 if (Iter != Config.SetSectionFlags.end()) {
676 const SectionFlagsUpdate &SFU = Iter->second;
Jordan Rupprecht017deaf2019-04-02 16:49:56 +0000677 setSectionFlagsAndType(Sec, SFU.NewFlags);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000678 }
679 }
680 }
Jordan Rupprechtbd95a9f2019-03-28 18:27:00 +0000681
Eugene Leviantc76671b2019-03-12 12:41:06 +0000682 for (const auto &Flag : Config.AddSection) {
683 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
684 StringRef SecName = SecPair.first;
685 StringRef File = SecPair.second;
686 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
687 MemoryBuffer::getFile(File);
688 if (!BufOrErr)
689 return createFileError(File, errorCodeToError(BufOrErr.getError()));
690 std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
691 ArrayRef<uint8_t> Data(
692 reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
693 Buf->getBufferSize());
694 OwnedDataSection &NewSection =
695 Obj.addSection<OwnedDataSection>(SecName, Data);
696 if (SecName.startswith(".note") && SecName != ".note.GNU-stack")
697 NewSection.Type = SHT_NOTE;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000698 }
699
Eugene Leviantc76671b2019-03-12 12:41:06 +0000700 for (const auto &Flag : Config.DumpSection) {
701 std::pair<StringRef, StringRef> SecPair = Flag.split("=");
702 StringRef SecName = SecPair.first;
703 StringRef File = SecPair.second;
704 if (Error E = dumpSectionToFile(SecName, File, Obj))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000705 return E;
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000706 }
707
708 if (!Config.AddGnuDebugLink.empty())
James Henderson9df38832019-05-14 10:59:04 +0000709 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink,
710 Config.GnuDebugLinkCRC32);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000711
Eugene Leviant51c1f642019-02-25 14:12:41 +0000712 for (const NewSymbolInfo &SI : Config.SymbolsToAdd) {
713 SectionBase *Sec = Obj.findSection(SI.SectionName);
714 uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value;
Simon Pilgrim65706cf2019-02-27 10:19:53 +0000715 Obj.SymbolTable->addSymbol(
716 SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility,
717 Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0);
Eugene Leviant51c1f642019-02-25 14:12:41 +0000718 }
719
Eugene Leviant53350d02019-02-26 09:24:22 +0000720 if (Config.EntryExpr)
721 Obj.Entry = Config.EntryExpr(Obj.Entry);
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000722 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000723}
724
Eugene Levianta6fb1832019-05-29 11:37:16 +0000725static Error writeOutput(const CopyConfig &Config, Object &Obj, Buffer &Out,
726 ElfType OutputElfType) {
727 std::unique_ptr<Writer> Writer =
728 createWriter(Config, Obj, Out, OutputElfType);
729 if (Error E = Writer->finalize())
730 return E;
731 return Writer->write();
732}
733
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000734Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In,
735 Buffer &Out) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000736 BinaryReader Reader(Config.BinaryArch, &In);
737 std::unique_ptr<Object> Obj = Reader.create();
738
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000739 // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
740 // (-B<arch>).
Eugene Levianta6fb1832019-05-29 11:37:16 +0000741 const ElfType OutputElfType =
742 getOutputElfType(Config.OutputArch.getValueOr(Config.BinaryArch));
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000743 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
744 return E;
Eugene Levianta6fb1832019-05-29 11:37:16 +0000745 return writeOutput(Config, *Obj, Out, OutputElfType);
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000746}
747
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000748Error executeObjcopyOnBinary(const CopyConfig &Config,
749 object::ELFObjectFileBase &In, Buffer &Out) {
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000750 ELFReader Reader(&In);
751 std::unique_ptr<Object> Obj = Reader.create();
Jordan Rupprecht70038e02019-01-07 16:59:12 +0000752 // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
753 const ElfType OutputElfType =
754 Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue())
755 : getOutputElfType(In);
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000756 ArrayRef<uint8_t> BuildIdBytes;
757
758 if (!Config.BuildIdLinkDir.empty()) {
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000759 auto BuildIdBytesOrErr = findBuildID(Config, In);
760 if (auto E = BuildIdBytesOrErr.takeError())
761 return E;
762 BuildIdBytes = *BuildIdBytesOrErr;
763
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000764 if (BuildIdBytes.size() < 2)
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000765 return createFileError(
766 Config.InputFilename,
767 createStringError(object_error::parse_failed,
James Henderson5316a0d2019-05-22 13:23:26 +0000768 "build ID is smaller than two bytes"));
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000769 }
770
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000771 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput)
772 if (Error E =
773 linkToBuildIdDir(Config, Config.InputFilename,
774 Config.BuildIdLinkInput.getValue(), BuildIdBytes))
775 return E;
776
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000777 if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000778 return createFileError(Config.InputFilename, std::move(E));
779
Eugene Levianta6fb1832019-05-29 11:37:16 +0000780 if (Error E = writeOutput(Config, *Obj, Out, OutputElfType))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000781 return createFileError(Config.InputFilename, std::move(E));
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000782 if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput)
783 if (Error E =
784 linkToBuildIdDir(Config, Config.OutputFilename,
785 Config.BuildIdLinkOutput.getValue(), BuildIdBytes))
Seiya Nutaada9d2d82019-05-23 00:42:46 +0000786 return createFileError(Config.OutputFilename, std::move(E));
Jordan Rupprechtfc832e92019-01-30 18:13:30 +0000787
Jordan Rupprecht307deab2019-01-30 14:36:53 +0000788 return Error::success();
Alexander Shaposhnikovf4e75a52018-10-29 21:22:58 +0000789}
790
791} // end namespace elf
792} // end namespace objcopy
793} // end namespace llvm