Skip to content

Commit 3662127

Browse files
author
George Rimar
committed
[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 llvm-svn: 359818
1 parent d179ce9 commit 3662127

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed

llvm/tools/yaml2obj/yaml2elf.cpp

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,34 @@ class ContiguousBlobAccumulator {
5757
} // end anonymous namespace
5858

5959
// Used to keep track of section and symbol names, so that in the YAML file
60-
// sections and symbols can be referenced by name instead of by index.
61-
namespace {
62-
class NameToIdxMap {
63-
StringMap<int> Map;
64-
public:
65-
/// \returns true if name is already present in the map.
66-
bool addName(StringRef Name, unsigned i) {
67-
return !Map.insert(std::make_pair(Name, (int)i)).second;
68-
}
69-
/// \returns true if name is not present in the map
70-
bool lookup(StringRef Name, unsigned &Idx) const {
71-
StringMap<int>::const_iterator I = Map.find(Name);
72-
if (I == Map.end())
73-
return true;
74-
Idx = I->getValue();
75-
return false;
76-
}
77-
/// asserts if name is not present in the map
78-
unsigned get(StringRef Name) const {
79-
unsigned Idx = 0;
80-
auto missing = lookup(Name, Idx);
81-
(void)missing;
82-
assert(!missing && "Expected section not found in index");
83-
return Idx;
84-
}
85-
unsigned size() const { return Map.size(); }
86-
};
60+
// sections and symbols can be referenced by name instead of by index.
61+
namespace {
62+
class NameToIdxMap {
63+
StringMap<unsigned> Map;
64+
65+
public:
66+
/// \Returns false if name is already present in the map.
67+
bool addName(StringRef Name, unsigned Ndx) {
68+
return Map.insert({Name, Ndx}).second;
69+
}
70+
/// \Returns false if name is not present in the map.
71+
bool lookup(StringRef Name, unsigned &Idx) const {
72+
auto I = Map.find(Name);
73+
if (I == Map.end())
74+
return false;
75+
Idx = I->getValue();
76+
return true;
77+
}
78+
/// Asserts if name is not present in the map.
79+
unsigned get(StringRef Name) const {
80+
unsigned Idx;
81+
if (lookup(Name, Idx))
82+
return Idx;
83+
assert(false && "Expected section not found in index");
84+
return 0;
85+
}
86+
unsigned size() const { return Map.size(); }
87+
};
8788
} // end anonymous namespace
8889

8990
template <class T>
@@ -235,13 +236,13 @@ void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
235236
PHeaders.push_back(Phdr);
236237
}
237238
}
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;
239+
240+
static bool convertSectionIndex(NameToIdxMap &SN2I, StringRef SecName,
241+
StringRef IndexSrc, unsigned &IndexDest) {
242+
if (!SN2I.lookup(IndexSrc, IndexDest) && !to_integer(IndexSrc, IndexDest)) {
243+
WithColor::error() << "Unknown section referenced: '" << IndexSrc
244+
<< "' at YAML section '" << SecName << "'.\n";
245+
return false;
245246
}
246247
return true;
247248
}
@@ -392,13 +393,13 @@ void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
392393
for (auto &YamlPhdr : Doc.ProgramHeaders) {
393394
Elf_Phdr &PHeader = PHeaders[PhdrIdx++];
394395

395-
std::vector<Elf_Shdr *> 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);
396+
std::vector<Elf_Shdr *> Sections;
397+
for (const ELFYAML::SectionName &SecName : YamlPhdr.Sections) {
398+
unsigned Index;
399+
if (!SN2I.lookup(SecName.Section, Index)) {
400+
WithColor::error() << "Unknown section referenced: '" << SecName.Section
401+
<< "' by program header.\n";
402+
exit(1);
402403
}
403404
Sections.push_back(&SHeaders[Index]);
404405
}
@@ -469,13 +470,13 @@ void ELFState<ELFT>::addSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
469470
zero(Symbol);
470471
if (!Sym.Name.empty())
471472
Symbol.st_name = Strtab.getOffset(Sym.Name);
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);
473+
Symbol.setBindingAndType(Sym.Binding, Sym.Type);
474+
if (!Sym.Section.empty()) {
475+
unsigned Index;
476+
if (!SN2I.lookup(Sym.Section, Index)) {
477+
WithColor::error() << "Unknown section referenced: '" << Sym.Section
478+
<< "' by YAML symbol " << Sym.Name << ".\n";
479+
exit(1);
479480
}
480481
Symbol.st_shndx = Index;
481482
} else if (Sym.Index) {
@@ -543,13 +544,13 @@ ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
543544
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
544545

545546
for (const auto &Rel : Section.Relocations) {
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";
547+
unsigned SymIdx = 0;
548+
// If a relocation references a symbol, try to look one up in the symbol
549+
// table. If it is not there, treat the value as a symbol index.
550+
if (Rel.Symbol && !SymN2I.lookup(*Rel.Symbol, SymIdx) &&
551+
!to_integer(*Rel.Symbol, SymIdx)) {
552+
WithColor::error() << "Unknown symbol referenced: '" << *Rel.Symbol
553+
<< "' at YAML section '" << Section.Name << "'.\n";
553554
return false;
554555
}
555556

@@ -579,13 +580,13 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
579580
"Section type is not SHT_GROUP");
580581

581582
SHeader.sh_entsize = 4;
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";
583+
SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
584+
585+
unsigned SymIdx;
586+
if (!SymN2I.lookup(Section.Signature, SymIdx) &&
587+
!to_integer(Section.Signature, SymIdx)) {
588+
WithColor::error() << "Unknown symbol referenced: '" << Section.Signature
589+
<< "' at YAML section '" << Section.Name << "'.\n";
589590
return false;
590591
}
591592
SHeader.sh_info = SymIdx;
@@ -780,20 +781,20 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
780781

781782
template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
782783
for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
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;
784+
StringRef Name = Doc.Sections[i]->Name;
785+
DotShStrtab.add(Name);
786+
// "+ 1" to take into account the SHT_NULL entry.
787+
if (!SN2I.addName(Name, i + 1)) {
788+
WithColor::error() << "Repeated section name: '" << Name
789+
<< "' at YAML section number " << i << ".\n";
790+
return false;
790791
}
791792
}
792793

793794
auto SecNo = 1 + Doc.Sections.size();
794795
// Add special sections after input sections, if necessary.
795796
for (StringRef Name : implicitSectionNames())
796-
if (!SN2I.addName(Name, SecNo)) {
797+
if (SN2I.addName(Name, SecNo)) {
797798
// Account for this section, since it wasn't in the Doc
798799
++SecNo;
799800
DotShStrtab.add(Name);
@@ -816,13 +817,13 @@ bool ELFState<ELFT>::buildSymbolIndex(ArrayRef<ELFYAML::Symbol> Symbols) {
816817
"' after global in Symbols list.\n";
817818
return false;
818819
}
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-
}
820+
if (Sym.Binding.value != ELF::STB_LOCAL)
821+
GlobalSymbolSeen = true;
822+
823+
if (!Name.empty() && !SymN2I.addName(Name, I)) {
824+
WithColor::error() << "Repeated symbol name: '" << Name << "'.\n";
825+
return false;
826+
}
826827
}
827828
return true;
828829
}

0 commit comments

Comments
 (0)