Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 1 | //===- subzero/src/IceELFObjectWriter.h - ELF object writer -----*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Abstraction for a writer that is responsible for writing an ELF file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| 15 | #define SUBZERO_SRC_ICEELFOBJECTWRITER_H |
| 16 | |
| 17 | #include "IceDefs.h" |
| 18 | #include "IceELFSection.h" |
| 19 | #include "IceELFStreamer.h" |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 20 | #include "IceTypes.h" |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 21 | |
| 22 | using namespace llvm::ELF; |
| 23 | |
| 24 | namespace Ice { |
| 25 | |
| 26 | // Higher level ELF object writer. Manages section information and writes |
| 27 | // the final ELF object. The object writer will write to file the code |
| 28 | // and data as it is being defined (rather than keep a copy). |
| 29 | // After all definitions are written out, it will finalize the bookkeeping |
| 30 | // sections and write them out. Expected usage: |
| 31 | // |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 32 | // (1) writeInitialELFHeader (invoke once) |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 33 | // (2) writeDataSection (may be invoked multiple times, as long as |
| 34 | // SectionSuffix is unique) |
| 35 | // (3) writeFunctionCode (must invoke once per function) |
| 36 | // (4) writeConstantPool (must invoke once per pooled primitive type) |
| 37 | // (5) setUndefinedSyms (invoke once) |
| 38 | // (6) writeNonUserSections (invoke once) |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 39 | // |
| 40 | // The requirement for writeDataSection to be invoked only once can |
| 41 | // be relaxed if using -fdata-sections. The requirement to invoke only once |
| 42 | // without -fdata-sections is so that variables that belong to each possible |
| 43 | // SectionType are contiguous in the file. With -fdata-sections, each global |
| 44 | // variable is in a separate section and therefore the sections will be |
| 45 | // trivially contiguous. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 46 | class ELFObjectWriter { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 47 | ELFObjectWriter() = delete; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 48 | ELFObjectWriter(const ELFObjectWriter &) = delete; |
| 49 | ELFObjectWriter &operator=(const ELFObjectWriter &) = delete; |
| 50 | |
| 51 | public: |
| 52 | ELFObjectWriter(GlobalContext &Ctx, ELFStreamer &Out); |
| 53 | |
| 54 | // Write the initial ELF header. This is just to reserve space in the ELF |
| 55 | // file. Reserving space allows the other functions to write text |
| 56 | // and data directly to the file and get the right file offsets. |
| 57 | void writeInitialELFHeader(); |
| 58 | |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 59 | // Copy initializer data for globals to file and note the offset and size |
| 60 | // of each global's definition in the symbol table. |
| 61 | // Use the given target's RelocationKind for any relocations. |
| 62 | void writeDataSection(const VariableDeclarationList &Vars, |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 63 | FixupKind RelocationKind, |
| 64 | const IceString &SectionSuffix); |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 65 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 66 | // Copy data of a function's text section to file and note the offset of the |
| 67 | // symbol's definition in the symbol table. |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 68 | // Copy the text fixups for use after all functions are written. |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 69 | // The text buffer and fixups are extracted from the Assembler object. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 70 | void writeFunctionCode(const IceString &FuncName, bool IsInternal, |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 71 | const Assembler *Asm); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 72 | |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 73 | // Queries the GlobalContext for constant pools of the given type |
| 74 | // and writes out read-only data sections for those constants. This also |
| 75 | // fills the symbol table with labels for each constant pool entry. |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 76 | template <typename ConstType> void writeConstantPool(Type Ty); |
| 77 | |
Jan Voung | 261cae3 | 2015-02-01 10:31:03 -0800 | [diff] [blame] | 78 | // Populate the symbol table with a list of external/undefined symbols. |
| 79 | void setUndefinedSyms(const ConstantList &UndefSyms); |
| 80 | |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 81 | // Do final layout and write out the rest of the object file. |
| 82 | // Finally, patch up the initial ELF header with the final info. |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 83 | void writeNonUserSections(); |
| 84 | |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 85 | // Which type of ELF section a global variable initializer belongs to. |
| 86 | // This is used as an array index so should start at 0 and be contiguous. |
| 87 | enum SectionType { ROData = 0, Data, BSS, NumSectionTypes }; |
| 88 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 89 | private: |
| 90 | GlobalContext &Ctx; |
| 91 | ELFStreamer &Str; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 92 | bool SectionNumbersAssigned = false; |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 93 | bool ELF64; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 94 | |
| 95 | // All created sections, separated into different pools. |
| 96 | typedef std::vector<ELFSection *> SectionList; |
| 97 | typedef std::vector<ELFTextSection *> TextSectionList; |
| 98 | typedef std::vector<ELFDataSection *> DataSectionList; |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 99 | typedef std::vector<ELFRelocationSection *> RelSectionList; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 100 | TextSectionList TextSections; |
| 101 | RelSectionList RelTextSections; |
| 102 | DataSectionList DataSections; |
| 103 | RelSectionList RelDataSections; |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 104 | DataSectionList RODataSections; |
| 105 | RelSectionList RelRODataSections; |
| 106 | DataSectionList BSSSections; |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 107 | |
| 108 | // Handles to special sections that need incremental bookkeeping. |
| 109 | ELFSection *NullSection; |
| 110 | ELFStringTableSection *ShStrTab; |
| 111 | ELFSymbolTableSection *SymTab; |
| 112 | ELFStringTableSection *StrTab; |
| 113 | |
| 114 | template <typename T> |
| 115 | T *createSection(const IceString &Name, Elf64_Word ShType, |
| 116 | Elf64_Xword ShFlags, Elf64_Xword ShAddralign, |
| 117 | Elf64_Xword ShEntsize); |
| 118 | |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 119 | // Create a relocation section, given the related section |
| 120 | // (e.g., .text, .data., .rodata). |
| 121 | ELFRelocationSection * |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 122 | createRelocationSection(const ELFSection *RelatedSection); |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 123 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 124 | // Align the file position before writing out a section's data, |
| 125 | // and return the position of the file. |
| 126 | Elf64_Off alignFileOffset(Elf64_Xword Align); |
| 127 | |
| 128 | // Assign an ordering / section numbers to each section. |
| 129 | // Fill in other information that is only known near the end |
| 130 | // (such as the size, if it wasn't already incrementally updated). |
| 131 | // This then collects all sections in the decided order, into one vector, |
| 132 | // for conveniently writing out all of the section headers. |
| 133 | void assignSectionNumbersInfo(SectionList &AllSections); |
| 134 | |
| 135 | // This function assigns .foo and .rel.foo consecutive section numbers. |
| 136 | // It also sets the relocation section's sh_info field to the related |
| 137 | // section's number. |
| 138 | template <typename UserSectionList> |
| 139 | void assignRelSectionNumInPairs(SizeT &CurSectionNumber, |
| 140 | UserSectionList &UserSections, |
| 141 | RelSectionList &RelSections, |
| 142 | SectionList &AllSections); |
| 143 | |
| 144 | // Link the relocation sections to the symbol table. |
| 145 | void assignRelLinkNum(SizeT SymTabNumber, RelSectionList &RelSections); |
| 146 | |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 147 | // Helper function for writeDataSection. Writes a data section of type |
| 148 | // SectionType, given the global variables Vars belonging to that SectionType. |
| 149 | void writeDataOfType(SectionType SectionType, |
| 150 | const VariableDeclarationList &Vars, |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 151 | FixupKind RelocationKind, |
| 152 | const IceString &SectionSuffix); |
Jan Voung | 72984d8 | 2015-01-29 14:42:38 -0800 | [diff] [blame] | 153 | |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 154 | // Write the final relocation sections given the final symbol table. |
| 155 | // May also be able to seek around the file and resolve function calls |
| 156 | // that are for functions within the same section. |
Jan Voung | 1f47ad0 | 2015-03-20 15:01:26 -0700 | [diff] [blame] | 157 | void writeAllRelocationSections(); |
| 158 | void writeRelocationSections(RelSectionList &RelSections); |
Jan Voung | ec27073 | 2015-01-12 17:00:22 -0800 | [diff] [blame] | 159 | |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 160 | // Write the ELF file header with the given information about sections. |
| 161 | template <bool IsELF64> |
Jan Voung | a601cc5 | 2014-12-03 15:51:22 -0800 | [diff] [blame] | 162 | void writeELFHeaderInternal(Elf64_Off SectionHeaderOffset, |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 163 | SizeT SectHeaderStrIndex, SizeT NumSections); |
| 164 | }; |
| 165 | |
| 166 | } // end of namespace Ice |
| 167 | |
| 168 | #endif // SUBZERO_SRC_ICEELFOBJECTWRITER_H |