LLVM 18.0.0git
ELFObjcopy.cpp
Go to the documentation of this file.
1//===- ELFObjcopy.cpp -----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "ELFObject.h"
12#include "llvm/ADT/DenseSet.h"
13#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
21#include "llvm/Object/Binary.h"
24#include "llvm/Object/Error.h"
25#include "llvm/Option/Option.h"
28#include "llvm/Support/Errc.h"
29#include "llvm/Support/Error.h"
33#include "llvm/Support/Memory.h"
34#include "llvm/Support/Path.h"
36#include <algorithm>
37#include <cassert>
38#include <cstdlib>
39#include <functional>
40#include <iterator>
41#include <memory>
42#include <string>
43#include <system_error>
44#include <utility>
45
46using namespace llvm;
47using namespace llvm::ELF;
48using namespace llvm::objcopy;
49using namespace llvm::objcopy::elf;
50using namespace llvm::object;
51
52using SectionPred = std::function<bool(const SectionBase &Sec)>;
53
54static bool isDebugSection(const SectionBase &Sec) {
55 return StringRef(Sec.Name).startswith(".debug") || Sec.Name == ".gdb_index";
56}
57
58static bool isDWOSection(const SectionBase &Sec) {
59 return StringRef(Sec.Name).endswith(".dwo");
60}
61
62static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) {
63 // We can't remove the section header string table.
64 if (&Sec == Obj.SectionNames)
65 return false;
66 // Short of keeping the string table we want to keep everything that is a DWO
67 // section and remove everything else.
68 return !isDWOSection(Sec);
69}
70
72 uint16_t EMachine) {
73 uint64_t NewFlags = 0;
74 if (AllFlags & SectionFlag::SecAlloc)
75 NewFlags |= ELF::SHF_ALLOC;
76 if (!(AllFlags & SectionFlag::SecReadonly))
77 NewFlags |= ELF::SHF_WRITE;
78 if (AllFlags & SectionFlag::SecCode)
79 NewFlags |= ELF::SHF_EXECINSTR;
80 if (AllFlags & SectionFlag::SecMerge)
81 NewFlags |= ELF::SHF_MERGE;
82 if (AllFlags & SectionFlag::SecStrings)
83 NewFlags |= ELF::SHF_STRINGS;
84 if (AllFlags & SectionFlag::SecExclude)
85 NewFlags |= ELF::SHF_EXCLUDE;
86 if (AllFlags & SectionFlag::SecLarge) {
87 if (EMachine != EM_X86_64)
88 return createStringError(errc::invalid_argument,
89 "section flag SHF_X86_64_LARGE can only be used "
90 "with x86_64 architecture");
91 NewFlags |= ELF::SHF_X86_64_LARGE;
92 }
93 return NewFlags;
94}
95
97 uint64_t NewFlags,
98 uint16_t EMachine) {
99 // Preserve some flags which should not be dropped when setting flags.
100 // Also, preserve anything OS/processor dependant.
101 const uint64_t PreserveMask =
106 ~(EMachine == EM_X86_64 ? (uint64_t)ELF::SHF_X86_64_LARGE : 0UL);
107 return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
108}
109
111 // If Sec's type is changed from SHT_NOBITS due to --set-section-flags,
112 // Offset may not be aligned. Align it to max(Align, 1).
113 if (Sec.Type == ELF::SHT_NOBITS && Type != ELF::SHT_NOBITS)
114 Sec.Offset = alignTo(Sec.Offset, std::max(Sec.Align, uint64_t(1)));
115 Sec.Type = Type;
116}
117
119 uint16_t EMachine) {
120 Expected<uint64_t> NewFlags = getNewShfFlags(Flags, EMachine);
121 if (!NewFlags)
122 return NewFlags.takeError();
123 Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, *NewFlags, EMachine);
124
125 // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule
126 // may promote more non-ALLOC sections than GNU objcopy, but it is fine as
127 // non-ALLOC SHT_NOBITS sections do not make much sense.
128 if (Sec.Type == SHT_NOBITS &&
129 (!(Sec.Flags & ELF::SHF_ALLOC) ||
130 Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
132
133 return Error::success();
134}
135
137 // Infer output ELF type from the input ELF object
139 return ELFT_ELF32LE;
141 return ELFT_ELF64LE;
143 return ELFT_ELF32BE;
145 return ELFT_ELF64BE;
146 llvm_unreachable("Invalid ELFType");
147}
148
150 // Infer output ELF type from the binary arch specified
151 if (MI.Is64Bit)
152 return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE;
153 else
154 return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE;
155}
156
157static std::unique_ptr<Writer> createELFWriter(const CommonConfig &Config,
158 Object &Obj, raw_ostream &Out,
159 ElfType OutputElfType) {
160 // Depending on the initial ELFT and OutputFormat we need a different Writer.
161 switch (OutputElfType) {
162 case ELFT_ELF32LE:
163 return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !Config.StripSections,
164 Config.OnlyKeepDebug);
165 case ELFT_ELF64LE:
166 return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !Config.StripSections,
167 Config.OnlyKeepDebug);
168 case ELFT_ELF32BE:
169 return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !Config.StripSections,
170 Config.OnlyKeepDebug);
171 case ELFT_ELF64BE:
172 return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !Config.StripSections,
173 Config.OnlyKeepDebug);
174 }
175 llvm_unreachable("Invalid output format");
176}
177
178static std::unique_ptr<Writer> createWriter(const CommonConfig &Config,
179 Object &Obj, raw_ostream &Out,
180 ElfType OutputElfType) {
181 switch (Config.OutputFormat) {
182 case FileFormat::Binary:
183 return std::make_unique<BinaryWriter>(Obj, Out);
184 case FileFormat::IHex:
185 return std::make_unique<IHexWriter>(Obj, Out);
186 default:
187 return createELFWriter(Config, Obj, Out, OutputElfType);
188 }
189}
190
192 Object &Obj) {
193 for (auto &Sec : Obj.sections()) {
194 if (Sec.Name == SecName) {
195 if (Sec.Type == SHT_NOBITS)
196 return createStringError(object_error::parse_failed,
197 "cannot dump section '%s': it has no contents",
198 SecName.str().c_str());
200 FileOutputBuffer::create(Filename, Sec.OriginalData.size());
201 if (!BufferOrErr)
202 return BufferOrErr.takeError();
203 std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
204 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(),
205 Buf->getBufferStart());
206 if (Error E = Buf->commit())
207 return E;
208 return Error::success();
209 }
210 }
211 return createStringError(object_error::parse_failed, "section '%s' not found",
212 SecName.str().c_str());
213}
214
215static bool isCompressable(const SectionBase &Sec) {
216 return !(Sec.Flags & ELF::SHF_COMPRESSED) &&
217 StringRef(Sec.Name).startswith(".debug");
218}
219
221 Object &Obj, function_ref<bool(const SectionBase &)> ShouldReplace,
222 function_ref<Expected<SectionBase *>(const SectionBase *)> AddSection) {
223 // Build a list of the debug sections we are going to replace.
224 // We can't call `AddSection` while iterating over sections,
225 // because it would mutate the sections array.
227 for (auto &Sec : Obj.sections())
228 if (ShouldReplace(Sec))
229 ToReplace.push_back(&Sec);
230
231 // Build a mapping from original section to a new one.
233 for (SectionBase *S : ToReplace) {
234 Expected<SectionBase *> NewSection = AddSection(S);
235 if (!NewSection)
236 return NewSection.takeError();
237
238 FromTo[S] = *NewSection;
239 }
240
241 return Obj.replaceSections(FromTo);
242}
243
244static bool isAArch64MappingSymbol(const Symbol &Sym) {
245 if (Sym.Binding != STB_LOCAL || Sym.Type != STT_NOTYPE ||
246 Sym.getShndx() == SHN_UNDEF)
247 return false;
248 StringRef Name = Sym.Name;
249 if (!Name.consume_front("$x") && !Name.consume_front("$d"))
250 return false;
251 return Name.empty() || Name.startswith(".");
252}
253
254static bool isArmMappingSymbol(const Symbol &Sym) {
255 if (Sym.Binding != STB_LOCAL || Sym.Type != STT_NOTYPE ||
256 Sym.getShndx() == SHN_UNDEF)
257 return false;
258 StringRef Name = Sym.Name;
259 if (!Name.consume_front("$a") && !Name.consume_front("$d") &&
260 !Name.consume_front("$t"))
261 return false;
262 return Name.empty() || Name.startswith(".");
263}
264
265// Check if the symbol should be preserved because it is required by ABI.
266static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym) {
267 switch (Obj.Machine) {
268 case EM_AARCH64:
269 // Mapping symbols should be preserved for a relocatable object file.
271 case EM_ARM:
272 // Mapping symbols should be preserved for a relocatable object file.
273 return Obj.isRelocatable() && isArmMappingSymbol(Sym);
274 default:
275 return false;
276 }
277}
278
279static bool isUnneededSymbol(const Symbol &Sym) {
280 return !Sym.Referenced &&
281 (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) &&
282 Sym.Type != STT_SECTION;
283}
284
286 const ELFConfig &ELFConfig, Object &Obj) {
287 // TODO: update or remove symbols only if there is an option that affects
288 // them.
289 if (!Obj.SymbolTable)
290 return Error::success();
291
293 // Common and undefined symbols don't make sense as local symbols, and can
294 // even cause crashes if we localize those, so skip them.
295 if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF &&
297 (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
298 Config.SymbolsToLocalize.matches(Sym.Name)))
299 Sym.Binding = STB_LOCAL;
300
301 // Note: these two globalize flags have very similar names but different
302 // meanings:
303 //
304 // --globalize-symbol: promote a symbol to global
305 // --keep-global-symbol: all symbols except for these should be made local
306 //
307 // If --globalize-symbol is specified for a given symbol, it will be
308 // global in the output file even if it is not included via
309 // --keep-global-symbol. Because of that, make sure to check
310 // --globalize-symbol second.
311 if (!Config.SymbolsToKeepGlobal.empty() &&
312 !Config.SymbolsToKeepGlobal.matches(Sym.Name) &&
313 Sym.getShndx() != SHN_UNDEF)
314 Sym.Binding = STB_LOCAL;
315
316 if (Config.SymbolsToGlobalize.matches(Sym.Name) &&
317 Sym.getShndx() != SHN_UNDEF)
318 Sym.Binding = STB_GLOBAL;
319
320 // SymbolsToWeaken applies to both STB_GLOBAL and STB_GNU_UNIQUE.
321 if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding != STB_LOCAL)
322 Sym.Binding = STB_WEAK;
323
324 if (Config.Weaken && Sym.Binding != STB_LOCAL &&
325 Sym.getShndx() != SHN_UNDEF)
326 Sym.Binding = STB_WEAK;
327
328 const auto I = Config.SymbolsToRename.find(Sym.Name);
329 if (I != Config.SymbolsToRename.end())
330 Sym.Name = std::string(I->getValue());
331
332 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION)
333 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str();
334 });
335
336 // The purpose of this loop is to mark symbols referenced by sections
337 // (like GroupSection or RelocationSection). This way, we know which
338 // symbols are still 'needed' and which are not.
339 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() ||
340 !Config.OnlySection.empty()) {
341 for (SectionBase &Sec : Obj.sections())
342 Sec.markSymbols();
343 }
344
345 auto RemoveSymbolsPred = [&](const Symbol &Sym) {
346 if (Config.SymbolsToKeep.matches(Sym.Name) ||
348 return false;
349
350 if (Config.SymbolsToRemove.matches(Sym.Name))
351 return true;
352
353 if (Config.StripAll || Config.StripAllGNU)
354 return true;
355
356 if (isRequiredByABISymbol(Obj, Sym))
357 return false;
358
359 if (Config.StripDebug && Sym.Type == STT_FILE)
360 return true;
361
362 if ((Config.DiscardMode == DiscardType::All ||
363 (Config.DiscardMode == DiscardType::Locals &&
364 StringRef(Sym.Name).startswith(".L"))) &&
365 Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF &&
366 Sym.Type != STT_FILE && Sym.Type != STT_SECTION)
367 return true;
368
369 if ((Config.StripUnneeded ||
370 Config.UnneededSymbolsToRemove.matches(Sym.Name)) &&
372 return true;
373
374 // We want to remove undefined symbols if all references have been stripped.
375 if (!Config.OnlySection.empty() && !Sym.Referenced &&
376 Sym.getShndx() == SHN_UNDEF)
377 return true;
378
379 return false;
380 };
381
382 return Obj.removeSymbols(RemoveSymbolsPred);
383}
384
386 const ELFConfig &ELFConfig, Object &Obj) {
387 SectionPred RemovePred = [](const SectionBase &) { return false; };
388
389 // Removes:
390 if (!Config.ToRemove.empty()) {
391 RemovePred = [&Config](const SectionBase &Sec) {
392 return Config.ToRemove.matches(Sec.Name);
393 };
394 }
395
396 if (Config.StripDWO)
397 RemovePred = [RemovePred](const SectionBase &Sec) {
398 return isDWOSection(Sec) || RemovePred(Sec);
399 };
400
401 if (Config.ExtractDWO)
402 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
403 return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec);
404 };
405
406 if (Config.StripAllGNU)
407 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
408 if (RemovePred(Sec))
409 return true;
410 if ((Sec.Flags & SHF_ALLOC) != 0)
411 return false;
412 if (&Sec == Obj.SectionNames)
413 return false;
414 switch (Sec.Type) {
415 case SHT_SYMTAB:
416 case SHT_REL:
417 case SHT_RELA:
418 case SHT_STRTAB:
419 return true;
420 }
421 return isDebugSection(Sec);
422 };
423
424 if (Config.StripSections) {
425 RemovePred = [RemovePred](const SectionBase &Sec) {
426 return RemovePred(Sec) || Sec.ParentSegment == nullptr;
427 };
428 }
429
430 if (Config.StripDebug || Config.StripUnneeded) {
431 RemovePred = [RemovePred](const SectionBase &Sec) {
432 return RemovePred(Sec) || isDebugSection(Sec);
433 };
434 }
435
436 if (Config.StripNonAlloc)
437 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
438 if (RemovePred(Sec))
439 return true;
440 if (&Sec == Obj.SectionNames)
441 return false;
442 return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr;
443 };
444
445 if (Config.StripAll)
446 RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
447 if (RemovePred(Sec))
448 return true;
449 if (&Sec == Obj.SectionNames)
450 return false;
451 if (StringRef(Sec.Name).startswith(".gnu.warning"))
452 return false;
453 // We keep the .ARM.attribute section to maintain compatibility
454 // with Debian derived distributions. This is a bug in their
455 // patchset as documented here:
456 // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=943798
457 if (Sec.Type == SHT_ARM_ATTRIBUTES)
458 return false;
459 if (Sec.ParentSegment != nullptr)
460 return false;
461 return (Sec.Flags & SHF_ALLOC) == 0;
462 };
463
464 if (Config.ExtractPartition || Config.ExtractMainPartition) {
465 RemovePred = [RemovePred](const SectionBase &Sec) {
466 if (RemovePred(Sec))
467 return true;
468 if (Sec.Type == SHT_LLVM_PART_EHDR || Sec.Type == SHT_LLVM_PART_PHDR)
469 return true;
470 return (Sec.Flags & SHF_ALLOC) != 0 && !Sec.ParentSegment;
471 };
472 }
473
474 // Explicit copies:
475 if (!Config.OnlySection.empty()) {
476 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) {
477 // Explicitly keep these sections regardless of previous removes.
478 if (Config.OnlySection.matches(Sec.Name))
479 return false;
480
481 // Allow all implicit removes.
482 if (RemovePred(Sec))
483 return true;
484
485 // Keep special sections.
486 if (Obj.SectionNames == &Sec)
487 return false;
488 if (Obj.SymbolTable == &Sec ||
489 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec))
490 return false;
491
492 // Remove everything else.
493 return true;
494 };
495 }
496
497 if (!Config.KeepSection.empty()) {
498 RemovePred = [&Config, RemovePred](const SectionBase &Sec) {
499 // Explicitly keep these sections regardless of previous removes.
500 if (Config.KeepSection.matches(Sec.Name))
501 return false;
502 // Otherwise defer to RemovePred.
503 return RemovePred(Sec);
504 };
505 }
506
507 // This has to be the last predicate assignment.
508 // If the option --keep-symbol has been specified
509 // and at least one of those symbols is present
510 // (equivalently, the updated symbol table is not empty)
511 // the symbol table and the string table should not be removed.
512 if ((!Config.SymbolsToKeep.empty() || ELFConfig.KeepFileSymbols) &&
513 Obj.SymbolTable && !Obj.SymbolTable->empty()) {
514 RemovePred = [&Obj, RemovePred](const SectionBase &Sec) {
515 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab())
516 return false;
517 return RemovePred(Sec);
518 };
519 }
520
521 if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred))
522 return E;
523
524 if (Config.CompressionType != DebugCompressionType::None) {
525 if (Error Err = replaceDebugSections(
526 Obj, isCompressable,
527 [&Config, &Obj](const SectionBase *S) -> Expected<SectionBase *> {
528 return &Obj.addSection<CompressedSection>(
529 CompressedSection(*S, Config.CompressionType, Obj.Is64Bits));
530 }))
531 return Err;
532 } else if (Config.DecompressDebugSections) {
533 if (Error Err = replaceDebugSections(
534 Obj,
535 [](const SectionBase &S) { return isa<CompressedSection>(&S); },
536 [&Obj](const SectionBase *S) {
537 const CompressedSection *CS = cast<CompressedSection>(S);
538 return &Obj.addSection<DecompressedSection>(*CS);
539 }))
540 return Err;
541 }
542
543 return Error::success();
544}
545
546// Add symbol to the Object symbol table with the specified properties.
547static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo,
548 uint8_t DefaultVisibility) {
549 SectionBase *Sec = Obj.findSection(SymInfo.SectionName);
550 uint64_t Value = Sec ? Sec->Addr + SymInfo.Value : SymInfo.Value;
551
552 uint8_t Bind = ELF::STB_GLOBAL;
553 uint8_t Type = ELF::STT_NOTYPE;
554 uint8_t Visibility = DefaultVisibility;
555
556 for (SymbolFlag FlagValue : SymInfo.Flags)
557 switch (FlagValue) {
558 case SymbolFlag::Global:
559 Bind = ELF::STB_GLOBAL;
560 break;
561 case SymbolFlag::Local:
562 Bind = ELF::STB_LOCAL;
563 break;
564 case SymbolFlag::Weak:
565 Bind = ELF::STB_WEAK;
566 break;
567 case SymbolFlag::Default:
568 Visibility = ELF::STV_DEFAULT;
569 break;
570 case SymbolFlag::Hidden:
571 Visibility = ELF::STV_HIDDEN;
572 break;
573 case SymbolFlag::Protected:
574 Visibility = ELF::STV_PROTECTED;
575 break;
576 case SymbolFlag::File:
578 break;
579 case SymbolFlag::Section:
581 break;
582 case SymbolFlag::Object:
584 break;
585 case SymbolFlag::Function:
587 break;
588 case SymbolFlag::IndirectFunction:
590 break;
591 default: /* Other flag values are ignored for ELF. */
592 break;
593 };
594
596 SymInfo.SymbolName, Bind, Type, Sec, Value, Visibility,
598}
599
600static Error
603 ArrayRef<uint8_t> Data(reinterpret_cast<const uint8_t *>(
604 NewSection.SectionData->getBufferStart()),
605 NewSection.SectionData->getBufferSize());
606 return F(NewSection.SectionName, Data);
607}
608
609// This function handles the high level operations of GNU objcopy including
610// handling command line options. It's important to outline certain properties
611// we expect to hold of the command line operations. Any operation that "keeps"
612// should keep regardless of a remove. Additionally any removal should respect
613// any previous removals. Lastly whether or not something is removed shouldn't
614// depend a) on the order the options occur in or b) on some opaque priority
615// system. The only priority is that keeps/copies overrule removes.
617 Object &Obj) {
618 if (Config.OutputArch) {
619 Obj.Machine = Config.OutputArch->EMachine;
620 Obj.OSABI = Config.OutputArch->OSABI;
621 }
622
623 if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
624 return Obj.removeSections(
626 [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); });
627 }
628
629 // Dump sections before add/remove for compatibility with GNU objcopy.
630 for (StringRef Flag : Config.DumpSection) {
632 StringRef FileName;
633 std::tie(SectionName, FileName) = Flag.split('=');
634 if (Error E = dumpSectionToFile(SectionName, FileName, Obj))
635 return E;
636 }
637
638 // It is important to remove the sections first. For example, we want to
639 // remove the relocation sections before removing the symbols. That allows
640 // us to avoid reporting the inappropriate errors about removing symbols
641 // named in relocations.
643 return E;
644
646 return E;
647
648 if (!Config.SetSectionAlignment.empty()) {
649 for (SectionBase &Sec : Obj.sections()) {
650 auto I = Config.SetSectionAlignment.find(Sec.Name);
651 if (I != Config.SetSectionAlignment.end())
652 Sec.Align = I->second;
653 }
654 }
655
656 if (Config.OnlyKeepDebug)
657 for (auto &Sec : Obj.sections())
658 if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE)
659 Sec.Type = SHT_NOBITS;
660
661 for (const NewSectionInfo &AddedSection : Config.AddSection) {
662 auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
663 OwnedDataSection &NewSection =
665 if (Name.startswith(".note") && Name != ".note.GNU-stack")
666 NewSection.Type = SHT_NOTE;
667 return Error::success();
668 };
669 if (Error E = handleUserSection(AddedSection, AddSection))
670 return E;
671 }
672
673 for (const NewSectionInfo &NewSection : Config.UpdateSection) {
674 auto UpdateSection = [&](StringRef Name, ArrayRef<uint8_t> Data) {
675 return Obj.updateSection(Name, Data);
676 };
677 if (Error E = handleUserSection(NewSection, UpdateSection))
678 return E;
679 }
680
681 if (!Config.AddGnuDebugLink.empty())
682 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink,
683 Config.GnuDebugLinkCRC32);
684
685 // If the symbol table was previously removed, we need to create a new one
686 // before adding new symbols.
687 if (!Obj.SymbolTable && !Config.SymbolsToAdd.empty())
688 if (Error E = Obj.addNewSymbolTable())
689 return E;
690
691 for (const NewSymbolInfo &SI : Config.SymbolsToAdd)
693
694 // --set-section-{flags,type} work with sections added by --add-section.
695 if (!Config.SetSectionFlags.empty() || !Config.SetSectionType.empty()) {
696 for (auto &Sec : Obj.sections()) {
697 const auto Iter = Config.SetSectionFlags.find(Sec.Name);
698 if (Iter != Config.SetSectionFlags.end()) {
699 const SectionFlagsUpdate &SFU = Iter->second;
700 if (Error E = setSectionFlagsAndType(Sec, SFU.NewFlags, Obj.Machine))
701 return E;
702 }
703 auto It2 = Config.SetSectionType.find(Sec.Name);
704 if (It2 != Config.SetSectionType.end())
705 setSectionType(Sec, It2->second);
706 }
707 }
708
709 if (!Config.SectionsToRename.empty()) {
710 std::vector<RelocationSectionBase *> RelocSections;
711 DenseSet<SectionBase *> RenamedSections;
712 for (SectionBase &Sec : Obj.sections()) {
713 auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec);
714 const auto Iter = Config.SectionsToRename.find(Sec.Name);
715 if (Iter != Config.SectionsToRename.end()) {
716 const SectionRename &SR = Iter->second;
717 Sec.Name = std::string(SR.NewName);
718 if (SR.NewFlags) {
719 if (Error E = setSectionFlagsAndType(Sec, *SR.NewFlags, Obj.Machine))
720 return E;
721 }
722 RenamedSections.insert(&Sec);
723 } else if (RelocSec && !(Sec.Flags & SHF_ALLOC))
724 // Postpone processing relocation sections which are not specified in
725 // their explicit '--rename-section' commands until after their target
726 // sections are renamed.
727 // Dynamic relocation sections (i.e. ones with SHF_ALLOC) should be
728 // renamed only explicitly. Otherwise, renaming, for example, '.got.plt'
729 // would affect '.rela.plt', which is not desirable.
730 RelocSections.push_back(RelocSec);
731 }
732
733 // Rename relocation sections according to their target sections.
734 for (RelocationSectionBase *RelocSec : RelocSections) {
735 auto Iter = RenamedSections.find(RelocSec->getSection());
736 if (Iter != RenamedSections.end())
737 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str();
738 }
739 }
740
741 // Add a prefix to allocated sections and their relocation sections. This
742 // should be done after renaming the section by Config.SectionToRename to
743 // imitate the GNU objcopy behavior.
744 if (!Config.AllocSectionsPrefix.empty()) {
745 DenseSet<SectionBase *> PrefixedSections;
746 for (SectionBase &Sec : Obj.sections()) {
747 if (Sec.Flags & SHF_ALLOC) {
748 Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str();
749 PrefixedSections.insert(&Sec);
750 } else if (auto *RelocSec = dyn_cast<RelocationSectionBase>(&Sec)) {
751 // Rename relocation sections associated to the allocated sections.
752 // For example, if we rename .text to .prefix.text, we also rename
753 // .rel.text to .rel.prefix.text.
754 //
755 // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled
756 // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not
757 // .rela.prefix.plt since GNU objcopy does so.
758 const SectionBase *TargetSec = RelocSec->getSection();
759 if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) {
760 // If the relocation section comes *after* the target section, we
761 // don't add Config.AllocSectionsPrefix because we've already added
762 // the prefix to TargetSec->Name. Otherwise, if the relocation
763 // section comes *before* the target section, we add the prefix.
764 if (PrefixedSections.count(TargetSec))
765 Sec.Name = (RelocSec->getNamePrefix() + TargetSec->Name).str();
766 else
767 Sec.Name = (RelocSec->getNamePrefix() + Config.AllocSectionsPrefix +
768 TargetSec->Name)
769 .str();
770 }
771 }
772 }
773 }
774
776 Obj.Entry = ELFConfig.EntryExpr(Obj.Entry);
777 return Error::success();
778}
779
781 raw_ostream &Out, ElfType OutputElfType) {
782 std::unique_ptr<Writer> Writer =
783 createWriter(Config, Obj, Out, OutputElfType);
784 if (Error E = Writer->finalize())
785 return E;
786 return Writer->write();
787}
788
790 const ELFConfig &ELFConfig,
791 MemoryBuffer &In, raw_ostream &Out) {
792 IHexReader Reader(&In);
794 if (!Obj)
795 return Obj.takeError();
796
797 const ElfType OutputElfType =
798 getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
799 if (Error E = handleArgs(Config, ELFConfig, **Obj))
800 return E;
801 return writeOutput(Config, **Obj, Out, OutputElfType);
802}
803
805 const ELFConfig &ELFConfig,
806 MemoryBuffer &In,
807 raw_ostream &Out) {
810 if (!Obj)
811 return Obj.takeError();
812
813 // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch
814 // (-B<arch>).
815 const ElfType OutputElfType =
816 getOutputElfType(Config.OutputArch.value_or(MachineInfo()));
817 if (Error E = handleArgs(Config, ELFConfig, **Obj))
818 return E;
819 return writeOutput(Config, **Obj, Out, OutputElfType);
820}
821
823 const ELFConfig &ELFConfig,
825 raw_ostream &Out) {
826 ELFReader Reader(&In, Config.ExtractPartition);
828 Reader.create(!Config.SymbolsToAdd.empty());
829 if (!Obj)
830 return Obj.takeError();
831 // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input.
832 const ElfType OutputElfType = Config.OutputArch
833 ? getOutputElfType(*Config.OutputArch)
834 : getOutputElfType(In);
835
836 if (Error E = handleArgs(Config, ELFConfig, **Obj))
837 return createFileError(Config.InputFilename, std::move(E));
838
839 if (Error E = writeOutput(Config, **Obj, Out, OutputElfType))
840 return createFileError(Config.InputFilename, std::move(E));
841
842 return Error::success();
843}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
std::string Name
static Error replaceAndRemoveSections(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:385
static bool isArmMappingSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:254
static Error handleUserSection(const NewSectionInfo &NewSection, function_ref< Error(StringRef, ArrayRef< uint8_t >)> F)
Definition: ELFObjcopy.cpp:601
static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, uint8_t DefaultVisibility)
Definition: ELFObjcopy.cpp:547
static Error updateAndRemoveSymbols(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:285
std::function< bool(const SectionBase &Sec)> SectionPred
Definition: ELFObjcopy.cpp:52
static bool isDWOSection(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:58
static bool isCompressable(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:215
static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym)
Definition: ELFObjcopy.cpp:266
static Error replaceDebugSections(Object &Obj, function_ref< bool(const SectionBase &)> ShouldReplace, function_ref< Expected< SectionBase * >(const SectionBase *)> AddSection)
Definition: ELFObjcopy.cpp:220
static bool isDebugSection(const SectionBase &Sec)
Definition: ELFObjcopy.cpp:54
static Error dumpSectionToFile(StringRef SecName, StringRef Filename, Object &Obj)
Definition: ELFObjcopy.cpp:191
static Error writeOutput(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:780
static bool isUnneededSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:279
static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, Object &Obj)
Definition: ELFObjcopy.cpp:616
static bool isAArch64MappingSymbol(const Symbol &Sym)
Definition: ELFObjcopy.cpp:244
static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec)
Definition: ELFObjcopy.cpp:62
static void setSectionType(SectionBase &Sec, uint64_t Type)
Definition: ELFObjcopy.cpp:110
static Expected< uint64_t > getNewShfFlags(SectionFlag AllFlags, uint16_t EMachine)
Definition: ELFObjcopy.cpp:71
static Error setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags, uint16_t EMachine)
Definition: ELFObjcopy.cpp:118
static std::unique_ptr< Writer > createELFWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:157
static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, uint64_t NewFlags, uint16_t EMachine)
Definition: ELFObjcopy.cpp:96
static std::unique_ptr< Writer > createWriter(const CommonConfig &Config, Object &Obj, raw_ostream &Out, ElfType OutputElfType)
Definition: ELFObjcopy.cpp:178
static ElfType getOutputElfType(const Binary &Bin)
Definition: ELFObjcopy.cpp:136
RelaxConfig Config
Definition: ELF_riscv.cpp:495
Symbol * Sym
Definition: ELF_riscv.cpp:468
Provides ErrorOr<T> smart pointer.
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
if(VerifyEach)
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
static Expected< std::unique_ptr< FileOutputBuffer > > create(StringRef FilePath, size_t Size, unsigned Flags=0)
Factory method to create an OutputBuffer object which manages a read/write buffer of the specified si...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
bool endswith(StringRef Suffix) const
Definition: StringRef.h:280
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
iterator find(const_arg_type_t< ValueT > V)
Definition: DenseSet.h:179
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:97
An efficient, type-erasing, non-owning reference to a callable.
SectionTableRef sections() const
Definition: ELFObject.h:1072
StringTableSection * SectionNames
Definition: ELFObject.h:1066
bool isRelocatable() const
Definition: ELFObject.h:1109
Error updateSection(StringRef Name, ArrayRef< uint8_t > Data)
Definition: ELFObject.cpp:2120
Error removeSymbols(function_ref< bool(const Symbol &)> ToRemove)
Definition: ELFObject.cpp:2227
T & addSection(Ts &&...Args)
Definition: ELFObject.h:1096
Error removeSections(bool AllowBrokenLinks, std::function< bool(const SectionBase &)> ToRemove)
Definition: ELFObject.cpp:2151
SymbolTableSection * SymbolTable
Definition: ELFObject.h:1067
SectionBase * findSection(StringRef Name)
Definition: ELFObject.h:1083
Error replaceSections(const DenseMap< SectionBase *, SectionBase * > &FromTo)
Definition: ELFObject.cpp:2203
virtual Expected< std::unique_ptr< Object > > create(bool EnsureSymtab) const =0
const SectionBase * getStrTab() const
Definition: ELFObject.h:717
void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, uint16_t Shndx, uint64_t SymbolSize)
Definition: ELFObject.cpp:697
void updateSymbols(function_ref< void(Symbol &)> Callable)
Definition: ELFObject.cpp:739
virtual Error finalize()=0
virtual Error write()=0
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ELF.h:27
@ STB_GLOBAL
Definition: ELF.h:1243
@ STB_LOCAL
Definition: ELF.h:1242
@ STB_WEAK
Definition: ELF.h:1244
@ EM_X86_64
Definition: ELF.h:178
@ EM_AARCH64
Definition: ELF.h:280
@ EM_ARM
Definition: ELF.h:156
@ SHT_STRTAB
Definition: ELF.h:1002
@ SHT_PROGBITS
Definition: ELF.h:1000
@ SHT_REL
Definition: ELF.h:1008
@ SHT_ARM_ATTRIBUTES
Definition: ELF.h:1056
@ SHT_NOBITS
Definition: ELF.h:1007
@ SHT_SYMTAB
Definition: ELF.h:1001
@ SHT_LLVM_PART_EHDR
Definition: ELF.h:1031
@ SHT_RELA
Definition: ELF.h:1003
@ SHT_LLVM_PART_PHDR
Definition: ELF.h:1032
@ SHT_NOTE
Definition: ELF.h:1006
@ SHN_ABS
Definition: ELF.h:991
@ SHN_UNDEF
Definition: ELF.h:985
@ STV_INTERNAL
Definition: ELF.h:1273
@ STV_HIDDEN
Definition: ELF.h:1274
@ STV_PROTECTED
Definition: ELF.h:1275
@ STV_DEFAULT
Definition: ELF.h:1272
@ SHF_MERGE
Definition: ELF.h:1095
@ SHF_STRINGS
Definition: ELF.h:1098
@ SHF_INFO_LINK
Definition: ELF.h:1101
@ SHF_EXCLUDE
Definition: ELF.h:1123
@ SHF_ALLOC
Definition: ELF.h:1089
@ SHF_MASKPROC
Definition: ELF.h:1133
@ SHF_LINK_ORDER
Definition: ELF.h:1104
@ SHF_GROUP
Definition: ELF.h:1111
@ SHF_COMPRESSED
Definition: ELF.h:1117
@ SHF_MASKOS
Definition: ELF.h:1127
@ SHF_X86_64_LARGE
Definition: ELF.h:1152
@ SHF_WRITE
Definition: ELF.h:1086
@ SHF_TLS
Definition: ELF.h:1114
@ SHF_EXECINSTR
Definition: ELF.h:1092
@ STT_FUNC
Definition: ELF.h:1256
@ STT_NOTYPE
Definition: ELF.h:1254
@ STT_SECTION
Definition: ELF.h:1257
@ STT_FILE
Definition: ELF.h:1258
@ STT_GNU_IFUNC
Definition: ELF.h:1261
@ STT_OBJECT
Definition: ELF.h:1255
Error executeObjcopyOnIHex(const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In, which must represent an IHex file,...
Definition: ELFObjcopy.cpp:789
Error executeObjcopyOnBinary(const CommonConfig &Config, const ELFConfig &ELFConfig, object::ELFObjectFileBase &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In and writes the result into Out.
Definition: ELFObjcopy.cpp:822
Error executeObjcopyOnRawBinary(const CommonConfig &Config, const ELFConfig &ELFConfig, MemoryBuffer &In, raw_ostream &Out)
Apply the transformations described by Config and ELFConfig to In, which is treated as a raw binary i...
Definition: ELFObjcopy.cpp:804
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1325
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1244
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition: Casting.h:548
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
std::function< uint64_t(uint64_t)> EntryExpr
Definition: ELFConfig.h:27
std::shared_ptr< MemoryBuffer > SectionData
Definition: CommonConfig.h:198
std::optional< SectionFlag > NewFlags
Definition: CommonConfig.h:79