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