blob: 9d8c3c5600dde4ce2afeea384be02256855014ac [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- 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"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000011
12#include "llvm/ADT/Twine.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000013#include "llvm/MC/MCSectionMachO.h"
14#include "llvm/Support/DataTypes.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000016#include "llvm/Support/raw_ostream.h"
17#include "llvm/Target/TargetMachOWriterInfo.h"
18
19using namespace llvm;
20
21namespace {
22
23class MachObjectWriter {
24 // See <mach-o/loader.h>.
25 enum {
26 Header_Magic32 = 0xFEEDFACE,
27 Header_Magic64 = 0xFEEDFACF
28 };
29
30 static const unsigned Header32Size = 28;
31 static const unsigned Header64Size = 32;
32 static const unsigned SegmentLoadCommand32Size = 56;
33 static const unsigned Section32Size = 68;
34
35 enum HeaderFileType {
36 HFT_Object = 0x1
37 };
38
39 enum LoadCommandType {
40 LCT_Segment = 0x1
41 };
42
43 raw_ostream &OS;
44 bool IsLSB;
45
46public:
47 MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true)
48 : OS(_OS), IsLSB(_IsLSB) {
49 }
50
51 /// @name Helper Methods
52 /// @{
53
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000054 void Write8(uint8_t Value) {
55 OS << char(Value);
56 }
57
58 void Write16(uint16_t Value) {
59 if (IsLSB) {
60 Write8(uint8_t(Value >> 0));
61 Write8(uint8_t(Value >> 8));
62 } else {
63 Write8(uint8_t(Value >> 8));
64 Write8(uint8_t(Value >> 0));
65 }
66 }
67
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000068 void Write32(uint32_t Value) {
69 if (IsLSB) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000070 Write16(uint16_t(Value >> 0));
71 Write16(uint16_t(Value >> 16));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000072 } else {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000073 Write16(uint16_t(Value >> 16));
74 Write16(uint16_t(Value >> 0));
75 }
76 }
77
78 void Write64(uint64_t Value) {
79 if (IsLSB) {
80 Write32(uint32_t(Value >> 0));
81 Write32(uint32_t(Value >> 32));
82 } else {
83 Write32(uint32_t(Value >> 32));
84 Write32(uint32_t(Value >> 0));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000085 }
86 }
87
88 void WriteZeros(unsigned N) {
89 const char Zeros[16] = { 0 };
90
91 for (unsigned i = 0, e = N / 16; i != e; ++i)
92 OS << StringRef(Zeros, 16);
93
94 OS << StringRef(Zeros, N % 16);
95 }
96
97 void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) {
98 OS << Str;
99 if (ZeroFillSize)
100 WriteZeros(ZeroFillSize - Str.size());
101 }
102
103 /// @}
104
105 static unsigned getPrologSize32(unsigned NumSections) {
106 return Header32Size + SegmentLoadCommand32Size +
107 NumSections * Section32Size;
108 }
109
110 void WriteHeader32(unsigned NumSections) {
111 // struct mach_header (28 bytes)
112
113 uint64_t Start = OS.tell();
114 (void) Start;
115
116 Write32(Header_Magic32);
117
118 // FIXME: Support cputype.
119 Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386);
120
121 // FIXME: Support cpusubtype.
122 Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL);
123
124 Write32(HFT_Object);
125
126 // Object files have a single load command, the segment.
127 Write32(1);
128 Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
129 Write32(0); // Flags
130
131 assert(OS.tell() - Start == Header32Size);
132 }
133
134 void WriteLoadCommandHeader(uint32_t Cmd, uint32_t CmdSize) {
135 assert((CmdSize & 0x3) == 0 && "Invalid size!");
136
137 Write32(Cmd);
138 Write32(CmdSize);
139 }
140
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000141 /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command.
142 ///
143 /// \arg NumSections - The number of sections in this segment.
144 /// \arg SectionDataSize - The total size of the sections.
145 void WriteSegmentLoadCommand32(unsigned NumSections,
146 uint64_t SectionDataSize) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000147 // struct segment_command (56 bytes)
148
149 uint64_t Start = OS.tell();
150 (void) Start;
151
152 Write32(LCT_Segment);
153 Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
154
155 WriteString("", 16);
156 Write32(0); // vmaddr
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000157 Write32(SectionDataSize); // vmsize
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000158 Write32(Header32Size + SegmentLoadCommand32Size +
159 NumSections * Section32Size); // file offset
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000160 Write32(SectionDataSize); // file size
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000161 Write32(0x7); // maxprot
162 Write32(0x7); // initprot
163 Write32(NumSections);
164 Write32(0); // flags
165
166 assert(OS.tell() - Start == SegmentLoadCommand32Size);
167 }
168
169 void WriteSection32(const MCSectionData &SD) {
170 // struct section (68 bytes)
171
172 uint64_t Start = OS.tell();
173 (void) Start;
174
175 // FIXME: cast<> support!
176 const MCSectionMachO &Section =
177 static_cast<const MCSectionMachO&>(SD.getSection());
178 WriteString(Section.getSectionName(), 16);
179 WriteString(Section.getSegmentName(), 16);
180 Write32(0); // address
181 Write32(SD.getFileSize()); // size
182 Write32(SD.getFileOffset());
183
184 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
185 Write32(Log2_32(SD.getAlignment()));
186 Write32(0); // file offset of relocation entries
187 Write32(0); // number of relocation entrions
188 Write32(Section.getTypeAndAttributes());
189 Write32(0); // reserved1
190 Write32(Section.getStubSize()); // reserved2
191
192 assert(OS.tell() - Start == Section32Size);
193 }
194};
195
196}
197
198/* *** */
199
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000200MCFragment::MCFragment() : Kind(FragmentType(~0)) {
201}
202
203MCFragment::MCFragment(FragmentType _Kind, MCSectionData *SD)
204 : Kind(_Kind),
205 FileOffset(~UINT64_C(0)),
206 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000207{
208 if (SD)
209 SD->getFragmentList().push_back(this);
210}
211
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000212MCFragment::~MCFragment() {
213}
214
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000215/* *** */
216
217MCSectionData::MCSectionData() : Section(*(MCSection*)0) {}
218
219MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
220 : Section(_Section),
221 Alignment(1),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000222 FileOffset(~UINT64_C(0)),
223 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000224{
225 if (A)
226 A->getSectionList().push_back(this);
227}
228
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000229/* *** */
230
231MCAssembler::MCAssembler(raw_ostream &_OS) : OS(_OS) {}
232
233MCAssembler::~MCAssembler() {
234}
235
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000236void MCAssembler::LayoutSection(MCSectionData &SD) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000237 uint64_t FileOffset = SD.getFileOffset();
238 uint64_t SectionOffset = 0;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000239
240 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
241 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000242
243 F.setFileOffset(FileOffset);
244
245 // Evaluate fragment size.
246 switch (F.getKind()) {
247 case MCFragment::FT_Align: {
248 MCAlignFragment &AF = cast<MCAlignFragment>(F);
249
250 uint64_t AlignedOffset =
251 RoundUpToAlignment(SectionOffset, AF.getAlignment());
252 uint64_t PaddingBytes = AlignedOffset - SectionOffset;
253
254 if (PaddingBytes > AF.getMaxBytesToEmit())
255 AF.setFileSize(0);
256 else
257 AF.setFileSize(PaddingBytes);
258 break;
259 }
260
261 case MCFragment::FT_Data:
262 case MCFragment::FT_Fill:
263 F.setFileSize(F.getMaxFileSize());
264 break;
265
266 case MCFragment::FT_Org: {
267 MCOrgFragment &OF = cast<MCOrgFragment>(F);
268
269 if (!OF.getOffset().isAbsolute())
270 llvm_unreachable("FIXME: Not yet implemented!");
271 uint64_t OrgOffset = OF.getOffset().getConstant();
272
273 // FIXME: We need a way to communicate this error.
274 if (OrgOffset < SectionOffset)
275 llvm_report_error("invalid .org offset '" + Twine(OrgOffset) +
276 "' (section offset '" + Twine(SectionOffset) + "'");
277
278 F.setFileSize(OrgOffset - SectionOffset);
279 break;
280 }
281 }
282
283 FileOffset += F.getFileSize();
284 SectionOffset += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000285 }
286
287 // FIXME: Pad section?
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000288 SD.setFileSize(FileOffset - SD.getFileOffset());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000289}
290
291/// WriteFileData - Write the \arg F data to the output file.
292static void WriteFileData(raw_ostream &OS, const MCFragment &F,
293 MachObjectWriter &MOW) {
294 uint64_t Start = OS.tell();
295 (void) Start;
296
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000297 assert(F.getFileOffset() == Start && "Invalid file offset!");
298
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000299 // FIXME: Embed in fragments instead?
300 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000301 case MCFragment::FT_Align: {
302 MCAlignFragment &AF = cast<MCAlignFragment>(F);
303 uint64_t Count = AF.getFileSize() / AF.getValueSize();
304
305 // FIXME: This error shouldn't actually occur (the front end should emit
306 // multiple .align directives to enforce the semantics it wants), but is
307 // severe enough that we want to report it. How to handle this?
308 if (Count * AF.getValueSize() != AF.getFileSize())
309 llvm_report_error("undefined .align directive, value size '" +
310 Twine(AF.getValueSize()) +
311 "' is not a divisor of padding size '" +
312 Twine(AF.getFileSize()) + "'");
313
314 for (uint64_t i = 0; i != Count; ++i) {
315 switch (AF.getValueSize()) {
316 default:
317 assert(0 && "Invalid size!");
318 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
319 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
320 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
321 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
322 }
323 }
324 break;
325 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000326
327 case MCFragment::FT_Data:
328 OS << cast<MCDataFragment>(F).getContents().str();
329 break;
330
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000331 case MCFragment::FT_Fill: {
332 MCFillFragment &FF = cast<MCFillFragment>(F);
333
334 if (!FF.getValue().isAbsolute())
335 llvm_unreachable("FIXME: Not yet implemented!");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000336 int64_t Value = FF.getValue().getConstant();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000337
338 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
339 switch (FF.getValueSize()) {
340 default:
341 assert(0 && "Invalid size!");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000342 case 1: MOW.Write8 (uint8_t (Value)); break;
343 case 2: MOW.Write16(uint16_t(Value)); break;
344 case 4: MOW.Write32(uint32_t(Value)); break;
345 case 8: MOW.Write64(uint64_t(Value)); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000346 }
347 }
348 break;
349 }
350
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000351 case MCFragment::FT_Org: {
352 MCOrgFragment &OF = cast<MCOrgFragment>(F);
353
354 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
355 MOW.Write8(uint8_t(OF.getValue()));
356
357 break;
358 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000359 }
360
361 assert(OS.tell() - Start == F.getFileSize());
362}
363
364/// WriteFileData - Write the \arg SD data to the output file.
365static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
366 MachObjectWriter &MOW) {
367 uint64_t Start = OS.tell();
368 (void) Start;
369
370 for (MCSectionData::const_iterator it = SD.begin(),
371 ie = SD.end(); it != ie; ++it)
372 WriteFileData(OS, *it, MOW);
373
374 assert(OS.tell() - Start == SD.getFileSize());
375}
376
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000377void MCAssembler::Finish() {
378 unsigned NumSections = Sections.size();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000379
380 // Layout the sections and fragments.
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000381 uint64_t Offset = MachObjectWriter::getPrologSize32(NumSections);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000382 uint64_t SectionDataSize = 0;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000383 for (iterator it = begin(), ie = end(); it != ie; ++it) {
384 it->setFileOffset(Offset);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000385
386 LayoutSection(*it);
387
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000388 Offset += it->getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000389 SectionDataSize += it->getFileSize();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000390 }
391
392 MachObjectWriter MOW(OS);
393
394 // Write the prolog, starting with the header and load command...
395 MOW.WriteHeader32(NumSections);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000396 MOW.WriteSegmentLoadCommand32(NumSections, SectionDataSize);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000397
398 // ... and then the section headers.
399 for (iterator it = begin(), ie = end(); it != ie; ++it)
400 MOW.WriteSection32(*it);
401
402 // Finally, write the section data.
403 for (iterator it = begin(), ie = end(); it != ie; ++it)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000404 WriteFileData(OS, *it, MOW);
405
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000406 OS.flush();
407}