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 | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 45 | static const unsigned RelocationInfoSize = 8; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 46 | |
| 47 | enum HeaderFileType { |
| 48 | HFT_Object = 0x1 |
| 49 | }; |
| 50 | |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame^] | 51 | enum HeaderFlags { |
| 52 | HF_SubsectionsViaSymbols = 0x2000 |
| 53 | }; |
| 54 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 55 | enum LoadCommandType { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 56 | LCT_Segment = 0x1, |
| 57 | LCT_Symtab = 0x2, |
| 58 | LCT_Dysymtab = 0xb |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 61 | // See <mach-o/nlist.h>. |
| 62 | enum SymbolTypeType { |
| 63 | STT_Undefined = 0x00, |
| 64 | STT_Absolute = 0x02, |
| 65 | STT_Section = 0x0e |
| 66 | }; |
| 67 | |
| 68 | enum SymbolTypeFlags { |
| 69 | // If any of these bits are set, then the entry is a stab entry number (see |
| 70 | // <mach-o/stab.h>. Otherwise the other masks apply. |
| 71 | STF_StabsEntryMask = 0xe0, |
| 72 | |
| 73 | STF_TypeMask = 0x0e, |
| 74 | STF_External = 0x01, |
| 75 | STF_PrivateExtern = 0x10 |
| 76 | }; |
| 77 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 78 | /// IndirectSymbolFlags - Flags for encoding special values in the indirect |
| 79 | /// symbol entry. |
| 80 | enum IndirectSymbolFlags { |
| 81 | ISF_Local = 0x80000000, |
| 82 | ISF_Absolute = 0x40000000 |
| 83 | }; |
| 84 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 85 | /// RelocationFlags - Special flags for addresses. |
| 86 | enum RelocationFlags { |
| 87 | RF_Scattered = 0x80000000 |
| 88 | }; |
| 89 | |
| 90 | enum RelocationInfoType { |
| 91 | RIT_Vanilla = 0, |
| 92 | RIT_Pair = 1, |
| 93 | RIT_Difference = 2, |
| 94 | RIT_PreboundLazyPointer = 3, |
| 95 | RIT_LocalDifference = 4 |
| 96 | }; |
| 97 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 98 | /// MachSymbolData - Helper struct for containing some precomputed information |
| 99 | /// on symbols. |
| 100 | struct MachSymbolData { |
| 101 | MCSymbolData *SymbolData; |
| 102 | uint64_t StringIndex; |
| 103 | uint8_t SectionIndex; |
| 104 | |
| 105 | // Support lexicographic sorting. |
| 106 | bool operator<(const MachSymbolData &RHS) const { |
| 107 | const std::string &Name = SymbolData->getSymbol().getName(); |
| 108 | return Name < RHS.SymbolData->getSymbol().getName(); |
| 109 | } |
| 110 | }; |
| 111 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 112 | raw_ostream &OS; |
| 113 | bool IsLSB; |
| 114 | |
| 115 | public: |
| 116 | MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true) |
| 117 | : OS(_OS), IsLSB(_IsLSB) { |
| 118 | } |
| 119 | |
| 120 | /// @name Helper Methods |
| 121 | /// @{ |
| 122 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 123 | void Write8(uint8_t Value) { |
| 124 | OS << char(Value); |
| 125 | } |
| 126 | |
| 127 | void Write16(uint16_t Value) { |
| 128 | if (IsLSB) { |
| 129 | Write8(uint8_t(Value >> 0)); |
| 130 | Write8(uint8_t(Value >> 8)); |
| 131 | } else { |
| 132 | Write8(uint8_t(Value >> 8)); |
| 133 | Write8(uint8_t(Value >> 0)); |
| 134 | } |
| 135 | } |
| 136 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 137 | void Write32(uint32_t Value) { |
| 138 | if (IsLSB) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 139 | Write16(uint16_t(Value >> 0)); |
| 140 | Write16(uint16_t(Value >> 16)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 141 | } else { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 142 | Write16(uint16_t(Value >> 16)); |
| 143 | Write16(uint16_t(Value >> 0)); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void Write64(uint64_t Value) { |
| 148 | if (IsLSB) { |
| 149 | Write32(uint32_t(Value >> 0)); |
| 150 | Write32(uint32_t(Value >> 32)); |
| 151 | } else { |
| 152 | Write32(uint32_t(Value >> 32)); |
| 153 | Write32(uint32_t(Value >> 0)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | void WriteZeros(unsigned N) { |
| 158 | const char Zeros[16] = { 0 }; |
| 159 | |
| 160 | for (unsigned i = 0, e = N / 16; i != e; ++i) |
| 161 | OS << StringRef(Zeros, 16); |
| 162 | |
| 163 | OS << StringRef(Zeros, N % 16); |
| 164 | } |
| 165 | |
| 166 | void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) { |
| 167 | OS << Str; |
| 168 | if (ZeroFillSize) |
| 169 | WriteZeros(ZeroFillSize - Str.size()); |
| 170 | } |
| 171 | |
| 172 | /// @} |
| 173 | |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame^] | 174 | void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize, |
| 175 | bool SubsectionsViaSymbols) { |
| 176 | uint32_t Flags = 0; |
| 177 | |
| 178 | if (SubsectionsViaSymbols) |
| 179 | Flags |= HF_SubsectionsViaSymbols; |
| 180 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 181 | // struct mach_header (28 bytes) |
| 182 | |
| 183 | uint64_t Start = OS.tell(); |
| 184 | (void) Start; |
| 185 | |
| 186 | Write32(Header_Magic32); |
| 187 | |
| 188 | // FIXME: Support cputype. |
| 189 | Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 190 | // FIXME: Support cpusubtype. |
| 191 | Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 192 | Write32(HFT_Object); |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame^] | 193 | Write32(NumLoadCommands); // Object files have a single load command, the |
| 194 | // segment. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 195 | Write32(LoadCommandsSize); |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame^] | 196 | Write32(Flags); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 197 | |
| 198 | assert(OS.tell() - Start == Header32Size); |
| 199 | } |
| 200 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 201 | /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command. |
| 202 | /// |
| 203 | /// \arg NumSections - The number of sections in this segment. |
| 204 | /// \arg SectionDataSize - The total size of the sections. |
| 205 | void WriteSegmentLoadCommand32(unsigned NumSections, |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 206 | uint64_t SectionDataStartOffset, |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 207 | uint64_t SectionDataSize) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 208 | // struct segment_command (56 bytes) |
| 209 | |
| 210 | uint64_t Start = OS.tell(); |
| 211 | (void) Start; |
| 212 | |
| 213 | Write32(LCT_Segment); |
| 214 | Write32(SegmentLoadCommand32Size + NumSections * Section32Size); |
| 215 | |
| 216 | WriteString("", 16); |
| 217 | Write32(0); // vmaddr |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 218 | Write32(SectionDataSize); // vmsize |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 219 | Write32(SectionDataStartOffset); // file offset |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 220 | Write32(SectionDataSize); // file size |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 221 | Write32(0x7); // maxprot |
| 222 | Write32(0x7); // initprot |
| 223 | Write32(NumSections); |
| 224 | Write32(0); // flags |
| 225 | |
| 226 | assert(OS.tell() - Start == SegmentLoadCommand32Size); |
| 227 | } |
| 228 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 229 | void WriteSection32(const MCSectionData &SD, uint64_t FileOffset, |
| 230 | uint64_t RelocationsStart, unsigned NumRelocations) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 231 | // struct section (68 bytes) |
| 232 | |
| 233 | uint64_t Start = OS.tell(); |
| 234 | (void) Start; |
| 235 | |
| 236 | // FIXME: cast<> support! |
| 237 | const MCSectionMachO &Section = |
| 238 | static_cast<const MCSectionMachO&>(SD.getSection()); |
| 239 | WriteString(Section.getSectionName(), 16); |
| 240 | WriteString(Section.getSegmentName(), 16); |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 241 | Write32(SD.getAddress()); // address |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 242 | Write32(SD.getSize()); // size |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 243 | Write32(FileOffset); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 244 | |
| 245 | assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!"); |
| 246 | Write32(Log2_32(SD.getAlignment())); |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 247 | Write32(NumRelocations ? RelocationsStart : 0); |
| 248 | Write32(NumRelocations); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 249 | Write32(Section.getTypeAndAttributes()); |
| 250 | Write32(0); // reserved1 |
| 251 | Write32(Section.getStubSize()); // reserved2 |
| 252 | |
| 253 | assert(OS.tell() - Start == Section32Size); |
| 254 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 256 | void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols, |
| 257 | uint32_t StringTableOffset, |
| 258 | uint32_t StringTableSize) { |
| 259 | // struct symtab_command (24 bytes) |
| 260 | |
| 261 | uint64_t Start = OS.tell(); |
| 262 | (void) Start; |
| 263 | |
| 264 | Write32(LCT_Symtab); |
| 265 | Write32(SymtabLoadCommandSize); |
| 266 | Write32(SymbolOffset); |
| 267 | Write32(NumSymbols); |
| 268 | Write32(StringTableOffset); |
| 269 | Write32(StringTableSize); |
| 270 | |
| 271 | assert(OS.tell() - Start == SymtabLoadCommandSize); |
| 272 | } |
| 273 | |
| 274 | void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
| 275 | uint32_t NumLocalSymbols, |
| 276 | uint32_t FirstExternalSymbol, |
| 277 | uint32_t NumExternalSymbols, |
| 278 | uint32_t FirstUndefinedSymbol, |
| 279 | uint32_t NumUndefinedSymbols, |
| 280 | uint32_t IndirectSymbolOffset, |
| 281 | uint32_t NumIndirectSymbols) { |
| 282 | // struct dysymtab_command (80 bytes) |
| 283 | |
| 284 | uint64_t Start = OS.tell(); |
| 285 | (void) Start; |
| 286 | |
| 287 | Write32(LCT_Dysymtab); |
| 288 | Write32(DysymtabLoadCommandSize); |
| 289 | Write32(FirstLocalSymbol); |
| 290 | Write32(NumLocalSymbols); |
| 291 | Write32(FirstExternalSymbol); |
| 292 | Write32(NumExternalSymbols); |
| 293 | Write32(FirstUndefinedSymbol); |
| 294 | Write32(NumUndefinedSymbols); |
| 295 | Write32(0); // tocoff |
| 296 | Write32(0); // ntoc |
| 297 | Write32(0); // modtaboff |
| 298 | Write32(0); // nmodtab |
| 299 | Write32(0); // extrefsymoff |
| 300 | Write32(0); // nextrefsyms |
| 301 | Write32(IndirectSymbolOffset); |
| 302 | Write32(NumIndirectSymbols); |
| 303 | Write32(0); // extreloff |
| 304 | Write32(0); // nextrel |
| 305 | Write32(0); // locreloff |
| 306 | Write32(0); // nlocrel |
| 307 | |
| 308 | assert(OS.tell() - Start == DysymtabLoadCommandSize); |
| 309 | } |
| 310 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 311 | void WriteNlist32(MachSymbolData &MSD) { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 312 | MCSymbolData &Data = *MSD.SymbolData; |
| 313 | MCSymbol &Symbol = Data.getSymbol(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 314 | uint8_t Type = 0; |
| 315 | |
| 316 | // Set the N_TYPE bits. See <mach-o/nlist.h>. |
| 317 | // |
| 318 | // FIXME: Are the prebound or indirect fields possible here? |
| 319 | if (Symbol.isUndefined()) |
| 320 | Type = STT_Undefined; |
| 321 | else if (Symbol.isAbsolute()) |
| 322 | Type = STT_Absolute; |
| 323 | else |
| 324 | Type = STT_Section; |
| 325 | |
| 326 | // FIXME: Set STAB bits. |
| 327 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 328 | if (Data.isPrivateExtern()) |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 329 | Type |= STF_PrivateExtern; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 330 | |
| 331 | // Set external bit. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 332 | if (Data.isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 333 | Type |= STF_External; |
| 334 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 335 | // struct nlist (12 bytes) |
| 336 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 337 | Write32(MSD.StringIndex); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 338 | Write8(Type); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 339 | Write8(MSD.SectionIndex); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 340 | |
| 341 | // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' |
| 342 | // value. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 343 | Write16(Data.getFlags() & 0xFFFF); |
Daniel Dunbar | 6aff2fb | 2009-08-24 08:40:12 +0000 | [diff] [blame] | 344 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 345 | // Write the symbol address. |
| 346 | uint32_t Address = 0; |
| 347 | if (Symbol.isDefined()) { |
| 348 | if (Symbol.isAbsolute()) { |
| 349 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 350 | } else { |
| 351 | Address = Data.getFragment()->getAddress() + Data.getOffset(); |
| 352 | } |
| 353 | } |
| 354 | Write32(Address); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 357 | struct MachRelocationEntry { |
| 358 | uint32_t Word0; |
| 359 | uint32_t Word1; |
| 360 | }; |
| 361 | void ComputeScatteredRelocationInfo(MCAssembler &Asm, |
| 362 | MCSectionData::Fixup &Fixup, |
| 363 | DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap, |
| 364 | std::vector<MachRelocationEntry> &Relocs) { |
| 365 | uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset; |
| 366 | unsigned IsPCRel = 0; |
| 367 | unsigned Type = RIT_Vanilla; |
| 368 | |
| 369 | // See <reloc.h>. |
| 370 | |
| 371 | const MCSymbol *A = Fixup.Value.getSymA(); |
| 372 | MCSymbolData *SD = SymbolMap.lookup(A); |
| 373 | uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset(); |
| 374 | uint32_t Value2 = 0; |
| 375 | |
| 376 | if (const MCSymbol *B = Fixup.Value.getSymB()) { |
| 377 | Type = RIT_LocalDifference; |
| 378 | |
| 379 | MCSymbolData *SD = SymbolMap.lookup(B); |
| 380 | Value2 = SD->getFragment()->getAddress() + SD->getOffset(); |
| 381 | } |
| 382 | |
| 383 | unsigned Log2Size = Log2_32(Fixup.Size); |
| 384 | assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!"); |
| 385 | |
| 386 | // The value which goes in the fixup is current value of the expression. |
| 387 | Fixup.FixedValue = Value - Value2 + Fixup.Value.getConstant(); |
| 388 | |
| 389 | MachRelocationEntry MRE; |
| 390 | MRE.Word0 = ((Address << 0) | |
| 391 | (Type << 24) | |
| 392 | (Log2Size << 28) | |
| 393 | (IsPCRel << 30) | |
| 394 | RF_Scattered); |
| 395 | MRE.Word1 = Value; |
| 396 | Relocs.push_back(MRE); |
| 397 | |
| 398 | if (Type == RIT_LocalDifference) { |
| 399 | Type = RIT_Pair; |
| 400 | |
| 401 | MachRelocationEntry MRE; |
| 402 | MRE.Word0 = ((0 << 0) | |
| 403 | (Type << 24) | |
| 404 | (Log2Size << 28) | |
| 405 | (0 << 30) | |
| 406 | RF_Scattered); |
| 407 | MRE.Word1 = Value2; |
| 408 | Relocs.push_back(MRE); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void ComputeRelocationInfo(MCAssembler &Asm, |
| 413 | MCSectionData::Fixup &Fixup, |
| 414 | DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap, |
| 415 | std::vector<MachRelocationEntry> &Relocs) { |
| 416 | // If this is a local symbol plus an offset or a difference, then we need a |
| 417 | // scattered relocation entry. |
| 418 | if (Fixup.Value.getSymB()) // a - b |
| 419 | return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs); |
| 420 | if (Fixup.Value.getSymA() && Fixup.Value.getConstant()) |
| 421 | if (!Fixup.Value.getSymA()->isUndefined()) |
| 422 | return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs); |
| 423 | |
| 424 | // See <reloc.h>. |
| 425 | uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset; |
| 426 | uint32_t Value = 0; |
| 427 | unsigned Index = 0; |
| 428 | unsigned IsPCRel = 0; |
| 429 | unsigned IsExtern = 0; |
| 430 | unsigned Type = 0; |
| 431 | |
| 432 | if (Fixup.Value.isAbsolute()) { // constant |
| 433 | // SymbolNum of 0 indicates the absolute section. |
| 434 | Type = RIT_Vanilla; |
| 435 | Value = 0; |
| 436 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 437 | } else { |
| 438 | const MCSymbol *Symbol = Fixup.Value.getSymA(); |
| 439 | MCSymbolData *SD = SymbolMap.lookup(Symbol); |
| 440 | |
| 441 | if (Symbol->isUndefined()) { |
| 442 | IsExtern = 1; |
| 443 | Index = SD->getIndex(); |
| 444 | Value = 0; |
| 445 | } else { |
| 446 | // The index is the section ordinal. |
| 447 | // |
| 448 | // FIXME: O(N) |
| 449 | Index = 1; |
| 450 | for (MCAssembler::iterator it = Asm.begin(), |
| 451 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 452 | if (&*it == SD->getFragment()->getParent()) |
| 453 | break; |
| 454 | Value = SD->getFragment()->getAddress() + SD->getOffset(); |
| 455 | } |
| 456 | |
| 457 | Type = RIT_Vanilla; |
| 458 | } |
| 459 | |
| 460 | // The value which goes in the fixup is current value of the expression. |
| 461 | Fixup.FixedValue = Value + Fixup.Value.getConstant(); |
| 462 | |
| 463 | unsigned Log2Size = Log2_32(Fixup.Size); |
| 464 | assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!"); |
| 465 | |
| 466 | // struct relocation_info (8 bytes) |
| 467 | MachRelocationEntry MRE; |
| 468 | MRE.Word0 = Address; |
| 469 | MRE.Word1 = ((Index << 0) | |
| 470 | (IsPCRel << 24) | |
| 471 | (Log2Size << 25) | |
| 472 | (IsExtern << 27) | |
| 473 | (Type << 28)); |
| 474 | Relocs.push_back(MRE); |
| 475 | } |
| 476 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 477 | void BindIndirectSymbols(MCAssembler &Asm, |
Daniel Dunbar | be96355 | 2009-08-26 13:57:54 +0000 | [diff] [blame] | 478 | DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 479 | // This is the point where 'as' creates actual symbols for indirect symbols |
| 480 | // (in the following two passes). It would be easier for us to do this |
| 481 | // sooner when we see the attribute, but that makes getting the order in the |
| 482 | // symbol table much more complicated than it is worth. |
| 483 | // |
| 484 | // FIXME: Revisit this when the dust settles. |
| 485 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 486 | // Bind non lazy symbol pointers first. |
| 487 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 488 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 489 | // FIXME: cast<> support! |
| 490 | const MCSectionMachO &Section = |
| 491 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 492 | |
| 493 | unsigned Type = |
| 494 | Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE; |
| 495 | if (Type != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) |
| 496 | continue; |
| 497 | |
| 498 | MCSymbolData *&Entry = SymbolMap[it->Symbol]; |
| 499 | if (!Entry) |
| 500 | Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm); |
| 501 | } |
| 502 | |
| 503 | // Then lazy symbol pointers and symbol stubs. |
| 504 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
| 505 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 506 | // FIXME: cast<> support! |
| 507 | const MCSectionMachO &Section = |
| 508 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 509 | |
| 510 | unsigned Type = |
| 511 | Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE; |
| 512 | if (Type != MCSectionMachO::S_LAZY_SYMBOL_POINTERS && |
| 513 | Type != MCSectionMachO::S_SYMBOL_STUBS) |
| 514 | continue; |
| 515 | |
| 516 | MCSymbolData *&Entry = SymbolMap[it->Symbol]; |
| 517 | if (!Entry) { |
| 518 | Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm); |
| 519 | |
| 520 | // Set the symbol type to undefined lazy, but only on construction. |
| 521 | // |
| 522 | // FIXME: Do not hardcode. |
| 523 | Entry->setFlags(Entry->getFlags() | 0x0001); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 528 | /// ComputeSymbolTable - Compute the symbol table data |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 529 | /// |
| 530 | /// \param StringTable [out] - The string table data. |
| 531 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 532 | /// string table. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 533 | void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable, |
| 534 | std::vector<MachSymbolData> &LocalSymbolData, |
| 535 | std::vector<MachSymbolData> &ExternalSymbolData, |
| 536 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
| 537 | // Build section lookup table. |
| 538 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 539 | unsigned Index = 1; |
| 540 | for (MCAssembler::iterator it = Asm.begin(), |
| 541 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 542 | SectionIndexMap[&it->getSection()] = Index; |
| 543 | assert(Index <= 256 && "Too many sections!"); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 544 | |
| 545 | // Index 0 is always the empty string. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 546 | StringMap<uint64_t> StringIndexMap; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 547 | StringTable += '\x00'; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 548 | |
| 549 | // Build the symbol arrays and the string table, but only for non-local |
| 550 | // symbols. |
| 551 | // |
| 552 | // The particular order that we collect the symbols and create the string |
| 553 | // table, then sort the symbols is chosen to match 'as'. Even though it |
| 554 | // doesn't matter for correctness, this is important for letting us diff .o |
| 555 | // files. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 556 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 557 | ie = Asm.symbol_end(); it != ie; ++it) { |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 558 | MCSymbol &Symbol = it->getSymbol(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 559 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 560 | if (!it->isExternal() && !Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 561 | continue; |
| 562 | |
| 563 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 564 | if (!Entry) { |
| 565 | Entry = StringTable.size(); |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 566 | StringTable += Symbol.getName(); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 567 | StringTable += '\x00'; |
| 568 | } |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 569 | |
| 570 | MachSymbolData MSD; |
| 571 | MSD.SymbolData = it; |
| 572 | MSD.StringIndex = Entry; |
| 573 | |
| 574 | if (Symbol.isUndefined()) { |
| 575 | MSD.SectionIndex = 0; |
| 576 | UndefinedSymbolData.push_back(MSD); |
| 577 | } else if (Symbol.isAbsolute()) { |
| 578 | MSD.SectionIndex = 0; |
| 579 | ExternalSymbolData.push_back(MSD); |
| 580 | } else { |
| 581 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 582 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 583 | ExternalSymbolData.push_back(MSD); |
| 584 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 587 | // Now add the data for local symbols. |
| 588 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 589 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 590 | MCSymbol &Symbol = it->getSymbol(); |
| 591 | |
Daniel Dunbar | 50e48b3 | 2009-08-24 08:39:57 +0000 | [diff] [blame] | 592 | if (it->isExternal() || Symbol.isUndefined()) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 593 | continue; |
| 594 | |
| 595 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 596 | if (!Entry) { |
| 597 | Entry = StringTable.size(); |
| 598 | StringTable += Symbol.getName(); |
| 599 | StringTable += '\x00'; |
| 600 | } |
| 601 | |
| 602 | MachSymbolData MSD; |
| 603 | MSD.SymbolData = it; |
| 604 | MSD.StringIndex = Entry; |
| 605 | |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 606 | if (Symbol.isAbsolute()) { |
| 607 | MSD.SectionIndex = 0; |
| 608 | LocalSymbolData.push_back(MSD); |
| 609 | } else { |
| 610 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 611 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 612 | LocalSymbolData.push_back(MSD); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | // External and undefined symbols are required to be in lexicographic order. |
| 617 | std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 618 | std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 619 | |
Daniel Dunbar | be96355 | 2009-08-26 13:57:54 +0000 | [diff] [blame] | 620 | // Set the symbol indices. |
| 621 | Index = 0; |
| 622 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 623 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
| 624 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 625 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 626 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 627 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
| 628 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 629 | // The string table is padded to a multiple of 4. |
| 630 | // |
| 631 | // FIXME: Check to see if this varies per arch. |
| 632 | while (StringTable.size() % 4) |
| 633 | StringTable += '\x00'; |
| 634 | } |
| 635 | |
| 636 | void WriteObject(MCAssembler &Asm) { |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 637 | unsigned NumSections = Asm.size(); |
| 638 | |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 639 | // Compute the symbol -> symbol data map. |
| 640 | // |
| 641 | // FIXME: This should not be here. |
Daniel Dunbar | be96355 | 2009-08-26 13:57:54 +0000 | [diff] [blame] | 642 | DenseMap<const MCSymbol*, MCSymbolData *> SymbolMap; |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 643 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 644 | ie = Asm.symbol_end(); it != ie; ++it) |
| 645 | SymbolMap[&it->getSymbol()] = it; |
| 646 | |
| 647 | // Create symbol data for any indirect symbols. |
| 648 | BindIndirectSymbols(Asm, SymbolMap); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 649 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 650 | // Compute symbol table information. |
| 651 | SmallString<256> StringTable; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 652 | std::vector<MachSymbolData> LocalSymbolData; |
| 653 | std::vector<MachSymbolData> ExternalSymbolData; |
| 654 | std::vector<MachSymbolData> UndefinedSymbolData; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 655 | unsigned NumSymbols = Asm.symbol_size(); |
| 656 | |
| 657 | // No symbol table command is written if there are no symbols. |
| 658 | if (NumSymbols) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 659 | ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData, |
| 660 | UndefinedSymbolData); |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 661 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 662 | // The section data starts after the header, the segment load command (and |
| 663 | // section headers) and the symbol table. |
| 664 | unsigned NumLoadCommands = 1; |
| 665 | uint64_t LoadCommandsSize = |
| 666 | SegmentLoadCommand32Size + NumSections * Section32Size; |
| 667 | |
| 668 | // Add the symbol table load command sizes, if used. |
| 669 | if (NumSymbols) { |
| 670 | NumLoadCommands += 2; |
| 671 | LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize; |
| 672 | } |
| 673 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 674 | uint64_t SectionDataStart = Header32Size + LoadCommandsSize; |
| 675 | uint64_t SectionDataEnd = SectionDataStart; |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 676 | uint64_t SectionDataSize = 0; |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 677 | if (!Asm.getSectionList().empty()) { |
| 678 | MCSectionData &SD = Asm.getSectionList().back(); |
| 679 | SectionDataSize = SD.getAddress() + SD.getSize(); |
| 680 | SectionDataEnd = SectionDataStart + SD.getAddress() + SD.getFileSize(); |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // Write the prolog, starting with the header and load command... |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame^] | 684 | WriteHeader32(NumLoadCommands, LoadCommandsSize, |
| 685 | Asm.getSubsectionsViaSymbols()); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 686 | WriteSegmentLoadCommand32(NumSections, SectionDataStart, SectionDataSize); |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 687 | |
| 688 | // ... and then the section headers. |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 689 | // |
| 690 | // We also compute the section relocations while we do this. Note that |
| 691 | // compute relocation info will also update the fixup to have the correct |
| 692 | // value; this will be overwrite the appropriate data in the fragment when |
| 693 | // it is written. |
| 694 | std::vector<MachRelocationEntry> RelocInfos; |
| 695 | uint64_t RelocTableEnd = SectionDataEnd; |
| 696 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; |
| 697 | ++it) { |
| 698 | MCSectionData &SD = *it; |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 699 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 700 | // The assembler writes relocations in the reverse order they were seen. |
| 701 | // |
| 702 | // FIXME: It is probably more complicated than this. |
| 703 | unsigned NumRelocsStart = RelocInfos.size(); |
| 704 | for (unsigned i = 0, e = SD.fixup_size(); i != e; ++i) |
| 705 | ComputeRelocationInfo(Asm, SD.getFixups()[e - i - 1], SymbolMap, |
| 706 | RelocInfos); |
| 707 | |
| 708 | unsigned NumRelocs = RelocInfos.size() - NumRelocsStart; |
| 709 | uint64_t SectionStart = SectionDataStart + SD.getAddress(); |
| 710 | WriteSection32(SD, SectionStart, RelocTableEnd, NumRelocs); |
| 711 | RelocTableEnd += NumRelocs * RelocationInfoSize; |
| 712 | } |
| 713 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 714 | // Write the symbol table load command, if used. |
| 715 | if (NumSymbols) { |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 716 | unsigned FirstLocalSymbol = 0; |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 717 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
| 718 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
| 719 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
| 720 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
| 721 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 722 | unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); |
| 723 | unsigned NumSymTabSymbols = |
| 724 | NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; |
| 725 | uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; |
| 726 | uint64_t IndirectSymbolOffset = 0; |
| 727 | |
| 728 | // If used, the indirect symbols are written after the section data. |
| 729 | if (NumIndirectSymbols) |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 730 | IndirectSymbolOffset = RelocTableEnd; |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 731 | |
| 732 | // The symbol table is written after the indirect symbol data. |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 733 | uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize; |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 734 | |
| 735 | // The string table is written after symbol table. |
| 736 | uint64_t StringTableOffset = |
| 737 | SymbolTableOffset + NumSymTabSymbols * Nlist32Size; |
| 738 | WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, |
| 739 | StringTableOffset, StringTable.size()); |
| 740 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 741 | WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
| 742 | FirstExternalSymbol, NumExternalSymbols, |
| 743 | FirstUndefinedSymbol, NumUndefinedSymbols, |
| 744 | IndirectSymbolOffset, NumIndirectSymbols); |
| 745 | } |
| 746 | |
| 747 | // Write the actual section data. |
| 748 | for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) |
| 749 | WriteFileData(OS, *it, *this); |
| 750 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 751 | // Write the relocation entries. |
| 752 | for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) { |
| 753 | Write32(RelocInfos[i].Word0); |
| 754 | Write32(RelocInfos[i].Word1); |
| 755 | } |
| 756 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 757 | // Write the symbol table data, if used. |
| 758 | if (NumSymbols) { |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 759 | // Write the indirect symbol entries. |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 760 | for (MCAssembler::indirect_symbol_iterator |
| 761 | it = Asm.indirect_symbol_begin(), |
| 762 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
| 763 | // Indirect symbols in the non lazy symbol pointer section have some |
| 764 | // special handling. |
| 765 | const MCSectionMachO &Section = |
| 766 | static_cast<const MCSectionMachO&>(it->SectionData->getSection()); |
| 767 | unsigned Type = |
| 768 | Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE; |
| 769 | if (Type == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) { |
| 770 | // If this symbol is defined and internal, mark it as such. |
| 771 | if (it->Symbol->isDefined() && |
| 772 | !SymbolMap.lookup(it->Symbol)->isExternal()) { |
| 773 | uint32_t Flags = ISF_Local; |
| 774 | if (it->Symbol->isAbsolute()) |
| 775 | Flags |= ISF_Absolute; |
| 776 | Write32(Flags); |
| 777 | continue; |
| 778 | } |
| 779 | } |
| 780 | |
Daniel Dunbar | be96355 | 2009-08-26 13:57:54 +0000 | [diff] [blame] | 781 | Write32(SymbolMap[it->Symbol]->getIndex()); |
Daniel Dunbar | ad7c3d5 | 2009-08-26 00:18:21 +0000 | [diff] [blame] | 782 | } |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 783 | |
Daniel Dunbar | 0c7761b | 2009-08-24 11:56:58 +0000 | [diff] [blame] | 784 | // FIXME: Check that offsets match computed ones. |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 785 | |
| 786 | // Write the symbol table entries. |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 787 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 788 | WriteNlist32(LocalSymbolData[i]); |
| 789 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 790 | WriteNlist32(ExternalSymbolData[i]); |
| 791 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 792 | WriteNlist32(UndefinedSymbolData[i]); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 793 | |
| 794 | // Write the string table. |
| 795 | OS << StringTable.str(); |
| 796 | } |
Daniel Dunbar | 2ae58f2 | 2009-08-22 08:28:27 +0000 | [diff] [blame] | 797 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 798 | }; |
| 799 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 800 | /* *** */ |
| 801 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 802 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 803 | } |
| 804 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 805 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 806 | : Kind(_Kind), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 807 | Parent(_Parent), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 808 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 809 | { |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 810 | if (Parent) |
| 811 | Parent->getFragmentList().push_back(this); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 814 | MCFragment::~MCFragment() { |
| 815 | } |
| 816 | |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 817 | uint64_t MCFragment::getAddress() const { |
| 818 | assert(getParent() && "Missing Section!"); |
| 819 | return getParent()->getAddress() + Offset; |
| 820 | } |
| 821 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 822 | /* *** */ |
| 823 | |
| 824 | MCSectionData::MCSectionData() : Section(*(MCSection*)0) {} |
| 825 | |
| 826 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
| 827 | : Section(_Section), |
| 828 | Alignment(1), |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 829 | Address(~UINT64_C(0)), |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 830 | Size(~UINT64_C(0)), |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 831 | FileSize(~UINT64_C(0)), |
| 832 | LastFixupLookup(~0) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 833 | { |
| 834 | if (A) |
| 835 | A->getSectionList().push_back(this); |
| 836 | } |
| 837 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 838 | const MCSectionData::Fixup * |
| 839 | MCSectionData::LookupFixup(const MCFragment *Fragment, uint64_t Offset) const { |
| 840 | // Use a one level cache to turn the common case of accessing the fixups in |
| 841 | // order into O(1) instead of O(N). |
| 842 | unsigned i = LastFixupLookup, Count = Fixups.size(), End = Fixups.size(); |
| 843 | if (i >= End) |
| 844 | i = 0; |
| 845 | while (Count--) { |
| 846 | const Fixup &F = Fixups[i]; |
| 847 | if (F.Fragment == Fragment && F.Offset == Offset) { |
| 848 | LastFixupLookup = i; |
| 849 | return &F; |
| 850 | } |
| 851 | |
| 852 | ++i; |
| 853 | if (i == End) |
| 854 | i = 0; |
| 855 | } |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 860 | /* *** */ |
| 861 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 862 | MCSymbolData::MCSymbolData() : Symbol(*(MCSymbol*)0) {} |
| 863 | |
| 864 | MCSymbolData::MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment, |
| 865 | uint64_t _Offset, MCAssembler *A) |
Daniel Dunbar | 3edd9bb | 2009-08-22 11:41:10 +0000 | [diff] [blame] | 866 | : Symbol(_Symbol), Fragment(_Fragment), Offset(_Offset), |
Daniel Dunbar | be96355 | 2009-08-26 13:57:54 +0000 | [diff] [blame] | 867 | IsExternal(false), IsPrivateExtern(false), Flags(0), Index(0) |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 868 | { |
| 869 | if (A) |
| 870 | A->getSymbolList().push_back(this); |
| 871 | } |
| 872 | |
| 873 | /* *** */ |
| 874 | |
Daniel Dunbar | 6009db4 | 2009-08-26 21:22:22 +0000 | [diff] [blame^] | 875 | MCAssembler::MCAssembler(raw_ostream &_OS) |
| 876 | : OS(_OS), |
| 877 | SubsectionsViaSymbols(false) |
| 878 | { |
| 879 | } |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 880 | |
| 881 | MCAssembler::~MCAssembler() { |
| 882 | } |
| 883 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 884 | void MCAssembler::LayoutSection(MCSectionData &SD, unsigned NextAlign) { |
| 885 | uint64_t Address = SD.getAddress(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 886 | |
| 887 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 888 | MCFragment &F = *it; |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 889 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 890 | F.setOffset(Address - SD.getAddress()); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 891 | |
| 892 | // Evaluate fragment size. |
| 893 | switch (F.getKind()) { |
| 894 | case MCFragment::FT_Align: { |
| 895 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 896 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 897 | uint64_t Size = RoundUpToAlignment(Address, AF.getAlignment()) - Address; |
| 898 | if (Size > AF.getMaxBytesToEmit()) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 899 | AF.setFileSize(0); |
| 900 | else |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 901 | AF.setFileSize(Size); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 902 | break; |
| 903 | } |
| 904 | |
| 905 | case MCFragment::FT_Data: |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 906 | F.setFileSize(F.getMaxFileSize()); |
| 907 | break; |
| 908 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 909 | case MCFragment::FT_Fill: { |
| 910 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 911 | |
| 912 | F.setFileSize(F.getMaxFileSize()); |
| 913 | |
| 914 | // If the fill value is constant, thats it. |
| 915 | if (FF.getValue().isAbsolute()) |
| 916 | break; |
| 917 | |
| 918 | // Otherwise, add fixups for the values. |
| 919 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 920 | MCSectionData::Fixup Fix(F, i * FF.getValueSize(), |
| 921 | FF.getValue(),FF.getValueSize()); |
| 922 | SD.getFixups().push_back(Fix); |
| 923 | } |
| 924 | break; |
| 925 | } |
| 926 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 927 | case MCFragment::FT_Org: { |
| 928 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 929 | |
| 930 | if (!OF.getOffset().isAbsolute()) |
| 931 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 932 | uint64_t OrgOffset = OF.getOffset().getConstant(); |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 933 | uint64_t Offset = Address - SD.getAddress(); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 934 | |
| 935 | // FIXME: We need a way to communicate this error. |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 936 | if (OrgOffset < Offset) |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 937 | llvm_report_error("invalid .org offset '" + Twine(OrgOffset) + |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 938 | "' (at offset '" + Twine(Offset) + "'"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 939 | |
Daniel Dunbar | a5441fe | 2009-08-22 08:27:54 +0000 | [diff] [blame] | 940 | F.setFileSize(OrgOffset - Offset); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 945 | Address += F.getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 948 | // Set the section sizes. |
| 949 | SD.setSize(Address - SD.getAddress()); |
| 950 | SD.setFileSize(RoundUpToAlignment(Address, NextAlign) - SD.getAddress()); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | /// WriteFileData - Write the \arg F data to the output file. |
| 954 | static void WriteFileData(raw_ostream &OS, const MCFragment &F, |
| 955 | MachObjectWriter &MOW) { |
| 956 | uint64_t Start = OS.tell(); |
| 957 | (void) Start; |
| 958 | |
Daniel Dunbar | 0adcd35 | 2009-08-25 21:10:45 +0000 | [diff] [blame] | 959 | ++EmittedFragments; |
| 960 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 961 | // FIXME: Embed in fragments instead? |
| 962 | switch (F.getKind()) { |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 963 | case MCFragment::FT_Align: { |
| 964 | MCAlignFragment &AF = cast<MCAlignFragment>(F); |
| 965 | uint64_t Count = AF.getFileSize() / AF.getValueSize(); |
| 966 | |
| 967 | // FIXME: This error shouldn't actually occur (the front end should emit |
| 968 | // multiple .align directives to enforce the semantics it wants), but is |
| 969 | // severe enough that we want to report it. How to handle this? |
| 970 | if (Count * AF.getValueSize() != AF.getFileSize()) |
| 971 | llvm_report_error("undefined .align directive, value size '" + |
| 972 | Twine(AF.getValueSize()) + |
| 973 | "' is not a divisor of padding size '" + |
| 974 | Twine(AF.getFileSize()) + "'"); |
| 975 | |
| 976 | for (uint64_t i = 0; i != Count; ++i) { |
| 977 | switch (AF.getValueSize()) { |
| 978 | default: |
| 979 | assert(0 && "Invalid size!"); |
| 980 | case 1: MOW.Write8 (uint8_t (AF.getValue())); break; |
| 981 | case 2: MOW.Write16(uint16_t(AF.getValue())); break; |
| 982 | case 4: MOW.Write32(uint32_t(AF.getValue())); break; |
| 983 | case 8: MOW.Write64(uint64_t(AF.getValue())); break; |
| 984 | } |
| 985 | } |
| 986 | break; |
| 987 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 988 | |
| 989 | case MCFragment::FT_Data: |
| 990 | OS << cast<MCDataFragment>(F).getContents().str(); |
| 991 | break; |
| 992 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 993 | case MCFragment::FT_Fill: { |
| 994 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 995 | |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 996 | int64_t Value = 0; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 997 | if (!FF.getValue().isAbsolute()) |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 998 | Value = FF.getValue().getConstant(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 999 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
Daniel Dunbar | 3f6a960 | 2009-08-26 13:58:10 +0000 | [diff] [blame] | 1000 | if (!FF.getValue().isAbsolute()) { |
| 1001 | // Find the fixup. |
| 1002 | // |
| 1003 | // FIXME: Find a better way to write in the fixes. |
| 1004 | const MCSectionData::Fixup *Fixup = |
| 1005 | F.getParent()->LookupFixup(&F, i * FF.getValueSize()); |
| 1006 | assert(Fixup && "Missing fixup for fill value!"); |
| 1007 | Value = Fixup->FixedValue; |
| 1008 | } |
| 1009 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1010 | switch (FF.getValueSize()) { |
| 1011 | default: |
| 1012 | assert(0 && "Invalid size!"); |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1013 | case 1: MOW.Write8 (uint8_t (Value)); break; |
| 1014 | case 2: MOW.Write16(uint16_t(Value)); break; |
| 1015 | case 4: MOW.Write32(uint32_t(Value)); break; |
| 1016 | case 8: MOW.Write64(uint64_t(Value)); break; |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | break; |
| 1020 | } |
| 1021 | |
Daniel Dunbar | d6f761e | 2009-08-21 23:07:38 +0000 | [diff] [blame] | 1022 | case MCFragment::FT_Org: { |
| 1023 | MCOrgFragment &OF = cast<MCOrgFragment>(F); |
| 1024 | |
| 1025 | for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i) |
| 1026 | MOW.Write8(uint8_t(OF.getValue())); |
| 1027 | |
| 1028 | break; |
| 1029 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | assert(OS.tell() - Start == F.getFileSize()); |
| 1033 | } |
| 1034 | |
| 1035 | /// WriteFileData - Write the \arg SD data to the output file. |
| 1036 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 1037 | MachObjectWriter &MOW) { |
| 1038 | uint64_t Start = OS.tell(); |
| 1039 | (void) Start; |
| 1040 | |
| 1041 | for (MCSectionData::const_iterator it = SD.begin(), |
| 1042 | ie = SD.end(); it != ie; ++it) |
| 1043 | WriteFileData(OS, *it, MOW); |
| 1044 | |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1045 | // Add section padding. |
| 1046 | assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!"); |
| 1047 | MOW.WriteZeros(SD.getFileSize() - SD.getSize()); |
| 1048 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1049 | assert(OS.tell() - Start == SD.getFileSize()); |
| 1050 | } |
| 1051 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1052 | void MCAssembler::Finish() { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1053 | // Layout the sections and fragments. |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 1054 | uint64_t Address = 0; |
Daniel Dunbar | 6742e34 | 2009-08-26 04:13:32 +0000 | [diff] [blame] | 1055 | for (iterator it = begin(), ie = end(); it != ie;) { |
| 1056 | MCSectionData &SD = *it; |
| 1057 | |
| 1058 | // Select the amount of padding alignment we need, based on either the next |
| 1059 | // sections alignment or the default alignment. |
| 1060 | // |
| 1061 | // FIXME: This should probably match the native word size. |
| 1062 | unsigned NextAlign = 4; |
| 1063 | ++it; |
| 1064 | if (it != ie) |
| 1065 | NextAlign = it->getAlignment(); |
| 1066 | |
| 1067 | // Layout the section fragments and its size. |
| 1068 | SD.setAddress(Address); |
| 1069 | LayoutSection(SD, NextAlign); |
| 1070 | Address += SD.getFileSize(); |
Daniel Dunbar | 5e83596 | 2009-08-26 02:48:04 +0000 | [diff] [blame] | 1071 | } |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1072 | |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 1073 | // Write the object file. |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1074 | MachObjectWriter MOW(OS); |
Daniel Dunbar | f3d2ef0 | 2009-08-22 10:13:24 +0000 | [diff] [blame] | 1075 | MOW.WriteObject(*this); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame] | 1076 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 1077 | OS.flush(); |
| 1078 | } |