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