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 | // |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "elfwriter" |
| 32 | |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 33 | #include "ELFWriter.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 34 | #include "ELFCodeEmitter.h" |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 35 | #include "ELF.h" |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 36 | #include "llvm/Constants.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 37 | #include "llvm/Module.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 38 | #include "llvm/PassManager.h" |
Chris Lattner | 02b73ab | 2009-01-04 20:19:20 +0000 | [diff] [blame] | 39 | #include "llvm/DerivedTypes.h" |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/BinaryObject.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/FileWriters.h" |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 43 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Mangler.h" |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Streams.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Debug.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 51 | #include <list> |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 54 | char ELFWriter::ID = 0; |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 55 | /// AddELFWriter - Concrete function to add the ELF writer to the function pass |
| 56 | /// manager. |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 57 | MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM, |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 58 | raw_ostream &O, |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 59 | TargetMachine &TM) { |
| 60 | ELFWriter *EW = new ELFWriter(O, TM); |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 61 | PM.add(EW); |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 62 | return &EW->getMachineCodeEmitter(); |
| 63 | } |
| 64 | |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 65 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0e18050 | 2005-07-11 06:34:30 +0000 | [diff] [blame] | 66 | // ELFWriter Implementation |
| 67 | //===----------------------------------------------------------------------===// |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 68 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 69 | ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 70 | : MachineFunctionPass(&ID), O(o), TM(tm), |
| 71 | is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64), |
| 72 | isLittleEndian(TM.getTargetData()->isLittleEndian()), |
| 73 | ElfHdr(isLittleEndian, is64Bit) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 74 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 75 | TAI = TM.getTargetAsmInfo(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 76 | TEW = TM.getELFWriterInfo(); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 77 | |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 78 | // Create the machine code emitter object for this target. |
Bill Wendling | e911615 | 2007-01-17 09:06:13 +0000 | [diff] [blame] | 79 | MCE = new ELFCodeEmitter(*this); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 80 | |
| 81 | // Inital number of sections |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 82 | NumSections = 0; |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | ELFWriter::~ELFWriter() { |
| 86 | delete MCE; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // doInitialization - Emit the file header and all of the global variables for |
| 90 | // the module to the ELF file. |
| 91 | bool ELFWriter::doInitialization(Module &M) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 92 | Mang = new Mangler(M); |
| 93 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 94 | // ELF Header |
| 95 | // ---------- |
| 96 | // Fields e_shnum e_shstrndx are only known after all section have |
| 97 | // been emitted. They locations in the ouput buffer are recorded so |
| 98 | // to be patched up later. |
| 99 | // |
| 100 | // Note |
| 101 | // ---- |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 102 | // emitWord method behaves differently for ELF32 and ELF64, writing |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 103 | // 4 bytes in the former and 8 in the last for *_off and *_addr elf types |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 104 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 105 | ElfHdr.emitByte(0x7f); // e_ident[EI_MAG0] |
| 106 | ElfHdr.emitByte('E'); // e_ident[EI_MAG1] |
| 107 | ElfHdr.emitByte('L'); // e_ident[EI_MAG2] |
| 108 | ElfHdr.emitByte('F'); // e_ident[EI_MAG3] |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 109 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 110 | ElfHdr.emitByte(TEW->getEIClass()); // e_ident[EI_CLASS] |
| 111 | ElfHdr.emitByte(TEW->getEIData()); // e_ident[EI_DATA] |
| 112 | ElfHdr.emitByte(EV_CURRENT); // e_ident[EI_VERSION] |
| 113 | ElfHdr.emitAlignment(16); // e_ident[EI_NIDENT-EI_PAD] |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 114 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 115 | ElfHdr.emitWord16(ET_REL); // e_type |
| 116 | ElfHdr.emitWord16(TEW->getEMachine()); // e_machine = target |
| 117 | ElfHdr.emitWord32(EV_CURRENT); // e_version |
| 118 | ElfHdr.emitWord(0); // e_entry, no entry point in .o file |
| 119 | ElfHdr.emitWord(0); // e_phoff, no program header for .o |
| 120 | ELFHdr_e_shoff_Offset = ElfHdr.size(); |
| 121 | ElfHdr.emitWord(0); // e_shoff = sec hdr table off in bytes |
| 122 | ElfHdr.emitWord32(TEW->getEFlags()); // e_flags = whatever the target wants |
| 123 | ElfHdr.emitWord16(TEW->getHdrSize()); // e_ehsize = ELF header size |
| 124 | ElfHdr.emitWord16(0); // e_phentsize = prog header entry size |
| 125 | ElfHdr.emitWord16(0); // e_phnum = # prog header entries = 0 |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 126 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 127 | // e_shentsize = Section header entry size |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 128 | ElfHdr.emitWord16(TEW->getSHdrSize()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 129 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 130 | // e_shnum = # of section header ents |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 131 | ELFHdr_e_shnum_Offset = ElfHdr.size(); |
| 132 | ElfHdr.emitWord16(0); // Placeholder |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 133 | |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 134 | // e_shstrndx = Section # of '.shstrtab' |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 135 | ELFHdr_e_shstrndx_Offset = ElfHdr.size(); |
| 136 | ElfHdr.emitWord16(0); // Placeholder |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 138 | // Add the null section, which is required to be first in the file. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 139 | getSection("", ELFSection::SHT_NULL, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 140 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 141 | // Start up the symbol table. The first entry in the symtab is the null |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 142 | // entry. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 143 | SymbolList.push_back(ELFSym(0)); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 148 | void ELFWriter::EmitGlobal(GlobalVariable *GV) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 149 | |
| 150 | // XXX: put local symbols *before* global ones! |
| 151 | const Section *S = TAI->SectionForGlobal(GV); |
| 152 | DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n"; |
| 153 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 154 | // If this is an external global, emit it now. TODO: Note that it would be |
| 155 | // better to ignore the symbol here and only add it to the symbol table if |
| 156 | // referenced. |
| 157 | if (!GV->hasInitializer()) { |
| 158 | ELFSym ExternalSym(GV); |
| 159 | ExternalSym.SetBind(ELFSym::STB_GLOBAL); |
| 160 | ExternalSym.SetType(ELFSym::STT_NOTYPE); |
| 161 | ExternalSym.SectionIdx = ELFSection::SHN_UNDEF; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 162 | SymbolList.push_back(ExternalSym); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 165 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 166 | const TargetData *TD = TM.getTargetData(); |
| 167 | unsigned Align = TD->getPreferredAlignment(GV); |
| 168 | Constant *CV = GV->getInitializer(); |
| 169 | unsigned Size = TD->getTypeAllocSize(CV->getType()); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 170 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 171 | // If this global has a zero initializer, go to .bss or common section. |
| 172 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 173 | // If this global is part of the common block, add it now. Variables are |
| 174 | // part of the common block if they are zero initialized and allowed to be |
| 175 | // merged with other symbols. |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 176 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
| 177 | GV->hasCommonLinkage()) { |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 178 | ELFSym CommonSym(GV); |
| 179 | // Value for common symbols is the alignment required. |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 180 | CommonSym.Value = Align; |
| 181 | CommonSym.Size = Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 182 | CommonSym.SetBind(ELFSym::STB_GLOBAL); |
| 183 | CommonSym.SetType(ELFSym::STT_OBJECT); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 184 | CommonSym.SectionIdx = ELFSection::SHN_COMMON; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 185 | SymbolList.push_back(CommonSym); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 186 | getSection(S->getName(), ELFSection::SHT_NOBITS, |
| 187 | ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 1); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 188 | return; |
| 189 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 191 | // Otherwise, this symbol is part of the .bss section. Emit it now. |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 192 | // Handle alignment. Ensure section is aligned at least as much as required |
| 193 | // by this symbol. |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 194 | ELFSection &BSSSection = getBSSSection(); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 195 | BSSSection.Align = std::max(BSSSection.Align, Align); |
| 196 | |
| 197 | // Within the section, emit enough virtual padding to get us to an alignment |
| 198 | // boundary. |
| 199 | if (Align) |
| 200 | BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1); |
| 201 | |
| 202 | ELFSym BSSSym(GV); |
| 203 | BSSSym.Value = BSSSection.Size; |
| 204 | BSSSym.Size = Size; |
| 205 | BSSSym.SetType(ELFSym::STT_OBJECT); |
| 206 | |
| 207 | switch (GV->getLinkage()) { |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 208 | default: // weak/linkonce/common handled above |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 209 | assert(0 && "Unexpected linkage type!"); |
| 210 | case GlobalValue::AppendingLinkage: // FIXME: This should be improved! |
| 211 | case GlobalValue::ExternalLinkage: |
| 212 | BSSSym.SetBind(ELFSym::STB_GLOBAL); |
| 213 | break; |
| 214 | case GlobalValue::InternalLinkage: |
| 215 | BSSSym.SetBind(ELFSym::STB_LOCAL); |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | // Set the idx of the .bss section |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 220 | BSSSym.SectionIdx = BSSSection.SectionIdx; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 221 | if (!GV->hasPrivateLinkage()) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 222 | SymbolList.push_back(BSSSym); |
Chris Lattner | 4c47e3a | 2005-07-08 05:47:00 +0000 | [diff] [blame] | 223 | |
| 224 | // Reserve space in the .bss section for this symbol. |
| 225 | BSSSection.Size += Size; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 228 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 229 | /// Emit the Global symbol to the right ELF section |
| 230 | ELFSym GblSym(GV); |
| 231 | GblSym.Size = Size; |
| 232 | GblSym.SetType(ELFSym::STT_OBJECT); |
| 233 | GblSym.SetBind(ELFSym::STB_GLOBAL); |
| 234 | unsigned Flags = S->getFlags(); |
| 235 | unsigned SectType = ELFSection::SHT_PROGBITS; |
| 236 | unsigned SHdrFlags = ELFSection::SHF_ALLOC; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 237 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 238 | if (Flags & SectionFlags::Code) |
| 239 | SHdrFlags |= ELFSection::SHF_EXECINSTR; |
| 240 | if (Flags & SectionFlags::Writeable) |
| 241 | SHdrFlags |= ELFSection::SHF_WRITE; |
| 242 | if (Flags & SectionFlags::Mergeable) |
| 243 | SHdrFlags |= ELFSection::SHF_MERGE; |
| 244 | if (Flags & SectionFlags::TLS) |
| 245 | SHdrFlags |= ELFSection::SHF_TLS; |
| 246 | if (Flags & SectionFlags::Strings) |
| 247 | SHdrFlags |= ELFSection::SHF_STRINGS; |
| 248 | |
| 249 | // Remove tab from section name prefix |
| 250 | std::string SectionName(S->getName()); |
| 251 | size_t Pos = SectionName.find("\t"); |
| 252 | if (Pos != std::string::npos) |
| 253 | SectionName.erase(Pos, 1); |
| 254 | |
| 255 | // The section alignment should be bound to the element with |
| 256 | // the largest alignment |
| 257 | ELFSection &ElfS = getSection(SectionName, SectType, SHdrFlags); |
| 258 | GblSym.SectionIdx = ElfS.SectionIdx; |
| 259 | if (Align > ElfS.Align) |
| 260 | ElfS.Align = Align; |
| 261 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 262 | // S.Value should contain the symbol index inside the section, |
| 263 | // and all symbols should start on their required alignment boundary |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 264 | GblSym.Value = (ElfS.size() + (Align-1)) & (-Align); |
| 265 | ElfS.emitAlignment(Align); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 266 | |
| 267 | // Emit the constant symbol to its section |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 268 | EmitGlobalConstant(CV, ElfS); |
| 269 | SymbolList.push_back(GblSym); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS, |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 273 | ELFSection &GblS) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 274 | |
| 275 | // Print the fields in successive locations. Pad to align if needed! |
| 276 | const TargetData *TD = TM.getTargetData(); |
| 277 | unsigned Size = TD->getTypeAllocSize(CVS->getType()); |
| 278 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
| 279 | uint64_t sizeSoFar = 0; |
| 280 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 281 | const Constant* field = CVS->getOperand(i); |
| 282 | |
| 283 | // Check if padding is needed and insert one or more 0s. |
| 284 | uint64_t fieldSize = TD->getTypeAllocSize(field->getType()); |
| 285 | uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) |
| 286 | - cvsLayout->getElementOffset(i)) - fieldSize; |
| 287 | sizeSoFar += fieldSize + padSize; |
| 288 | |
| 289 | // Now print the actual field value. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 290 | EmitGlobalConstant(field, GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 291 | |
| 292 | // Insert padding - this may include padding to increase the size of the |
| 293 | // current field up to the ABI size (if the struct is not packed) as well |
| 294 | // as padding to ensure that the next field starts at the right offset. |
| 295 | for (unsigned p=0; p < padSize; p++) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 296 | GblS.emitByte(0); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 297 | } |
| 298 | assert(sizeSoFar == cvsLayout->getSizeInBytes() && |
| 299 | "Layout of constant struct may be incorrect!"); |
| 300 | } |
| 301 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 302 | void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 303 | const TargetData *TD = TM.getTargetData(); |
| 304 | unsigned Size = TD->getTypeAllocSize(CV->getType()); |
| 305 | |
| 306 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 307 | if (CVA->isString()) { |
| 308 | std::string GblStr = CVA->getAsString(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 309 | GblS.emitString(GblStr); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 310 | } else { // Not a string. Print the values in successive locations |
| 311 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 312 | EmitGlobalConstant(CVA->getOperand(i), GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 313 | } |
| 314 | return; |
| 315 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 316 | EmitGlobalConstantStruct(CVS, GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 317 | return; |
| 318 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 319 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 320 | if (CFP->getType() == Type::DoubleTy) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 321 | GblS.emitWord64(Val); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 322 | else if (CFP->getType() == Type::FloatTy) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 323 | GblS.emitWord32(Val); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 324 | else if (CFP->getType() == Type::X86_FP80Ty) { |
| 325 | assert(0 && "X86_FP80Ty global emission not implemented"); |
| 326 | } else if (CFP->getType() == Type::PPC_FP128Ty) |
| 327 | assert(0 && "PPC_FP128Ty global emission not implemented"); |
| 328 | return; |
| 329 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 330 | if (Size == 4) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 331 | GblS.emitWord32(CI->getZExtValue()); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 332 | else if (Size == 8) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 333 | GblS.emitWord64(CI->getZExtValue()); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 334 | else |
| 335 | assert(0 && "LargeInt global emission not implemented"); |
| 336 | return; |
| 337 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
| 338 | const VectorType *PTy = CP->getType(); |
| 339 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 340 | EmitGlobalConstant(CP->getOperand(I), GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 341 | return; |
| 342 | } |
| 343 | assert(0 && "unknown global constant"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
| 347 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 348 | // 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] | 349 | return false; |
| 350 | } |
| 351 | |
| 352 | /// doFinalization - Now that the module has been completely processed, emit |
| 353 | /// the ELF file to 'O'. |
| 354 | bool ELFWriter::doFinalization(Module &M) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 355 | /// FIXME: This should be removed when moving to ObjectCodeEmiter. Since the |
| 356 | /// current ELFCodeEmiter uses CurrBuff, ... it doesn't update S.Data |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 357 | /// vector size for .text sections, so this is a quick dirty fix |
| 358 | ELFSection &TS = getTextSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 359 | if (TS.Size) { |
| 360 | BinaryData &BD = TS.getData(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 361 | for (unsigned e=0; e<TS.Size; ++e) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 362 | BD.push_back(BD[e]); |
| 363 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 364 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 365 | // Emit .data section placeholder |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 366 | getDataSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 367 | |
| 368 | // Emit .bss section placeholder |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 369 | getBSSSection(); |
| 370 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 371 | // Build and emit data, bss and "common" sections. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 372 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 373 | I != E; ++I) |
Chris Lattner | 0589523 | 2005-07-16 17:41:06 +0000 | [diff] [blame] | 374 | EmitGlobal(I); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 375 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 376 | // Emit non-executable stack note |
| 377 | if (TAI->getNonexecutableStackDirective()) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 378 | getNonExecStackSection(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 380 | // Emit the symbol table now, if non-empty. |
| 381 | EmitSymbolTable(); |
| 382 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 383 | // Emit the relocation sections. |
| 384 | EmitRelocations(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 385 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 386 | // Emit the sections string table. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 387 | EmitSectionTableStringTable(); |
| 388 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 389 | // Dump the sections and section table to the .o file. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 390 | OutputSectionsAndSectionTable(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 392 | // We are done with the abstract symbols. |
| 393 | SectionList.clear(); |
| 394 | NumSections = 0; |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 395 | |
| 396 | // Release the name mangler object. |
| 397 | delete Mang; Mang = 0; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 401 | /// EmitRelocations - Emit relocations |
| 402 | void ELFWriter::EmitRelocations() { |
| 403 | } |
| 404 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 405 | /// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable' |
| 406 | void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 407 | if (is64Bit) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 408 | SymbolTable.emitWord32(Sym.NameIdx); |
| 409 | SymbolTable.emitByte(Sym.Info); |
| 410 | SymbolTable.emitByte(Sym.Other); |
| 411 | SymbolTable.emitWord16(Sym.SectionIdx); |
| 412 | SymbolTable.emitWord64(Sym.Value); |
| 413 | SymbolTable.emitWord64(Sym.Size); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 414 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 415 | SymbolTable.emitWord32(Sym.NameIdx); |
| 416 | SymbolTable.emitWord32(Sym.Value); |
| 417 | SymbolTable.emitWord32(Sym.Size); |
| 418 | SymbolTable.emitByte(Sym.Info); |
| 419 | SymbolTable.emitByte(Sym.Other); |
| 420 | SymbolTable.emitWord16(Sym.SectionIdx); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 424 | /// EmitSectionHeader - Write section 'Section' header in 'SHdrTab' |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 425 | /// Section Header Table |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 426 | void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab, |
| 427 | const ELFSection &SHdr) { |
| 428 | SHdrTab.emitWord32(SHdr.NameIdx); |
| 429 | SHdrTab.emitWord32(SHdr.Type); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 430 | if (is64Bit) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 431 | SHdrTab.emitWord64(SHdr.Flags); |
| 432 | SHdrTab.emitWord(SHdr.Addr); |
| 433 | SHdrTab.emitWord(SHdr.Offset); |
| 434 | SHdrTab.emitWord64(SHdr.Size); |
| 435 | SHdrTab.emitWord32(SHdr.Link); |
| 436 | SHdrTab.emitWord32(SHdr.Info); |
| 437 | SHdrTab.emitWord64(SHdr.Align); |
| 438 | SHdrTab.emitWord64(SHdr.EntSize); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 439 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 440 | SHdrTab.emitWord32(SHdr.Flags); |
| 441 | SHdrTab.emitWord(SHdr.Addr); |
| 442 | SHdrTab.emitWord(SHdr.Offset); |
| 443 | SHdrTab.emitWord32(SHdr.Size); |
| 444 | SHdrTab.emitWord32(SHdr.Link); |
| 445 | SHdrTab.emitWord32(SHdr.Info); |
| 446 | SHdrTab.emitWord32(SHdr.Align); |
| 447 | SHdrTab.emitWord32(SHdr.EntSize); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 451 | /// EmitSymbolTable - If the current symbol table is non-empty, emit the string |
| 452 | /// table for it and then the symbol table itself. |
| 453 | void ELFWriter::EmitSymbolTable() { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 454 | if (SymbolList.size() == 1) return; // Only the null entry. |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 455 | |
| 456 | // FIXME: compact all local symbols to the start of the symtab. |
| 457 | unsigned FirstNonLocalSymbol = 1; |
| 458 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 459 | ELFSection &StrTab = getStringTableSection(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 461 | // Set the zero'th symbol to a null byte, as required. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 462 | StrTab.emitByte(0); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 464 | unsigned Index = 1; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 465 | for (unsigned i = 1, e = SymbolList.size(); i != e; ++i) { |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 466 | // Use the name mangler to uniquify the LLVM symbol. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 467 | std::string Name = Mang->getValueName(SymbolList[i].GV); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 468 | |
| 469 | if (Name.empty()) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 470 | SymbolList[i].NameIdx = 0; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 471 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 472 | SymbolList[i].NameIdx = Index; |
| 473 | StrTab.emitString(Name); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 474 | |
| 475 | // Keep track of the number of bytes emitted to this section. |
| 476 | Index += Name.size()+1; |
| 477 | } |
| 478 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 479 | assert(Index == StrTab.size()); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 480 | StrTab.Size = Index; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 481 | |
| 482 | // Now that we have emitted the string table and know the offset into the |
| 483 | // string table of each symbol, emit the symbol table itself. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 484 | ELFSection &SymTab = getSymbolTableSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 485 | SymTab.Align = TEW->getSymTabAlignment(); |
| 486 | SymTab.Link = StrTab.SectionIdx; // Section Index of .strtab. |
| 487 | SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 488 | |
| 489 | // Size of each symtab entry. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 490 | SymTab.EntSize = TEW->getSymTabEntrySize(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 491 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 492 | for (unsigned i = 0, e = SymbolList.size(); i != e; ++i) |
| 493 | EmitSymbol(SymTab, SymbolList[i]); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 494 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 495 | SymTab.Size = SymTab.size(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 498 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 499 | /// ELF Section Table string table: the string table that holds all of the |
| 500 | /// section names. |
| 501 | void ELFWriter::EmitSectionTableStringTable() { |
| 502 | // 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] | 503 | ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 504 | |
| 505 | // Now that we know which section number is the .shstrtab section, update the |
| 506 | // e_shstrndx entry in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 507 | ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 508 | |
| 509 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 510 | // the string table. |
| 511 | unsigned Index = 0; |
| 512 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 513 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 514 | E = SectionList.end(); I != E; ++I) { |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 515 | // Set the index into the table. Note if we have lots of entries with |
| 516 | // common suffixes, we could memoize them here if we cared. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 517 | I->NameIdx = Index; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 518 | SHStrTab.emitString(I->getName()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 519 | |
| 520 | // Keep track of the number of bytes emitted to this section. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 521 | Index += I->getName().size()+1; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | // Set the size of .shstrtab now that we know what it is. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 525 | assert(Index == SHStrTab.size()); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 526 | SHStrTab.Size = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 529 | /// OutputSectionsAndSectionTable - Now that we have constructed the file header |
| 530 | /// and all of the sections, emit these to the ostream destination and emit the |
| 531 | /// SectionTable. |
| 532 | void ELFWriter::OutputSectionsAndSectionTable() { |
| 533 | // Pass #1: Compute the file offset for each section. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 534 | size_t FileOff = ElfHdr.size(); // File header first. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 535 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 536 | // Adjust alignment of all section if needed. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 537 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 538 | E = SectionList.end(); I != E; ++I) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 539 | |
| 540 | // Section idx 0 has 0 offset |
| 541 | if (!I->SectionIdx) |
| 542 | continue; |
| 543 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 544 | if (!I->size()) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 545 | I->Offset = FileOff; |
| 546 | continue; |
| 547 | } |
| 548 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 549 | // Update Section size |
| 550 | if (!I->Size) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 551 | I->Size = I->size(); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 553 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 554 | if (I->Align) |
| 555 | FileOff = (FileOff+I->Align-1) & ~(I->Align-1); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 556 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 557 | I->Offset = FileOff; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 558 | FileOff += I->Size; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 561 | // Align Section Header. |
| 562 | unsigned TableAlign = is64Bit ? 8 : 4; |
| 563 | FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 564 | |
| 565 | // Now that we know where all of the sections will be emitted, set the e_shnum |
| 566 | // entry in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 567 | ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 569 | // Now that we know the offset in the file of the section table, update the |
| 570 | // e_shoff address in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 571 | ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset); |
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 | // Now that we know all of the data in the file header, emit it and all of the |
| 574 | // sections! |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 575 | O.write((char *)&ElfHdr.getData()[0], ElfHdr.size()); |
| 576 | FileOff = ElfHdr.size(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 577 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 578 | // Section Header Table blob |
| 579 | BinaryObject SHdrTable(isLittleEndian, is64Bit); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 580 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 581 | // Emit all of sections to the file and build the section header table. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 582 | while (!SectionList.empty()) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 583 | ELFSection &S = *SectionList.begin(); |
| 584 | DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName() |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 585 | << ", Size: " << S.Size << ", Offset: " << S.Offset |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 586 | << ", SectionData Size: " << S.size() << "\n"; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 587 | |
| 588 | // Align FileOff to whatever the alignment restrictions of the section are. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 589 | if (S.Align) { |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 590 | for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 591 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 592 | O << (char)0xAB; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 593 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 594 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 595 | if (S.size()) { |
| 596 | O.write((char *)&S.getData()[0], S.Size); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 597 | FileOff += S.Size; |
| 598 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 599 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 600 | EmitSectionHeader(SHdrTable, S); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 601 | SectionList.pop_front(); |
| 602 | } |
| 603 | |
| 604 | // Align output for the section table. |
| 605 | for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 606 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 607 | O << (char)0xAB; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 609 | // Emit the section table itself. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 610 | O.write((char *)&SHdrTable.getData()[0], SHdrTable.size()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 611 | } |