Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "assembler" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAssembler.h" |
Chris Lattner | bea2c95 | 2009-08-22 19:19:12 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCSectionMachO.h" |
| 13 | #include "llvm/Target/TargetMachOWriterInfo.h" |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.h" |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 23132b1 | 2009-08-24 03:52:50 +0000 | [diff] [blame] | 21 | #include <vector> |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 24 | class MachObjectWriter; |
| 25 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 26 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments"); |
| 27 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 28 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 29 | MachObjectWriter &MOW); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 30 | |
| 31 | class MachObjectWriter { |
| 32 | // See <mach-o/loader.h>. |
| 33 | enum { |
| 34 | Header_Magic32 = 0xFEEDFACE, |
| 35 | Header_Magic64 = 0xFEEDFACF |
| 36 | }; |
| 37 | |
| 38 | static const unsigned Header32Size = 28; |
| 39 | static const unsigned Header64Size = 32; |
| 40 | static const unsigned SegmentLoadCommand32Size = 56; |
| 41 | static const unsigned Section32Size = 68; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 42 | static const unsigned SymtabLoadCommandSize = 24; |
| 43 | static const unsigned DysymtabLoadCommandSize = 80; |
| 44 | static const unsigned Nlist32Size = 12; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 45 | |
| 46 | enum HeaderFileType { |
| 47 | HFT_Object = 0x1 |
| 48 | }; |
| 49 | |
| 50 | enum LoadCommandType { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 51 | LCT_Segment = 0x1, |
| 52 | LCT_Symtab = 0x2, |
| 53 | LCT_Dysymtab = 0xb |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 56 | // See <mach-o/nlist.h>. |
| 57 | enum SymbolTypeType { |
| 58 | STT_Undefined = 0x00, |
| 59 | STT_Absolute = 0x02, |
| 60 | STT_Section = 0x0e |
| 61 | }; |
| 62 | |
| 63 | enum SymbolTypeFlags { |
| 64 | // If any of these bits are set, then the entry is a stab entry number (see |
| 65 | // <mach-o/stab.h>. Otherwise the other masks apply. |
| 66 | STF_StabsEntryMask = 0xe0, |
| 67 | |
| 68 | STF_TypeMask = 0x0e, |
| 69 | STF_External = 0x01, |
| 70 | STF_PrivateExtern = 0x10 |
| 71 | }; |
| 72 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame^] | 73 | /// IndirectSymbolFlags - Flags for encoding special values in the indirect |
| 74 | /// symbol entry. |
| 75 | enum IndirectSymbolFlags { |
| 76 | ISF_Local = 0x80000000, |
| 77 | ISF_Absolute = 0x40000000 |
| 78 | }; |
| 79 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 80 | /// MachSymbolData - Helper struct for containing some precomputed information |
| 81 | /// on symbols. |
| 82 | struct MachSymbolData { |
| 83 | MCSymbolData *SymbolData; |
| 84 | uint64_t StringIndex; |
| 85 | uint8_t SectionIndex; |
| 86 | |
| 87 | // Support lexicographic sorting. |
| 88 | bool operator<(const MachSymbolData &RHS) const { |
| 89 | const std::string &Name = SymbolData->getSymbol().getName(); |
| 90 | return Name < RHS.SymbolData->getSymbol().getName(); |
| 91 | } |
| 92 | }; |
| 93 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 94 | raw_ostream &OS; |
| 95 | bool IsLSB; |
| 96 | |
| 97 | public: |
| 98 | MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true) |
| 99 | : OS(_OS), IsLSB(_IsLSB) { |
| 100 | } |
| 101 | |
| 102 | /// @name Helper Methods |
| 103 | /// @{ |
| 104 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 105 | void Write8(uint8_t Value) { |
| 106 | OS << char(Value); |
| 107 | } |
| 108 | |
| 109 | void Write16(uint16_t Value) { |
| 110 | if (IsLSB) { |
| 111 | Write8(uint8_t(Value >> 0)); |
| 112 | Write8(uint8_t(Value >> 8)); |
| 113 | } else { |
| 114 | Write8(uint8_t(Value >> 8)); |
| 115 | Write8(uint8_t(Value >> 0)); |
| 116 | } |
| 117 | } |
| 118 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 119 | void Write32(uint32_t Value) { |
| 120 | if (IsLSB) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 121 | Write16(uint16_t(Value >> 0)); |
| 122 | Write16(uint16_t(Value >> 16)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 123 | } else { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 124 | Write16(uint16_t(Value >> 16)); |
| 125 | Write16(uint16_t(Value >> 0)); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void Write64(uint64_t Value) { |
| 130 | if (IsLSB) { |
| 131 | Write32(uint32_t(Value >> 0)); |
| 132 | Write32(uint32_t(Value >> 32)); |
| 133 | } else { |
| 134 | Write32(uint32_t(Value >> 32)); |
| 135 | Write32(uint32_t(Value >> 0)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | void WriteZeros(unsigned N) { |
| 140 | const char Zeros[16] = { 0 }; |
| 141 | |
| 142 | for (unsigned i = 0, e = N / 16; i != e; ++i) |
| 143 | OS << StringRef(Zeros, 16); |
| 144 | |
| 145 | OS << StringRef(Zeros, N % 16); |
| 146 | } |
| 147 | |
| 148 | void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) { |
| 149 | OS << Str; |
| 150 | if (ZeroFillSize) |
| 151 | WriteZeros(ZeroFillSize - Str.size()); |
| 152 | } |
| 153 | |
| 154 | /// @} |
| 155 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 156 | void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 157 | // struct mach_header (28 bytes) |
| 158 | |
| 159 | uint64_t Start = OS.tell(); |
| 160 | (void) Start; |
| 161 | |
| 162 | Write32(Header_Magic32); |
| 163 | |
| 164 | // FIXME: Support cputype. |
| 165 | Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386); |
| 166 | |
| 167 | // FIXME: Support cpusubtype. |
| 168 | Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL); |
| 169 | |
| 170 | Write32(HFT_Object); |
| 171 | |
| 172 | // Object files have a single load command, the segment. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 173 | Write32(NumLoadCommands); |
| 174 | Write32(LoadCommandsSize); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 175 | Write32(0); // Flags |
| 176 | |
| 177 | assert(OS.tell() - Start == Header32Size); |
| 178 | } |
| 179 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 180 | /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command. |
| 181 | /// |
| 182 | /// \arg NumSections - The number of sections in this segment. |
| 183 | /// \arg SectionDataSize - The total size of the sections. |
| 184 | void WriteSegmentLoadCommand32(unsigned NumSections, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 185 | uint64_t SectionDataStartOffset, |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 186 | uint64_t SectionDataSize) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 187 | // struct segment_command (56 bytes) |
| 188 | |
| 189 | uint64_t Start = OS.tell(); |
| 190 | (void) Start; |
| 191 | |
| 192 | Write32(LCT_Segment); |
| 193 | Write32(SegmentLoadCommand32Size + NumSections * Section32Size); |
| 194 | |
| 195 | WriteString("", 16); |
| 196 | Write32(0); // vmaddr |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 197 | Write32(SectionDataSize); // vmsize |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 198 | Write32(SectionDataStartOffset); // file offset |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 199 | Write32(SectionDataSize); // file size |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 200 | Write32(0x7); // maxprot |
| 201 | Write32(0x7); // initprot |
| 202 | Write32(NumSections); |
| 203 | Write32(0); // flags |
| 204 | |
| 205 | assert(OS.tell() - Start == SegmentLoadCommand32Size); |
| 206 | } |
| 207 | |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 208 | void WriteSection32(const MCSectionData &SD, uint64_t FileOffset) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 209 | // struct section (68 bytes) |
| 210 | |
| 211 | uint64_t Start = OS.tell(); |
| 212 | (void) Start; |
| 213 | |
| 214 | // FIXME: cast<> support! |
| 215 | const MCSectionMachO &Section = |
| 216 | static_cast<const MCSectionMachO&>(SD.getSection()); |
| 217 | WriteString(Section.getSectionName(), 16); |
| 218 | WriteString(Section.getSegmentName(), 16); |
| 219 | Write32(0); // address |
| 220 | Write32(SD.getFileSize()); // size |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 221 | Write32(FileOffset); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 222 | |
| 223 | assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!"); |
| 224 | Write32(Log2_32(SD.getAlignment())); |
| 225 | Write32(0); // file offset of relocation entries |
| 226 | Write32(0); // number of relocation entrions |
| 227 | Write32(Section.getTypeAndAttributes()); |
| 228 | Write32(0); // reserved1 |
| 229 | Write32(Section.getStubSize()); // reserved2 |
| 230 | |
| 231 | assert(OS.tell() - Start == Section32Size); |
| 232 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 233 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 234 | void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, |
| 235 | uint32_t StringTableOffset, |
| 236 | uint32_t StringTableSize) { |
| 237 | // struct symtab_command (24 bytes) |
| 238 | |
| 239 | uint64_t Start = OS.tell(); |
| 240 | (void) Start; |
| 241 | |
| 242 | Write32(LCT_Symtab); |
| 243 | Write32(SymtabLoadCommandSize); |
| 244 | Write32(SymbolOffset); |
| 245 | Write32(NumSymbols); |
| 246 | Write32(StringTableOffset); |
| 247 | Write32(StringTableSize); |
| 248 | |
| 249 | assert(OS.tell() - Start == SymtabLoadCommandSize); |
| 250 | } |
| 251 | |
| 252 | void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
| 253 | uint32_t NumLocalSymbols, |
| 254 | uint32_t FirstExternalSymbol, |
| 255 | uint32_t NumExternalSymbols, |
| 256 | uint32_t FirstUndefinedSymbol, |
| 257 | uint32_t NumUndefinedSymbols, |
| 258 | uint32_t IndirectSymbolOffset, |
| 259 | uint32_t NumIndirectSymbols) { |
| 260 | // struct dysymtab_command (80 bytes) |
| 261 | |
| 262 | uint64_t Start = OS.tell(); |
| 263 | (void) Start; |
| 264 | |
| 265 | Write32(LCT_Dysymtab); |
| 266 | Write32(DysymtabLoadCommandSize); |
| 267 | Write32(FirstLocalSymbol); |
| 268 | Write32(NumLocalSymbols); |
| 269 | Write32(FirstExternalSymbol); |
| 270 | Write32(NumExternalSymbols); |
| 271 | Write32(FirstUndefinedSymbol); |
| 272 | Write32(NumUndefinedSymbols); |
| 273 | Write32(0); // tocoff |
| 274 | Write32(0); // ntoc |
| 275 | Write32(0); // modtaboff |
| 276 | Write32(0); // nmodtab |
| 277 | Write32(0); // extrefsymoff |
| 278 | Write32(0); // nextrefsyms |
| 279 | Write32(IndirectSymbolOffset); |
| 280 | Write32(NumIndirectSymbols); |
| 281 | Write32(0); // extreloff |
| 282 | Write32(0); // nextrel |
| 283 | Write32(0); // locreloff |
| 284 | Write32(0); // nlocrel |
| 285 | |
| 286 | assert(OS.tell() - Start == DysymtabLoadCommandSize); |
| 287 | } |
| 288 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 289 | void WriteNlist32(MachSymbolData &MSD) { |
| 290 | MCSymbol &Symbol = MSD.SymbolData->getSymbol(); |
| 291 | uint8_t Type = 0; |
| 292 | |
| 293 | // Set the N_TYPE bits. See <mach-o/nlist.h>. |
| 294 | // |
| 295 | // FIXME: Are the prebound or indirect fields possible here? |
| 296 | if (Symbol.isUndefined()) |
| 297 | Type = STT_Undefined; |
| 298 | else if (Symbol.isAbsolute()) |
| 299 | Type = STT_Absolute; |
| 300 | else |
| 301 | Type = STT_Section; |
| 302 | |
| 303 | // FIXME: Set STAB bits. |
| 304 | |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 305 | if (MSD.SymbolData->isPrivateExtern()) |
| 306 | Type |= STF_PrivateExtern; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 307 | |
| 308 | // Set external bit. |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 309 | if (MSD.SymbolData->isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 310 | Type |= STF_External; |
| 311 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 312 | // struct nlist (12 bytes) |
| 313 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 314 | Write32(MSD.StringIndex); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 315 | Write8(Type); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 316 | Write8(MSD.SectionIndex); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 317 | |
| 318 | // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' |
| 319 | // value. |
| 320 | Write16(MSD.SymbolData->getFlags() & 0xFFFF); |
| 321 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 322 | Write32(0); // FIXME: Value |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame^] | 325 | void BindIndirectSymbols(MCAssembler &Asm, |
| 326 | DenseMap<MCSymbol*, MCSymbolData*> &SymbolMap) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 327 | // This is the point where 'as' creates actual symbols for indirect symbols |
| 328 | // (in the following two passes). It would be easier for us to do this |
| 329 | // sooner when we see the attribute, but that makes getting the order in the |
| 330 | // symbol table much more complicated than it is worth. |
| 331 | // |
| 332 | // FIXME: Revisit this when the dust settles. |
| 333 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 334 | // Bind non lazy symbol pointers first. |
| 335 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 336 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 337 | // FIXME: cast<> support! |
| 338 | const MCSectionMachO &Section = |
| 339 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 340 | |
| 341 | unsigned Type = |
| 342 | Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE; |
| 343 | if (Type != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) |
| 344 | continue; |
| 345 | |
| 346 | MCSymbolData *&Entry = SymbolMap[it->Symbol]; |
| 347 | if (!Entry) |
| 348 | Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm); |
| 349 | } |
| 350 | |
| 351 | // Then lazy symbol pointers and symbol stubs. |
| 352 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 353 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 354 | // FIXME: cast<> support! |
| 355 | const MCSectionMachO &Section = |
| 356 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 357 | |
| 358 | unsigned Type = |
| 359 | Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE; |
| 360 | if (Type != MCSectionMachO::S_LAZY_SYMBOL_POINTERS && |
| 361 | Type != MCSectionMachO::S_SYMBOL_STUBS) |
| 362 | continue; |
| 363 | |
| 364 | MCSymbolData *&Entry = SymbolMap[it->Symbol]; |
| 365 | if (!Entry) { |
| 366 | Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm); |
| 367 | |
| 368 | // Set the symbol type to undefined lazy, but only on construction. |
| 369 | // |
| 370 | // FIXME: Do not hardcode. |
| 371 | Entry->setFlags(Entry->getFlags() | 0x0001); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 376 | /// ComputeSymbolTable - Compute the symbol table data |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 377 | /// |
| 378 | /// \param StringTable [out] - The string table data. |
| 379 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 380 | /// string table. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 381 | void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable, |
| 382 | std::vector<MachSymbolData> &LocalSymbolData, |
| 383 | std::vector<MachSymbolData> &ExternalSymbolData, |
| 384 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
| 385 | // Build section lookup table. |
| 386 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 387 | unsigned Index = 1; |
| 388 | for (MCAssembler::iterator it = Asm.begin(), |
| 389 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 390 | SectionIndexMap[&it->getSection()] = Index; |
| 391 | assert(Index <= 256 && "Too many sections!"); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 392 | |
| 393 | // Index 0 is always the empty string. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 394 | StringMap<uint64_t> StringIndexMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 395 | StringTable += '\x00'; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 396 | |
| 397 | // Build the symbol arrays and the string table, but only for non-local |
| 398 | // symbols. |
| 399 | // |
| 400 | // The particular order that we collect the symbols and create the string |
| 401 | // table, then sort the symbols is chosen to match 'as'. Even though it |
| 402 | // doesn't matter for correctness, this is important for letting us diff .o |
| 403 | // files. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 404 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 405 | ie = Asm.symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 406 | MCSymbol &Symbol = it->getSymbol(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 408 | if (!it->isExternal() && !Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 409 | continue; |
| 410 | |
| 411 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 412 | if (!Entry) { |
| 413 | Entry = StringTable.size(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 414 | StringTable += Symbol.getName(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 415 | StringTable += '\x00'; |
| 416 | } |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 417 | |
| 418 | MachSymbolData MSD; |
| 419 | MSD.SymbolData = it; |
| 420 | MSD.StringIndex = Entry; |
| 421 | |
| 422 | if (Symbol.isUndefined()) { |
| 423 | MSD.SectionIndex = 0; |
| 424 | UndefinedSymbolData.push_back(MSD); |
| 425 | } else if (Symbol.isAbsolute()) { |
| 426 | MSD.SectionIndex = 0; |
| 427 | ExternalSymbolData.push_back(MSD); |
| 428 | } else { |
| 429 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 430 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 431 | ExternalSymbolData.push_back(MSD); |
| 432 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 435 | // Now add the data for local symbols. |
| 436 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 437 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 438 | MCSymbol &Symbol = it->getSymbol(); |
| 439 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 440 | if (it->isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 441 | continue; |
| 442 | |
| 443 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 444 | if (!Entry) { |
| 445 | Entry = StringTable.size(); |
| 446 | StringTable += Symbol.getName(); |
| 447 | StringTable += '\x00'; |
| 448 | } |
| 449 | |
| 450 | MachSymbolData MSD; |
| 451 | MSD.SymbolData = it; |
| 452 | MSD.StringIndex = Entry; |
| 453 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 454 | if (Symbol.isAbsolute()) { |
| 455 | MSD.SectionIndex = 0; |
| 456 | LocalSymbolData.push_back(MSD); |
| 457 | } else { |
| 458 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 459 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 460 | LocalSymbolData.push_back(MSD); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // External and undefined symbols are required to be in lexicographic order. |
| 465 | std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 466 | std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 467 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 468 | // The string table is padded to a multiple of 4. |
| 469 | // |
| 470 | // FIXME: Check to see if this varies per arch. |
| 471 | while (StringTable.size() % 4) |
| 472 | StringTable += '\x00'; |
| 473 | } |
| 474 | |
| 475 | void WriteObject(MCAssembler &Asm) { |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 476 | unsigned NumSections = Asm.size(); |
| 477 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame^] | 478 | // Compute the symbol -> symbol data map. |
| 479 | // |
| 480 | // FIXME: This should not be here. |
| 481 | DenseMap<MCSymbol*, MCSymbolData *> SymbolMap; |
| 482 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 483 | ie = Asm.symbol_end(); it != ie; ++it) |
| 484 | SymbolMap[&it->getSymbol()] = it; |
| 485 | |
| 486 | // Create symbol data for any indirect symbols. |
| 487 | BindIndirectSymbols(Asm, SymbolMap); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 488 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 489 | // Compute symbol table information. |
| 490 | SmallString<256> StringTable; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 491 | std::vector<MachSymbolData> LocalSymbolData; |
| 492 | std::vector<MachSymbolData> ExternalSymbolData; |
| 493 | std::vector<MachSymbolData> UndefinedSymbolData; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 494 | unsigned NumSymbols = Asm.symbol_size(); |
| 495 | |
| 496 | // No symbol table command is written if there are no symbols. |
| 497 | if (NumSymbols) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 498 | ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData, |
| 499 | UndefinedSymbolData); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 500 | |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 501 | // Compute the file offsets for all the sections in advance, so that we can |
| 502 | // write things out in order. |
| 503 | SmallVector<uint64_t, 16> SectionFileOffsets; |
| 504 | SectionFileOffsets.resize(NumSections); |
| 505 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 506 | // The section data starts after the header, the segment load command (and |
| 507 | // section headers) and the symbol table. |
| 508 | unsigned NumLoadCommands = 1; |
| 509 | uint64_t LoadCommandsSize = |
| 510 | SegmentLoadCommand32Size + NumSections * Section32Size; |
| 511 | |
| 512 | // Add the symbol table load command sizes, if used. |
| 513 | if (NumSymbols) { |
| 514 | NumLoadCommands += 2; |
| 515 | LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize; |
| 516 | } |
| 517 | |
| 518 | uint64_t FileOffset = Header32Size + LoadCommandsSize; |
| 519 | uint64_t SectionDataStartOffset = FileOffset; |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 520 | uint64_t SectionDataSize = 0; |
| 521 | unsigned Index = 0; |
| 522 | for (MCAssembler::iterator it = Asm.begin(), |
| 523 | ie = Asm.end(); it != ie; ++it, ++Index) { |
| 524 | SectionFileOffsets[Index] = FileOffset; |
| 525 | FileOffset += it->getFileSize(); |
| 526 | SectionDataSize += it->getFileSize(); |
| 527 | } |
| 528 | |
| 529 | // Write the prolog, starting with the header and load command... |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 530 | WriteHeader32(NumLoadCommands, LoadCommandsSize); |
| 531 | WriteSegmentLoadCommand32(NumSections, SectionDataStartOffset, |
| 532 | SectionDataSize); |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 533 | |
| 534 | // ... and then the section headers. |
| 535 | Index = 0; |
| 536 | for (MCAssembler::iterator it = Asm.begin(), |
| 537 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 538 | WriteSection32(*it, SectionFileOffsets[Index]); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 539 | |
| 540 | // Write the symbol table load command, if used. |
| 541 | if (NumSymbols) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 542 | unsigned FirstLocalSymbol = 0; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 543 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
| 544 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
| 545 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
| 546 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
| 547 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 548 | unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); |
| 549 | unsigned NumSymTabSymbols = |
| 550 | NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; |
| 551 | uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; |
| 552 | uint64_t IndirectSymbolOffset = 0; |
| 553 | |
| 554 | // If used, the indirect symbols are written after the section data. |
| 555 | if (NumIndirectSymbols) |
| 556 | IndirectSymbolOffset = SectionDataStartOffset + SectionDataSize; |
| 557 | |
| 558 | // The symbol table is written after the indirect symbol data. |
| 559 | uint64_t SymbolTableOffset = |
| 560 | SectionDataStartOffset + SectionDataSize + IndirectSymbolSize; |
| 561 | |
| 562 | // The string table is written after symbol table. |
| 563 | uint64_t StringTableOffset = |
| 564 | SymbolTableOffset + NumSymTabSymbols * Nlist32Size; |
| 565 | WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, |
| 566 | StringTableOffset, StringTable.size()); |
| 567 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 568 | WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
| 569 | FirstExternalSymbol, NumExternalSymbols, |
| 570 | FirstUndefinedSymbol, NumUndefinedSymbols, |
| 571 | IndirectSymbolOffset, NumIndirectSymbols); |
| 572 | } |
| 573 | |
| 574 | // Write the actual section data. |
| 575 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
| 576 | WriteFileData(OS, *it, *this); |
| 577 | |
| 578 | // Write the symbol table data, if used. |
| 579 | if (NumSymbols) { |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame^] | 580 | // FIXME: We shouldn't need this index table. |
| 581 | DenseMap<MCSymbol*, unsigned> SymbolIndexMap; |
| 582 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) { |
| 583 | MCSymbol &Symbol = LocalSymbolData[i].SymbolData->getSymbol(); |
| 584 | SymbolIndexMap.insert(std::make_pair(&Symbol, SymbolIndexMap.size())); |
| 585 | } |
| 586 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) { |
| 587 | MCSymbol &Symbol = ExternalSymbolData[i].SymbolData->getSymbol(); |
| 588 | SymbolIndexMap.insert(std::make_pair(&Symbol, SymbolIndexMap.size())); |
| 589 | } |
| 590 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) { |
| 591 | MCSymbol &Symbol = UndefinedSymbolData[i].SymbolData->getSymbol(); |
| 592 | SymbolIndexMap.insert(std::make_pair(&Symbol, SymbolIndexMap.size())); |
| 593 | } |
| 594 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 595 | // Write the indirect symbol entries. |
| 596 | // |
| 597 | // FIXME: We need the symbol index map for this. |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame^] | 598 | for (MCAssembler::indirect_symbol_iterator |
| 599 | it = Asm.indirect_symbol_begin(), |
| 600 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 601 | // Indirect symbols in the non lazy symbol pointer section have some |
| 602 | // special handling. |
| 603 | const MCSectionMachO &Section = |
| 604 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 605 | unsigned Type = |
| 606 | Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE; |
| 607 | if (Type == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) { |
| 608 | // If this symbol is defined and internal, mark it as such. |
| 609 | if (it->Symbol->isDefined() && |
| 610 | !SymbolMap.lookup(it->Symbol)->isExternal()) { |
| 611 | uint32_t Flags = ISF_Local; |
| 612 | if (it->Symbol->isAbsolute()) |
| 613 | Flags |= ISF_Absolute; |
| 614 | Write32(Flags); |
| 615 | continue; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | Write32(SymbolIndexMap[it->Symbol]); |
| 620 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 621 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 622 | // FIXME: Check that offsets match computed ones. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 623 | |
| 624 | // Write the symbol table entries. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 625 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 626 | WriteNlist32(LocalSymbolData[i]); |
| 627 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 628 | WriteNlist32(ExternalSymbolData[i]); |
| 629 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 630 | WriteNlist32(UndefinedSymbolData[i]); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 631 | |
| 632 | // Write the string table. |
| 633 | OS << StringTable.str(); |
| 634 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 635 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 636 | }; |
| 637 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 638 | /* *** */ |
| 639 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 640 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 641 | } |
| 642 | |
| 643 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *SD) |
| 644 | : Kind(_Kind), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 645 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 646 | { |
| 647 | if (SD) |
| 648 | SD->getFragmentList().push_back(this); |
| 649 | } |
| 650 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 651 | MCFragment::~MCFragment() { |
| 652 | } |
| 653 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 654 | /* *** */ |
| 655 | |
| 656 | MCSectionData::MCSectionData() : Section(*(MCSection*)0) {} |
| 657 | |
| 658 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
| 659 | : Section(_Section), |
| 660 | Alignment(1), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 661 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 662 | { |
| 663 | if (A) |
| 664 | A->getSectionList().push_back(this); |
| 665 | } |
| 666 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 667 | /* *** */ |
| 668 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 669 | MCSymbolData::MCSymbolData() : Symbol(*(MCSymbol*)0) {} |
| 670 | |
| 671 | MCSymbolData::MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment, |
| 672 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 673 | : Symbol(_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 674 | IsExternal(false), IsPrivateExtern(false), Flags(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 675 | { |
| 676 | if (A) |
| 677 | A->getSymbolList().push_back(this); |
| 678 | } |
| 679 | |
| 680 | /* *** */ |
| 681 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 682 | MCAssembler::MCAssembler(raw_ostream &_OS) : OS(_OS) {} |
| 683 | |
| 684 | MCAssembler::~MCAssembler() { |
| 685 | } |
| 686 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 687 | void MCAssembler::LayoutSection(MCSectionData &SD) { |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 688 | uint64_t Offset = 0; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 689 | |
| 690 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 691 | MCFragment &F = *it; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 692 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 693 | F.setOffset(Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 694 | |
| 695 | // Evaluate fragment size. |
| 696 | switch (F.getKind()) { |
| 697 | case MCFragment::FT_Align: { |
| 698 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 699 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 700 | uint64_t AlignedOffset = RoundUpToAlignment(Offset, AF.getAlignment()); |
| 701 | uint64_t PaddingBytes = AlignedOffset - Offset; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 702 | |
| 703 | if (PaddingBytes > AF.getMaxBytesToEmit()) |
| 704 | AF.setFileSize(0); |
| 705 | else |
| 706 | AF.setFileSize(PaddingBytes); |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | case MCFragment::FT_Data: |
| 711 | case MCFragment::FT_Fill: |
| 712 | F.setFileSize(F.getMaxFileSize()); |
| 713 | break; |
| 714 | |
| 715 | case MCFragment::FT_Org: { |
| 716 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 717 | |
| 718 | if (!OF.getOffset().isAbsolute()) |
| 719 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 720 | uint64_t OrgOffset = OF.getOffset().getConstant(); |
| 721 | |
| 722 | // FIXME: We need a way to communicate this error. |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 723 | if (OrgOffset < Offset) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 724 | llvm_report_error("invalid .org offset '" + Twine(OrgOffset) + |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 725 | "' (section offset '" + Twine(Offset) + "'"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 727 | F.setFileSize(OrgOffset - Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | } |
| 731 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 732 | Offset += F.getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // FIXME: Pad section? |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 736 | SD.setFileSize(Offset); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /// WriteFileData - Write the \arg F data to the output file. |
| 740 | static void WriteFileData(raw_ostream &OS, const MCFragment &F, |
| 741 | MachObjectWriter &MOW) { |
| 742 | uint64_t Start = OS.tell(); |
| 743 | (void) Start; |
| 744 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 745 | ++EmittedFragments; |
| 746 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 747 | // FIXME: Embed in fragments instead? |
| 748 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 749 | case MCFragment::FT_Align: { |
| 750 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 751 | uint64_t Count = AF.getFileSize() / AF.getValueSize(); |
| 752 | |
| 753 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 754 | // multiple .align directives to enforce the semantics it wants), but is |
| 755 | // severe enough that we want to report it. How to handle this? |
| 756 | if (Count * AF.getValueSize() != AF.getFileSize()) |
| 757 | llvm_report_error("undefined .align directive, value size '" + |
| 758 | Twine(AF.getValueSize()) + |
| 759 | "' is not a divisor of padding size '" + |
| 760 | Twine(AF.getFileSize()) + "'"); |
| 761 | |
| 762 | for (uint64_t i = 0; i != Count; ++i) { |
| 763 | switch (AF.getValueSize()) { |
| 764 | default: |
| 765 | assert(0 && "Invalid size!"); |
| 766 | case 1: MOW.Write8 (uint8_t (AF.getValue())); break; |
| 767 | case 2: MOW.Write16(uint16_t(AF.getValue())); break; |
| 768 | case 4: MOW.Write32(uint32_t(AF.getValue())); break; |
| 769 | case 8: MOW.Write64(uint64_t(AF.getValue())); break; |
| 770 | } |
| 771 | } |
| 772 | break; |
| 773 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 774 | |
| 775 | case MCFragment::FT_Data: |
| 776 | OS << cast<MCDataFragment>(F).getContents().str(); |
| 777 | break; |
| 778 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 779 | case MCFragment::FT_Fill: { |
| 780 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 781 | |
| 782 | if (!FF.getValue().isAbsolute()) |
| 783 | llvm_unreachable("FIXME: Not yet implemented!"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 784 | int64_t Value = FF.getValue().getConstant(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 785 | |
| 786 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 787 | switch (FF.getValueSize()) { |
| 788 | default: |
| 789 | assert(0 && "Invalid size!"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 790 | case 1: MOW.Write8 (uint8_t (Value)); break; |
| 791 | case 2: MOW.Write16(uint16_t(Value)); break; |
| 792 | case 4: MOW.Write32(uint32_t(Value)); break; |
| 793 | case 8: MOW.Write64(uint64_t(Value)); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | break; |
| 797 | } |
| 798 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 799 | case MCFragment::FT_Org: { |
| 800 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 801 | |
| 802 | for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i) |
| 803 | MOW.Write8(uint8_t(OF.getValue())); |
| 804 | |
| 805 | break; |
| 806 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | assert(OS.tell() - Start == F.getFileSize()); |
| 810 | } |
| 811 | |
| 812 | /// WriteFileData - Write the \arg SD data to the output file. |
| 813 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 814 | MachObjectWriter &MOW) { |
| 815 | uint64_t Start = OS.tell(); |
| 816 | (void) Start; |
| 817 | |
| 818 | for (MCSectionData::const_iterator it = SD.begin(), |
| 819 | ie = SD.end(); it != ie; ++it) |
| 820 | WriteFileData(OS, *it, MOW); |
| 821 | |
| 822 | assert(OS.tell() - Start == SD.getFileSize()); |
| 823 | } |
| 824 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 825 | void MCAssembler::Finish() { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 826 | // Layout the sections and fragments. |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 827 | for (iterator it = begin(), ie = end(); it != ie; ++it) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 828 | LayoutSection(*it); |
| 829 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 830 | // Write the object file. |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 831 | MachObjectWriter MOW(OS); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 832 | MOW.WriteObject(*this); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 833 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 834 | OS.flush(); |
| 835 | } |