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