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