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" |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ELFObjectFile.h" |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELFYAML.h" |
| 19 | #include "llvm/Support/ELF.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/YAMLTraits.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 26 | // There is similar code in yaml2coff, but with some slight COFF-specific |
| 27 | // variations like different initial state. Might be able to deduplicate |
| 28 | // some day, but also want to make sure that the Mach-O use case is served. |
| 29 | // |
| 30 | // This class has a deliberately small interface, since a lot of |
| 31 | // implementation variation is possible. |
| 32 | // |
| 33 | // TODO: Use an ordered container with a suffix-based comparison in order |
| 34 | // to deduplicate suffixes. std::map<> with a custom comparator is likely |
| 35 | // to be the simplest implementation, but a suffix trie could be more |
| 36 | // suitable for the job. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 37 | namespace { |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 38 | class StringTableBuilder { |
| 39 | /// \brief Indices of strings currently present in `Buf`. |
| 40 | StringMap<unsigned> StringIndices; |
| 41 | /// \brief The contents of the string table as we build it. |
| 42 | std::string Buf; |
| 43 | public: |
| 44 | StringTableBuilder() { |
| 45 | Buf.push_back('\0'); |
| 46 | } |
| 47 | /// \returns Index of string in string table. |
| 48 | unsigned addString(StringRef S) { |
| 49 | StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S); |
| 50 | unsigned &I = Entry.getValue(); |
| 51 | if (I != 0) |
| 52 | return I; |
| 53 | I = Buf.size(); |
| 54 | Buf.append(S.begin(), S.end()); |
| 55 | Buf.push_back('\0'); |
| 56 | return I; |
| 57 | } |
| 58 | size_t size() const { |
| 59 | return Buf.size(); |
| 60 | } |
| 61 | void writeToStream(raw_ostream &OS) { |
| 62 | OS.write(Buf.data(), Buf.size()); |
| 63 | } |
| 64 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 65 | } // end anonymous namespace |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 66 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 67 | // This class is used to build up a contiguous binary blob while keeping |
| 68 | // track of an offset in the output (which notionally begins at |
| 69 | // `InitialOffset`). |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 70 | namespace { |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 71 | class ContiguousBlobAccumulator { |
| 72 | const uint64_t InitialOffset; |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 73 | SmallVector<char, 128> Buf; |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 74 | raw_svector_ostream OS; |
| 75 | |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 76 | /// \returns The new offset. |
| 77 | uint64_t padToAlignment(unsigned Align) { |
| 78 | uint64_t CurrentOffset = InitialOffset + OS.tell(); |
| 79 | uint64_t AlignedOffset = RoundUpToAlignment(CurrentOffset, Align); |
| 80 | for (; CurrentOffset != AlignedOffset; ++CurrentOffset) |
| 81 | OS.write('\0'); |
| 82 | return AlignedOffset; // == CurrentOffset; |
| 83 | } |
| 84 | |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 85 | public: |
Sean Silva | bd3bc69 | 2013-06-20 19:11:41 +0000 | [diff] [blame] | 86 | ContiguousBlobAccumulator(uint64_t InitialOffset_) |
| 87 | : InitialOffset(InitialOffset_), Buf(), OS(Buf) {} |
Sean Silva | d93323f | 2013-06-22 00:47:43 +0000 | [diff] [blame] | 88 | template <class Integer> |
| 89 | raw_ostream &getOSAndAlignedOffset(Integer &Offset, unsigned Align = 16) { |
| 90 | Offset = padToAlignment(Align); |
| 91 | return OS; |
| 92 | } |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 93 | void writeBlobToStream(raw_ostream &Out) { Out << OS.str(); } |
| 94 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 95 | } // end anonymous namespace |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 96 | |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 97 | // Used to keep track of section and symbol names, so that in the YAML file |
| 98 | // sections and symbols can be referenced by name instead of by index. |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 99 | namespace { |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 100 | class NameToIdxMap { |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 101 | StringMap<int> Map; |
| 102 | public: |
| 103 | /// \returns true if name is already present in the map. |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 104 | bool addName(StringRef Name, unsigned i) { |
| 105 | StringMapEntry<int> &Entry = Map.GetOrCreateValue(Name, -1); |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 106 | if (Entry.getValue() != -1) |
| 107 | return true; |
| 108 | Entry.setValue((int)i); |
| 109 | return false; |
| 110 | } |
| 111 | /// \returns true if name is not present in the map |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 112 | bool lookup(StringRef Name, unsigned &Idx) const { |
| 113 | StringMap<int>::const_iterator I = Map.find(Name); |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 114 | if (I == Map.end()) |
| 115 | return true; |
| 116 | Idx = I->getValue(); |
| 117 | return false; |
| 118 | } |
| 119 | }; |
Sean Silva | 2a74f70 | 2013-06-15 00:31:46 +0000 | [diff] [blame] | 120 | } // end anonymous namespace |
Sean Silva | a6423eb | 2013-06-15 00:25:26 +0000 | [diff] [blame] | 121 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 122 | template <class T> |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 123 | static size_t arrayDataSize(ArrayRef<T> A) { |
| 124 | return A.size() * sizeof(T); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | template <class T> |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 128 | static void writeArrayData(raw_ostream &OS, ArrayRef<T> A) { |
| 129 | OS.write((const char *)A.data(), arrayDataSize(A)); |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | template <class T> |
| 133 | static void zero(T &Obj) { |
| 134 | memset(&Obj, 0, sizeof(Obj)); |
| 135 | } |
| 136 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 137 | namespace { |
| 138 | /// \brief "Single point of truth" for the ELF file construction. |
| 139 | /// TODO: This class still has a ways to go before it is truly a "single |
| 140 | /// point of truth". |
| 141 | template <class ELFT> |
| 142 | class ELFState { |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 143 | typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr; |
| 144 | typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 145 | typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 146 | typedef typename object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 147 | typedef typename object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 148 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 149 | /// \brief The future ".strtab" section. |
| 150 | StringTableBuilder DotStrtab; |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 151 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 152 | /// \brief The future ".shstrtab" section. |
| 153 | StringTableBuilder DotShStrtab; |
| 154 | |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 155 | NameToIdxMap SN2I; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 156 | NameToIdxMap SymN2I; |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 157 | const ELFYAML::Object &Doc; |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 158 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 159 | bool buildSectionIndex(); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 160 | bool buildSymbolIndex(std::size_t &StartIndex, |
| 161 | const std::vector<ELFYAML::Symbol> &Symbols); |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 162 | void initELFHeader(Elf_Ehdr &Header); |
| 163 | bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
| 164 | ContiguousBlobAccumulator &CBA); |
| 165 | void initSymtabSectionHeader(Elf_Shdr &SHeader, |
| 166 | ContiguousBlobAccumulator &CBA); |
| 167 | void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 168 | StringTableBuilder &STB, |
| 169 | ContiguousBlobAccumulator &CBA); |
| 170 | void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
| 171 | std::vector<Elf_Sym> &Syms, unsigned SymbolBinding); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 172 | void writeSectionContent(Elf_Shdr &SHeader, |
| 173 | const ELFYAML::RawContentSection &Section, |
| 174 | ContiguousBlobAccumulator &CBA); |
| 175 | bool writeSectionContent(Elf_Shdr &SHeader, |
| 176 | const ELFYAML::RelocationSection &Section, |
| 177 | ContiguousBlobAccumulator &CBA); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 178 | |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 179 | // - SHT_NULL entry (placed first, i.e. 0'th entry) |
| 180 | // - symbol table (.symtab) (placed third to last) |
| 181 | // - string table (.strtab) (placed second to last) |
| 182 | // - section header string table (.shstrtab) (placed last) |
| 183 | unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; } |
| 184 | unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; } |
| 185 | unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; } |
| 186 | unsigned getSectionCount() const { return Doc.Sections.size() + 4; } |
| 187 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 188 | ELFState(const ELFYAML::Object &D) : Doc(D) {} |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 189 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 190 | public: |
| 191 | static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc); |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 192 | }; |
| 193 | } // end anonymous namespace |
| 194 | |
Sean Silva | 37e817c | 2013-06-21 00:33:01 +0000 | [diff] [blame] | 195 | template <class ELFT> |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 196 | void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) { |
| 197 | using namespace llvm::ELF; |
| 198 | zero(Header); |
| 199 | Header.e_ident[EI_MAG0] = 0x7f; |
| 200 | Header.e_ident[EI_MAG1] = 'E'; |
| 201 | Header.e_ident[EI_MAG2] = 'L'; |
| 202 | Header.e_ident[EI_MAG3] = 'F'; |
| 203 | Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 204 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 205 | Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB; |
| 206 | Header.e_ident[EI_VERSION] = EV_CURRENT; |
| 207 | Header.e_ident[EI_OSABI] = Doc.Header.OSABI; |
| 208 | Header.e_ident[EI_ABIVERSION] = 0; |
| 209 | Header.e_type = Doc.Header.Type; |
| 210 | Header.e_machine = Doc.Header.Machine; |
| 211 | Header.e_version = EV_CURRENT; |
| 212 | Header.e_entry = Doc.Header.Entry; |
| 213 | Header.e_flags = Doc.Header.Flags; |
| 214 | Header.e_ehsize = sizeof(Elf_Ehdr); |
| 215 | Header.e_shentsize = sizeof(Elf_Shdr); |
| 216 | // Immediately following the ELF header. |
| 217 | Header.e_shoff = sizeof(Header); |
| 218 | Header.e_shnum = getSectionCount(); |
| 219 | Header.e_shstrndx = getDotShStrTabSecNo(); |
| 220 | } |
| 221 | |
| 222 | template <class ELFT> |
| 223 | bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders, |
| 224 | ContiguousBlobAccumulator &CBA) { |
| 225 | // Ensure SHN_UNDEF entry is present. An all-zero section header is a |
| 226 | // valid SHN_UNDEF entry since SHT_NULL == 0. |
| 227 | Elf_Shdr SHeader; |
| 228 | zero(SHeader); |
| 229 | SHeaders.push_back(SHeader); |
| 230 | |
| 231 | for (const auto &Sec : Doc.Sections) { |
| 232 | zero(SHeader); |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 233 | SHeader.sh_name = DotShStrtab.addString(Sec->Name); |
| 234 | SHeader.sh_type = Sec->Type; |
| 235 | SHeader.sh_flags = Sec->Flags; |
| 236 | SHeader.sh_addr = Sec->Address; |
| 237 | SHeader.sh_addralign = Sec->AddressAlign; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 238 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 239 | if (!Sec->Link.empty()) { |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 240 | unsigned Index; |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 241 | if (SN2I.lookup(Sec->Link, Index)) { |
| 242 | errs() << "error: Unknown section referenced: '" << Sec->Link |
| 243 | << "' at YAML section '" << Sec->Name << "'.\n"; |
| 244 | return false; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 245 | } |
| 246 | SHeader.sh_link = Index; |
| 247 | } |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 248 | |
| 249 | if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec.get())) |
| 250 | writeSectionContent(SHeader, *S, CBA); |
| 251 | else if (auto S = dyn_cast<ELFYAML::RelocationSection>(Sec.get())) { |
| 252 | if (S->Link.empty()) |
| 253 | // For relocation section set link to .symtab by default. |
| 254 | SHeader.sh_link = getDotSymTabSecNo(); |
| 255 | |
| 256 | unsigned Index; |
| 257 | if (SN2I.lookup(S->Info, Index)) { |
| 258 | errs() << "error: Unknown section referenced: '" << S->Info |
| 259 | << "' at YAML section '" << S->Name << "'.\n"; |
| 260 | return false; |
| 261 | } |
| 262 | SHeader.sh_info = Index; |
| 263 | |
| 264 | if (!writeSectionContent(SHeader, *S, CBA)) |
| 265 | return false; |
| 266 | } else |
| 267 | llvm_unreachable("Unknown section type"); |
| 268 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 269 | SHeaders.push_back(SHeader); |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | template <class ELFT> |
| 275 | void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader, |
| 276 | ContiguousBlobAccumulator &CBA) { |
| 277 | zero(SHeader); |
| 278 | SHeader.sh_name = DotShStrtab.addString(StringRef(".symtab")); |
| 279 | SHeader.sh_type = ELF::SHT_SYMTAB; |
| 280 | SHeader.sh_link = getDotStrTabSecNo(); |
| 281 | // One greater than symbol table index of the last local symbol. |
| 282 | SHeader.sh_info = Doc.Symbols.Local.size() + 1; |
| 283 | SHeader.sh_entsize = sizeof(Elf_Sym); |
| 284 | |
| 285 | std::vector<Elf_Sym> Syms; |
| 286 | { |
| 287 | // Ensure STN_UNDEF is present |
| 288 | Elf_Sym Sym; |
| 289 | zero(Sym); |
| 290 | Syms.push_back(Sym); |
| 291 | } |
| 292 | addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL); |
| 293 | addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL); |
| 294 | addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK); |
| 295 | |
| 296 | writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset), |
| 297 | makeArrayRef(Syms)); |
| 298 | SHeader.sh_size = arrayDataSize(makeArrayRef(Syms)); |
| 299 | } |
| 300 | |
| 301 | template <class ELFT> |
| 302 | void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name, |
| 303 | StringTableBuilder &STB, |
| 304 | ContiguousBlobAccumulator &CBA) { |
| 305 | zero(SHeader); |
| 306 | SHeader.sh_name = DotShStrtab.addString(Name); |
| 307 | SHeader.sh_type = ELF::SHT_STRTAB; |
| 308 | STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset)); |
| 309 | SHeader.sh_size = STB.size(); |
| 310 | SHeader.sh_addralign = 1; |
| 311 | } |
| 312 | |
| 313 | template <class ELFT> |
| 314 | void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, |
| 315 | std::vector<Elf_Sym> &Syms, |
| 316 | unsigned SymbolBinding) { |
Simon Atanasyan | 048baca | 2014-03-14 06:53:30 +0000 | [diff] [blame] | 317 | for (const auto &Sym : Symbols) { |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 318 | Elf_Sym Symbol; |
| 319 | zero(Symbol); |
| 320 | if (!Sym.Name.empty()) |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 321 | Symbol.st_name = DotStrtab.addString(Sym.Name); |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 322 | Symbol.setBindingAndType(SymbolBinding, Sym.Type); |
Sean Silva | c4afa6d | 2013-06-21 01:11:48 +0000 | [diff] [blame] | 323 | if (!Sym.Section.empty()) { |
| 324 | unsigned Index; |
Simon Atanasyan | 35babf9 | 2014-04-06 09:02:55 +0000 | [diff] [blame] | 325 | if (SN2I.lookup(Sym.Section, Index)) { |
Sean Silva | c4afa6d | 2013-06-21 01:11:48 +0000 | [diff] [blame] | 326 | errs() << "error: Unknown section referenced: '" << Sym.Section |
| 327 | << "' by YAML symbol " << Sym.Name << ".\n"; |
| 328 | exit(1); |
| 329 | } |
| 330 | Symbol.st_shndx = Index; |
| 331 | } // else Symbol.st_shndex == SHN_UNDEF (== 0), since it was zero'd earlier. |
Sean Silva | 05001b9 | 2013-06-20 20:59:47 +0000 | [diff] [blame] | 332 | Symbol.st_value = Sym.Value; |
| 333 | Symbol.st_size = Sym.Size; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 334 | Syms.push_back(Symbol); |
| 335 | } |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 338 | template <class ELFT> |
| 339 | void |
| 340 | ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 341 | const ELFYAML::RawContentSection &Section, |
| 342 | ContiguousBlobAccumulator &CBA) { |
| 343 | Section.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset)); |
| 344 | SHeader.sh_entsize = 0; |
| 345 | SHeader.sh_size = Section.Content.binary_size(); |
| 346 | } |
| 347 | |
| 348 | template <class ELFT> |
| 349 | bool |
| 350 | ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader, |
| 351 | const ELFYAML::RelocationSection &Section, |
| 352 | ContiguousBlobAccumulator &CBA) { |
| 353 | if (Section.Type != llvm::ELF::SHT_REL && |
| 354 | Section.Type != llvm::ELF::SHT_RELA) { |
| 355 | errs() << "error: Invalid relocation section type.\n"; |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | bool IsRela = Section.Type == llvm::ELF::SHT_RELA; |
| 360 | SHeader.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel); |
| 361 | SHeader.sh_size = SHeader.sh_entsize * Section.Relocations.size(); |
| 362 | |
| 363 | auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset); |
| 364 | |
| 365 | for (const auto &Rel : Section.Relocations) { |
| 366 | unsigned SymIdx; |
| 367 | if (SymN2I.lookup(Rel.Symbol, SymIdx)) { |
| 368 | errs() << "error: Unknown symbol referenced: '" << Rel.Symbol |
| 369 | << "' at YAML relocation.\n"; |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | if (IsRela) { |
| 374 | Elf_Rela REntry; |
| 375 | zero(REntry); |
| 376 | REntry.r_offset = Rel.Offset; |
| 377 | REntry.r_addend = Rel.Addend; |
| 378 | REntry.setSymbolAndType(SymIdx, Rel.Type); |
| 379 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 380 | } else { |
| 381 | Elf_Rel REntry; |
| 382 | zero(REntry); |
| 383 | REntry.r_offset = Rel.Offset; |
| 384 | REntry.setSymbolAndType(SymIdx, Rel.Type); |
| 385 | OS.write((const char *)&REntry, sizeof(REntry)); |
| 386 | } |
| 387 | } |
| 388 | return true; |
| 389 | } |
| 390 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 391 | template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() { |
| 392 | SN2I.addName(".symtab", getDotSymTabSecNo()); |
| 393 | SN2I.addName(".strtab", getDotStrTabSecNo()); |
| 394 | SN2I.addName(".shstrtab", getDotShStrTabSecNo()); |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 395 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 396 | for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) { |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 397 | StringRef Name = Doc.Sections[i]->Name; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 398 | if (Name.empty()) |
| 399 | continue; |
| 400 | // "+ 1" to take into account the SHT_NULL entry. |
| 401 | if (SN2I.addName(Name, i + 1)) { |
| 402 | errs() << "error: Repeated section name: '" << Name |
| 403 | << "' at YAML section number " << i << ".\n"; |
| 404 | return false; |
| 405 | } |
Sean Silva | aff5125 | 2013-06-21 00:27:50 +0000 | [diff] [blame] | 406 | } |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 407 | return true; |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 410 | template <class ELFT> |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 411 | bool |
| 412 | ELFState<ELFT>::buildSymbolIndex(std::size_t &StartIndex, |
| 413 | const std::vector<ELFYAML::Symbol> &Symbols) { |
| 414 | for (const auto &Sym : Symbols) { |
| 415 | ++StartIndex; |
| 416 | if (Sym.Name.empty()) |
| 417 | continue; |
| 418 | if (SymN2I.addName(Sym.Name, StartIndex)) { |
| 419 | errs() << "error: Repeated symbol name: '" << Sym.Name << "'.\n"; |
| 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | template <class ELFT> |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 427 | int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { |
Simon Atanasyan | 220c54a | 2014-04-02 16:34:40 +0000 | [diff] [blame] | 428 | ELFState<ELFT> State(Doc); |
| 429 | if (!State.buildSectionIndex()) |
| 430 | return 1; |
| 431 | |
Simon Atanasyan | 42ac0dd | 2014-04-11 04:13:39 +0000 | [diff] [blame^] | 432 | std::size_t StartSymIndex = 0; |
| 433 | if (!State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Local) || |
| 434 | !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Global) || |
| 435 | !State.buildSymbolIndex(StartSymIndex, Doc.Symbols.Weak)) |
| 436 | return 1; |
| 437 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 438 | Elf_Ehdr Header; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 439 | State.initELFHeader(Header); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 440 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 441 | // TODO: Flesh out section header support. |
| 442 | // TODO: Program headers. |
| 443 | |
Sean Silva | 08a75ae | 2013-06-20 19:11:44 +0000 | [diff] [blame] | 444 | // XXX: This offset is tightly coupled with the order that we write |
| 445 | // things to `OS`. |
| 446 | const size_t SectionContentBeginOffset = |
| 447 | Header.e_ehsize + Header.e_shentsize * Header.e_shnum; |
| 448 | ContiguousBlobAccumulator CBA(SectionContentBeginOffset); |
Sean Silva | c1c290b | 2013-06-20 20:59:34 +0000 | [diff] [blame] | 449 | |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 450 | std::vector<Elf_Shdr> SHeaders; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 451 | if(!State.initSectionHeaders(SHeaders, CBA)) |
| 452 | return 1; |
Sean Silva | 3820593 | 2013-06-13 22:19:48 +0000 | [diff] [blame] | 453 | |
Sean Silva | 8217757 | 2013-06-22 01:38:00 +0000 | [diff] [blame] | 454 | // .symtab section. |
| 455 | Elf_Shdr SymtabSHeader; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 456 | State.initSymtabSectionHeader(SymtabSHeader, CBA); |
Sean Silva | 8217757 | 2013-06-22 01:38:00 +0000 | [diff] [blame] | 457 | SHeaders.push_back(SymtabSHeader); |
| 458 | |
Sean Silva | 6b08388 | 2013-06-18 23:14:03 +0000 | [diff] [blame] | 459 | // .strtab string table header. |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 460 | Elf_Shdr DotStrTabSHeader; |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 461 | State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab, |
| 462 | CBA); |
Sean Silva | 7d61722 | 2013-06-22 01:06:12 +0000 | [diff] [blame] | 463 | SHeaders.push_back(DotStrTabSHeader); |
Sean Silva | c313192 | 2013-06-18 21:37:50 +0000 | [diff] [blame] | 464 | |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 465 | // .shstrtab string table header. |
| 466 | Elf_Shdr ShStrTabSHeader; |
| 467 | State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab, |
| 468 | CBA); |
| 469 | SHeaders.push_back(ShStrTabSHeader); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 470 | |
| 471 | OS.write((const char *)&Header, sizeof(Header)); |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 472 | writeArrayData(OS, makeArrayRef(SHeaders)); |
Sean Silva | 46dffff | 2013-06-13 22:20:01 +0000 | [diff] [blame] | 473 | CBA.writeBlobToStream(OS); |
Sean Silva | 415d93f | 2013-06-17 20:14:59 +0000 | [diff] [blame] | 474 | return 0; |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 477 | static bool is64Bit(const ELFYAML::Object &Doc) { |
| 478 | return Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); |
| 479 | } |
| 480 | |
| 481 | static bool isLittleEndian(const ELFYAML::Object &Doc) { |
| 482 | return Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); |
| 483 | } |
| 484 | |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 485 | int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) { |
| 486 | yaml::Input YIn(Buf->getBuffer()); |
| 487 | ELFYAML::Object Doc; |
| 488 | YIn >> Doc; |
| 489 | if (YIn.error()) { |
| 490 | errs() << "yaml2obj: Failed to parse YAML file!\n"; |
| 491 | return 1; |
| 492 | } |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 493 | using object::ELFType; |
| 494 | typedef ELFType<support::little, 8, true> LE64; |
| 495 | typedef ELFType<support::big, 8, true> BE64; |
| 496 | typedef ELFType<support::little, 4, false> LE32; |
| 497 | typedef ELFType<support::big, 4, false> BE32; |
| 498 | if (is64Bit(Doc)) { |
| 499 | if (isLittleEndian(Doc)) |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 500 | return ELFState<LE64>::writeELF(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 501 | else |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 502 | return ELFState<BE64>::writeELF(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 503 | } else { |
Sean Silva | 11caeba | 2013-06-22 01:03:35 +0000 | [diff] [blame] | 504 | if (isLittleEndian(Doc)) |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 505 | return ELFState<LE32>::writeELF(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 506 | else |
Simon Atanasyan | 3ee21b0 | 2014-04-02 16:34:54 +0000 | [diff] [blame] | 507 | return ELFState<BE32>::writeELF(outs(), Doc); |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 508 | } |
Sean Silva | f99309c | 2013-06-10 23:44:15 +0000 | [diff] [blame] | 509 | } |