Index: tools/yaml2obj/yaml2elf.cpp =================================================================== --- tools/yaml2obj/yaml2elf.cpp +++ tools/yaml2obj/yaml2elf.cpp @@ -61,26 +61,25 @@ // sections and symbols can be referenced by name instead of by index. namespace { class NameToIdxMap { - StringMap Map; + StringMap Map; public: - /// \returns true if name is already present in the map. - bool addName(StringRef Name, unsigned i) { - return !Map.insert(std::make_pair(Name, (int)i)).second; + /// \returns false if name is already present in the map. + bool addName(StringRef Name, unsigned Ndx) { + return Map.insert({Name, Ndx}).second; } - /// \returns true if name is not present in the map + /// \returns false if name is not present in the map. bool lookup(StringRef Name, unsigned &Idx) const { - StringMap::const_iterator I = Map.find(Name); + auto I = Map.find(Name); if (I == Map.end()) - return true; + return false; Idx = I->getValue(); - return false; + return true; } - /// asserts if name is not present in the map + /// asserts if name is not present in the map. unsigned get(StringRef Name) const { unsigned Idx = 0; - auto missing = lookup(Name, Idx); - (void)missing; - assert(!missing && "Expected section not found in index"); + if (!lookup(Name, Idx)) + assert(false && "Expected section not found in index"); return Idx; } unsigned size() const { return Map.size(); } @@ -239,7 +238,7 @@ static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName, StringRef IndexSrc, unsigned &IndexDest) { - if (SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) { + if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) { WithColor::error() << "Unknown section referenced: '" << IndexSrc << "' at YAML section '" << SecName << "'.\n"; return false; @@ -398,9 +397,7 @@ // Find the minimum offset for the program header. for (auto SecName : YamlPhdr.Sections) { - uint32_t Index = 0; - SN2I.lookup(SecName.Section, Index); - const auto &SHeader = SHeaders[Index]; + const auto &SHeader = SHeaders[SN2I.get(SecName.Section)]; PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset); } } @@ -412,9 +409,7 @@ } else { PHeader.p_filesz = 0; for (auto SecName : YamlPhdr.Sections) { - uint32_t Index = 0; - SN2I.lookup(SecName.Section, Index); - const auto &SHeader = SHeaders[Index]; + const auto &SHeader = SHeaders[SN2I.get(SecName.Section)]; uint64_t EndOfSection; if (SHeader.sh_type == llvm::ELF::SHT_NOBITS) EndOfSection = SHeader.sh_offset; @@ -434,9 +429,7 @@ } else { PHeader.p_memsz = PHeader.p_filesz; for (auto SecName : YamlPhdr.Sections) { - uint32_t Index = 0; - SN2I.lookup(SecName.Section, Index); - const auto &SHeader = SHeaders[Index]; + const auto &SHeader = SHeaders[SN2I.get(SecName.Section)]; if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz) PHeader.p_memsz += SHeader.sh_size; } @@ -450,9 +443,7 @@ } else { PHeader.p_align = 1; for (auto SecName : YamlPhdr.Sections) { - uint32_t Index = 0; - SN2I.lookup(SecName.Section, Index); - const auto &SHeader = SHeaders[Index]; + const auto &SHeader = SHeaders[SN2I.get(SecName.Section)]; if (SHeader.sh_offset == PHeader.p_offset) PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign); } @@ -472,7 +463,7 @@ Symbol.setBindingAndType(Sym.Binding, Sym.Type); if (!Sym.Section.empty()) { unsigned Index; - if (SN2I.lookup(Sym.Section, Index)) { + if (!SN2I.lookup(Sym.Section, Index)) { WithColor::error() << "Unknown section referenced: '" << Sym.Section << "' by YAML symbol " << Sym.Name << ".\n"; exit(1); @@ -546,7 +537,7 @@ unsigned SymIdx = 0; // If a relocation references a symbol, try to look one up in the symbol // table. If it is not there, treat the value as a symbol index. - if (Rel.Symbol && SymN2I.lookup(*Rel.Symbol, SymIdx) && + if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) && !to_integer(*Rel.Symbol, SymIdx)) { WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol << "' at YAML section '" << Section.Name << "'.\n"; @@ -582,7 +573,7 @@ SHeader.sh_size = SHeader.sh_entsize * Section.Members.size(); unsigned SymIdx; - if (SymN2I.lookup(Section.Signature, SymIdx) && + if (!SymN2I.lookup(Section.Signature, SymIdx) && !to_integer(Section.Signature, SymIdx)) { WithColor::error() << "Unknown symbol referenced: '" << Section.Signature << "' at YAML section '" << Section.Name << "'.\n"; @@ -783,7 +774,7 @@ StringRef Name = Doc.Sections[i]->Name; DotShStrtab.add(Name); // "+ 1" to take into account the SHT_NULL entry. - if (SN2I.addName(Name, i + 1)) { + if (!SN2I.addName(Name, i + 1)) { WithColor::error() << "Repeated section name: '" << Name << "' at YAML section number " << i << ".\n"; return false; @@ -793,7 +784,7 @@ auto SecNo = 1 + Doc.Sections.size(); // Add special sections after input sections, if necessary. for (StringRef Name : implicitSectionNames()) - if (!SN2I.addName(Name, SecNo)) { + if (SN2I.addName(Name, SecNo)) { // Account for this section, since it wasn't in the Doc ++SecNo; DotShStrtab.add(Name); @@ -819,7 +810,7 @@ if (Sym.Binding.value != ELF::STB_LOCAL) GlobalSymbolSeen = true; - if (!Name.empty() && SymN2I.addName(Name, I)) { + if (!Name.empty() && !SymN2I.addName(Name, I)) { WithColor::error() << "Repeated symbol name: '" << Name << "'.\n"; return false; }