[yaml2obj][ELF] Allow symbols to reference sections.
[oota-llvm.git] / tools / yaml2obj / yaml2elf.cpp
index 942e75904ef60f8b0b9452c9d44f9f134f20c838..421781ce14c9d5423c4c26bfb9fd2cad391df6e3 100644 (file)
@@ -69,11 +69,12 @@ public:
 namespace {
 class ContiguousBlobAccumulator {
   const uint64_t InitialOffset;
+  SmallVector<char, 128> Buf;
   raw_svector_ostream OS;
 
 public:
-  ContiguousBlobAccumulator(uint64_t InitialOffset_, SmallVectorImpl<char> &Buf)
-      : InitialOffset(InitialOffset_), OS(Buf) {}
+  ContiguousBlobAccumulator(uint64_t InitialOffset_)
+      : InitialOffset(InitialOffset_), Buf(), OS(Buf) {}
   raw_ostream &getOS() { return OS; }
   uint64_t currentOffset() const { return InitialOffset + OS.tell(); }
   void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); }
@@ -133,21 +134,52 @@ static void createStringTableSectionHeader(Elf_Shdr &SHeader,
   SHeader.sh_addralign = 1;
 }
 
-// FIXME: This function is hideous. Between the sheer number of parameters
-// and the hideous ELF typenames, it's just a travesty. Factor the ELF
-// output into a class (templated on ELFT) and share some typedefs.
+namespace {
+/// \brief "Single point of truth" for the ELF file construction.
+/// TODO: This class still has a ways to go before it is truly a "single
+/// point of truth".
+template <class ELFT>
+class ELFState {
+  /// \brief The future ".strtab" section.
+  StringTableBuilder DotStrtab;
+  /// \brief The section number of the ".strtab" section.
+  unsigned DotStrtabSecNo;
+  /// \brief The accumulated contents of all sections so far.
+  ContiguousBlobAccumulator &SectionContentAccum;
+  typedef typename object::ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
+  /// \brief The ELF file header.
+  Elf_Ehdr &Header;
+
+  SectionNameToIdxMap &SN2I;
+
+public:
+
+  ELFState(Elf_Ehdr &Header_, ContiguousBlobAccumulator &Accum,
+           unsigned DotStrtabSecNo_, SectionNameToIdxMap &SN2I_)
+      : DotStrtab(), DotStrtabSecNo(DotStrtabSecNo_),
+        SectionContentAccum(Accum), Header(Header_), SN2I(SN2I_) {}
+
+  unsigned getDotStrTabSecNo() const { return DotStrtabSecNo; }
+  StringTableBuilder &getStringTable() { return DotStrtab; }
+  ContiguousBlobAccumulator &getSectionContentAccum() {
+    return SectionContentAccum;
+  }
+  SectionNameToIdxMap &getSN2I() { return SN2I; }
+};
+} // end anonymous namespace
+
+// FIXME: This function is hideous. The hideous ELF type names are hideous.
+// Factor the ELF output into a class (templated on ELFT) and share some
+// typedefs.
 template <class ELFT>
 static void handleSymtabSectionHeader(
-    const ELFYAML::Section &Sec,
-    const typename object::ELFObjectFile<ELFT>::Elf_Ehdr &Header,
-    typename object::ELFObjectFile<ELFT>::Elf_Shdr &SHeader,
-    StringTableBuilder &StrTab, ContiguousBlobAccumulator &CBA,
-    unsigned DotStrtabSecNo) {
+    const ELFYAML::Section &Sec, ELFState<ELFT> &State,
+    typename object::ELFObjectFile<ELFT>::Elf_Shdr &SHeader) {
 
   typedef typename object::ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
   // TODO: Ensure that a manually specified `Link` field is diagnosed as an
   // error for SHT_SYMTAB.
-  SHeader.sh_link = DotStrtabSecNo;
+  SHeader.sh_link = State.getDotStrTabSecNo();
   // TODO: Once we handle symbol binding, this should be one greater than
   // symbol table index of the last local symbol.
   SHeader.sh_info = 0;
@@ -165,11 +197,19 @@ static void handleSymtabSectionHeader(
     Elf_Sym Symbol;
     zero(Symbol);
     if (!Sym.Name.empty())
-      Symbol.st_name = StrTab.addString(Sym.Name);
+      Symbol.st_name = State.getStringTable().addString(Sym.Name);
     Symbol.setBindingAndType(Sym.Binding, Sym.Type);
+    unsigned Index;
+    if (State.getSN2I().lookupSection(Sym.Section, Index)) {
+      errs() << "error: Unknown section referenced: '" << Sym.Section
+             << "' by YAML symbol " << Sym.Name << ".\n";
+      exit(1);
+    }
+    Symbol.st_shndx = Index;
     Syms.push_back(Symbol);
   }
 
+  ContiguousBlobAccumulator &CBA = State.getSectionContentAccum();
   SHeader.sh_offset = CBA.currentOffset();
   SHeader.sh_size = vectorDataSize(Syms);
   writeVectorData(CBA.getOS(), Syms);
@@ -178,9 +218,8 @@ static void handleSymtabSectionHeader(
 template <class ELFT>
 static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
   using namespace llvm::ELF;
-  using namespace llvm::object;
-  typedef typename ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
-  typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
+  typedef typename object::ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr;
+  typedef typename object::ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
 
   const ELFYAML::FileHeader &Hdr = Doc.Header;
 
@@ -194,8 +233,7 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
   bool IsLittleEndian = ELFT::TargetEndianness == support::little;
   Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
   Header.e_ident[EI_VERSION] = EV_CURRENT;
-  // TODO: Implement ELF_ELFOSABI enum.
-  Header.e_ident[EI_OSABI] = ELFOSABI_NONE;
+  Header.e_ident[EI_OSABI] = Hdr.OSABI;
   Header.e_ident[EI_ABIVERSION] = 0;
   Header.e_type = Hdr.Type;
   Header.e_machine = Hdr.Machine;
@@ -219,6 +257,11 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
   Header.e_shstrndx = Header.e_shnum - 1;
   const unsigned DotStrtabSecNo = Header.e_shnum - 2;
 
+  // XXX: This offset is tightly coupled with the order that we write
+  // things to `OS`.
+  const size_t SectionContentBeginOffset =
+      Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
+  ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
   SectionNameToIdxMap SN2I;
   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
     StringRef Name = Sections[i].Name;
@@ -232,13 +275,9 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
     }
   }
 
+  ELFState<ELFT> State(Header, CBA, DotStrtabSecNo, SN2I);
+
   StringTableBuilder SHStrTab;
-  SmallVector<char, 128> Buf;
-  // XXX: This offset is tightly coupled with the order that we write
-  // things to `OS`.
-  const size_t SectionContentBeginOffset =
-      Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
-  ContiguousBlobAccumulator CBA(SectionContentBeginOffset, Buf);
   std::vector<Elf_Shdr> SHeaders;
   {
     // Ensure SHN_UNDEF entry is present. An all-zero section header is a
@@ -247,7 +286,6 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
     zero(SHdr);
     SHeaders.push_back(SHdr);
   }
-  StringTableBuilder DotStrTab;
   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
     const ELFYAML::Section &Sec = Sections[i];
     Elf_Shdr SHeader;
@@ -273,10 +311,11 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
     SHeader.sh_info = 0;
     SHeader.sh_addralign = Sec.AddressAlign;
     SHeader.sh_entsize = 0;
-    // XXX: Really ugly right now. Need to put common state into a class.
+    // XXX: Really ugly right now. Should not be writing to `CBA` above
+    // (and setting sh_offset and sh_size) when going through this branch
+    // here.
     if (Sec.Type == ELFYAML::ELF_SHT(SHT_SYMTAB))
-      handleSymtabSectionHeader<ELFT>(Sec, Header, SHeader, DotStrTab, CBA,
-                                      DotStrtabSecNo);
+      handleSymtabSectionHeader<ELFT>(Sec, State, SHeader);
     SHeaders.push_back(SHeader);
   }
 
@@ -284,7 +323,7 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
   Elf_Shdr DotStrTabSHeader;
   zero(DotStrTabSHeader);
   DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab"));
-  createStringTableSectionHeader(DotStrTabSHeader, DotStrTab, CBA);
+  createStringTableSectionHeader(DotStrTabSHeader, State.getStringTable(), CBA);
 
   // Section header string table header.
   Elf_Shdr SHStrTabSHeader;