mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-23 03:02:36 +01:00
[yaml2obj] - Convert ELFState<ELFT>::addSymbols
method to toELFSymbols
helper. NFCI.
ELFState<ELFT>::addSymbols method looks a bit strange. User code have to create the destination symbols vector outside, add a null symbol and then pass it to addSymbols when it seems the more natural logic is to isolate all work with symbols inside some function, build the list right there and return it. Differential revision: https://reviews.llvm.org/D63493 llvm-svn: 363930
This commit is contained in:
parent
612d65a071
commit
853b959dd0
@ -151,8 +151,6 @@ class ELFState {
|
||||
ELFYAML::Section *YAMLSec);
|
||||
void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
|
||||
std::vector<Elf_Shdr> &SHeaders);
|
||||
void addSymbols(ArrayRef<ELFYAML::Symbol> Symbols, std::vector<Elf_Sym> &Syms,
|
||||
const StringTableBuilder &Strtab);
|
||||
bool writeSectionContent(Elf_Shdr &SHeader,
|
||||
const ELFYAML::RawContentSection &Section,
|
||||
ContiguousBlobAccumulator &CBA);
|
||||
@ -374,6 +372,48 @@ static uint64_t writeRawSectionData(raw_ostream &OS,
|
||||
return *RawSec.Size;
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
static std::vector<typename ELFT::Sym>
|
||||
toELFSymbols(NameToIdxMap &SN2I, ArrayRef<ELFYAML::Symbol> Symbols,
|
||||
const StringTableBuilder &Strtab) {
|
||||
using Elf_Sym = typename ELFT::Sym;
|
||||
|
||||
std::vector<Elf_Sym> 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 <class ELFT>
|
||||
void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
|
||||
SymtabType STType,
|
||||
@ -451,15 +491,8 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<Elf_Sym> Syms;
|
||||
{
|
||||
// Ensure STN_UNDEF is present
|
||||
Elf_Sym Sym;
|
||||
zero(Sym);
|
||||
Syms.push_back(Sym);
|
||||
}
|
||||
|
||||
addSymbols(Symbols, Syms, IsStatic ? DotStrtab : DotDynstr);
|
||||
std::vector<Elf_Sym> Syms =
|
||||
toELFSymbols<ELFT>(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr);
|
||||
writeArrayData(OS, makeArrayRef(Syms));
|
||||
SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
|
||||
}
|
||||
@ -577,42 +610,6 @@ void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
|
||||
}
|
||||
}
|
||||
|
||||
template <class ELFT>
|
||||
void ELFState<ELFT>::addSymbols(ArrayRef<ELFYAML::Symbol> Symbols,
|
||||
std::vector<Elf_Sym> &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 <class ELFT>
|
||||
bool ELFState<ELFT>::writeSectionContent(
|
||||
Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
|
||||
|
Loading…
Reference in New Issue
Block a user