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 | |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 33 | #include "ELF.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 34 | #include "ELFWriter.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 35 | #include "ELFCodeEmitter.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" |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 44 | #include "llvm/Target/TargetAsmInfo.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetData.h" |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 46 | #include "llvm/Target/TargetELFWriterInfo.h" |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Mangler.h" |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Streams.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 50 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
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 | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 139 | getNullSection(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 144 | unsigned ELFWriter::getGlobalELFVisibility(const GlobalValue *GV) { |
| 145 | switch (GV->getVisibility()) { |
| 146 | default: |
| 147 | assert(0 && "unknown visibility type"); |
| 148 | case GlobalValue::DefaultVisibility: |
| 149 | return ELFSym::STV_DEFAULT; |
| 150 | case GlobalValue::HiddenVisibility: |
| 151 | return ELFSym::STV_HIDDEN; |
| 152 | case GlobalValue::ProtectedVisibility: |
| 153 | return ELFSym::STV_PROTECTED; |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | unsigned ELFWriter::getGlobalELFLinkage(const GlobalValue *GV) { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 160 | if (GV->hasInternalLinkage()) |
| 161 | return ELFSym::STB_LOCAL; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 162 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 163 | if (GV->hasWeakLinkage()) |
| 164 | return ELFSym::STB_WEAK; |
| 165 | |
| 166 | return ELFSym::STB_GLOBAL; |
| 167 | } |
| 168 | |
| 169 | // For global symbols without a section, return the Null section as a |
| 170 | // placeholder |
| 171 | ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV, |
| 172 | ELFSym &Sym) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 173 | const Section *S = TAI->SectionForGlobal(GV); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 174 | unsigned Flags = S->getFlags(); |
| 175 | unsigned SectionType = ELFSection::SHT_PROGBITS; |
| 176 | unsigned SHdrFlags = ELFSection::SHF_ALLOC; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 177 | DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n"; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 178 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 179 | // If this is an external global, the symbol does not have a section. |
| 180 | if (!GV->hasInitializer()) { |
| 181 | Sym.SectionIdx = ELFSection::SHN_UNDEF; |
| 182 | return getNullSection(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 184 | |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 185 | const TargetData *TD = TM.getTargetData(); |
| 186 | unsigned Align = TD->getPreferredAlignment(GV); |
| 187 | Constant *CV = GV->getInitializer(); |
| 188 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 189 | if (Flags & SectionFlags::Code) |
| 190 | SHdrFlags |= ELFSection::SHF_EXECINSTR; |
| 191 | if (Flags & SectionFlags::Writeable) |
| 192 | SHdrFlags |= ELFSection::SHF_WRITE; |
| 193 | if (Flags & SectionFlags::Mergeable) |
| 194 | SHdrFlags |= ELFSection::SHF_MERGE; |
| 195 | if (Flags & SectionFlags::TLS) |
| 196 | SHdrFlags |= ELFSection::SHF_TLS; |
| 197 | if (Flags & SectionFlags::Strings) |
| 198 | SHdrFlags |= ELFSection::SHF_STRINGS; |
| 199 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 200 | // If this global has a zero initializer, go to .bss or common section. |
| 201 | // Variables are part of the common block if they are zero initialized |
| 202 | // and allowed to be merged with other symbols. |
| 203 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
| 204 | SectionType = ELFSection::SHT_NOBITS; |
| 205 | ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags); |
| 206 | if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
| 207 | GV->hasCommonLinkage()) { |
| 208 | Sym.SectionIdx = ELFSection::SHN_COMMON; |
| 209 | Sym.IsCommon = true; |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 210 | ElfS.Align = 1; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 211 | return ElfS; |
| 212 | } |
| 213 | Sym.IsBss = true; |
| 214 | Sym.SectionIdx = ElfS.SectionIdx; |
| 215 | if (Align) ElfS.Size = (ElfS.Size + Align-1) & ~(Align-1); |
| 216 | ElfS.Align = std::max(ElfS.Align, Align); |
| 217 | return ElfS; |
| 218 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 219 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 220 | Sym.IsConstant = true; |
| 221 | ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags); |
| 222 | Sym.SectionIdx = ElfS.SectionIdx; |
| 223 | ElfS.Align = std::max(ElfS.Align, Align); |
| 224 | return ElfS; |
| 225 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 226 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 227 | void ELFWriter::EmitFunctionDeclaration(const Function *F) { |
| 228 | ELFSym GblSym(F); |
| 229 | GblSym.setBind(ELFSym::STB_GLOBAL); |
| 230 | GblSym.setType(ELFSym::STT_NOTYPE); |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 231 | GblSym.setVisibility(ELFSym::STV_DEFAULT); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 232 | GblSym.SectionIdx = ELFSection::SHN_UNDEF; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 233 | SymbolList.push_back(GblSym); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 236 | void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) { |
| 237 | unsigned SymBind = getGlobalELFLinkage(GV); |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 238 | unsigned Align=0, Size=0; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 239 | ELFSym GblSym(GV); |
| 240 | GblSym.setBind(SymBind); |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 241 | GblSym.setVisibility(getGlobalELFVisibility(GV)); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 242 | |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 243 | if (GV->hasInitializer()) { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 244 | GblSym.setType(ELFSym::STT_OBJECT); |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 245 | const TargetData *TD = TM.getTargetData(); |
| 246 | Align = TD->getPreferredAlignment(GV); |
| 247 | Size = TD->getTypeAllocSize(GV->getInitializer()->getType()); |
| 248 | GblSym.Size = Size; |
| 249 | } else { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 250 | GblSym.setType(ELFSym::STT_NOTYPE); |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 251 | } |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 252 | |
| 253 | ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 254 | |
| 255 | if (GblSym.IsCommon) { |
| 256 | GblSym.Value = Align; |
| 257 | } else if (GblSym.IsBss) { |
| 258 | GblSym.Value = GblSection.Size; |
| 259 | GblSection.Size += Size; |
| 260 | } else if (GblSym.IsConstant){ |
| 261 | // GblSym.Value should contain the symbol index inside the section, |
| 262 | // and all symbols should start on their required alignment boundary |
| 263 | GblSym.Value = (GblSection.size() + (Align-1)) & (-Align); |
| 264 | GblSection.emitAlignment(Align); |
| 265 | EmitGlobalConstant(GV->getInitializer(), GblSection); |
| 266 | } |
| 267 | |
| 268 | // Local symbols should come first on the symbol table. |
| 269 | if (!GV->hasPrivateLinkage()) { |
| 270 | if (SymBind == ELFSym::STB_LOCAL) |
| 271 | SymbolList.push_front(GblSym); |
| 272 | else |
| 273 | SymbolList.push_back(GblSym); |
| 274 | } |
| 275 | } |
| 276 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 277 | void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS, |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 278 | ELFSection &GblS) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 279 | |
| 280 | // Print the fields in successive locations. Pad to align if needed! |
| 281 | const TargetData *TD = TM.getTargetData(); |
| 282 | unsigned Size = TD->getTypeAllocSize(CVS->getType()); |
| 283 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
| 284 | uint64_t sizeSoFar = 0; |
| 285 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 286 | const Constant* field = CVS->getOperand(i); |
| 287 | |
| 288 | // Check if padding is needed and insert one or more 0s. |
| 289 | uint64_t fieldSize = TD->getTypeAllocSize(field->getType()); |
| 290 | uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) |
| 291 | - cvsLayout->getElementOffset(i)) - fieldSize; |
| 292 | sizeSoFar += fieldSize + padSize; |
| 293 | |
| 294 | // Now print the actual field value. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 295 | EmitGlobalConstant(field, GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 296 | |
| 297 | // Insert padding - this may include padding to increase the size of the |
| 298 | // current field up to the ABI size (if the struct is not packed) as well |
| 299 | // as padding to ensure that the next field starts at the right offset. |
| 300 | for (unsigned p=0; p < padSize; p++) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 301 | GblS.emitByte(0); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 302 | } |
| 303 | assert(sizeSoFar == cvsLayout->getSizeInBytes() && |
| 304 | "Layout of constant struct may be incorrect!"); |
| 305 | } |
| 306 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 307 | void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 308 | const TargetData *TD = TM.getTargetData(); |
| 309 | unsigned Size = TD->getTypeAllocSize(CV->getType()); |
| 310 | |
| 311 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 312 | if (CVA->isString()) { |
| 313 | std::string GblStr = CVA->getAsString(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 314 | GblStr.resize(GblStr.size()-1); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 315 | GblS.emitString(GblStr); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 316 | } else { // Not a string. Print the values in successive locations |
| 317 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 318 | EmitGlobalConstant(CVA->getOperand(i), GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 319 | } |
| 320 | return; |
| 321 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 322 | EmitGlobalConstantStruct(CVS, GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 323 | return; |
| 324 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 325 | uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); |
| 326 | if (CFP->getType() == Type::DoubleTy) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 327 | GblS.emitWord64(Val); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 328 | else if (CFP->getType() == Type::FloatTy) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 329 | GblS.emitWord32(Val); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 330 | else if (CFP->getType() == Type::X86_FP80Ty) { |
| 331 | assert(0 && "X86_FP80Ty global emission not implemented"); |
| 332 | } else if (CFP->getType() == Type::PPC_FP128Ty) |
| 333 | assert(0 && "PPC_FP128Ty global emission not implemented"); |
| 334 | return; |
| 335 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 336 | if (Size == 4) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 337 | GblS.emitWord32(CI->getZExtValue()); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 338 | else if (Size == 8) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 339 | GblS.emitWord64(CI->getZExtValue()); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 340 | else |
| 341 | assert(0 && "LargeInt global emission not implemented"); |
| 342 | return; |
| 343 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
| 344 | const VectorType *PTy = CP->getType(); |
| 345 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 346 | EmitGlobalConstant(CP->getOperand(I), GblS); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | assert(0 && "unknown global constant"); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | bool ELFWriter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | aa507db | 2005-07-11 05:17:18 +0000 | [diff] [blame] | 354 | // 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] | 355 | return false; |
| 356 | } |
| 357 | |
| 358 | /// doFinalization - Now that the module has been completely processed, emit |
| 359 | /// the ELF file to 'O'. |
| 360 | bool ELFWriter::doFinalization(Module &M) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 361 | /// FIXME: This should be removed when moving to ObjectCodeEmiter. Since the |
| 362 | /// current ELFCodeEmiter uses CurrBuff, ... it doesn't update S.Data |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 363 | /// vector size for .text sections, so this is a quick dirty fix |
| 364 | ELFSection &TS = getTextSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 365 | if (TS.Size) { |
| 366 | BinaryData &BD = TS.getData(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 367 | for (unsigned e=0; e<TS.Size; ++e) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 368 | BD.push_back(BD[e]); |
| 369 | } |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 370 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 371 | // Emit .data section placeholder |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 372 | getDataSection(); |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 373 | |
| 374 | // Emit .bss section placeholder |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 375 | getBSSSection(); |
| 376 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 377 | // Build and emit data, bss and "common" sections. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 378 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 379 | I != E; ++I) { |
| 380 | EmitGlobalVar(I); |
| 381 | GblSymLookup[I] = 0; |
| 382 | } |
| 383 | |
| 384 | // Emit all pending globals |
| 385 | // TODO: this should be done only for referenced symbols |
| 386 | for (SetVector<GlobalValue*>::const_iterator I = PendingGlobals.begin(), |
| 387 | E = PendingGlobals.end(); I != E; ++I) { |
| 388 | |
| 389 | // No need to emit the symbol again |
| 390 | if (GblSymLookup.find(*I) != GblSymLookup.end()) |
| 391 | continue; |
| 392 | |
| 393 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) { |
| 394 | EmitGlobalVar(GV); |
| 395 | } else if (Function *F = dyn_cast<Function>(*I)) { |
| 396 | // If function is not in GblSymLookup, it doesn't have a body, |
| 397 | // so emit the symbol as a function declaration (no section associated) |
| 398 | EmitFunctionDeclaration(F); |
| 399 | } else { |
| 400 | assert("unknown howto handle pending global"); |
| 401 | } |
| 402 | GblSymLookup[*I] = 0; |
| 403 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 404 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 405 | // Emit non-executable stack note |
| 406 | if (TAI->getNonexecutableStackDirective()) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 407 | getNonExecStackSection(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 408 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 409 | // Emit a symbol for each section created until now |
| 410 | for (std::map<std::string, ELFSection*>::iterator I = SectionLookup.begin(), |
| 411 | E = SectionLookup.end(); I != E; ++I) { |
| 412 | ELFSection *ES = I->second; |
| 413 | |
| 414 | // Skip null section |
| 415 | if (ES->SectionIdx == 0) continue; |
| 416 | |
| 417 | ELFSym SectionSym(0); |
| 418 | SectionSym.SectionIdx = ES->SectionIdx; |
| 419 | SectionSym.Size = 0; |
| 420 | SectionSym.setBind(ELFSym::STB_LOCAL); |
| 421 | SectionSym.setType(ELFSym::STT_SECTION); |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 422 | SectionSym.setVisibility(ELFSym::STV_DEFAULT); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 423 | |
| 424 | // Local symbols go in the list front |
| 425 | SymbolList.push_front(SectionSym); |
| 426 | } |
| 427 | |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame] | 428 | // Emit string table |
| 429 | EmitStringTable(); |
| 430 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 431 | // Emit the symbol table now, if non-empty. |
| 432 | EmitSymbolTable(); |
| 433 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 434 | // Emit the relocation sections. |
| 435 | EmitRelocations(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 436 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 437 | // Emit the sections string table. |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 438 | EmitSectionTableStringTable(); |
| 439 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 440 | // Dump the sections and section table to the .o file. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 441 | OutputSectionsAndSectionTable(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 443 | // We are done with the abstract symbols. |
| 444 | SectionList.clear(); |
| 445 | NumSections = 0; |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 446 | |
| 447 | // Release the name mangler object. |
| 448 | delete Mang; Mang = 0; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 452 | /// EmitRelocations - Emit relocations |
| 453 | void ELFWriter::EmitRelocations() { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 454 | |
| 455 | // Create Relocation sections for each section which needs it. |
| 456 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 457 | E = SectionList.end(); I != E; ++I) { |
| 458 | |
| 459 | // This section does not have relocations |
| 460 | if (!I->hasRelocations()) continue; |
| 461 | |
| 462 | // Get the relocation section for section 'I' |
| 463 | bool HasRelA = TEW->hasRelocationAddend(); |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame^] | 464 | ELFSection &RelSec = getRelocSection(I->getName(), HasRelA, |
| 465 | TEW->getPrefELFAlignment()); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 466 | |
| 467 | // 'Link' - Section hdr idx of the associated symbol table |
| 468 | // 'Info' - Section hdr idx of the section to which the relocation applies |
| 469 | ELFSection &SymTab = getSymbolTableSection(); |
| 470 | RelSec.Link = SymTab.SectionIdx; |
| 471 | RelSec.Info = I->SectionIdx; |
| 472 | RelSec.EntSize = TEW->getRelocationEntrySize(); |
| 473 | |
| 474 | // Get the relocations from Section |
| 475 | std::vector<MachineRelocation> Relos = I->getRelocations(); |
| 476 | for (std::vector<MachineRelocation>::iterator MRI = Relos.begin(), |
| 477 | MRE = Relos.end(); MRI != MRE; ++MRI) { |
| 478 | MachineRelocation &MR = *MRI; |
| 479 | |
| 480 | // Offset from the start of the section containing the symbol |
| 481 | unsigned Offset = MR.getMachineCodeOffset(); |
| 482 | |
| 483 | // Symbol index in the symbol table |
| 484 | unsigned SymIdx = 0; |
| 485 | |
| 486 | // Target specific ELF relocation type |
| 487 | unsigned RelType = TEW->getRelocationType(MR.getRelocationType()); |
| 488 | |
| 489 | // Constant addend used to compute the value to be stored |
| 490 | // into the relocatable field |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 491 | int64_t Addend = 0; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 492 | |
| 493 | // There are several machine relocations types, and each one of |
| 494 | // them needs a different approach to retrieve the symbol table index. |
| 495 | if (MR.isGlobalValue()) { |
| 496 | const GlobalValue *G = MR.getGlobalValue(); |
| 497 | SymIdx = GblSymLookup[G]; |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 498 | Addend = TEW->getAddendForRelTy(RelType); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 499 | } else { |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 500 | unsigned SectionIdx = MR.getConstantVal(); |
| 501 | // TODO: use a map for this. |
| 502 | for (std::list<ELFSym>::iterator I = SymbolList.begin(), |
| 503 | E = SymbolList.end(); I != E; ++I) |
| 504 | if ((SectionIdx == I->SectionIdx) && |
| 505 | (I->getType() == ELFSym::STT_SECTION)) { |
| 506 | SymIdx = I->SymTabIdx; |
| 507 | break; |
| 508 | } |
| 509 | Addend = (uint64_t)MR.getResultPointer(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Get the relocation entry and emit to the relocation section |
| 513 | ELFRelocation Rel(Offset, SymIdx, RelType, HasRelA, Addend); |
| 514 | EmitRelocation(RelSec, Rel, HasRelA); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel' |
| 520 | void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, |
| 521 | bool HasRelA) { |
| 522 | RelSec.emitWord(Rel.getOffset()); |
| 523 | RelSec.emitWord(Rel.getInfo(is64Bit)); |
| 524 | if (HasRelA) |
| 525 | RelSec.emitWord(Rel.getAddend()); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 528 | /// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable' |
| 529 | void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 530 | if (is64Bit) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 531 | SymbolTable.emitWord32(Sym.NameIdx); |
| 532 | SymbolTable.emitByte(Sym.Info); |
| 533 | SymbolTable.emitByte(Sym.Other); |
| 534 | SymbolTable.emitWord16(Sym.SectionIdx); |
| 535 | SymbolTable.emitWord64(Sym.Value); |
| 536 | SymbolTable.emitWord64(Sym.Size); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 537 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 538 | SymbolTable.emitWord32(Sym.NameIdx); |
| 539 | SymbolTable.emitWord32(Sym.Value); |
| 540 | SymbolTable.emitWord32(Sym.Size); |
| 541 | SymbolTable.emitByte(Sym.Info); |
| 542 | SymbolTable.emitByte(Sym.Other); |
| 543 | SymbolTable.emitWord16(Sym.SectionIdx); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 547 | /// EmitSectionHeader - Write section 'Section' header in 'SHdrTab' |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 548 | /// Section Header Table |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 549 | void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab, |
| 550 | const ELFSection &SHdr) { |
| 551 | SHdrTab.emitWord32(SHdr.NameIdx); |
| 552 | SHdrTab.emitWord32(SHdr.Type); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 553 | if (is64Bit) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 554 | SHdrTab.emitWord64(SHdr.Flags); |
| 555 | SHdrTab.emitWord(SHdr.Addr); |
| 556 | SHdrTab.emitWord(SHdr.Offset); |
| 557 | SHdrTab.emitWord64(SHdr.Size); |
| 558 | SHdrTab.emitWord32(SHdr.Link); |
| 559 | SHdrTab.emitWord32(SHdr.Info); |
| 560 | SHdrTab.emitWord64(SHdr.Align); |
| 561 | SHdrTab.emitWord64(SHdr.EntSize); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 562 | } else { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 563 | SHdrTab.emitWord32(SHdr.Flags); |
| 564 | SHdrTab.emitWord(SHdr.Addr); |
| 565 | SHdrTab.emitWord(SHdr.Offset); |
| 566 | SHdrTab.emitWord32(SHdr.Size); |
| 567 | SHdrTab.emitWord32(SHdr.Link); |
| 568 | SHdrTab.emitWord32(SHdr.Info); |
| 569 | SHdrTab.emitWord32(SHdr.Align); |
| 570 | SHdrTab.emitWord32(SHdr.EntSize); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame] | 574 | /// EmitStringTable - If the current symbol table is non-empty, emit the string |
| 575 | /// table for it |
| 576 | void ELFWriter::EmitStringTable() { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 577 | if (!SymbolList.size()) return; // Empty symbol table. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 578 | ELFSection &StrTab = getStringTableSection(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 580 | // Set the zero'th symbol to a null byte, as required. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 581 | StrTab.emitByte(0); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 582 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 583 | // Walk on the symbol list and write symbol names into the |
| 584 | // string table. |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 585 | unsigned Index = 1; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 586 | for (std::list<ELFSym>::iterator I = SymbolList.begin(), |
| 587 | E = SymbolList.end(); I != E; ++I) { |
| 588 | |
Chris Lattner | 5acd120 | 2005-07-11 03:11:47 +0000 | [diff] [blame] | 589 | // Use the name mangler to uniquify the LLVM symbol. |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 590 | std::string Name; |
| 591 | if (I->GV) Name.append(Mang->getValueName(I->GV)); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 592 | |
| 593 | if (Name.empty()) { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 594 | I->NameIdx = 0; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 595 | } else { |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 596 | I->NameIdx = Index; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 597 | StrTab.emitString(Name); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 598 | |
| 599 | // Keep track of the number of bytes emitted to this section. |
| 600 | Index += Name.size()+1; |
| 601 | } |
| 602 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 603 | assert(Index == StrTab.size()); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 604 | StrTab.Size = Index; |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 606 | |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame] | 607 | /// EmitSymbolTable - Emit the symbol table itself. |
| 608 | void ELFWriter::EmitSymbolTable() { |
| 609 | if (!SymbolList.size()) return; // Empty symbol table. |
| 610 | |
| 611 | unsigned FirstNonLocalSymbol = 1; |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 612 | // Now that we have emitted the string table and know the offset into the |
| 613 | // string table of each symbol, emit the symbol table itself. |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 614 | ELFSection &SymTab = getSymbolTableSection(); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 615 | SymTab.Align = TEW->getPrefELFAlignment(); |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame] | 616 | |
| 617 | // Section Index of .strtab. |
| 618 | SymTab.Link = getStringTableSection().SectionIdx; |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 619 | |
| 620 | // Size of each symtab entry. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 621 | SymTab.EntSize = TEW->getSymTabEntrySize(); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 622 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 623 | // The first entry in the symtab is the null symbol |
| 624 | ELFSym NullSym = ELFSym(0); |
| 625 | EmitSymbol(SymTab, NullSym); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 626 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 627 | // Emit all the symbols to the symbol table. Skip the null |
| 628 | // symbol, cause it's emitted already |
Bruno Cardoso Lopes | c236a34 | 2009-06-22 19:29:56 +0000 | [diff] [blame] | 629 | unsigned Index = 1; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 630 | for (std::list<ELFSym>::iterator I = SymbolList.begin(), |
| 631 | E = SymbolList.end(); I != E; ++I, ++Index) { |
| 632 | // Keep track of the first non-local symbol |
| 633 | if (I->getBind() == ELFSym::STB_LOCAL) |
| 634 | FirstNonLocalSymbol++; |
| 635 | |
| 636 | // Emit symbol to the symbol table |
| 637 | EmitSymbol(SymTab, *I); |
| 638 | |
| 639 | // Record the symbol table index for each global value |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 640 | if (I->GV) |
| 641 | GblSymLookup[I->GV] = Index; |
| 642 | |
| 643 | // Keep track on the symbol index into the symbol table |
| 644 | I->SymTabIdx = Index; |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | SymTab.Info = FirstNonLocalSymbol; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 648 | SymTab.Size = SymTab.size(); |
Chris Lattner | 80ed8fa | 2005-07-07 07:02:20 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 651 | /// EmitSectionTableStringTable - This method adds and emits a section for the |
| 652 | /// ELF Section Table string table: the string table that holds all of the |
| 653 | /// section names. |
| 654 | void ELFWriter::EmitSectionTableStringTable() { |
| 655 | // First step: add the section for the string table to the list of sections: |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 656 | ELFSection &SHStrTab = getSectionHeaderStringTableSection(); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 657 | |
| 658 | // Now that we know which section number is the .shstrtab section, update the |
| 659 | // e_shstrndx entry in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 660 | ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 661 | |
| 662 | // Set the NameIdx of each section in the string table and emit the bytes for |
| 663 | // the string table. |
| 664 | unsigned Index = 0; |
| 665 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 666 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 667 | E = SectionList.end(); I != E; ++I) { |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 668 | // Set the index into the table. Note if we have lots of entries with |
| 669 | // common suffixes, we could memoize them here if we cared. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 670 | I->NameIdx = Index; |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 671 | SHStrTab.emitString(I->getName()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 672 | |
| 673 | // Keep track of the number of bytes emitted to this section. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 674 | Index += I->getName().size()+1; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // 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] | 678 | assert(Index == SHStrTab.size()); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 679 | SHStrTab.Size = Index; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 682 | /// OutputSectionsAndSectionTable - Now that we have constructed the file header |
| 683 | /// and all of the sections, emit these to the ostream destination and emit the |
| 684 | /// SectionTable. |
| 685 | void ELFWriter::OutputSectionsAndSectionTable() { |
| 686 | // Pass #1: Compute the file offset for each section. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 687 | size_t FileOff = ElfHdr.size(); // File header first. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 688 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 689 | // Adjust alignment of all section if needed. |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 690 | for (std::list<ELFSection>::iterator I = SectionList.begin(), |
| 691 | E = SectionList.end(); I != E; ++I) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 692 | |
| 693 | // Section idx 0 has 0 offset |
| 694 | if (!I->SectionIdx) |
| 695 | continue; |
| 696 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 697 | if (!I->size()) { |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 698 | I->Offset = FileOff; |
| 699 | continue; |
| 700 | } |
| 701 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 702 | // Update Section size |
| 703 | if (!I->Size) |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 704 | I->Size = I->size(); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 706 | // Align FileOff to whatever the alignment restrictions of the section are. |
| 707 | if (I->Align) |
| 708 | FileOff = (FileOff+I->Align-1) & ~(I->Align-1); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 710 | I->Offset = FileOff; |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 711 | FileOff += I->Size; |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 714 | // Align Section Header. |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 715 | unsigned TableAlign = TEW->getPrefELFAlignment(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 716 | FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 717 | |
| 718 | // Now that we know where all of the sections will be emitted, set the e_shnum |
| 719 | // entry in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 720 | ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 722 | // Now that we know the offset in the file of the section table, update the |
| 723 | // e_shoff address in the ELF header. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 724 | ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 726 | // Now that we know all of the data in the file header, emit it and all of the |
| 727 | // sections! |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 728 | O.write((char *)&ElfHdr.getData()[0], ElfHdr.size()); |
| 729 | FileOff = ElfHdr.size(); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 730 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 731 | // Section Header Table blob |
| 732 | BinaryObject SHdrTable(isLittleEndian, is64Bit); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 733 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 734 | // 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] | 735 | while (!SectionList.empty()) { |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 736 | ELFSection &S = *SectionList.begin(); |
| 737 | DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName() |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 738 | << ", Size: " << S.Size << ", Offset: " << S.Offset |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 739 | << ", SectionData Size: " << S.size() << "\n"; |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 740 | |
| 741 | // Align FileOff to whatever the alignment restrictions of the section are. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 742 | if (S.size()) { |
Bruno Cardoso Lopes | e39493e | 2009-06-23 04:39:27 +0000 | [diff] [blame] | 743 | if (S.Align) { |
| 744 | for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1); |
| 745 | FileOff != NewFileOff; ++FileOff) |
| 746 | O << (char)0xAB; |
| 747 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 748 | O.write((char *)&S.getData()[0], S.Size); |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 749 | FileOff += S.Size; |
| 750 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 751 | |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 752 | EmitSectionHeader(SHdrTable, S); |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 753 | SectionList.pop_front(); |
| 754 | } |
| 755 | |
| 756 | // Align output for the section table. |
| 757 | for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1); |
| 758 | FileOff != NewFileOff; ++FileOff) |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 759 | O << (char)0xAB; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 5f48ff7 | 2005-07-16 08:01:13 +0000 | [diff] [blame] | 761 | // Emit the section table itself. |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 762 | O.write((char *)&SHdrTable.getData()[0], SHdrTable.size()); |
Chris Lattner | 35f0a4f | 2005-06-27 06:29:00 +0000 | [diff] [blame] | 763 | } |