llvm.org GIT mirror llvm / d86591e
[yaml2obj] - Make interface of `NameToIdxMap` class be human friendly and fix users. This patch inverses the values returned by `addName` and `lookup` methods of the class mentioned so that they now return true on success and false on failure. Also, it does minor code cleanup. Differential revision: https://reviews.llvm.org/D61190 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359818 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 5 months ago
1 changed file(s) with 78 addition(s) and 77 deletion(s). Raw diff Collapse all Expand all
5656 } // end anonymous namespace
5757
5858 // Used to keep track of section and symbol names, so that in the YAML file
59 // sections and symbols can be referenced by name instead of by index.
60 namespace {
61 class NameToIdxMap {
62 StringMap Map;
63 public:
64 /// \returns true if name is already present in the map.
65 bool addName(StringRef Name, unsigned i) {
66 return !Map.insert(std::make_pair(Name, (int)i)).second;
67 }
68 /// \returns true if name is not present in the map
69 bool lookup(StringRef Name, unsigned &Idx) const {
70 StringMap::const_iterator I = Map.find(Name);
71 if (I == Map.end())
72 return true;
73 Idx = I->getValue();
74 return false;
75 }
76 /// asserts if name is not present in the map
77 unsigned get(StringRef Name) const {
78 unsigned Idx = 0;
79 auto missing = lookup(Name, Idx);
80 (void)missing;
81 assert(!missing && "Expected section not found in index");
82 return Idx;
83 }
84 unsigned size() const { return Map.size(); }
85 };
59 // sections and symbols can be referenced by name instead of by index.
60 namespace {
61 class NameToIdxMap {
62 StringMap Map;
63
64 public:
65 /// \Returns false if name is already present in the map.
66 bool addName(StringRef Name, unsigned Ndx) {
67 return Map.insert({Name, Ndx}).second;
68 }
69 /// \Returns false if name is not present in the map.
70 bool lookup(StringRef Name, unsigned &Idx) const {
71 auto I = Map.find(Name);
72 if (I == Map.end())
73 return false;
74 Idx = I->getValue();
75 return true;
76 }
77 /// Asserts if name is not present in the map.
78 unsigned get(StringRef Name) const {
79 unsigned Idx;
80 if (lookup(Name, Idx))
81 return Idx;
82 assert(false && "Expected section not found in index");
83 return 0;
84 }
85 unsigned size() const { return Map.size(); }
86 };
8687 } // end anonymous namespace
8788
8889 template
234235 PHeaders.push_back(Phdr);
235236 }
236237 }
237
238 static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
239 StringRef IndexSrc, unsigned &IndexDest) {
240 if (SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
241 WithColor::error() << "Unknown section referenced: '" << IndexSrc
242 << "' at YAML section '" << SecName << "'.\n";
243 return false;
238
239 static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
240 StringRef IndexSrc, unsigned &IndexDest) {
241 if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
242 WithColor::error() << "Unknown section referenced: '" << IndexSrc
243 << "' at YAML section '" << SecName << "'.\n";
244 return false;
244245 }
245246 return true;
246247 }
391392 for (auto &YamlPhdr : Doc.ProgramHeaders) {
392393 Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
393394
394 std::vector Sections;
395 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
396 unsigned Index;
397 if (SN2I.lookup(SecName.Section, Index)) {
398 WithColor::error() << "Unknown section referenced: '" << SecName.Section
399 << "' by program header.\n";
400 exit(1);
395 std::vector Sections;
396 for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
397 unsigned Index;
398 if (!SN2I.lookup(SecName.Section, Index)) {
399 WithColor::error() << "Unknown section referenced: '" << SecName.Section
400 << "' by program header.\n";
401 exit(1);
401402 }
402403 Sections.push_back(&SHeaders[Index]);
403404 }
468469 zero(Symbol);
469470 if (!Sym.Name.empty())
470471 Symbol.st_name = Strtab.getOffset(Sym.Name);
471 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
472 if (!Sym.Section.empty()) {
473 unsigned Index;
474 if (SN2I.lookup(Sym.Section, Index)) {
475 WithColor::error() << "Unknown section referenced: '" << Sym.Section
476 << "' by YAML symbol " << Sym.Name << ".\n";
477 exit(1);
472 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
473 if (!Sym.Section.empty()) {
474 unsigned Index;
475 if (!SN2I.lookup(Sym.Section, Index)) {
476 WithColor::error() << "Unknown section referenced: '" << Sym.Section
477 << "' by YAML symbol " << Sym.Name << ".\n";
478 exit(1);
478479 }
479480 Symbol.st_shndx = Index;
480481 } else if (Sym.Index) {
542543 auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
543544
544545 for (const auto &Rel : Section.Relocations) {
545 unsigned SymIdx = 0;
546 // If a relocation references a symbol, try to look one up in the symbol
547 // table. If it is not there, treat the value as a symbol index.
548 if (Rel.Symbol && SymN2I.lookup(*Rel.Symbol, SymIdx) &&
549 !to_integer(*Rel.Symbol, SymIdx)) {
550 WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
551 << "' at YAML section '" << Section.Name << "'.\n";
546 unsigned SymIdx = 0;
547 // If a relocation references a symbol, try to look one up in the symbol
548 // table. If it is not there, treat the value as a symbol index.
549 if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) &&
550 !to_integer(*Rel.Symbol, SymIdx)) {
551 WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
552 << "' at YAML section '" << Section.Name << "'.\n";
552553 return false;
553554 }
554555
578579 "Section type is not SHT_GROUP");
579580
580581 SHeader.sh_entsize = 4;
581 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
582
583 unsigned SymIdx;
584 if (SymN2I.lookup(Section.Signature, SymIdx) &&
585 !to_integer(Section.Signature, SymIdx)) {
586 WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
587 << "' at YAML section '" << Section.Name << "'.\n";
582 SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
583
584 unsigned SymIdx;
585 if (!SymN2I.lookup(Section.Signature, SymIdx) &&
586 !to_integer(Section.Signature, SymIdx)) {
587 WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
588 << "' at YAML section '" << Section.Name << "'.\n";
588589 return false;
589590 }
590591 SHeader.sh_info = SymIdx;
779780
780781 template bool ELFState::buildSectionIndex() {
781782 for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
782 StringRef Name = Doc.Sections[i]->Name;
783 DotShStrtab.add(Name);
784 // "+ 1" to take into account the SHT_NULL entry.
785 if (SN2I.addName(Name, i + 1)) {
786 WithColor::error() << "Repeated section name: '" << Name
787 << "' at YAML section number " << i << ".\n";
788 return false;
783 StringRef Name = Doc.Sections[i]->Name;
784 DotShStrtab.add(Name);
785 // "+ 1" to take into account the SHT_NULL entry.
786 if (!SN2I.addName(Name, i + 1)) {
787 WithColor::error() << "Repeated section name: '" << Name
788 << "' at YAML section number " << i << ".\n";
789 return false;
789790 }
790791 }
791792
792793 auto SecNo = 1 + Doc.Sections.size();
793794 // Add special sections after input sections, if necessary.
794795 for (StringRef Name : implicitSectionNames())
795 if (!SN2I.addName(Name, SecNo)) {
796 if (SN2I.addName(Name, SecNo)) {
796797 // Account for this section, since it wasn't in the Doc
797798 ++SecNo;
798799 DotShStrtab.add(Name);
815816 "' after global in Symbols list.\n";
816817 return false;
817818 }
818 if (Sym.Binding.value != ELF::STB_LOCAL)
819 GlobalSymbolSeen = true;
820
821 if (!Name.empty() && SymN2I.addName(Name, I)) {
822 WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
823 return false;
824 }
819 if (Sym.Binding.value != ELF::STB_LOCAL)
820 GlobalSymbolSeen = true;
821
822 if (!Name.empty() && !SymN2I.addName(Name, I)) {
823 WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
824 return false;
825 }
825826 }
826827 return true;
827828 }