Index: llvm/trunk/tools/yaml2obj/yaml2elf.cpp =================================================================== --- llvm/trunk/tools/yaml2obj/yaml2elf.cpp +++ llvm/trunk/tools/yaml2obj/yaml2elf.cpp @@ -151,8 +151,6 @@ ELFYAML::Section *YAMLSec); void setProgramHeaderLayout(std::vector &PHeaders, std::vector &SHeaders); - void addSymbols(ArrayRef Symbols, std::vector &Syms, - const StringTableBuilder &Strtab); bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA); @@ -375,6 +373,48 @@ } template +static std::vector +toELFSymbols(NameToIdxMap &SN2I, ArrayRef Symbols, + const StringTableBuilder &Strtab) { + using Elf_Sym = typename ELFT::Sym; + + std::vector Ret; + Ret.resize(Symbols.size() + 1); + + size_t I = 0; + for (const auto &Sym : Symbols) { + Elf_Sym &Symbol = Ret[++I]; + + // If NameIndex, which contains the name offset, is explicitly specified, we + // use it. This is useful for preparing broken objects. Otherwise, we add + // the specified Name to the string table builder to get its offset. + if (Sym.NameIndex) + Symbol.st_name = *Sym.NameIndex; + else if (!Sym.Name.empty()) + Symbol.st_name = Strtab.getOffset(Sym.Name); + + Symbol.setBindingAndType(Sym.Binding, Sym.Type); + if (!Sym.Section.empty()) { + unsigned Index; + if (!SN2I.lookup(Sym.Section, Index)) { + WithColor::error() << "Unknown section referenced: '" << Sym.Section + << "' by YAML symbol " << Sym.Name << ".\n"; + exit(1); + } + Symbol.st_shndx = Index; + } else if (Sym.Index) { + Symbol.st_shndx = *Sym.Index; + } + // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier. + Symbol.st_value = Sym.Value; + Symbol.st_other = Sym.Other; + Symbol.st_size = Sym.Size; + } + + return Ret; +} + +template void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType, ContiguousBlobAccumulator &CBA, @@ -451,15 +491,8 @@ return; } - std::vector Syms; - { - // Ensure STN_UNDEF is present - Elf_Sym Sym; - zero(Sym); - Syms.push_back(Sym); - } - - addSymbols(Symbols, Syms, IsStatic ? DotStrtab : DotDynstr); + std::vector Syms = + toELFSymbols(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr); writeArrayData(OS, makeArrayRef(Syms)); SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); } @@ -578,42 +611,6 @@ } template -void ELFState::addSymbols(ArrayRef Symbols, - std::vector &Syms, - const StringTableBuilder &Strtab) { - for (const auto &Sym : Symbols) { - Elf_Sym Symbol; - zero(Symbol); - - // If NameIndex, which contains the name offset, is explicitly specified, we - // use it. This is useful for preparing broken objects. Otherwise, we add - // the specified Name to the string table builder to get its offset. - if (Sym.NameIndex) - Symbol.st_name = *Sym.NameIndex; - else if (!Sym.Name.empty()) - Symbol.st_name = Strtab.getOffset(Sym.Name); - - Symbol.setBindingAndType(Sym.Binding, Sym.Type); - if (!Sym.Section.empty()) { - unsigned Index; - if (!SN2I.lookup(Sym.Section, Index)) { - WithColor::error() << "Unknown section referenced: '" << Sym.Section - << "' by YAML symbol " << Sym.Name << ".\n"; - exit(1); - } - Symbol.st_shndx = Index; - } else if (Sym.Index) { - Symbol.st_shndx = *Sym.Index; - } - // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier. - Symbol.st_value = Sym.Value; - Symbol.st_other = Sym.Other; - Symbol.st_size = Sym.Size; - Syms.push_back(Symbol); - } -} - -template bool ELFState::writeSectionContent( Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section, ContiguousBlobAccumulator &CBA) {