Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 1 | //===- yaml2elf - Convert YAML to a ELF object file -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief The ELF component of yaml2obj. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "yaml2obj.h" |
| 16 | #include "llvm/Object/ELF.h" |
| 17 | #include "llvm/Object/ELFYAML.h" |
| 18 | #include "llvm/Support/ELF.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/Support/YAMLTraits.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 25 | // There is similar code in yaml2coff, but with some slight COFF-specific |
| 26 | // variations like different initial state. Might be able to deduplicate |
| 27 | // some day, but also want to make sure that the Mach-O use case is served. |
| 28 | // |
| 29 | // This class has a deliberately small interface, since a lot of |
| 30 | // implementation variation is possible. |
| 31 | // |
| 32 | // TODO: Use an ordered container with a suffix-based comparison in order |
| 33 | // to deduplicate suffixes. std::map<> with a custom comparator is likely |
| 34 | // to be the simplest implementation, but a suffix trie could be more |
| 35 | // suitable for the job. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 36 | namespace { |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 37 | class StringTableBuilder { |
| 38 | /// \brief Indices of strings currently present in `Buf`. |
| 39 | StringMap<unsigned> StringIndices; |
| 40 | /// \brief The contents of the string table as we build it. |
| 41 | std::string Buf; |
| 42 | public: |
| 43 | StringTableBuilder() { |
| 44 | Buf.push_back('\0'); |
| 45 | } |
| 46 | /// \returns Index of string in string table. |
| 47 | unsigned addString(StringRef S) { |
| 48 | StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S); |
| 49 | unsigned &I = Entry.getValue(); |
| 50 | if (I != 0) |
| 51 | return I; |
| 52 | I = Buf.size(); |
| 53 | Buf.append(S.begin(), S.end()); |
| 54 | Buf.push_back('\0'); |
| 55 | return I; |
| 56 | } |
| 57 | size_t size() const { |
| 58 | return Buf.size(); |
| 59 | } |
| 60 | void writeToStream(raw_ostream &OS) { |
| 61 | OS.write(Buf.data(), Buf.size()); |
| 62 | } |
| 63 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 64 | } // end anonymous namespace |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 65 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 66 | // This class is used to build up a contiguous binary blob while keeping |
| 67 | // track of an offset in the output (which notionally begins at |
| 68 | // `InitialOffset`). |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 69 | namespace { |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 70 | class ContiguousBlobAccumulator { |
| 71 | const uint64_t InitialOffset; |
| 72 | raw_svector_ostream OS; |
| 73 | |
| 74 | public: |
| 75 | ContiguousBlobAccumulator(uint64_t InitialOffset_, SmallVectorImpl<char> &Buf) |
| 76 | : InitialOffset(InitialOffset_), OS(Buf) {} |
| 77 | raw_ostream &getOS() { return OS; } |
| 78 | uint64_t currentOffset() const { return InitialOffset + OS.tell(); } |
| 79 | void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } |
| 80 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 81 | } // end anonymous namespace |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 82 | |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 83 | // Used to keep track of section names, so that in the YAML file sections |
| 84 | // can be referenced by name instead of by index. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 85 | namespace { |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 86 | class SectionNameToIdxMap { |
| 87 | StringMap<int> Map; |
| 88 | public: |
| 89 | /// \returns true if name is already present in the map. |
| 90 | bool addName(StringRef SecName, unsigned i) { |
| 91 | StringMapEntry<int> &Entry = Map.GetOrCreateValue(SecName, -1); |
| 92 | if (Entry.getValue() != -1) |
| 93 | return true; |
| 94 | Entry.setValue((int)i); |
| 95 | return false; |
| 96 | } |
| 97 | /// \returns true if name is not present in the map |
| 98 | bool lookupSection(StringRef SecName, unsigned &Idx) const { |
| 99 | StringMap<int>::const_iterator I = Map.find(SecName); |
| 100 | if (I == Map.end()) |
| 101 | return true; |
| 102 | Idx = I->getValue(); |
| 103 | return false; |
| 104 | } |
| 105 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 106 | } // end anonymous namespace |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 107 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 108 | template <class T> |
| 109 | static size_t vectorDataSize(const std::vector<T> &Vec) { |
| 110 | return Vec.size() * sizeof(T); |
| 111 | } |
| 112 | |
| 113 | template <class T> |
| 114 | static void writeVectorData(raw_ostream &OS, const std::vector<T> &Vec) { |
| 115 | OS.write((const char *)Vec.data(), vectorDataSize(Vec)); |
| 116 | } |
| 117 | |
| 118 | template <class T> |
| 119 | static void zero(T &Obj) { |
| 120 | memset(&Obj, 0, sizeof(Obj)); |
| 121 | } |
| 122 | |
Sean Silva | 85d3eeb | 2013-06-18 01:11:27 +0000 | [diff] [blame] | 123 | /// \brief Create a string table in `SHeader`, which we assume is already |
| 124 | /// zero'd. |
| 125 | template <class Elf_Shdr> |
| 126 | static void createStringTableSectionHeader(Elf_Shdr &SHeader, |
| 127 | StringTableBuilder &STB, |
| 128 | ContiguousBlobAccumulator &CBA) { |
| 129 | SHeader.sh_type = ELF::SHT_STRTAB; |
| 130 | SHeader.sh_offset = CBA.currentOffset(); |
| 131 | SHeader.sh_size = STB.size(); |
| 132 | STB.writeToStream(CBA.getOS()); |
| 133 | SHeader.sh_addralign = 1; |
| 134 | } |
| 135 | |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame^] | 136 | // FIXME: This function is hideous. Between the sheer number of parameters |
| 137 | // and the hideous ELF typenames, it's just a travesty. Factor the ELF |
| 138 | // output into a class (templated on ELFT) and share some typedefs. |
| 139 | template <class ELFT> |
| 140 | static void handleSymtabSectionHeader( |
| 141 | const ELFYAML::Section &Sec, |
| 142 | const typename object::ELFObjectFile<ELFT>::Elf_Ehdr &Header, |
| 143 | typename object::ELFObjectFile<ELFT>::Elf_Shdr &SHeader, |
| 144 | StringTableBuilder &StrTab, ContiguousBlobAccumulator &CBA, |
| 145 | unsigned DotStrtabSecNo) { |
| 146 | |
| 147 | typedef typename object::ELFObjectFile<ELFT>::Elf_Sym Elf_Sym; |
| 148 | // TODO: Ensure that a manually specified `Link` field is diagnosed as an |
| 149 | // error for SHT_SYMTAB. |
| 150 | SHeader.sh_link = DotStrtabSecNo; |
| 151 | // TODO: Once we handle symbol binding, this should be one greater than |
| 152 | // symbol table index of the last local symbol. |
| 153 | SHeader.sh_info = 0; |
| 154 | SHeader.sh_entsize = sizeof(Elf_Sym); |
| 155 | |
| 156 | std::vector<Elf_Sym> Syms; |
| 157 | // FIXME: Ensure STN_UNDEF entry is present. |
| 158 | for (unsigned i = 0, e = Sec.Symbols.size(); i != e; ++i) { |
| 159 | const ELFYAML::Symbol &Sym = Sec.Symbols[i]; |
| 160 | Elf_Sym Symbol; |
| 161 | zero(Symbol); |
| 162 | if (!Sym.Name.empty()) |
| 163 | Symbol.st_name = StrTab.addString(Sym.Name); |
| 164 | Syms.push_back(Symbol); |
| 165 | } |
| 166 | |
| 167 | SHeader.sh_offset = CBA.currentOffset(); |
| 168 | SHeader.sh_size = vectorDataSize(Syms); |
| 169 | writeVectorData(CBA.getOS(), Syms); |
| 170 | } |
| 171 | |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 172 | template <class ELFT> |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 173 | static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 174 | using namespace llvm::ELF; |
| 175 | using namespace llvm::object; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 176 | typedef typename ELFObjectFile<ELFT>::Elf_Ehdr Elf_Ehdr; |
| 177 | typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 178 | |
| 179 | const ELFYAML::FileHeader &Hdr = Doc.Header; |
| 180 | |
| 181 | Elf_Ehdr Header; |
| 182 | zero(Header); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 183 | Header.e_ident[EI_MAG0] = 0x7f; |
| 184 | Header.e_ident[EI_MAG1] = 'E'; |
| 185 | Header.e_ident[EI_MAG2] = 'L'; |
| 186 | Header.e_ident[EI_MAG3] = 'F'; |
| 187 | Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 188 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 189 | Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 190 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 191 | // TODO: Implement ELF_ELFOSABI enum. |
| 192 | Header.e_ident[EI_OSABI] = ELFOSABI_NONE; |
| 193 | // TODO: Implement ELF_ABIVERSION enum. |
| 194 | Header.e_ident[EI_ABIVERSION] = 0; |
| 195 | Header.e_type = Hdr.Type; |
| 196 | Header.e_machine = Hdr.Machine; |
| 197 | Header.e_version = EV_CURRENT; |
| 198 | Header.e_entry = Hdr.Entry; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 199 | Header.e_ehsize = sizeof(Elf_Ehdr); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 200 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 201 | // TODO: Flesh out section header support. |
| 202 | // TODO: Program headers. |
| 203 | |
| 204 | Header.e_shentsize = sizeof(Elf_Shdr); |
| 205 | // Immediately following the ELF header. |
| 206 | Header.e_shoff = sizeof(Header); |
| 207 | std::vector<ELFYAML::Section> Sections = Doc.Sections; |
| 208 | if (Sections.empty() || Sections.front().Type != SHT_NULL) { |
| 209 | ELFYAML::Section S; |
| 210 | S.Type = SHT_NULL; |
| 211 | zero(S.Flags); |
Sean Silva | f62a600 | 2013-06-18 01:11:21 +0000 | [diff] [blame] | 212 | zero(S.Address); |
| 213 | zero(S.AddressAlign); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 214 | Sections.insert(Sections.begin(), S); |
| 215 | } |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 216 | // "+ 2" for string table and section header string table. |
| 217 | Header.e_shnum = Sections.size() + 2; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 218 | // Place section header string table last. |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 219 | Header.e_shstrndx = Sections.size() + 1; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame^] | 220 | const unsigned DotStrtabSecNo = Sections.size(); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 221 | |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 222 | SectionNameToIdxMap SN2I; |
| 223 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 224 | StringRef Name = Sections[i].Name; |
| 225 | if (Name.empty()) |
| 226 | continue; |
| 227 | if (SN2I.addName(Name, i)) { |
| 228 | errs() << "error: Repeated section name: '" << Name |
| 229 | << "' at YAML section number " << i << ".\n"; |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 230 | return 1; |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 234 | StringTableBuilder SHStrTab; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 235 | SmallVector<char, 128> Buf; |
| 236 | // XXX: This offset is tightly coupled with the order that we write |
| 237 | // things to `OS`. |
| 238 | const size_t SectionContentBeginOffset = |
| 239 | Header.e_ehsize + Header.e_shentsize * Header.e_shnum; |
| 240 | ContiguousBlobAccumulator CBA(SectionContentBeginOffset, Buf); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 241 | std::vector<Elf_Shdr> SHeaders; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame^] | 242 | StringTableBuilder DotStrTab; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 243 | for (unsigned i = 0, e = Sections.size(); i != e; ++i) { |
| 244 | const ELFYAML::Section &Sec = Sections[i]; |
| 245 | Elf_Shdr SHeader; |
| 246 | zero(SHeader); |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 247 | SHeader.sh_name = SHStrTab.addString(Sec.Name); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 248 | SHeader.sh_type = Sec.Type; |
| 249 | SHeader.sh_flags = Sec.Flags; |
Sean Silva | f4bfced | 2013-06-13 22:19:54 +0000 | [diff] [blame] | 250 | SHeader.sh_addr = Sec.Address; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 251 | |
| 252 | SHeader.sh_offset = CBA.currentOffset(); |
| 253 | SHeader.sh_size = Sec.Content.binary_size(); |
| 254 | Sec.Content.writeAsBinary(CBA.getOS()); |
| 255 | |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 256 | if (!Sec.Link.empty()) { |
| 257 | unsigned Index; |
| 258 | if (SN2I.lookupSection(Sec.Link, Index)) { |
| 259 | errs() << "error: Unknown section referenced: '" << Sec.Link |
| 260 | << "' at YAML section number " << i << ".\n"; |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 261 | return 1; |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 262 | } |
| 263 | SHeader.sh_link = Index; |
| 264 | } |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 265 | SHeader.sh_info = 0; |
Sean Silva | 0a409cf | 2013-06-14 00:38:02 +0000 | [diff] [blame] | 266 | SHeader.sh_addralign = Sec.AddressAlign; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 267 | SHeader.sh_entsize = 0; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame^] | 268 | // XXX: Really ugly right now. Need to put common state into a class. |
| 269 | if (Sec.Type == ELFYAML::ELF_SHT(SHT_SYMTAB)) |
| 270 | handleSymtabSectionHeader<ELFT>(Sec, Header, SHeader, DotStrTab, CBA, |
| 271 | DotStrtabSecNo); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 272 | SHeaders.push_back(SHeader); |
| 273 | } |
| 274 | |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame^] | 275 | // .strtab string table header. |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 276 | Elf_Shdr DotStrTabSHeader; |
| 277 | zero(DotStrTabSHeader); |
| 278 | DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab")); |
| 279 | createStringTableSectionHeader(DotStrTabSHeader, DotStrTab, CBA); |
| 280 | |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 281 | // Section header string table header. |
| 282 | Elf_Shdr SHStrTabSHeader; |
| 283 | zero(SHStrTabSHeader); |
Sean Silva | 85d3eeb | 2013-06-18 01:11:27 +0000 | [diff] [blame] | 284 | createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 285 | |
| 286 | OS.write((const char *)&Header, sizeof(Header)); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 287 | writeVectorData(OS, SHeaders); |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 288 | OS.write((const char *)&DotStrTabSHeader, sizeof(DotStrTabSHeader)); |
Sean Silva | fde4ab0 | 2013-06-18 01:11:24 +0000 | [diff] [blame] | 289 | OS.write((const char *)&SHStrTabSHeader, sizeof(SHStrTabSHeader)); |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 290 | CBA.writeBlobToStream(OS); |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 291 | return 0; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) { |
| 295 | yaml::Input YIn(Buf->getBuffer()); |
| 296 | ELFYAML::Object Doc; |
| 297 | YIn >> Doc; |
| 298 | if (YIn.error()) { |
| 299 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 300 | return 1; |
| 301 | } |
| 302 | if (Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { |
| 303 | if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 304 | return writeELF<object::ELFType<support::little, 8, true> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 305 | else |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 306 | return writeELF<object::ELFType<support::big, 8, true> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 307 | } else { |
| 308 | if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB)) |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 309 | return writeELF<object::ELFType<support::little, 4, false> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 310 | else |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 311 | return writeELF<object::ELFType<support::big, 4, false> >(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 312 | } |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 313 | } |