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