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 | |
| 10 | #include "llvm/MC/MCAssembler.h" |
| 11 | #include "llvm/MC/MCSectionMachO.h" |
| 12 | #include "llvm/Support/DataTypes.h" |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 13 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | #include "llvm/Target/TargetMachOWriterInfo.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class MachObjectWriter { |
| 22 | // See <mach-o/loader.h>. |
| 23 | enum { |
| 24 | Header_Magic32 = 0xFEEDFACE, |
| 25 | Header_Magic64 = 0xFEEDFACF |
| 26 | }; |
| 27 | |
| 28 | static const unsigned Header32Size = 28; |
| 29 | static const unsigned Header64Size = 32; |
| 30 | static const unsigned SegmentLoadCommand32Size = 56; |
| 31 | static const unsigned Section32Size = 68; |
| 32 | |
| 33 | enum HeaderFileType { |
| 34 | HFT_Object = 0x1 |
| 35 | }; |
| 36 | |
| 37 | enum LoadCommandType { |
| 38 | LCT_Segment = 0x1 |
| 39 | }; |
| 40 | |
| 41 | raw_ostream &OS; |
| 42 | bool IsLSB; |
| 43 | |
| 44 | public: |
| 45 | MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true) |
| 46 | : OS(_OS), IsLSB(_IsLSB) { |
| 47 | } |
| 48 | |
| 49 | /// @name Helper Methods |
| 50 | /// @{ |
| 51 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 52 | void Write8(uint8_t Value) { |
| 53 | OS << char(Value); |
| 54 | } |
| 55 | |
| 56 | void Write16(uint16_t Value) { |
| 57 | if (IsLSB) { |
| 58 | Write8(uint8_t(Value >> 0)); |
| 59 | Write8(uint8_t(Value >> 8)); |
| 60 | } else { |
| 61 | Write8(uint8_t(Value >> 8)); |
| 62 | Write8(uint8_t(Value >> 0)); |
| 63 | } |
| 64 | } |
| 65 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 66 | void Write32(uint32_t Value) { |
| 67 | if (IsLSB) { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 68 | Write16(uint16_t(Value >> 0)); |
| 69 | Write16(uint16_t(Value >> 16)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 70 | } else { |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 71 | Write16(uint16_t(Value >> 16)); |
| 72 | Write16(uint16_t(Value >> 0)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void Write64(uint64_t Value) { |
| 77 | if (IsLSB) { |
| 78 | Write32(uint32_t(Value >> 0)); |
| 79 | Write32(uint32_t(Value >> 32)); |
| 80 | } else { |
| 81 | Write32(uint32_t(Value >> 32)); |
| 82 | Write32(uint32_t(Value >> 0)); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void WriteZeros(unsigned N) { |
| 87 | const char Zeros[16] = { 0 }; |
| 88 | |
| 89 | for (unsigned i = 0, e = N / 16; i != e; ++i) |
| 90 | OS << StringRef(Zeros, 16); |
| 91 | |
| 92 | OS << StringRef(Zeros, N % 16); |
| 93 | } |
| 94 | |
| 95 | void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) { |
| 96 | OS << Str; |
| 97 | if (ZeroFillSize) |
| 98 | WriteZeros(ZeroFillSize - Str.size()); |
| 99 | } |
| 100 | |
| 101 | /// @} |
| 102 | |
| 103 | static unsigned getPrologSize32(unsigned NumSections) { |
| 104 | return Header32Size + SegmentLoadCommand32Size + |
| 105 | NumSections * Section32Size; |
| 106 | } |
| 107 | |
| 108 | void WriteHeader32(unsigned NumSections) { |
| 109 | // struct mach_header (28 bytes) |
| 110 | |
| 111 | uint64_t Start = OS.tell(); |
| 112 | (void) Start; |
| 113 | |
| 114 | Write32(Header_Magic32); |
| 115 | |
| 116 | // FIXME: Support cputype. |
| 117 | Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386); |
| 118 | |
| 119 | // FIXME: Support cpusubtype. |
| 120 | Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL); |
| 121 | |
| 122 | Write32(HFT_Object); |
| 123 | |
| 124 | // Object files have a single load command, the segment. |
| 125 | Write32(1); |
| 126 | Write32(SegmentLoadCommand32Size + NumSections * Section32Size); |
| 127 | Write32(0); // Flags |
| 128 | |
| 129 | assert(OS.tell() - Start == Header32Size); |
| 130 | } |
| 131 | |
| 132 | void WriteLoadCommandHeader(uint32_t Cmd, uint32_t CmdSize) { |
| 133 | assert((CmdSize & 0x3) == 0 && "Invalid size!"); |
| 134 | |
| 135 | Write32(Cmd); |
| 136 | Write32(CmdSize); |
| 137 | } |
| 138 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 139 | /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command. |
| 140 | /// |
| 141 | /// \arg NumSections - The number of sections in this segment. |
| 142 | /// \arg SectionDataSize - The total size of the sections. |
| 143 | void WriteSegmentLoadCommand32(unsigned NumSections, |
| 144 | uint64_t SectionDataSize) { |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 145 | // struct segment_command (56 bytes) |
| 146 | |
| 147 | uint64_t Start = OS.tell(); |
| 148 | (void) Start; |
| 149 | |
| 150 | Write32(LCT_Segment); |
| 151 | Write32(SegmentLoadCommand32Size + NumSections * Section32Size); |
| 152 | |
| 153 | WriteString("", 16); |
| 154 | Write32(0); // vmaddr |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 155 | Write32(SectionDataSize); // vmsize |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 156 | Write32(Header32Size + SegmentLoadCommand32Size + |
| 157 | NumSections * Section32Size); // file offset |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 158 | Write32(SectionDataSize); // file size |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 159 | Write32(0x7); // maxprot |
| 160 | Write32(0x7); // initprot |
| 161 | Write32(NumSections); |
| 162 | Write32(0); // flags |
| 163 | |
| 164 | assert(OS.tell() - Start == SegmentLoadCommand32Size); |
| 165 | } |
| 166 | |
| 167 | void WriteSection32(const MCSectionData &SD) { |
| 168 | // struct section (68 bytes) |
| 169 | |
| 170 | uint64_t Start = OS.tell(); |
| 171 | (void) Start; |
| 172 | |
| 173 | // FIXME: cast<> support! |
| 174 | const MCSectionMachO &Section = |
| 175 | static_cast<const MCSectionMachO&>(SD.getSection()); |
| 176 | WriteString(Section.getSectionName(), 16); |
| 177 | WriteString(Section.getSegmentName(), 16); |
| 178 | Write32(0); // address |
| 179 | Write32(SD.getFileSize()); // size |
| 180 | Write32(SD.getFileOffset()); |
| 181 | |
| 182 | assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!"); |
| 183 | Write32(Log2_32(SD.getAlignment())); |
| 184 | Write32(0); // file offset of relocation entries |
| 185 | Write32(0); // number of relocation entrions |
| 186 | Write32(Section.getTypeAndAttributes()); |
| 187 | Write32(0); // reserved1 |
| 188 | Write32(Section.getStubSize()); // reserved2 |
| 189 | |
| 190 | assert(OS.tell() - Start == Section32Size); |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | } |
| 195 | |
| 196 | /* *** */ |
| 197 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 198 | MCFragment::MCFragment() : Kind(FragmentType(~0)) { |
| 199 | } |
| 200 | |
| 201 | MCFragment::MCFragment(FragmentType _Kind, MCSectionData *SD) |
| 202 | : Kind(_Kind), |
| 203 | FileOffset(~UINT64_C(0)), |
| 204 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 205 | { |
| 206 | if (SD) |
| 207 | SD->getFragmentList().push_back(this); |
| 208 | } |
| 209 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 210 | MCFragment::~MCFragment() { |
| 211 | } |
| 212 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 213 | /* *** */ |
| 214 | |
| 215 | MCSectionData::MCSectionData() : Section(*(MCSection*)0) {} |
| 216 | |
| 217 | MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A) |
| 218 | : Section(_Section), |
| 219 | Alignment(1), |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 220 | FileOffset(~UINT64_C(0)), |
| 221 | FileSize(~UINT64_C(0)) |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 222 | { |
| 223 | if (A) |
| 224 | A->getSectionList().push_back(this); |
| 225 | } |
| 226 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 227 | /* *** */ |
| 228 | |
| 229 | MCAssembler::MCAssembler(raw_ostream &_OS) : OS(_OS) {} |
| 230 | |
| 231 | MCAssembler::~MCAssembler() { |
| 232 | } |
| 233 | |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 234 | void MCAssembler::LayoutSection(MCSectionData &SD) { |
| 235 | uint64_t Offset = SD.getFileOffset(); |
| 236 | |
| 237 | for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { |
| 238 | MCFragment &F = *it; |
| 239 | F.setFileOffset(Offset); |
| 240 | F.setFileSize(F.getMaxFileSize()); |
| 241 | Offset += F.getFileSize(); |
| 242 | } |
| 243 | |
| 244 | // FIXME: Pad section? |
| 245 | SD.setFileSize(Offset - SD.getFileOffset()); |
| 246 | } |
| 247 | |
| 248 | /// WriteFileData - Write the \arg F data to the output file. |
| 249 | static void WriteFileData(raw_ostream &OS, const MCFragment &F, |
| 250 | MachObjectWriter &MOW) { |
| 251 | uint64_t Start = OS.tell(); |
| 252 | (void) Start; |
| 253 | |
| 254 | // FIXME: Embed in fragments instead? |
| 255 | switch (F.getKind()) { |
| 256 | default: |
| 257 | assert(0 && "Invalid section kind!"); |
| 258 | |
| 259 | case MCFragment::FT_Data: |
| 260 | OS << cast<MCDataFragment>(F).getContents().str(); |
| 261 | break; |
| 262 | |
| 263 | case MCFragment::FT_Align: |
| 264 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 265 | |
| 266 | case MCFragment::FT_Fill: { |
| 267 | MCFillFragment &FF = cast<MCFillFragment>(F); |
| 268 | |
| 269 | if (!FF.getValue().isAbsolute()) |
| 270 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 271 | |
| 272 | for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) { |
| 273 | switch (FF.getValueSize()) { |
| 274 | default: |
| 275 | assert(0 && "Invalid size!"); |
| 276 | case 1: MOW.Write8 (uint8_t (FF.getValue().getConstant())); break; |
| 277 | case 2: MOW.Write16(uint16_t(FF.getValue().getConstant())); break; |
| 278 | case 4: MOW.Write32(uint32_t(FF.getValue().getConstant())); break; |
| 279 | case 8: MOW.Write64(uint64_t(FF.getValue().getConstant())); break; |
| 280 | } |
| 281 | } |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | case MCFragment::FT_Org: |
| 286 | llvm_unreachable("FIXME: Not yet implemented!"); |
| 287 | } |
| 288 | |
| 289 | assert(OS.tell() - Start == F.getFileSize()); |
| 290 | } |
| 291 | |
| 292 | /// WriteFileData - Write the \arg SD data to the output file. |
| 293 | static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, |
| 294 | MachObjectWriter &MOW) { |
| 295 | uint64_t Start = OS.tell(); |
| 296 | (void) Start; |
| 297 | |
| 298 | for (MCSectionData::const_iterator it = SD.begin(), |
| 299 | ie = SD.end(); it != ie; ++it) |
| 300 | WriteFileData(OS, *it, MOW); |
| 301 | |
| 302 | assert(OS.tell() - Start == SD.getFileSize()); |
| 303 | } |
| 304 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 305 | void MCAssembler::Finish() { |
| 306 | unsigned NumSections = Sections.size(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 307 | |
| 308 | // Layout the sections and fragments. |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 309 | uint64_t Offset = MachObjectWriter::getPrologSize32(NumSections); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 310 | uint64_t SectionDataSize = 0; |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 311 | for (iterator it = begin(), ie = end(); it != ie; ++it) { |
| 312 | it->setFileOffset(Offset); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 313 | |
| 314 | LayoutSection(*it); |
| 315 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 316 | Offset += it->getFileSize(); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 317 | SectionDataSize += it->getFileSize(); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | MachObjectWriter MOW(OS); |
| 321 | |
| 322 | // Write the prolog, starting with the header and load command... |
| 323 | MOW.WriteHeader32(NumSections); |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 324 | MOW.WriteSegmentLoadCommand32(NumSections, SectionDataSize); |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 325 | |
| 326 | // ... and then the section headers. |
| 327 | for (iterator it = begin(), ie = end(); it != ie; ++it) |
| 328 | MOW.WriteSection32(*it); |
| 329 | |
| 330 | // Finally, write the section data. |
| 331 | for (iterator it = begin(), ie = end(); it != ie; ++it) |
Daniel Dunbar | 0705fbf | 2009-08-21 18:29:01 +0000 | [diff] [blame^] | 332 | WriteFileData(OS, *it, MOW); |
| 333 | |
Daniel Dunbar | fb4a6b3 | 2009-08-21 09:11:24 +0000 | [diff] [blame] | 334 | OS.flush(); |
| 335 | } |