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