llvm.org GIT mirror llvm / 890c305
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363930 91177308-0d34-0410-b5e6-96231b3b80d8 George Rimar 2 months ago
1 changed file(s) with 44 addition(s) and 47 deletion(s). Raw diff Collapse all Expand all
150150 ELFYAML::Section *YAMLSec);
151151 void setProgramHeaderLayout(std::vector &PHeaders,
152152 std::vector &SHeaders);
153 void addSymbols(ArrayRef Symbols, std::vector &Syms,
154 const StringTableBuilder &Strtab);
155153 bool writeSectionContent(Elf_Shdr &SHeader,
156154 const ELFYAML::RawContentSection &Section,
157155 ContiguousBlobAccumulator &CBA);
374372 }
375373
376374 template
375 static std::vector
376 toELFSymbols(NameToIdxMap &SN2I, ArrayRef Symbols,
377 const StringTableBuilder &Strtab) {
378 using Elf_Sym = typename ELFT::Sym;
379
380 std::vector Ret;
381 Ret.resize(Symbols.size() + 1);
382
383 size_t I = 0;
384 for (const auto &Sym : Symbols) {
385 Elf_Sym &Symbol = Ret[++I];
386
387 // If NameIndex, which contains the name offset, is explicitly specified, we
388 // use it. This is useful for preparing broken objects. Otherwise, we add
389 // the specified Name to the string table builder to get its offset.
390 if (Sym.NameIndex)
391 Symbol.st_name = *Sym.NameIndex;
392 else if (!Sym.Name.empty())
393 Symbol.st_name = Strtab.getOffset(Sym.Name);
394
395 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
396 if (!Sym.Section.empty()) {
397 unsigned Index;
398 if (!SN2I.lookup(Sym.Section, Index)) {
399 WithColor::error() << "Unknown section referenced: '" << Sym.Section
400 << "' by YAML symbol " << Sym.Name << ".\n";
401 exit(1);
402 }
403 Symbol.st_shndx = Index;
404 } else if (Sym.Index) {
405 Symbol.st_shndx = *Sym.Index;
406 }
407 // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
408 Symbol.st_value = Sym.Value;
409 Symbol.st_other = Sym.Other;
410 Symbol.st_size = Sym.Size;
411 }
412
413 return Ret;
414 }
415
416 template
377417 void ELFState::initSymtabSectionHeader(Elf_Shdr &SHeader,
378418 SymtabType STType,
379419 ContiguousBlobAccumulator &CBA,
450490 return;
451491 }
452492
453 std::vector Syms;
454 {
455 // Ensure STN_UNDEF is present
456 Elf_Sym Sym;
457 zero(Sym);
458 Syms.push_back(Sym);
459 }
460
461 addSymbols(Symbols, Syms, IsStatic ? DotStrtab : DotDynstr);
493 std::vector Syms =
494 toELFSymbols(SN2I, Symbols, IsStatic ? DotStrtab : DotDynstr);
462495 writeArrayData(OS, makeArrayRef(Syms));
463496 SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
464497 }
577610 }
578611
579612 template
580 void ELFState::addSymbols(ArrayRef Symbols,
581 std::vector &Syms,
582 const StringTableBuilder &Strtab) {
583 for (const auto &Sym : Symbols) {
584 Elf_Sym Symbol;
585 zero(Symbol);
586
587 // If NameIndex, which contains the name offset, is explicitly specified, we
588 // use it. This is useful for preparing broken objects. Otherwise, we add
589 // the specified Name to the string table builder to get its offset.
590 if (Sym.NameIndex)
591 Symbol.st_name = *Sym.NameIndex;
592 else if (!Sym.Name.empty())
593 Symbol.st_name = Strtab.getOffset(Sym.Name);
594
595 Symbol.setBindingAndType(Sym.Binding, Sym.Type);
596 if (!Sym.Section.empty()) {
597 unsigned Index;
598 if (!SN2I.lookup(Sym.Section, Index)) {
599 WithColor::error() << "Unknown section referenced: '" << Sym.Section
600 << "' by YAML symbol " << Sym.Name << ".\n";
601 exit(1);
602 }
603 Symbol.st_shndx = Index;
604 } else if (Sym.Index) {
605 Symbol.st_shndx = *Sym.Index;
606 }
607 // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
608 Symbol.st_value = Sym.Value;
609 Symbol.st_other = Sym.Other;
610 Symbol.st_size = Sym.Size;
611 Syms.push_back(Symbol);
612 }
613 }
614
615 template
616613 bool ELFState::writeSectionContent(
617614 Elf_Shdr &SHeader, const ELFYAML::RawContentSection &Section,
618615 ContiguousBlobAccumulator &CBA) {