Lines Matching +full:static +full:- +full:config

1 //===- ELFObjcopy.cpp -----------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
54 static bool isDebugSection(const SectionBase &Sec) { in isDebugSection()
58 static bool isDWOSection(const SectionBase &Sec) { in isDWOSection()
62 static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { in onlyKeepDWOPred()
71 static Expected<uint64_t> getNewShfFlags(SectionFlag AllFlags, in getNewShfFlags()
96 static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, in getSectionFlagsPreserveMask()
110 static void setSectionType(SectionBase &Sec, uint64_t Type) { in setSectionType()
111 // If Sec's type is changed from SHT_NOBITS due to --set-section-flags, in setSectionType()
118 static Error setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags, in setSectionFlagsAndType()
126 // may promote more non-ALLOC sections than GNU objcopy, but it is fine as in setSectionFlagsAndType()
127 // non-ALLOC SHT_NOBITS sections do not make much sense. in setSectionFlagsAndType()
136 static ElfType getOutputElfType(const Binary &Bin) { in getOutputElfType()
149 static ElfType getOutputElfType(const MachineInfo &MI) { in getOutputElfType()
157 static std::unique_ptr<Writer> createELFWriter(const CommonConfig &Config, in createELFWriter() argument
163 return std::make_unique<ELFWriter<ELF32LE>>(Obj, Out, !Config.StripSections, in createELFWriter()
164 Config.OnlyKeepDebug); in createELFWriter()
166 return std::make_unique<ELFWriter<ELF64LE>>(Obj, Out, !Config.StripSections, in createELFWriter()
167 Config.OnlyKeepDebug); in createELFWriter()
169 return std::make_unique<ELFWriter<ELF32BE>>(Obj, Out, !Config.StripSections, in createELFWriter()
170 Config.OnlyKeepDebug); in createELFWriter()
172 return std::make_unique<ELFWriter<ELF64BE>>(Obj, Out, !Config.StripSections, in createELFWriter()
173 Config.OnlyKeepDebug); in createELFWriter()
178 static std::unique_ptr<Writer> createWriter(const CommonConfig &Config, in createWriter() argument
181 switch (Config.OutputFormat) { in createWriter()
183 return std::make_unique<BinaryWriter>(Obj, Out, Config); in createWriter()
185 return std::make_unique<IHexWriter>(Obj, Out, Config.OutputFilename); in createWriter()
187 return std::make_unique<SRECWriter>(Obj, Out, Config.OutputFilename); in createWriter()
189 return createELFWriter(Config, Obj, Out, OutputElfType); in createWriter()
193 static Error dumpSectionToFile(StringRef SecName, StringRef Filename, in dumpSectionToFile()
207 Buf->getBufferStart()); in dumpSectionToFile()
208 if (Error E = Buf->commit()) in dumpSectionToFile()
217 Error Object::compressOrDecompressSections(const CommonConfig &Config) { in compressOrDecompressSections() argument
225 for (auto &[Matcher, T] : Config.compressSections) in compressOrDecompressSections()
228 // Handle --compress-debug-sections and --decompress-debug-sections, which in compressOrDecompressSections()
229 // apply to non-ALLOC debug sections. in compressOrDecompressSections()
231 if (Config.CompressionType != DebugCompressionType::None) in compressOrDecompressSections()
232 CType = Config.CompressionType; in compressOrDecompressSections()
233 else if (Config.DecompressDebugSections) in compressOrDecompressSections()
263 static bool isAArch64MappingSymbol(const Symbol &Sym) { in isAArch64MappingSymbol()
273 static bool isArmMappingSymbol(const Symbol &Sym) { in isArmMappingSymbol()
285 static bool isRequiredByABISymbol(const Object &Obj, const Symbol &Sym) { in isRequiredByABISymbol()
298 static bool isUnneededSymbol(const Symbol &Sym) { in isUnneededSymbol()
304 static Error updateAndRemoveSymbols(const CommonConfig &Config, in updateAndRemoveSymbols() argument
311 Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { in updateAndRemoveSymbols()
312 if (Config.SymbolsToSkip.matches(Sym.Name)) in updateAndRemoveSymbols()
320 Config.SymbolsToLocalize.matches(Sym.Name))) in updateAndRemoveSymbols()
330 // --globalize-symbol: promote a symbol to global in updateAndRemoveSymbols()
331 // --keep-global-symbol: all symbols except for these should be made local in updateAndRemoveSymbols()
333 // If --globalize-symbol is specified for a given symbol, it will be in updateAndRemoveSymbols()
335 // --keep-global-symbol. Because of that, make sure to check in updateAndRemoveSymbols()
336 // --globalize-symbol second. in updateAndRemoveSymbols()
337 if (!Config.SymbolsToKeepGlobal.empty() && in updateAndRemoveSymbols()
338 !Config.SymbolsToKeepGlobal.matches(Sym.Name) && in updateAndRemoveSymbols()
342 if (Config.SymbolsToGlobalize.matches(Sym.Name) && in updateAndRemoveSymbols()
347 if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding != STB_LOCAL) in updateAndRemoveSymbols()
350 if (Config.Weaken && Sym.Binding != STB_LOCAL && in updateAndRemoveSymbols()
354 const auto I = Config.SymbolsToRename.find(Sym.Name); in updateAndRemoveSymbols()
355 if (I != Config.SymbolsToRename.end()) in updateAndRemoveSymbols()
356 Sym.Name = std::string(I->getValue()); in updateAndRemoveSymbols()
358 if (!Config.SymbolsPrefixRemove.empty() && Sym.Type != STT_SECTION) in updateAndRemoveSymbols()
359 if (Sym.Name.compare(0, Config.SymbolsPrefixRemove.size(), in updateAndRemoveSymbols()
360 Config.SymbolsPrefixRemove) == 0) in updateAndRemoveSymbols()
361 Sym.Name = Sym.Name.substr(Config.SymbolsPrefixRemove.size()); in updateAndRemoveSymbols()
363 if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION) in updateAndRemoveSymbols()
364 Sym.Name = (Config.SymbolsPrefix + Sym.Name).str(); in updateAndRemoveSymbols()
370 if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() || in updateAndRemoveSymbols()
371 !Config.OnlySection.empty() || Config.DiscardMode != DiscardType::None) { in updateAndRemoveSymbols()
377 if (Config.SymbolsToKeep.matches(Sym.Name) || in updateAndRemoveSymbols()
381 if (Config.SymbolsToRemove.matches(Sym.Name)) in updateAndRemoveSymbols()
384 if (Config.StripAll || Config.StripAllGNU) in updateAndRemoveSymbols()
390 if (Config.StripDebug && Sym.Type == STT_FILE) in updateAndRemoveSymbols()
393 if ((Config.StripUnneeded || in updateAndRemoveSymbols()
394 Config.UnneededSymbolsToRemove.matches(Sym.Name)) && in updateAndRemoveSymbols()
399 if ((Config.DiscardMode == DiscardType::All || in updateAndRemoveSymbols()
400 (Config.DiscardMode == DiscardType::Locals && in updateAndRemoveSymbols()
407 if (!Config.OnlySection.empty() && Sym.getShndx() == SHN_UNDEF) in updateAndRemoveSymbols()
417 static Error replaceAndRemoveSections(const CommonConfig &Config, in replaceAndRemoveSections() argument
422 if (!Config.ToRemove.empty()) { in replaceAndRemoveSections()
423 RemovePred = [&Config](const SectionBase &Sec) { in replaceAndRemoveSections()
424 return Config.ToRemove.matches(Sec.Name); in replaceAndRemoveSections()
428 if (Config.StripDWO) in replaceAndRemoveSections()
433 if (Config.ExtractDWO) in replaceAndRemoveSections()
438 if (Config.StripAllGNU) in replaceAndRemoveSections()
456 if (Config.StripSections) { in replaceAndRemoveSections()
462 if (Config.StripDebug || Config.StripUnneeded) { in replaceAndRemoveSections()
468 if (Config.StripNonAlloc) in replaceAndRemoveSections()
477 if (Config.StripAll) in replaceAndRemoveSections()
490 // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=943798 in replaceAndRemoveSections()
498 if (Config.ExtractPartition || Config.ExtractMainPartition) { in replaceAndRemoveSections()
509 if (!Config.OnlySection.empty()) { in replaceAndRemoveSections()
510 RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { in replaceAndRemoveSections()
512 if (Config.OnlySection.matches(Sec.Name)) in replaceAndRemoveSections()
523 (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec)) in replaceAndRemoveSections()
531 if (!Config.KeepSection.empty()) { in replaceAndRemoveSections()
532 RemovePred = [&Config, RemovePred](const SectionBase &Sec) { in replaceAndRemoveSections()
534 if (Config.KeepSection.matches(Sec.Name)) in replaceAndRemoveSections()
542 // If the option --keep-symbol has been specified in replaceAndRemoveSections()
546 if ((!Config.SymbolsToKeep.empty() || ELFConfig.KeepFileSymbols) && in replaceAndRemoveSections()
547 Obj.SymbolTable && !Obj.SymbolTable->empty()) { in replaceAndRemoveSections()
549 if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) in replaceAndRemoveSections()
558 if (Error E = Obj.compressOrDecompressSections(Config)) in replaceAndRemoveSections()
565 static void addSymbol(Object &Obj, const NewSymbolInfo &SymInfo, in addSymbol()
568 uint64_t Value = Sec ? Sec->Addr + SymInfo.Value : SymInfo.Value; in addSymbol()
613 Obj.SymbolTable->addSymbol( in addSymbol()
618 static Error
622 NewSection.SectionData->getBufferStart()), in handleUserSection()
623 NewSection.SectionData->getBufferSize()); in handleUserSection()
627 static Error verifyNoteSection(StringRef Name, endianness Endianness, in verifyNoteSection()
682 static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, in handleArgs() argument
684 if (Config.OutputArch) { in handleArgs()
685 Obj.Machine = Config.OutputArch->EMachine; in handleArgs()
686 Obj.OSABI = Config.OutputArch->OSABI; in handleArgs()
689 if (!Config.SplitDWO.empty() && Config.ExtractDWO) { in handleArgs()
696 for (StringRef Flag : Config.DumpSection) { in handleArgs()
708 if (Error E = replaceAndRemoveSections(Config, ELFConfig, Obj)) in handleArgs()
711 if (Error E = updateAndRemoveSymbols(Config, ELFConfig, Obj)) in handleArgs()
714 if (!Config.SetSectionAlignment.empty()) { in handleArgs()
716 auto I = Config.SetSectionAlignment.find(Sec.Name); in handleArgs()
717 if (I != Config.SetSectionAlignment.end()) in handleArgs()
718 Sec.Align = I->second; in handleArgs()
722 if (Config.ChangeSectionLMAValAll != 0) { in handleArgs()
725 if (Config.ChangeSectionLMAValAll > 0 && in handleArgs()
726 Seg.PAddr > std::numeric_limits<uint64_t>::max() - in handleArgs()
727 Config.ChangeSectionLMAValAll) { in handleArgs()
732 Twine::utohexstr(Config.ChangeSectionLMAValAll) + in handleArgs()
734 } else if (Config.ChangeSectionLMAValAll < 0 && in handleArgs()
735 Seg.PAddr < std::numeric_limits<uint64_t>::min() - in handleArgs()
736 Config.ChangeSectionLMAValAll) { in handleArgs()
741 Twine::utohexstr(std::abs(Config.ChangeSectionLMAValAll)) + in handleArgs()
744 Seg.PAddr += Config.ChangeSectionLMAValAll; in handleArgs()
749 if (Config.OnlyKeepDebug) in handleArgs()
758 for (const NewSectionInfo &AddedSection : Config.AddSection) { in handleArgs()
759 auto AddSection = [&](StringRef Name, ArrayRef<uint8_t> Data) -> Error { in handleArgs()
762 if (Name.starts_with(".note") && Name != ".note.GNU-stack") { in handleArgs()
773 for (const NewSectionInfo &NewSection : Config.UpdateSection) { in handleArgs()
781 if (!Config.AddGnuDebugLink.empty()) in handleArgs()
782 Obj.addSection<GnuDebugLinkSection>(Config.AddGnuDebugLink, in handleArgs()
783 Config.GnuDebugLinkCRC32); in handleArgs()
787 if (!Obj.SymbolTable && !Config.SymbolsToAdd.empty()) in handleArgs()
791 for (const NewSymbolInfo &SI : Config.SymbolsToAdd) in handleArgs()
794 // --set-section-{flags,type} work with sections added by --add-section. in handleArgs()
795 if (!Config.SetSectionFlags.empty() || !Config.SetSectionType.empty()) { in handleArgs()
797 const auto Iter = Config.SetSectionFlags.find(Sec.Name); in handleArgs()
798 if (Iter != Config.SetSectionFlags.end()) { in handleArgs()
799 const SectionFlagsUpdate &SFU = Iter->second; in handleArgs()
803 auto It2 = Config.SetSectionType.find(Sec.Name); in handleArgs()
804 if (It2 != Config.SetSectionType.end()) in handleArgs()
805 setSectionType(Sec, It2->second); in handleArgs()
809 if (!Config.SectionsToRename.empty()) { in handleArgs()
814 const auto Iter = Config.SectionsToRename.find(Sec.Name); in handleArgs()
815 if (Iter != Config.SectionsToRename.end()) { in handleArgs()
816 const SectionRename &SR = Iter->second; in handleArgs()
825 // their explicit '--rename-section' commands until after their target in handleArgs()
835 auto Iter = RenamedSections.find(RelocSec->getSection()); in handleArgs()
837 RelocSec->Name = (RelocSec->getNamePrefix() + (*Iter)->Name).str(); in handleArgs()
842 // should be done after renaming the section by Config.SectionToRename to in handleArgs()
844 if (!Config.AllocSectionsPrefix.empty()) { in handleArgs()
848 Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str(); in handleArgs()
858 const SectionBase *TargetSec = RelocSec->getSection(); in handleArgs()
859 if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) { in handleArgs()
861 // don't add Config.AllocSectionsPrefix because we've already added in handleArgs()
862 // the prefix to TargetSec->Name. Otherwise, if the relocation in handleArgs()
865 Sec.Name = (RelocSec->getNamePrefix() + TargetSec->Name).str(); in handleArgs()
867 Sec.Name = (RelocSec->getNamePrefix() + Config.AllocSectionsPrefix + in handleArgs()
868 TargetSec->Name) in handleArgs()
880 static Error writeOutput(const CommonConfig &Config, Object &Obj, in writeOutput() argument
883 createWriter(Config, Obj, Out, OutputElfType); in writeOutput()
884 if (Error E = Writer->finalize()) in writeOutput()
886 return Writer->write(); in writeOutput()
889 Error objcopy::elf::executeObjcopyOnIHex(const CommonConfig &Config, in executeObjcopyOnIHex() argument
898 getOutputElfType(Config.OutputArch.value_or(MachineInfo())); in executeObjcopyOnIHex()
899 if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj)) in executeObjcopyOnIHex()
901 return writeOutput(Config, **Obj, Out, OutputElfType); in executeObjcopyOnIHex()
904 Error objcopy::elf::executeObjcopyOnRawBinary(const CommonConfig &Config, in executeObjcopyOnRawBinary() argument
913 // Prefer OutputArch (-O<format>) if set, otherwise fallback to BinaryArch in executeObjcopyOnRawBinary()
914 // (-B<arch>). in executeObjcopyOnRawBinary()
916 getOutputElfType(Config.OutputArch.value_or(MachineInfo())); in executeObjcopyOnRawBinary()
917 if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj)) in executeObjcopyOnRawBinary()
919 return writeOutput(Config, **Obj, Out, OutputElfType); in executeObjcopyOnRawBinary()
922 Error objcopy::elf::executeObjcopyOnBinary(const CommonConfig &Config, in executeObjcopyOnBinary() argument
926 ELFReader Reader(&In, Config.ExtractPartition); in executeObjcopyOnBinary()
928 Reader.create(!Config.SymbolsToAdd.empty()); in executeObjcopyOnBinary()
931 // Prefer OutputArch (-O<format>) if set, otherwise infer it from the input. in executeObjcopyOnBinary()
932 const ElfType OutputElfType = Config.OutputArch in executeObjcopyOnBinary()
933 ? getOutputElfType(*Config.OutputArch) in executeObjcopyOnBinary()
936 if (Error E = handleArgs(Config, ELFConfig, OutputElfType, **Obj)) in executeObjcopyOnBinary()
937 return createFileError(Config.InputFilename, std::move(E)); in executeObjcopyOnBinary()
939 if (Error E = writeOutput(Config, **Obj, Out, OutputElfType)) in executeObjcopyOnBinary()
940 return createFileError(Config.InputFilename, std::move(E)); in executeObjcopyOnBinary()