2013-06-11 07:44:15 +08:00
|
|
|
//===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The ELF component of yaml2obj.
|
2013-06-11 07:44:15 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "yaml2obj.h"
|
2013-10-13 05:29:16 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2014-07-03 10:01:39 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2013-08-09 06:27:13 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2016-03-02 03:15:06 +08:00
|
|
|
#include "llvm/ObjectYAML/ELFYAML.h"
|
2013-06-11 07:44:15 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2018-04-22 05:11:59 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2013-06-11 07:44:15 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-06-14 06:20:01 +08:00
|
|
|
// This class is used to build up a contiguous binary blob while keeping
|
|
|
|
// track of an offset in the output (which notionally begins at
|
|
|
|
// `InitialOffset`).
|
2013-06-15 08:31:46 +08:00
|
|
|
namespace {
|
2013-06-14 06:20:01 +08:00
|
|
|
class ContiguousBlobAccumulator {
|
|
|
|
const uint64_t InitialOffset;
|
2013-06-21 03:11:41 +08:00
|
|
|
SmallVector<char, 128> Buf;
|
2013-06-14 06:20:01 +08:00
|
|
|
raw_svector_ostream OS;
|
|
|
|
|
2013-06-22 08:47:43 +08:00
|
|
|
/// \returns The new offset.
|
|
|
|
uint64_t padToAlignment(unsigned Align) {
|
2015-07-08 18:12:40 +08:00
|
|
|
if (Align == 0)
|
|
|
|
Align = 1;
|
2013-06-22 08:47:43 +08:00
|
|
|
uint64_t CurrentOffset = InitialOffset + OS.tell();
|
2016-01-15 05:06:47 +08:00
|
|
|
uint64_t AlignedOffset = alignTo(CurrentOffset, Align);
|
2013-06-22 08:47:43 +08:00
|
|
|
for (; CurrentOffset != AlignedOffset; ++CurrentOffset)
|
|
|
|
OS.write('\0');
|
|
|
|
return AlignedOffset; // == CurrentOffset;
|
|
|
|
}
|
|
|
|
|
2013-06-14 06:20:01 +08:00
|
|
|
public:
|
2013-06-21 03:11:41 +08:00
|
|
|
ContiguousBlobAccumulator(uint64_t InitialOffset_)
|
|
|
|
: InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
|
2013-06-22 08:47:43 +08:00
|
|
|
template <class Integer>
|
2015-07-08 18:12:40 +08:00
|
|
|
raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align) {
|
2013-06-22 08:47:43 +08:00
|
|
|
Offset = padToAlignment(Align);
|
|
|
|
return OS;
|
|
|
|
}
|
2013-06-14 06:20:01 +08:00
|
|
|
void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
|
|
|
|
};
|
2013-06-15 08:31:46 +08:00
|
|
|
} // end anonymous namespace
|
2013-06-14 06:20:01 +08:00
|
|
|
|
2014-04-06 17:02:55 +08:00
|
|
|
// Used to keep track of section and symbol names, so that in the YAML file
|
|
|
|
// sections and symbols can be referenced by name instead of by index.
|
2013-06-15 08:31:46 +08:00
|
|
|
namespace {
|
2014-04-06 17:02:55 +08:00
|
|
|
class NameToIdxMap {
|
2013-06-15 08:25:26 +08:00
|
|
|
StringMap<int> Map;
|
|
|
|
public:
|
|
|
|
/// \returns true if name is already present in the map.
|
2014-04-06 17:02:55 +08:00
|
|
|
bool addName(StringRef Name, unsigned i) {
|
2014-11-19 13:49:42 +08:00
|
|
|
return !Map.insert(std::make_pair(Name, (int)i)).second;
|
2013-06-15 08:25:26 +08:00
|
|
|
}
|
|
|
|
/// \returns true if name is not present in the map
|
2014-04-06 17:02:55 +08:00
|
|
|
bool lookup(StringRef Name, unsigned &Idx) const {
|
|
|
|
StringMap<int>::const_iterator I = Map.find(Name);
|
2013-06-15 08:25:26 +08:00
|
|
|
if (I == Map.end())
|
|
|
|
return true;
|
|
|
|
Idx = I->getValue();
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-09 22:53:43 +08:00
|
|
|
/// 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");
|
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
unsigned size() const { return Map.size(); }
|
2013-06-15 08:25:26 +08:00
|
|
|
};
|
2013-06-15 08:31:46 +08:00
|
|
|
} // end anonymous namespace
|
2013-06-15 08:25:26 +08:00
|
|
|
|
2013-06-14 06:19:48 +08:00
|
|
|
template <class T>
|
2013-10-13 05:29:16 +08:00
|
|
|
static size_t arrayDataSize(ArrayRef<T> A) {
|
|
|
|
return A.size() * sizeof(T);
|
2013-06-14 06:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2013-10-13 05:29:16 +08:00
|
|
|
static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) {
|
|
|
|
OS.write((const char *)A.data(), arrayDataSize(A));
|
2013-06-14 06:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static void zero(T &Obj) {
|
|
|
|
memset(&Obj, 0, sizeof(Obj));
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:11:44 +08:00
|
|
|
namespace {
|
2018-05-01 23:54:18 +08:00
|
|
|
/// "Single point of truth" for the ELF file construction.
|
2013-06-21 03:11:44 +08:00
|
|
|
/// TODO: This class still has a ways to go before it is truly a "single
|
|
|
|
/// point of truth".
|
|
|
|
template <class ELFT>
|
|
|
|
class ELFState {
|
2018-01-12 10:28:31 +08:00
|
|
|
typedef typename ELFT::Ehdr Elf_Ehdr;
|
|
|
|
typedef typename ELFT::Phdr Elf_Phdr;
|
|
|
|
typedef typename ELFT::Shdr Elf_Shdr;
|
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
typedef typename ELFT::Rel Elf_Rel;
|
|
|
|
typedef typename ELFT::Rela Elf_Rela;
|
2018-06-29 05:07:34 +08:00
|
|
|
typedef typename ELFT::Relr Elf_Relr;
|
2018-07-24 02:49:04 +08:00
|
|
|
typedef typename ELFT::Dyn Elf_Dyn;
|
2014-04-03 00:34:54 +08:00
|
|
|
|
2017-11-17 02:10:15 +08:00
|
|
|
enum class SymtabType { Static, Dynamic };
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The future ".strtab" section.
|
2015-10-24 05:48:05 +08:00
|
|
|
StringTableBuilder DotStrtab{StringTableBuilder::ELF};
|
2013-06-21 03:11:44 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The future ".shstrtab" section.
|
2015-10-24 05:48:05 +08:00
|
|
|
StringTableBuilder DotShStrtab{StringTableBuilder::ELF};
|
2014-04-03 00:34:54 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// The future ".dynstr" section.
|
2017-11-17 02:10:15 +08:00
|
|
|
StringTableBuilder DotDynstr{StringTableBuilder::ELF};
|
|
|
|
|
2014-04-06 17:02:55 +08:00
|
|
|
NameToIdxMap SN2I;
|
2014-04-11 12:13:39 +08:00
|
|
|
NameToIdxMap SymN2I;
|
2014-04-03 00:34:40 +08:00
|
|
|
const ELFYAML::Object &Doc;
|
2013-06-21 04:59:34 +08:00
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
bool buildSectionIndex();
|
2014-04-11 12:13:39 +08:00
|
|
|
bool buildSymbolIndex(std::size_t &StartIndex,
|
|
|
|
const std::vector<ELFYAML::Symbol> &Symbols);
|
2014-04-03 00:34:54 +08:00
|
|
|
void initELFHeader(Elf_Ehdr &Header);
|
2017-07-20 04:38:46 +08:00
|
|
|
void initProgramHeaders(std::vector<Elf_Phdr> &PHeaders);
|
2014-04-03 00:34:54 +08:00
|
|
|
bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
|
|
|
|
ContiguousBlobAccumulator &CBA);
|
2017-11-17 02:10:15 +08:00
|
|
|
void initSymtabSectionHeader(Elf_Shdr &SHeader, SymtabType STType,
|
2014-04-03 00:34:54 +08:00
|
|
|
ContiguousBlobAccumulator &CBA);
|
|
|
|
void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
|
|
|
|
StringTableBuilder &STB,
|
|
|
|
ContiguousBlobAccumulator &CBA);
|
2017-07-20 04:38:46 +08:00
|
|
|
void setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
|
|
|
|
std::vector<Elf_Shdr> &SHeaders);
|
2014-04-03 00:34:54 +08:00
|
|
|
void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
|
2017-11-17 02:10:15 +08:00
|
|
|
std::vector<Elf_Sym> &Syms, unsigned SymbolBinding,
|
|
|
|
const StringTableBuilder &Strtab);
|
2014-04-11 12:13:39 +08:00
|
|
|
void writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::RawContentSection &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA);
|
|
|
|
bool writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::RelocationSection &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA);
|
2015-02-21 12:28:26 +08:00
|
|
|
bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
|
|
|
|
ContiguousBlobAccumulator &CBA);
|
2015-05-07 23:40:48 +08:00
|
|
|
bool writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::MipsABIFlags &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA);
|
2017-11-17 02:10:15 +08:00
|
|
|
bool hasDynamicSymbols() const;
|
|
|
|
SmallVector<const char *, 5> implicitSectionNames() const;
|
2014-04-03 00:34:40 +08:00
|
|
|
|
|
|
|
// - SHT_NULL entry (placed first, i.e. 0'th entry)
|
2017-11-17 02:10:15 +08:00
|
|
|
// - symbol table (.symtab) (defaults to after last yaml section)
|
|
|
|
// - string table (.strtab) (defaults to after .symtab)
|
|
|
|
// - section header string table (.shstrtab) (defaults to after .strtab)
|
|
|
|
// - dynamic symbol table (.dynsym) (defaults to after .shstrtab)
|
|
|
|
// - dynamic string table (.dynstr) (defaults to after .dynsym)
|
2017-11-09 22:53:43 +08:00
|
|
|
unsigned getDotSymTabSecNo() const { return SN2I.get(".symtab"); }
|
|
|
|
unsigned getDotStrTabSecNo() const { return SN2I.get(".strtab"); }
|
|
|
|
unsigned getDotShStrTabSecNo() const { return SN2I.get(".shstrtab"); }
|
2017-11-17 02:10:15 +08:00
|
|
|
unsigned getDotDynSymSecNo() const { return SN2I.get(".dynsym"); }
|
|
|
|
unsigned getDotDynStrSecNo() const { return SN2I.get(".dynstr"); }
|
2017-11-09 22:53:43 +08:00
|
|
|
unsigned getSectionCount() const { return SN2I.size() + 1; }
|
2013-06-21 03:11:44 +08:00
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
ELFState(const ELFYAML::Object &D) : Doc(D) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
|
2013-06-21 03:11:44 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-06-21 08:33:01 +08:00
|
|
|
template <class ELFT>
|
2014-04-03 00:34:54 +08:00
|
|
|
void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
zero(Header);
|
|
|
|
Header.e_ident[EI_MAG0] = 0x7f;
|
|
|
|
Header.e_ident[EI_MAG1] = 'E';
|
|
|
|
Header.e_ident[EI_MAG2] = 'L';
|
|
|
|
Header.e_ident[EI_MAG3] = 'F';
|
|
|
|
Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
|
|
|
|
bool IsLittleEndian = ELFT::TargetEndianness == support::little;
|
|
|
|
Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
|
|
|
|
Header.e_ident[EI_VERSION] = EV_CURRENT;
|
|
|
|
Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
|
|
|
|
Header.e_ident[EI_ABIVERSION] = 0;
|
|
|
|
Header.e_type = Doc.Header.Type;
|
|
|
|
Header.e_machine = Doc.Header.Machine;
|
|
|
|
Header.e_version = EV_CURRENT;
|
|
|
|
Header.e_entry = Doc.Header.Entry;
|
2017-07-20 04:38:46 +08:00
|
|
|
Header.e_phoff = sizeof(Header);
|
2014-04-03 00:34:54 +08:00
|
|
|
Header.e_flags = Doc.Header.Flags;
|
|
|
|
Header.e_ehsize = sizeof(Elf_Ehdr);
|
2017-07-20 04:38:46 +08:00
|
|
|
Header.e_phentsize = sizeof(Elf_Phdr);
|
|
|
|
Header.e_phnum = Doc.ProgramHeaders.size();
|
2014-04-03 00:34:54 +08:00
|
|
|
Header.e_shentsize = sizeof(Elf_Shdr);
|
2017-07-20 04:38:46 +08:00
|
|
|
// Immediately following the ELF header and program headers.
|
|
|
|
Header.e_shoff =
|
|
|
|
sizeof(Header) + sizeof(Elf_Phdr) * Doc.ProgramHeaders.size();
|
2014-04-03 00:34:54 +08:00
|
|
|
Header.e_shnum = getSectionCount();
|
|
|
|
Header.e_shstrndx = getDotShStrTabSecNo();
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:38:46 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void ELFState<ELFT>::initProgramHeaders(std::vector<Elf_Phdr> &PHeaders) {
|
|
|
|
for (const auto &YamlPhdr : Doc.ProgramHeaders) {
|
|
|
|
Elf_Phdr Phdr;
|
|
|
|
Phdr.p_type = YamlPhdr.Type;
|
|
|
|
Phdr.p_flags = YamlPhdr.Flags;
|
|
|
|
Phdr.p_vaddr = YamlPhdr.VAddr;
|
|
|
|
Phdr.p_paddr = YamlPhdr.PAddr;
|
|
|
|
PHeaders.push_back(Phdr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
|
|
|
|
ContiguousBlobAccumulator &CBA) {
|
|
|
|
// Ensure SHN_UNDEF entry is present. An all-zero section header is a
|
|
|
|
// valid SHN_UNDEF entry since SHT_NULL == 0.
|
|
|
|
Elf_Shdr SHeader;
|
|
|
|
zero(SHeader);
|
|
|
|
SHeaders.push_back(SHeader);
|
|
|
|
|
|
|
|
for (const auto &Sec : Doc.Sections) {
|
|
|
|
zero(SHeader);
|
2014-05-01 03:38:09 +08:00
|
|
|
SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
|
2014-04-11 12:13:39 +08:00
|
|
|
SHeader.sh_type = Sec->Type;
|
|
|
|
SHeader.sh_flags = Sec->Flags;
|
|
|
|
SHeader.sh_addr = Sec->Address;
|
|
|
|
SHeader.sh_addralign = Sec->AddressAlign;
|
2014-04-03 00:34:54 +08:00
|
|
|
|
2014-04-11 12:13:39 +08:00
|
|
|
if (!Sec->Link.empty()) {
|
2013-06-21 09:11:48 +08:00
|
|
|
unsigned Index;
|
2014-04-11 12:13:39 +08:00
|
|
|
if (SN2I.lookup(Sec->Link, Index)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Unknown section referenced: '" << Sec->Link
|
|
|
|
<< "' at YAML section '" << Sec->Name << "'.\n";
|
2014-04-11 12:13:39 +08:00
|
|
|
return false;
|
2013-06-21 09:11:48 +08:00
|
|
|
}
|
2014-04-03 00:34:54 +08:00
|
|
|
SHeader.sh_link = Index;
|
|
|
|
}
|
2014-04-11 12:13:39 +08:00
|
|
|
|
|
|
|
if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get()))
|
|
|
|
writeSectionContent(SHeader, *S, CBA);
|
|
|
|
else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) {
|
|
|
|
if (S->Link.empty())
|
|
|
|
// For relocation section set link to .symtab by default.
|
|
|
|
SHeader.sh_link = getDotSymTabSecNo();
|
|
|
|
|
|
|
|
unsigned Index;
|
|
|
|
if (SN2I.lookup(S->Info, Index)) {
|
2015-08-28 07:11:20 +08:00
|
|
|
if (S->Info.getAsInteger(0, Index)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Unknown section referenced: '" << S->Info
|
|
|
|
<< "' at YAML section '" << S->Name << "'.\n";
|
2015-08-28 07:11:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-11 12:13:39 +08:00
|
|
|
}
|
|
|
|
SHeader.sh_info = Index;
|
|
|
|
|
2015-02-21 12:28:26 +08:00
|
|
|
if (!writeSectionContent(SHeader, *S, CBA))
|
|
|
|
return false;
|
|
|
|
} else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
|
|
|
|
unsigned SymIdx;
|
|
|
|
if (SymN2I.lookup(S->Info, SymIdx)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Unknown symbol referenced: '" << S->Info
|
|
|
|
<< "' at YAML section '" << S->Name << "'.\n";
|
2015-02-21 12:28:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SHeader.sh_info = SymIdx;
|
2014-04-11 12:13:39 +08:00
|
|
|
if (!writeSectionContent(SHeader, *S, CBA))
|
|
|
|
return false;
|
2015-05-07 23:40:48 +08:00
|
|
|
} else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
|
|
|
|
if (!writeSectionContent(SHeader, *S, CBA))
|
|
|
|
return false;
|
2015-07-04 07:00:54 +08:00
|
|
|
} else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
|
|
|
|
SHeader.sh_entsize = 0;
|
|
|
|
SHeader.sh_size = S->Size;
|
|
|
|
// SHT_NOBITS section does not have content
|
|
|
|
// so just to setup the section offset.
|
2015-07-08 18:12:40 +08:00
|
|
|
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
|
2014-04-11 12:13:39 +08:00
|
|
|
} else
|
|
|
|
llvm_unreachable("Unknown section type");
|
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
SHeaders.push_back(SHeader);
|
2013-06-21 08:27:50 +08:00
|
|
|
}
|
2014-04-03 00:34:54 +08:00
|
|
|
return true;
|
2013-06-21 08:27:50 +08:00
|
|
|
}
|
|
|
|
|
2013-06-19 07:14:03 +08:00
|
|
|
template <class ELFT>
|
2014-04-03 00:34:54 +08:00
|
|
|
void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
|
2017-11-17 02:10:15 +08:00
|
|
|
SymtabType STType,
|
2014-04-03 00:34:54 +08:00
|
|
|
ContiguousBlobAccumulator &CBA) {
|
|
|
|
zero(SHeader);
|
2017-11-17 02:10:15 +08:00
|
|
|
bool IsStatic = STType == SymtabType::Static;
|
|
|
|
SHeader.sh_name = DotShStrtab.getOffset(IsStatic ? ".symtab" : ".dynsym");
|
|
|
|
SHeader.sh_type = IsStatic ? ELF::SHT_SYMTAB : ELF::SHT_DYNSYM;
|
|
|
|
SHeader.sh_link = IsStatic ? getDotStrTabSecNo() : getDotDynStrSecNo();
|
|
|
|
const auto &Symbols = IsStatic ? Doc.Symbols : Doc.DynamicSymbols;
|
|
|
|
auto &Strtab = IsStatic ? DotStrtab : DotDynstr;
|
2013-06-21 08:27:50 +08:00
|
|
|
// One greater than symbol table index of the last local symbol.
|
2017-11-17 02:10:15 +08:00
|
|
|
SHeader.sh_info = Symbols.Local.size() + 1;
|
2013-06-19 07:14:03 +08:00
|
|
|
SHeader.sh_entsize = sizeof(Elf_Sym);
|
2015-07-10 02:23:02 +08:00
|
|
|
SHeader.sh_addralign = 8;
|
2013-06-19 07:14:03 +08:00
|
|
|
|
|
|
|
std::vector<Elf_Sym> Syms;
|
2013-06-19 07:19:17 +08:00
|
|
|
{
|
|
|
|
// Ensure STN_UNDEF is present
|
|
|
|
Elf_Sym Sym;
|
|
|
|
zero(Sym);
|
|
|
|
Syms.push_back(Sym);
|
|
|
|
}
|
2014-05-01 03:38:09 +08:00
|
|
|
|
2017-11-17 02:10:15 +08:00
|
|
|
// Add symbol names to .strtab or .dynstr.
|
|
|
|
for (const auto &Sym : Symbols.Local)
|
|
|
|
Strtab.add(Sym.Name);
|
|
|
|
for (const auto &Sym : Symbols.Global)
|
|
|
|
Strtab.add(Sym.Name);
|
|
|
|
for (const auto &Sym : Symbols.Weak)
|
|
|
|
Strtab.add(Sym.Name);
|
|
|
|
Strtab.finalize();
|
2014-05-01 03:38:09 +08:00
|
|
|
|
2017-11-17 02:10:15 +08:00
|
|
|
addSymbols(Symbols.Local, Syms, ELF::STB_LOCAL, Strtab);
|
|
|
|
addSymbols(Symbols.Global, Syms, ELF::STB_GLOBAL, Strtab);
|
|
|
|
addSymbols(Symbols.Weak, Syms, ELF::STB_WEAK, Strtab);
|
2013-06-19 07:14:03 +08:00
|
|
|
|
2015-07-08 18:12:40 +08:00
|
|
|
writeArrayData(
|
|
|
|
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign),
|
|
|
|
makeArrayRef(Syms));
|
2013-10-13 05:29:16 +08:00
|
|
|
SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
|
2013-06-19 07:14:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-11 07:44:15 +08:00
|
|
|
template <class ELFT>
|
2014-04-03 00:34:54 +08:00
|
|
|
void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
|
|
|
|
StringTableBuilder &STB,
|
|
|
|
ContiguousBlobAccumulator &CBA) {
|
|
|
|
zero(SHeader);
|
2014-05-01 03:38:09 +08:00
|
|
|
SHeader.sh_name = DotShStrtab.getOffset(Name);
|
2014-04-03 00:34:54 +08:00
|
|
|
SHeader.sh_type = ELF::SHT_STRTAB;
|
2016-10-05 06:43:25 +08:00
|
|
|
STB.write(CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign));
|
|
|
|
SHeader.sh_size = STB.getSize();
|
2014-04-03 00:34:54 +08:00
|
|
|
SHeader.sh_addralign = 1;
|
|
|
|
}
|
2013-06-14 06:19:48 +08:00
|
|
|
|
2017-07-20 04:38:46 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
|
|
|
|
std::vector<Elf_Shdr> &SHeaders) {
|
|
|
|
uint32_t PhdrIdx = 0;
|
|
|
|
for (auto &YamlPhdr : Doc.ProgramHeaders) {
|
|
|
|
auto &PHeader = PHeaders[PhdrIdx++];
|
|
|
|
|
|
|
|
if (YamlPhdr.Sections.size())
|
|
|
|
PHeader.p_offset = UINT32_MAX;
|
|
|
|
else
|
|
|
|
PHeader.p_offset = 0;
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
PHeader.p_offset = std::min(PHeader.p_offset, SHeader.sh_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the maximum offset of the end of a section in order to set p_filesz.
|
|
|
|
PHeader.p_filesz = 0;
|
|
|
|
for (auto SecName : YamlPhdr.Sections) {
|
|
|
|
uint32_t Index = 0;
|
|
|
|
SN2I.lookup(SecName.Section, Index);
|
|
|
|
const auto &SHeader = SHeaders[Index];
|
|
|
|
uint64_t EndOfSection;
|
|
|
|
if (SHeader.sh_type == llvm::ELF::SHT_NOBITS)
|
|
|
|
EndOfSection = SHeader.sh_offset;
|
|
|
|
else
|
|
|
|
EndOfSection = SHeader.sh_offset + SHeader.sh_size;
|
|
|
|
uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
|
|
|
|
EndOfSegment = std::max(EndOfSegment, EndOfSection);
|
|
|
|
PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the memory size by adding the size of sections at the end of the
|
|
|
|
// segment. These should be empty (size of zero) and NOBITS sections.
|
|
|
|
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];
|
|
|
|
if (SHeader.sh_offset == PHeader.p_offset + PHeader.p_filesz)
|
|
|
|
PHeader.p_memsz += SHeader.sh_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the alignment of the segment to be the same as the maximum alignment
|
2017-11-02 07:14:48 +08:00
|
|
|
// of the sections with the same offset so that by default the segment
|
2017-07-20 04:38:46 +08:00
|
|
|
// has a valid and sensible alignment.
|
2017-11-02 07:14:48 +08:00
|
|
|
if (YamlPhdr.Align) {
|
|
|
|
PHeader.p_align = *YamlPhdr.Align;
|
|
|
|
} else {
|
|
|
|
PHeader.p_align = 1;
|
|
|
|
for (auto SecName : YamlPhdr.Sections) {
|
|
|
|
uint32_t Index = 0;
|
|
|
|
SN2I.lookup(SecName.Section, Index);
|
|
|
|
const auto &SHeader = SHeaders[Index];
|
|
|
|
if (SHeader.sh_offset == PHeader.p_offset)
|
|
|
|
PHeader.p_align = std::max(PHeader.p_align, SHeader.sh_addralign);
|
|
|
|
}
|
2017-07-20 04:38:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
|
|
|
|
std::vector<Elf_Sym> &Syms,
|
2017-11-17 02:10:15 +08:00
|
|
|
unsigned SymbolBinding,
|
|
|
|
const StringTableBuilder &Strtab) {
|
2014-04-03 00:34:54 +08:00
|
|
|
for (const auto &Sym : Symbols) {
|
|
|
|
Elf_Sym Symbol;
|
|
|
|
zero(Symbol);
|
|
|
|
if (!Sym.Name.empty())
|
2017-11-17 02:10:15 +08:00
|
|
|
Symbol.st_name = Strtab.getOffset(Sym.Name);
|
2014-04-03 00:34:54 +08:00
|
|
|
Symbol.setBindingAndType(SymbolBinding, Sym.Type);
|
|
|
|
if (!Sym.Section.empty()) {
|
|
|
|
unsigned Index;
|
2014-04-06 17:02:55 +08:00
|
|
|
if (SN2I.lookup(Sym.Section, Index)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Unknown section referenced: '" << Sym.Section
|
|
|
|
<< "' by YAML symbol " << Sym.Name << ".\n";
|
2014-04-03 00:34:54 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Symbol.st_shndx = Index;
|
2017-09-08 04:44:16 +08:00
|
|
|
} else if (Sym.Index) {
|
|
|
|
Symbol.st_shndx = *Sym.Index;
|
|
|
|
}
|
|
|
|
// else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier.
|
2014-04-03 00:34:54 +08:00
|
|
|
Symbol.st_value = Sym.Value;
|
2014-11-07 06:46:24 +08:00
|
|
|
Symbol.st_other = Sym.Other;
|
2014-04-03 00:34:54 +08:00
|
|
|
Symbol.st_size = Sym.Size;
|
|
|
|
Syms.push_back(Symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 12:13:39 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void
|
|
|
|
ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::RawContentSection &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA) {
|
2014-05-17 00:01:00 +08:00
|
|
|
assert(Section.Size >= Section.Content.binary_size() &&
|
|
|
|
"Section size and section content are inconsistent");
|
2015-07-08 18:12:40 +08:00
|
|
|
raw_ostream &OS =
|
|
|
|
CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
|
2014-05-17 00:01:00 +08:00
|
|
|
Section.Content.writeAsBinary(OS);
|
|
|
|
for (auto i = Section.Content.binary_size(); i < Section.Size; ++i)
|
|
|
|
OS.write(0);
|
2018-08-07 16:11:38 +08:00
|
|
|
if (Section.EntSize)
|
|
|
|
SHeader.sh_entsize = *Section.EntSize;
|
|
|
|
else if (Section.Type == llvm::ELF::SHT_RELR)
|
2018-06-29 05:07:34 +08:00
|
|
|
SHeader.sh_entsize = sizeof(Elf_Relr);
|
2018-07-24 02:49:04 +08:00
|
|
|
else if (Section.Type == llvm::ELF::SHT_DYNAMIC)
|
|
|
|
SHeader.sh_entsize = sizeof(Elf_Dyn);
|
2018-06-29 05:07:34 +08:00
|
|
|
else
|
|
|
|
SHeader.sh_entsize = 0;
|
2014-05-17 00:01:00 +08:00
|
|
|
SHeader.sh_size = Section.Size;
|
2014-04-11 12:13:39 +08:00
|
|
|
}
|
|
|
|
|
2015-01-25 21:29:25 +08:00
|
|
|
static bool isMips64EL(const ELFYAML::Object &Doc) {
|
|
|
|
return Doc.Header.Machine == ELFYAML::ELF_EM(llvm::ELF::EM_MIPS) &&
|
|
|
|
Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64) &&
|
|
|
|
Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
|
|
|
|
}
|
|
|
|
|
2014-04-11 12:13:39 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool
|
|
|
|
ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::RelocationSection &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA) {
|
2015-05-08 15:05:04 +08:00
|
|
|
assert((Section.Type == llvm::ELF::SHT_REL ||
|
|
|
|
Section.Type == llvm::ELF::SHT_RELA) &&
|
|
|
|
"Section type is not SHT_REL nor SHT_RELA");
|
2014-04-11 12:13:39 +08:00
|
|
|
|
|
|
|
bool IsRela = Section.Type == llvm::ELF::SHT_RELA;
|
|
|
|
SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
|
|
|
|
SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size();
|
|
|
|
|
2015-07-08 18:12:40 +08:00
|
|
|
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
|
2014-04-11 12:13:39 +08:00
|
|
|
|
|
|
|
for (const auto &Rel : Section.Relocations) {
|
2015-04-22 23:26:43 +08:00
|
|
|
unsigned SymIdx = 0;
|
|
|
|
// Some special relocation, R_ARM_v4BX for instance, does not have
|
|
|
|
// an external reference. So it ignores the return value of lookup()
|
|
|
|
// here.
|
2017-08-31 07:13:31 +08:00
|
|
|
if (Rel.Symbol)
|
|
|
|
SymN2I.lookup(*Rel.Symbol, SymIdx);
|
2014-04-11 12:13:39 +08:00
|
|
|
|
|
|
|
if (IsRela) {
|
|
|
|
Elf_Rela REntry;
|
|
|
|
zero(REntry);
|
|
|
|
REntry.r_offset = Rel.Offset;
|
|
|
|
REntry.r_addend = Rel.Addend;
|
2015-01-25 21:29:25 +08:00
|
|
|
REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
|
2014-04-11 12:13:39 +08:00
|
|
|
OS.write((const char *)&REntry, sizeof(REntry));
|
|
|
|
} else {
|
|
|
|
Elf_Rel REntry;
|
|
|
|
zero(REntry);
|
|
|
|
REntry.r_offset = Rel.Offset;
|
2015-01-25 21:29:25 +08:00
|
|
|
REntry.setSymbolAndType(SymIdx, Rel.Type, isMips64EL(Doc));
|
2014-04-11 12:13:39 +08:00
|
|
|
OS.write((const char *)&REntry, sizeof(REntry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-21 12:28:26 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::Group &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA) {
|
2018-01-12 10:28:31 +08:00
|
|
|
typedef typename ELFT::Word Elf_Word;
|
2015-05-08 15:05:04 +08:00
|
|
|
assert(Section.Type == llvm::ELF::SHT_GROUP &&
|
|
|
|
"Section type is not SHT_GROUP");
|
2015-02-21 12:28:26 +08:00
|
|
|
|
|
|
|
SHeader.sh_entsize = sizeof(Elf_Word);
|
|
|
|
SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
|
|
|
|
|
2015-07-08 18:12:40 +08:00
|
|
|
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
|
2015-02-21 12:28:26 +08:00
|
|
|
|
|
|
|
for (auto member : Section.Members) {
|
|
|
|
Elf_Word SIdx;
|
|
|
|
unsigned int sectionIndex = 0;
|
|
|
|
if (member.sectionNameOrType == "GRP_COMDAT")
|
|
|
|
sectionIndex = llvm::ELF::GRP_COMDAT;
|
2018-08-15 19:43:00 +08:00
|
|
|
else if (SN2I.lookup(member.sectionNameOrType, sectionIndex) &&
|
|
|
|
!to_integer(member.sectionNameOrType, sectionIndex)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Unknown section referenced: '"
|
|
|
|
<< member.sectionNameOrType << "' at YAML section' "
|
|
|
|
<< Section.Name << "\n";
|
2015-02-21 12:28:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SIdx = sectionIndex;
|
|
|
|
OS.write((const char *)&SIdx, sizeof(SIdx));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:40:48 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
|
|
|
|
const ELFYAML::MipsABIFlags &Section,
|
|
|
|
ContiguousBlobAccumulator &CBA) {
|
2015-05-08 15:05:04 +08:00
|
|
|
assert(Section.Type == llvm::ELF::SHT_MIPS_ABIFLAGS &&
|
|
|
|
"Section type is not SHT_MIPS_ABIFLAGS");
|
2015-05-07 23:40:48 +08:00
|
|
|
|
|
|
|
object::Elf_Mips_ABIFlags<ELFT> Flags;
|
|
|
|
zero(Flags);
|
|
|
|
SHeader.sh_entsize = sizeof(Flags);
|
|
|
|
SHeader.sh_size = SHeader.sh_entsize;
|
|
|
|
|
2015-07-08 18:12:40 +08:00
|
|
|
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset, SHeader.sh_addralign);
|
2015-05-07 23:40:48 +08:00
|
|
|
Flags.version = Section.Version;
|
|
|
|
Flags.isa_level = Section.ISALevel;
|
|
|
|
Flags.isa_rev = Section.ISARevision;
|
|
|
|
Flags.gpr_size = Section.GPRSize;
|
|
|
|
Flags.cpr1_size = Section.CPR1Size;
|
|
|
|
Flags.cpr2_size = Section.CPR2Size;
|
|
|
|
Flags.fp_abi = Section.FpABI;
|
|
|
|
Flags.isa_ext = Section.ISAExtension;
|
|
|
|
Flags.ases = Section.ASEs;
|
|
|
|
Flags.flags1 = Section.Flags1;
|
|
|
|
Flags.flags2 = Section.Flags2;
|
|
|
|
OS.write((const char *)&Flags, sizeof(Flags));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
|
|
|
|
for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
|
2014-04-11 12:13:39 +08:00
|
|
|
StringRef Name = Doc.Sections[i]->Name;
|
2017-11-09 22:53:43 +08:00
|
|
|
DotShStrtab.add(Name);
|
2014-04-03 00:34:54 +08:00
|
|
|
// "+ 1" to take into account the SHT_NULL entry.
|
|
|
|
if (SN2I.addName(Name, i + 1)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Repeated section name: '" << Name
|
|
|
|
<< "' at YAML section number " << i << ".\n";
|
2014-04-03 00:34:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 22:53:43 +08:00
|
|
|
|
|
|
|
auto SecNo = 1 + Doc.Sections.size();
|
|
|
|
// Add special sections after input sections, if necessary.
|
2017-11-17 02:10:15 +08:00
|
|
|
for (const auto &Name : implicitSectionNames())
|
2017-11-09 22:53:43 +08:00
|
|
|
if (!SN2I.addName(Name, SecNo)) {
|
|
|
|
// Account for this section, since it wasn't in the Doc
|
|
|
|
++SecNo;
|
|
|
|
DotShStrtab.add(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
DotShStrtab.finalize();
|
2014-04-03 00:34:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-11 12:13:39 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool
|
|
|
|
ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex,
|
|
|
|
const std::vector<ELFYAML::Symbol> &Symbols) {
|
|
|
|
for (const auto &Sym : Symbols) {
|
|
|
|
++StartIndex;
|
|
|
|
if (Sym.Name.empty())
|
|
|
|
continue;
|
|
|
|
if (SymN2I.addName(Sym.Name, StartIndex)) {
|
2018-04-22 05:11:59 +08:00
|
|
|
WithColor::error() << "Repeated symbol name: '" << Sym.Name << "'.\n";
|
2014-04-11 12:13:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-03 00:34:54 +08:00
|
|
|
template <class ELFT>
|
|
|
|
int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
|
2014-04-03 00:34:40 +08:00
|
|
|
ELFState<ELFT> State(Doc);
|
|
|
|
if (!State.buildSectionIndex())
|
|
|
|
return 1;
|
|
|
|
|
2014-04-11 12:13:39 +08:00
|
|
|
std::size_t StartSymIndex = 0;
|
|
|
|
if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) ||
|
|
|
|
!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) ||
|
|
|
|
!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak))
|
|
|
|
return 1;
|
|
|
|
|
2013-06-14 06:19:48 +08:00
|
|
|
Elf_Ehdr Header;
|
2014-04-03 00:34:54 +08:00
|
|
|
State.initELFHeader(Header);
|
2013-06-14 06:19:48 +08:00
|
|
|
|
|
|
|
// TODO: Flesh out section header support.
|
2017-07-20 04:38:46 +08:00
|
|
|
|
|
|
|
std::vector<Elf_Phdr> PHeaders;
|
|
|
|
State.initProgramHeaders(PHeaders);
|
2013-06-14 06:19:48 +08:00
|
|
|
|
2013-06-21 03:11:44 +08:00
|
|
|
// XXX: This offset is tightly coupled with the order that we write
|
|
|
|
// things to `OS`.
|
2017-07-20 04:38:46 +08:00
|
|
|
const size_t SectionContentBeginOffset = Header.e_ehsize +
|
|
|
|
Header.e_phentsize * Header.e_phnum +
|
|
|
|
Header.e_shentsize * Header.e_shnum;
|
2013-06-21 03:11:44 +08:00
|
|
|
ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
|
2013-06-21 04:59:34 +08:00
|
|
|
|
2013-06-14 06:19:48 +08:00
|
|
|
std::vector<Elf_Shdr> SHeaders;
|
2014-04-03 00:34:54 +08:00
|
|
|
if(!State.initSectionHeaders(SHeaders, CBA))
|
|
|
|
return 1;
|
2013-06-11 07:44:15 +08:00
|
|
|
|
2017-11-09 22:53:43 +08:00
|
|
|
// Populate SHeaders with implicit sections not present in the Doc
|
2017-11-17 02:10:15 +08:00
|
|
|
for (const auto &Name : State.implicitSectionNames())
|
2017-11-09 22:53:43 +08:00
|
|
|
if (State.SN2I.get(Name) >= SHeaders.size())
|
|
|
|
SHeaders.push_back({});
|
|
|
|
|
|
|
|
// Initialize the implicit sections
|
|
|
|
auto Index = State.SN2I.get(".symtab");
|
2017-11-17 02:10:15 +08:00
|
|
|
State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Static, CBA);
|
2017-11-09 22:53:43 +08:00
|
|
|
Index = State.SN2I.get(".strtab");
|
|
|
|
State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA);
|
|
|
|
Index = State.SN2I.get(".shstrtab");
|
|
|
|
State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA);
|
2017-11-17 02:10:15 +08:00
|
|
|
if (State.hasDynamicSymbols()) {
|
|
|
|
Index = State.SN2I.get(".dynsym");
|
|
|
|
State.initSymtabSectionHeader(SHeaders[Index], SymtabType::Dynamic, CBA);
|
|
|
|
SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
|
|
|
|
Index = State.SN2I.get(".dynstr");
|
|
|
|
State.initStrtabSectionHeader(SHeaders[Index], ".dynstr", State.DotDynstr, CBA);
|
|
|
|
SHeaders[Index].sh_flags |= ELF::SHF_ALLOC;
|
|
|
|
}
|
2013-06-11 07:44:15 +08:00
|
|
|
|
2017-07-20 04:38:46 +08:00
|
|
|
// Now we can decide segment offsets
|
|
|
|
State.setProgramHeaderLayout(PHeaders, SHeaders);
|
|
|
|
|
2013-06-11 07:44:15 +08:00
|
|
|
OS.write((const char *)&Header, sizeof(Header));
|
2017-07-20 04:38:46 +08:00
|
|
|
writeArrayData(OS, makeArrayRef(PHeaders));
|
2013-10-13 05:29:16 +08:00
|
|
|
writeArrayData(OS, makeArrayRef(SHeaders));
|
2013-06-14 06:20:01 +08:00
|
|
|
CBA.writeBlobToStream(OS);
|
2013-06-18 04:14:59 +08:00
|
|
|
return 0;
|
2013-06-11 07:44:15 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:10:15 +08:00
|
|
|
template <class ELFT> bool ELFState<ELFT>::hasDynamicSymbols() const {
|
|
|
|
return Doc.DynamicSymbols.Global.size() > 0 ||
|
|
|
|
Doc.DynamicSymbols.Weak.size() > 0 ||
|
|
|
|
Doc.DynamicSymbols.Local.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> SmallVector<const char *, 5> ELFState<ELFT>::implicitSectionNames() const {
|
|
|
|
if (!hasDynamicSymbols())
|
|
|
|
return {".symtab", ".strtab", ".shstrtab"};
|
|
|
|
return {".symtab", ".strtab", ".shstrtab", ".dynsym", ".dynstr"};
|
|
|
|
}
|
|
|
|
|
2013-06-22 09:03:35 +08:00
|
|
|
static bool is64Bit(const ELFYAML::Object &Doc) {
|
|
|
|
return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isLittleEndian(const ELFYAML::Object &Doc) {
|
|
|
|
return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
|
|
|
|
}
|
|
|
|
|
2016-06-28 03:53:53 +08:00
|
|
|
int yaml2elf(llvm::ELFYAML::Object &Doc, raw_ostream &Out) {
|
2013-06-22 09:03:35 +08:00
|
|
|
if (is64Bit(Doc)) {
|
|
|
|
if (isLittleEndian(Doc))
|
2018-01-12 09:40:32 +08:00
|
|
|
return ELFState<object::ELF64LE>::writeELF(Out, Doc);
|
2013-06-11 07:44:15 +08:00
|
|
|
else
|
2018-01-12 09:40:32 +08:00
|
|
|
return ELFState<object::ELF64BE>::writeELF(Out, Doc);
|
2013-06-11 07:44:15 +08:00
|
|
|
} else {
|
2013-06-22 09:03:35 +08:00
|
|
|
if (isLittleEndian(Doc))
|
2018-01-12 09:40:32 +08:00
|
|
|
return ELFState<object::ELF32LE>::writeELF(Out, Doc);
|
2013-06-11 07:44:15 +08:00
|
|
|
else
|
2018-01-12 09:40:32 +08:00
|
|
|
return ELFState<object::ELF32BE>::writeELF(Out, Doc);
|
2013-06-11 07:44:15 +08:00
|
|
|
}
|
|
|
|
}
|