Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 1 | //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 5 | // This file was developed by Chris Lattner and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the target-independent ELF writer. This file writes out |
| 11 | // the ELF file in the following order: |
| 12 | // |
| 13 | // #1. ELF Header |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 14 | // #2. '.text' section |
| 15 | // #3. '.data' section |
| 16 | // #4. '.bss' section (conceptual position in file) |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 17 | // ... |
| 18 | // #X. '.shstrtab' section |
| 19 | // #Y. Section Table |
| 20 | // |
| 21 | // The entries in the section table are laid out as: |
| 22 | // #0. Null entry [required] |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 23 | // #1. ".text" entry - the program code |
| 24 | // #2. ".data" entry - global variables with initializers. [ if needed ] |
| 25 | // #3. ".bss" entry - global variables without initializers. [ if needed ] |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 26 | // ... |
| 27 | // #N. ".shstrtab" entry - String table for the section names. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 28 | // |
| 29 | // NOTE: This code should eventually be extended to support 64-bit ELF (this |
| 30 | // won't be hard), but we haven't done so yet! |
| 31 | // |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #include "llvm/CodeGen/ELFWriter.h" |
| 35 | #include "llvm/Module.h" |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 37 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // ELFCodeEmitter Implementation |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 46 | namespace llvm { |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 47 | /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for |
| 48 | /// functions to the ELF file. |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 49 | class ELFCodeEmitter : public MachineCodeEmitter { |
| 50 | ELFWriter &EW; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 51 | ELFWriter::ELFSection *ES; // Section to write to. |
| 52 | std::vector<unsigned char> *OutBuffer; |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 53 | size_t FnStart; |
| 54 | public: |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 55 | ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {} |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 57 | void startFunction(MachineFunction &F); |
| 58 | void finishFunction(MachineFunction &F); |
Chris Lattner | 5fe7b6e | 2005-07-11 06:17:35 +0000 | [diff] [blame] | 59 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 60 | void emitConstantPool(MachineConstantPool *MCP) { |
| 61 | if (MCP->isEmpty()) return; |
| 62 | assert(0 && "unimp"); |
| 63 | } |
| 64 | virtual void emitByte(unsigned char B) { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 65 | OutBuffer->push_back(B); |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 66 | } |
| 67 | virtual void emitWordAt(unsigned W, unsigned *Ptr) { |
| 68 | assert(0 && "ni"); |
| 69 | } |
| 70 | virtual void emitWord(unsigned W) { |
| 71 | assert(0 && "ni"); |
| 72 | } |
| 73 | virtual uint64_t getCurrentPCValue() { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 74 | return OutBuffer->size(); |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 75 | } |
| 76 | virtual uint64_t getCurrentPCOffset() { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 77 | return OutBuffer->size()-FnStart; |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 78 | } |
| 79 | void addRelocation(const MachineRelocation &MR) { |
| 80 | assert(0 && "relo not handled yet!"); |
| 81 | } |
| 82 | virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { |
| 83 | assert(0 && "CP not implementated yet!"); |
Jeff Cohen | 44213c9 | 2005-07-12 02:53:33 +0000 | [diff] [blame] | 84 | return 0; |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 86 | |
Andrew Lenharth | fe66039 | 2005-07-28 18:13:59 +0000 | [diff] [blame] | 87 | virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) { |
| 88 | assert(0 && "Globals not implemented yet!"); |
| 89 | return 0; |
| 90 | } |
| 91 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 92 | /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE! |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 93 | void startFunctionStub(unsigned StubSize) { |
| 94 | assert(0 && "JIT specific function called!"); |
| 95 | abort(); |
| 96 | } |
| 97 | void *finishFunctionStub(const Function *F) { |
| 98 | assert(0 && "JIT specific function called!"); |
| 99 | abort(); |
| 100 | return 0; |
| 101 | } |
| 102 | }; |
| 103 | } |
| 104 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 105 | /// startFunction - This callback is invoked when a new machine function is |
| 106 | /// about to be emitted. |
| 107 | void ELFCodeEmitter::startFunction(MachineFunction &F) { |
| 108 | // Align the output buffer to the appropriate alignment. |
| 109 | unsigned Align = 16; // FIXME: GENERICIZE!! |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 110 | // Get the ELF Section that this function belongs in. |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 111 | ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS, |
| 112 | ELFWriter::ELFSection::SHF_EXECINSTR | |
| 113 | ELFWriter::ELFSection::SHF_ALLOC); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 114 | OutBuffer = &ES->SectionData; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 116 | // Upgrade the section alignment if required. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 117 | if (ES->Align < Align) ES->Align = Align; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 119 | // Add padding zeros to the end of the buffer to make sure that the |
| 120 | // function will start on the correct byte alignment within the section. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 121 | size_t SectionOff = OutBuffer->size(); |
| 122 | ELFWriter::align(*OutBuffer, Align); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 124 | FnStart = OutBuffer->size(); |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /// finishFunction - This callback is invoked after the function is completely |
| 128 | /// finished. |
| 129 | void ELFCodeEmitter::finishFunction(MachineFunction &F) { |
| 130 | // We now know the size of the function, add a symbol to represent it. |
| 131 | ELFWriter::ELFSym FnSym(F.getFunction()); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 133 | // Figure out the binding (linkage) of the symbol. |
| 134 | switch (F.getFunction()->getLinkage()) { |
| 135 | default: |
| 136 | // appending linkage is illegal for functions. |
| 137 | assert(0 && "Unknown linkage type!"); |
| 138 | case GlobalValue::ExternalLinkage: |
| 139 | FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL); |
| 140 | break; |
| 141 | case GlobalValue::LinkOnceLinkage: |
| 142 | case GlobalValue::WeakLinkage: |
| 143 | FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK); |
| 144 | break; |
| 145 | case GlobalValue::InternalLinkage: |
| 146 | FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL); |
| 147 | break; |
| 148 | } |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 149 | |
| 150 | ES->Size = OutBuffer->size(); |
| 151 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 152 | FnSym.SetType(ELFWriter::ELFSym::STT_FUNC); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 153 | FnSym.SectionIdx = ES->SectionIdx; |
| 154 | FnSym.Value = FnStart; // Value = Offset from start of Section. |
| 155 | FnSym.Size = OutBuffer->size()-FnStart; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 157 | // Finally, add it to the symtab. |
| 158 | EW.SymbolTable.push_back(FnSym); |
| 159 | } |
| 160 | |
| 161 | //===----------------------------------------------------------------------===// |
| 162 | // ELFWriter Implementation |
| 163 | //===----------------------------------------------------------------------===// |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 165 | ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { |
| 166 | e_machine = 0; // e_machine defaults to 'No Machine' |
| 167 | e_flags = 0; // e_flags defaults to 0, no flags. |
| 168 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 169 | is64Bit = TM.getTargetData().getPointerSizeInBits() == 64; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 170 | isLittleEndian = TM.getTargetData().isLittleEndian(); |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 171 | |
| 172 | // Create the machine code emitter object for this target. |
| 173 | MCE = new ELFCodeEmitter(*this); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 174 | NumSections = 0; |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | ELFWriter::~ELFWriter() { |
| 178 | delete MCE; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // doInitialization - Emit the file header and all of the global variables for |
| 182 | // the module to the ELF file. |
| 183 | bool ELFWriter::doInitialization(Module &M) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 184 | Mang = new Mangler(M); |
| 185 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 186 | // Local alias to shortenify coming code. |
| 187 | std::vector<unsigned char> &FH = FileHeader; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 189 | outbyte(FH, 0x7F); // EI_MAG0 |
| 190 | outbyte(FH, 'E'); // EI_MAG1 |
| 191 | outbyte(FH, 'L'); // EI_MAG2 |
| 192 | outbyte(FH, 'F'); // EI_MAG3 |
| 193 | outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS |
| 194 | outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA |
| 195 | outbyte(FH, 1); // EI_VERSION |
| 196 | FH.resize(16); // EI_PAD up to 16 bytes. |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 198 | // This should change for shared objects. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 199 | outhalf(FH, 1); // e_type = ET_REL |
| 200 | outhalf(FH, e_machine); // e_machine = whatever the target wants |
| 201 | outword(FH, 1); // e_version = 1 |
| 202 | outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file |
| 203 | outaddr(FH, 0); // e_phoff = 0 -> no program header for .o |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 205 | ELFHeader_e_shoff_Offset = FH.size(); |
| 206 | outaddr(FH, 0); // e_shoff |
| 207 | outword(FH, e_flags); // e_flags = whatever the target wants |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 209 | outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size |
| 210 | outhalf(FH, 0); // e_phentsize = prog header entry size |
| 211 | outhalf(FH, 0); // e_phnum = # prog header entries = 0 |
| 212 | outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 213 | |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 215 | ELFHeader_e_shnum_Offset = FH.size(); |
| 216 | outhalf(FH, 0); // e_shnum = # of section header ents |
| 217 | ELFHeader_e_shstrndx_Offset = FH.size(); |
| 218 | outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab' |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 220 | // Add the null section, which is required to be first in the file. |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 221 | getSection("", 0, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 223 | // Start up the symbol table. The first entry in the symtab is the null |
| 224 | // entry. |
| 225 | SymbolTable.push_back(ELFSym(0)); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 230 | void ELFWriter::EmitGlobal(GlobalVariable *GV) { |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 231 | // If this is an external global, emit it now. TODO: Note that it would be |
| 232 | // better to ignore the symbol here and only add it to the symbol table if |
| 233 | // referenced. |
| 234 | if (!GV->hasInitializer()) { |
| 235 | ELFSym ExternalSym(GV); |
| 236 | ExternalSym.SetBind(ELFSym::STB_GLOBAL); |
| 237 | ExternalSym.SetType(ELFSym::STT_NOTYPE); |
| 238 | ExternalSym.SectionIdx = ELFSection::SHN_UNDEF; |
| 239 | SymbolTable.push_back(ExternalSym); |
| 240 | return; |
| 241 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 243 | const Type *GVType = (const Type*)GV->getType(); |
| 244 | unsigned Align = TM.getTargetData().getTypeAlignment(GVType); |
| 245 | unsigned Size = TM.getTargetData().getTypeSize(GVType); |
| 246 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 247 | // If this global has a zero initializer, it is part of the .bss or common |
| 248 | // section. |
| 249 | if (GV->getInitializer()->isNullValue()) { |
| 250 | // If this global is part of the common block, add it now. Variables are |
| 251 | // part of the common block if they are zero initialized and allowed to be |
| 252 | // merged with other symbols. |
| 253 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) { |
| 254 | ELFSym CommonSym(GV); |
| 255 | // Value for common symbols is the alignment required. |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 256 | CommonSym.Value = Align; |
| 257 | CommonSym.Size = Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 258 | CommonSym.SetBind(ELFSym::STB_GLOBAL); |
| 259 | CommonSym.SetType(ELFSym::STT_OBJECT); |
| 260 | // TODO SOMEDAY: add ELF visibility. |
| 261 | CommonSym.SectionIdx = ELFSection::SHN_COMMON; |
| 262 | SymbolTable.push_back(CommonSym); |
| 263 | return; |
| 264 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 266 | // Otherwise, this symbol is part of the .bss section. Emit it now. |
| 267 | |
| 268 | // Handle alignment. Ensure section is aligned at least as much as required |
| 269 | // by this symbol. |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 270 | ELFSection &BSSSection = getBSSSection(); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 271 | BSSSection.Align = std::max(BSSSection.Align, Align); |
| 272 | |
| 273 | // Within the section, emit enough virtual padding to get us to an alignment |
| 274 | // boundary. |
| 275 | if (Align) |
| 276 | BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1); |
| 277 | |
| 278 | ELFSym BSSSym(GV); |
| 279 | BSSSym.Value = BSSSection.Size; |
| 280 | BSSSym.Size = Size; |
| 281 | BSSSym.SetType(ELFSym::STT_OBJECT); |
| 282 | |
| 283 | switch (GV->getLinkage()) { |
| 284 | default: // weak/linkonce handled above |
| 285 | assert(0 && "Unexpected linkage type!"); |
| 286 | case GlobalValue::AppendingLinkage: // FIXME: This should be improved! |
| 287 | case GlobalValue::ExternalLinkage: |
| 288 | BSSSym.SetBind(ELFSym::STB_GLOBAL); |
| 289 | break; |
| 290 | case GlobalValue::InternalLinkage: |
| 291 | BSSSym.SetBind(ELFSym::STB_LOCAL); |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | // Set the idx of the .bss section |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 296 | BSSSym.SectionIdx = BSSSection.SectionIdx; |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 297 | SymbolTable.push_back(BSSSym); |
| 298 | |
| 299 | // Reserve space in the .bss section for this symbol. |
| 300 | BSSSection.Size += Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 301 | return; |
| 302 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 304 | // FIXME: handle .rodata |
| 305 | //assert(!GV->isConstant() && "unimp"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 307 | // FIXME: handle .data |
| 308 | //assert(0 && "unimp"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | |
| 312 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 313 | // Nothing to do here, this is all done through the MCE object above. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 314 | return false; |
| 315 | } |
| 316 | |
| 317 | /// doFinalization - Now that the module has been completely processed, emit |
| 318 | /// the ELF file to 'O'. |
| 319 | bool ELFWriter::doFinalization(Module &M) { |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 320 | // Okay, the ELF header and .text sections have been completed, build the |
| 321 | // .data, .bss, and "common" sections next. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 322 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 323 | I != E; ++I) |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 324 | EmitGlobal(I); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 325 | |
| 326 | // Emit the symbol table now, if non-empty. |
| 327 | EmitSymbolTable(); |
| 328 | |
| 329 | // FIXME: Emit the relocations now. |
| 330 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 331 | // Emit the string table for the sections in the ELF file we have. |
| 332 | EmitSectionTableStringTable(); |
| 333 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 334 | // Emit the sections to the .o file, and emit the section table for the file. |
| 335 | OutputSectionsAndSectionTable(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 337 | // We are done with the abstract symbols. |
| 338 | SectionList.clear(); |
| 339 | NumSections = 0; |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 340 | |
| 341 | // Release the name mangler object. |
| 342 | delete Mang; Mang = 0; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 346 | /// EmitSymbolTable - If the current symbol table is non-empty, emit the string |
| 347 | /// table for it and then the symbol table itself. |
| 348 | void ELFWriter::EmitSymbolTable() { |
| 349 | if (SymbolTable.size() == 1) return; // Only the null entry. |
| 350 | |
| 351 | // FIXME: compact all local symbols to the start of the symtab. |
| 352 | unsigned FirstNonLocalSymbol = 1; |
| 353 | |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 354 | ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 355 | StrTab.Align = 1; |
| 356 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 357 | DataBuffer &StrTabBuf = StrTab.SectionData; |
| 358 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 359 | // Set the zero'th symbol to a null byte, as required. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 360 | outbyte(StrTabBuf, 0); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 361 | SymbolTable[0].NameIdx = 0; |
| 362 | unsigned Index = 1; |
| 363 | for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 364 | // Use the name mangler to uniquify the LLVM symbol. |
| 365 | std::string Name = Mang->getValueName(SymbolTable[i].GV); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 366 | |
| 367 | if (Name.empty()) { |
| 368 | SymbolTable[i].NameIdx = 0; |
| 369 | } else { |
| 370 | SymbolTable[i].NameIdx = Index; |
| 371 | |
| 372 | // Add the name to the output buffer, including the null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 373 | StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end()); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 374 | |
| 375 | // Add a null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 376 | StrTabBuf.push_back(0); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 377 | |
| 378 | // Keep track of the number of bytes emitted to this section. |
| 379 | Index += Name.size()+1; |
| 380 | } |
| 381 | } |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 382 | assert(Index == StrTabBuf.size()); |
| 383 | StrTab.Size = Index; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 384 | |
| 385 | // Now that we have emitted the string table and know the offset into the |
| 386 | // string table of each symbol, emit the symbol table itself. |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 387 | ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0); |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 388 | SymTab.Align = is64Bit ? 8 : 4; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 389 | SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab. |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 390 | SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol. |
| 391 | SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64 |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 392 | DataBuffer &SymTabBuf = SymTab.SectionData; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 394 | if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit. |
| 395 | for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { |
| 396 | ELFSym &Sym = SymbolTable[i]; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 397 | outword(SymTabBuf, Sym.NameIdx); |
| 398 | outaddr32(SymTabBuf, Sym.Value); |
| 399 | outword(SymTabBuf, Sym.Size); |
| 400 | outbyte(SymTabBuf, Sym.Info); |
| 401 | outbyte(SymTabBuf, Sym.Other); |
| 402 | outhalf(SymTabBuf, Sym.SectionIdx); |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 403 | } |
| 404 | } else { |
| 405 | for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) { |
| 406 | ELFSym &Sym = SymbolTable[i]; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 407 | outword(SymTabBuf, Sym.NameIdx); |
| 408 | outbyte(SymTabBuf, Sym.Info); |
| 409 | outbyte(SymTabBuf, Sym.Other); |
| 410 | outhalf(SymTabBuf, Sym.SectionIdx); |
| 411 | outaddr64(SymTabBuf, Sym.Value); |
| 412 | outxword(SymTabBuf, Sym.Size); |
Chris Lattner | 46c5305 | 2005-07-12 06:57:52 +0000 | [diff] [blame] | 413 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 416 | SymTab.Size = SymTabBuf.size(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 419 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 420 | /// ELF Section Table string table: the string table that holds all of the |
| 421 | /// section names. |
| 422 | void ELFWriter::EmitSectionTableStringTable() { |
| 423 | // First step: add the section for the string table to the list of sections: |
Chris Lattner | 0003395 | 2005-07-16 17:36:04 +0000 | [diff] [blame] | 424 | ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 425 | |
| 426 | // Now that we know which section number is the .shstrtab section, update the |
| 427 | // e_shstrndx entry in the ELF header. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 428 | fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 429 | |
| 430 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 431 | // the string table. |
| 432 | unsigned Index = 0; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 433 | DataBuffer &Buf = SHStrTab.SectionData; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 435 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 436 | E = SectionList.end(); I != E; ++I) { |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 437 | // Set the index into the table. Note if we have lots of entries with |
| 438 | // common suffixes, we could memoize them here if we cared. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 439 | I->NameIdx = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 440 | |
| 441 | // Add the name to the output buffer, including the null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 442 | Buf.insert(Buf.end(), I->Name.begin(), I->Name.end()); |
| 443 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 444 | // Add a null terminator. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 445 | Buf.push_back(0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 446 | |
| 447 | // Keep track of the number of bytes emitted to this section. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 448 | Index += I->Name.size()+1; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Set the size of .shstrtab now that we know what it is. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 452 | assert(Index == Buf.size()); |
| 453 | SHStrTab.Size = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 456 | /// OutputSectionsAndSectionTable - Now that we have constructed the file header |
| 457 | /// and all of the sections, emit these to the ostream destination and emit the |
| 458 | /// SectionTable. |
| 459 | void ELFWriter::OutputSectionsAndSectionTable() { |
| 460 | // Pass #1: Compute the file offset for each section. |
| 461 | size_t FileOff = FileHeader.size(); // File header first. |
| 462 | |
| 463 | // Emit all of the section data in order. |
| 464 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 465 | E = SectionList.end(); I != E; ++I) { |
| 466 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 467 | if (I->Align) |
| 468 | FileOff = (FileOff+I->Align-1) & ~(I->Align-1); |
| 469 | I->Offset = FileOff; |
| 470 | FileOff += I->SectionData.size(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 473 | // Align Section Header. |
| 474 | unsigned TableAlign = is64Bit ? 8 : 4; |
| 475 | FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 476 | |
| 477 | // Now that we know where all of the sections will be emitted, set the e_shnum |
| 478 | // entry in the ELF header. |
| 479 | fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 480 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 481 | // Now that we know the offset in the file of the section table, update the |
| 482 | // e_shoff address in the ELF header. |
| 483 | fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 485 | // Now that we know all of the data in the file header, emit it and all of the |
| 486 | // sections! |
| 487 | O.write((char*)&FileHeader[0], FileHeader.size()); |
| 488 | FileOff = FileHeader.size(); |
| 489 | DataBuffer().swap(FileHeader); |
| 490 | |
| 491 | DataBuffer Table; |
| 492 | |
| 493 | // Emit all of the section data and build the section table itself. |
| 494 | while (!SectionList.empty()) { |
| 495 | const ELFSection &S = *SectionList.begin(); |
| 496 | |
| 497 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 498 | if (S.Align) |
| 499 | for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1); |
| 500 | FileOff != NewFileOff; ++FileOff) |
Jeff Cohen | 23c73a1 | 2005-08-19 16:19:21 +0000 | [diff] [blame] | 501 | O.put((char)0xAB); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 502 | O.write((char*)&S.SectionData[0], S.SectionData.size()); |
| 503 | FileOff += S.SectionData.size(); |
| 504 | |
| 505 | outword(Table, S.NameIdx); // sh_name - Symbol table name idx |
| 506 | outword(Table, S.Type); // sh_type - Section contents & semantics |
| 507 | outword(Table, S.Flags); // sh_flags - Section flags. |
| 508 | outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in. |
| 509 | outaddr(Table, S.Offset); // sh_offset - Offset from the file start. |
| 510 | outword(Table, S.Size); // sh_size - The section size. |
| 511 | outword(Table, S.Link); // sh_link - Section header table index link. |
| 512 | outword(Table, S.Info); // sh_info - Auxillary information. |
| 513 | outword(Table, S.Align); // sh_addralign - Alignment of section. |
| 514 | outword(Table, S.EntSize); // sh_entsize - Size of entries in the section. |
| 515 | |
| 516 | SectionList.pop_front(); |
| 517 | } |
| 518 | |
| 519 | // Align output for the section table. |
| 520 | for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 521 | FileOff != NewFileOff; ++FileOff) |
Jeff Cohen | 23c73a1 | 2005-08-19 16:19:21 +0000 | [diff] [blame] | 522 | O.put((char)0xAB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 524 | // Emit the section table itself. |
| 525 | O.write((char*)&Table[0], Table.size()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 526 | } |