blob: ad42364dff71fd32ab58cca948f11843e52d2953 [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
Daniel Dunbar0adcd352009-08-25 21:10:45 +000010#define DEBUG_TYPE "assembler"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +000012#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000013#include "llvm/MC/MCExpr.h"
Chris Lattnerbea2c952009-08-22 19:19:12 +000014#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000015#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +000017#include "llvm/ADT/DenseMap.h"
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000018#include "llvm/ADT/SmallString.h"
Daniel Dunbar0adcd352009-08-25 21:10:45 +000019#include "llvm/ADT/Statistic.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000021#include "llvm/ADT/StringMap.h"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000022#include "llvm/ADT/Twine.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner45f8c092010-02-02 19:38:14 +000024#include "llvm/Support/MachO.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000026#include "llvm/Support/Debug.h"
Daniel Dunbaree0d8922010-03-13 22:10:17 +000027#include "llvm/Target/TargetRegistry.h"
Daniel Dunbardf3c8f22010-03-12 21:00:49 +000028#include "llvm/Target/TargetAsmBackend.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000029
30// FIXME: Gross.
31#include "../Target/X86/X86FixupKinds.h"
32
Chris Lattner23132b12009-08-24 03:52:50 +000033#include <vector>
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000034using namespace llvm;
35
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000036class MachObjectWriter;
37
Daniel Dunbar0adcd352009-08-25 21:10:45 +000038STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
39
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000040// FIXME FIXME FIXME: There are number of places in this file where we convert
41// what is a 64-bit assembler value used for computation into a value in the
42// object file, which may truncate it. We should detect that truncation where
43// invalid and report errors back.
44
Daniel Dunbarbdd92812010-03-19 09:28:55 +000045class MCObjectWriter;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000046static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
Daniel Dunbarbdd92812010-03-19 09:28:55 +000047 MCObjectWriter *MOW);
Kevin Enderby6e720482010-02-23 18:26:34 +000048
Daniel Dunbard5a8e982009-08-28 05:49:21 +000049/// isVirtualSection - Check if this is a section which does not actually exist
50/// in the object file.
51static bool isVirtualSection(const MCSection &Section) {
52 // FIXME: Lame.
53 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
Daniel Dunbar99d22ad2010-03-15 21:56:38 +000054 return (SMO.getType() == MCSectionMachO::S_ZEROFILL);
Daniel Dunbard5a8e982009-08-28 05:49:21 +000055}
56
Duncan Sands9a7795c2010-02-17 14:52:22 +000057static unsigned getFixupKindLog2Size(unsigned Kind) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +000058 switch (Kind) {
59 default: llvm_unreachable("invalid fixup kind!");
Daniel Dunbarf6346762010-02-13 09:29:02 +000060 case X86::reloc_pcrel_1byte:
Daniel Dunbar2be2fd02010-02-13 09:28:54 +000061 case FK_Data_1: return 0;
62 case FK_Data_2: return 1;
Daniel Dunbarf6346762010-02-13 09:29:02 +000063 case X86::reloc_pcrel_4byte:
64 case X86::reloc_riprel_4byte:
Daniel Dunbar2be2fd02010-02-13 09:28:54 +000065 case FK_Data_4: return 2;
66 case FK_Data_8: return 3;
67 }
68}
69
Duncan Sands9a7795c2010-02-17 14:52:22 +000070static bool isFixupKindPCRel(unsigned Kind) {
Daniel Dunbar591047f2010-02-13 09:45:59 +000071 switch (Kind) {
72 default:
73 return false;
74 case X86::reloc_pcrel_1byte:
75 case X86::reloc_pcrel_4byte:
76 case X86::reloc_riprel_4byte:
77 return true;
78 }
79}
80
Daniel Dunbarbdd92812010-03-19 09:28:55 +000081class MCObjectWriter {
82 MCObjectWriter(const MCObjectWriter &); // DO NOT IMPLEMENT
83 void operator=(const MCObjectWriter &); // DO NOT IMPLEMENT
84
85protected:
86 raw_ostream &OS;
87
88 unsigned IsLittleEndian : 1;
89
90protected: // Can only create subclasses.
91 MCObjectWriter(raw_ostream &_OS, bool _IsLittleEndian)
92 : OS(_OS), IsLittleEndian(_IsLittleEndian) {}
93 virtual ~MCObjectWriter();
94
95public:
96
97 bool isLittleEndian() { return IsLittleEndian; }
98
99 raw_ostream &getStream() { return OS; }
100
101 /// @name Binary Output Methods
102 /// @{
103
104 void Write8(uint8_t Value) {
105 OS << char(Value);
106 }
107
108 void WriteLE16(uint16_t Value) {
109 Write8(uint8_t(Value >> 0));
110 Write8(uint8_t(Value >> 8));
111 }
112
113 void WriteLE32(uint32_t Value) {
114 WriteLE16(uint16_t(Value >> 0));
115 WriteLE16(uint16_t(Value >> 16));
116 }
117
118 void WriteLE64(uint64_t Value) {
119 WriteLE32(uint32_t(Value >> 0));
120 WriteLE32(uint32_t(Value >> 32));
121 }
122
123 void WriteBE16(uint16_t Value) {
124 Write8(uint8_t(Value >> 8));
125 Write8(uint8_t(Value >> 0));
126 }
127
128 void WriteBE32(uint32_t Value) {
129 WriteBE16(uint16_t(Value >> 16));
130 WriteBE16(uint16_t(Value >> 0));
131 }
132
133 void WriteBE64(uint64_t Value) {
134 WriteBE32(uint32_t(Value >> 32));
135 WriteBE32(uint32_t(Value >> 0));
136 }
137
138 void Write16(uint16_t Value) {
139 if (IsLittleEndian)
140 WriteLE16(Value);
141 else
142 WriteBE16(Value);
143 }
144
145 void Write32(uint32_t Value) {
146 if (IsLittleEndian)
147 WriteLE32(Value);
148 else
149 WriteBE32(Value);
150 }
151
152 void Write64(uint64_t Value) {
153 if (IsLittleEndian)
154 WriteLE64(Value);
155 else
156 WriteBE64(Value);
157 }
158
159 void WriteZeros(unsigned N) {
160 const char Zeros[16] = { 0 };
161
162 for (unsigned i = 0, e = N / 16; i != e; ++i)
163 OS << StringRef(Zeros, 16);
164
165 OS << StringRef(Zeros, N % 16);
166 }
167
168 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
169 OS << Str;
170 if (ZeroFillSize)
171 WriteZeros(ZeroFillSize - Str.size());
172 }
173
174 /// @}
175};
176
177MCObjectWriter::~MCObjectWriter() {
178}
179
180class MachObjectWriter : public MCObjectWriter {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000181 // See <mach-o/loader.h>.
182 enum {
183 Header_Magic32 = 0xFEEDFACE,
184 Header_Magic64 = 0xFEEDFACF
185 };
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000186
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000187 enum {
188 Header32Size = 28,
189 Header64Size = 32,
190 SegmentLoadCommand32Size = 56,
191 SegmentLoadCommand64Size = 72,
192 Section32Size = 68,
193 Section64Size = 80,
194 SymtabLoadCommandSize = 24,
195 DysymtabLoadCommandSize = 80,
196 Nlist32Size = 12,
197 Nlist64Size = 16,
198 RelocationInfoSize = 8
199 };
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000200
201 enum HeaderFileType {
202 HFT_Object = 0x1
203 };
204
Daniel Dunbar6009db42009-08-26 21:22:22 +0000205 enum HeaderFlags {
206 HF_SubsectionsViaSymbols = 0x2000
207 };
208
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000209 enum LoadCommandType {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000210 LCT_Segment = 0x1,
211 LCT_Symtab = 0x2,
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000212 LCT_Dysymtab = 0xb,
213 LCT_Segment64 = 0x19
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000214 };
215
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000216 // See <mach-o/nlist.h>.
217 enum SymbolTypeType {
218 STT_Undefined = 0x00,
219 STT_Absolute = 0x02,
220 STT_Section = 0x0e
221 };
222
223 enum SymbolTypeFlags {
224 // If any of these bits are set, then the entry is a stab entry number (see
225 // <mach-o/stab.h>. Otherwise the other masks apply.
226 STF_StabsEntryMask = 0xe0,
227
228 STF_TypeMask = 0x0e,
229 STF_External = 0x01,
230 STF_PrivateExtern = 0x10
231 };
232
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000233 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
234 /// symbol entry.
235 enum IndirectSymbolFlags {
236 ISF_Local = 0x80000000,
237 ISF_Absolute = 0x40000000
238 };
239
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000240 /// RelocationFlags - Special flags for addresses.
241 enum RelocationFlags {
242 RF_Scattered = 0x80000000
243 };
244
245 enum RelocationInfoType {
246 RIT_Vanilla = 0,
247 RIT_Pair = 1,
248 RIT_Difference = 2,
249 RIT_PreboundLazyPointer = 3,
250 RIT_LocalDifference = 4
251 };
252
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000253 /// MachSymbolData - Helper struct for containing some precomputed information
254 /// on symbols.
255 struct MachSymbolData {
256 MCSymbolData *SymbolData;
257 uint64_t StringIndex;
258 uint8_t SectionIndex;
259
260 // Support lexicographic sorting.
261 bool operator<(const MachSymbolData &RHS) const {
262 const std::string &Name = SymbolData->getSymbol().getName();
263 return Name < RHS.SymbolData->getSymbol().getName();
264 }
265 };
266
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000267 unsigned Is64Bit : 1;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000268
Daniel Dunbar17a06502010-03-19 07:09:18 +0000269 /// @name Relocation Data
270 /// @{
271
272 struct MachRelocationEntry {
273 uint32_t Word0;
274 uint32_t Word1;
275 };
276
277 llvm::DenseMap<const MCSectionData*,
278 std::vector<MachRelocationEntry> > Relocations;
279
280 /// @}
281 /// @name Symbol Table Data
282
283 SmallString<256> StringTable;
284 std::vector<MachSymbolData> LocalSymbolData;
285 std::vector<MachSymbolData> ExternalSymbolData;
286 std::vector<MachSymbolData> UndefinedSymbolData;
287
288 /// @}
289
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000290public:
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000291 MachObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool _IsLittleEndian = true)
292 : MCObjectWriter(_OS, _IsLittleEndian), Is64Bit(_Is64Bit) {
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000293 }
294
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000295 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
296 bool SubsectionsViaSymbols) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000297 uint32_t Flags = 0;
298
299 if (SubsectionsViaSymbols)
300 Flags |= HF_SubsectionsViaSymbols;
301
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000302 // struct mach_header (28 bytes) or
303 // struct mach_header_64 (32 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000304
305 uint64_t Start = OS.tell();
306 (void) Start;
307
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000308 Write32(Is64Bit ? Header_Magic64 : Header_Magic32);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000309
310 // FIXME: Support cputype.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000311 Write32(Is64Bit ? MachO::CPUTypeX86_64 : MachO::CPUTypeI386);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000312 // FIXME: Support cpusubtype.
Chris Lattner45f8c092010-02-02 19:38:14 +0000313 Write32(MachO::CPUSubType_I386_ALL);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000314 Write32(HFT_Object);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000315 Write32(NumLoadCommands); // Object files have a single load command, the
316 // segment.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000317 Write32(LoadCommandsSize);
Daniel Dunbar6009db42009-08-26 21:22:22 +0000318 Write32(Flags);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000319 if (Is64Bit)
320 Write32(0); // reserved
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000321
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000322 assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000323 }
324
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000325 /// WriteSegmentLoadCommand - Write a segment load command.
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000326 ///
327 /// \arg NumSections - The number of sections in this segment.
328 /// \arg SectionDataSize - The total size of the sections.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000329 void WriteSegmentLoadCommand(unsigned NumSections,
330 uint64_t VMSize,
331 uint64_t SectionDataStartOffset,
332 uint64_t SectionDataSize) {
333 // struct segment_command (56 bytes) or
334 // struct segment_command_64 (72 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000335
336 uint64_t Start = OS.tell();
337 (void) Start;
338
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000339 unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size :
340 SegmentLoadCommand32Size;
341 Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
342 Write32(SegmentLoadCommandSize +
343 NumSections * (Is64Bit ? Section64Size : Section32Size));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000344
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000345 WriteBytes("", 16);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000346 if (Is64Bit) {
347 Write64(0); // vmaddr
348 Write64(VMSize); // vmsize
349 Write64(SectionDataStartOffset); // file offset
350 Write64(SectionDataSize); // file size
351 } else {
352 Write32(0); // vmaddr
353 Write32(VMSize); // vmsize
354 Write32(SectionDataStartOffset); // file offset
355 Write32(SectionDataSize); // file size
356 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000357 Write32(0x7); // maxprot
358 Write32(0x7); // initprot
359 Write32(NumSections);
360 Write32(0); // flags
361
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000362 assert(OS.tell() - Start == SegmentLoadCommandSize);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000363 }
364
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000365 void WriteSection(const MCSectionData &SD, uint64_t FileOffset,
366 uint64_t RelocationsStart, unsigned NumRelocations) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000367 // The offset is unused for virtual sections.
368 if (isVirtualSection(SD.getSection())) {
369 assert(SD.getFileSize() == 0 && "Invalid file size!");
370 FileOffset = 0;
371 }
372
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000373 // struct section (68 bytes) or
374 // struct section_64 (80 bytes)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000375
376 uint64_t Start = OS.tell();
377 (void) Start;
378
379 // FIXME: cast<> support!
380 const MCSectionMachO &Section =
381 static_cast<const MCSectionMachO&>(SD.getSection());
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000382 WriteBytes(Section.getSectionName(), 16);
383 WriteBytes(Section.getSegmentName(), 16);
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000384 if (Is64Bit) {
385 Write64(SD.getAddress()); // address
386 Write64(SD.getSize()); // size
387 } else {
388 Write32(SD.getAddress()); // address
389 Write32(SD.getSize()); // size
390 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000391 Write32(FileOffset);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000392
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000393 unsigned Flags = Section.getTypeAndAttributes();
394 if (SD.hasInstructions())
395 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
396
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000397 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
398 Write32(Log2_32(SD.getAlignment()));
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000399 Write32(NumRelocations ? RelocationsStart : 0);
400 Write32(NumRelocations);
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000401 Write32(Flags);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000402 Write32(0); // reserved1
403 Write32(Section.getStubSize()); // reserved2
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000404 if (Is64Bit)
405 Write32(0); // reserved3
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000406
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000407 assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000408 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000409
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000410 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
411 uint32_t StringTableOffset,
412 uint32_t StringTableSize) {
413 // struct symtab_command (24 bytes)
414
415 uint64_t Start = OS.tell();
416 (void) Start;
417
418 Write32(LCT_Symtab);
419 Write32(SymtabLoadCommandSize);
420 Write32(SymbolOffset);
421 Write32(NumSymbols);
422 Write32(StringTableOffset);
423 Write32(StringTableSize);
424
425 assert(OS.tell() - Start == SymtabLoadCommandSize);
426 }
427
428 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
429 uint32_t NumLocalSymbols,
430 uint32_t FirstExternalSymbol,
431 uint32_t NumExternalSymbols,
432 uint32_t FirstUndefinedSymbol,
433 uint32_t NumUndefinedSymbols,
434 uint32_t IndirectSymbolOffset,
435 uint32_t NumIndirectSymbols) {
436 // struct dysymtab_command (80 bytes)
437
438 uint64_t Start = OS.tell();
439 (void) Start;
440
441 Write32(LCT_Dysymtab);
442 Write32(DysymtabLoadCommandSize);
443 Write32(FirstLocalSymbol);
444 Write32(NumLocalSymbols);
445 Write32(FirstExternalSymbol);
446 Write32(NumExternalSymbols);
447 Write32(FirstUndefinedSymbol);
448 Write32(NumUndefinedSymbols);
449 Write32(0); // tocoff
450 Write32(0); // ntoc
451 Write32(0); // modtaboff
452 Write32(0); // nmodtab
453 Write32(0); // extrefsymoff
454 Write32(0); // nextrefsyms
455 Write32(IndirectSymbolOffset);
456 Write32(NumIndirectSymbols);
457 Write32(0); // extreloff
458 Write32(0); // nextrel
459 Write32(0); // locreloff
460 Write32(0); // nlocrel
461
462 assert(OS.tell() - Start == DysymtabLoadCommandSize);
463 }
464
Daniel Dunbar5691e742010-03-13 22:49:35 +0000465 void WriteNlist(MachSymbolData &MSD) {
Daniel Dunbar5e835962009-08-26 02:48:04 +0000466 MCSymbolData &Data = *MSD.SymbolData;
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000467 const MCSymbol &Symbol = Data.getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000468 uint8_t Type = 0;
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000469 uint16_t Flags = Data.getFlags();
470 uint32_t Address = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000471
472 // Set the N_TYPE bits. See <mach-o/nlist.h>.
473 //
474 // FIXME: Are the prebound or indirect fields possible here?
475 if (Symbol.isUndefined())
476 Type = STT_Undefined;
477 else if (Symbol.isAbsolute())
478 Type = STT_Absolute;
479 else
480 Type = STT_Section;
481
482 // FIXME: Set STAB bits.
483
Daniel Dunbar5e835962009-08-26 02:48:04 +0000484 if (Data.isPrivateExtern())
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000485 Type |= STF_PrivateExtern;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000486
487 // Set external bit.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000488 if (Data.isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000489 Type |= STF_External;
490
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000491 // Compute the symbol address.
492 if (Symbol.isDefined()) {
493 if (Symbol.isAbsolute()) {
494 llvm_unreachable("FIXME: Not yet implemented!");
495 } else {
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000496 Address = Data.getAddress();
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000497 }
498 } else if (Data.isCommon()) {
499 // Common symbols are encoded with the size in the address
500 // field, and their alignment in the flags.
501 Address = Data.getCommonSize();
502
503 // Common alignment is packed into the 'desc' bits.
504 if (unsigned Align = Data.getCommonAlignment()) {
505 unsigned Log2Size = Log2_32(Align);
506 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
507 if (Log2Size > 15)
508 llvm_report_error("invalid 'common' alignment '" +
509 Twine(Align) + "'");
510 // FIXME: Keep this mask with the SymbolFlags enumeration.
511 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
512 }
513 }
514
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000515 // struct nlist (12 bytes)
516
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000517 Write32(MSD.StringIndex);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000518 Write8(Type);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000519 Write8(MSD.SectionIndex);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000520
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000521 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
522 // value.
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000523 Write16(Flags);
Daniel Dunbar5691e742010-03-13 22:49:35 +0000524 if (Is64Bit)
525 Write64(Address);
526 else
527 Write32(Address);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000528 }
529
Daniel Dunbar17a06502010-03-19 07:09:18 +0000530 void RecordScatteredRelocation(MCAssembler &Asm, MCFragment &Fragment,
531 const MCAsmFixup &Fixup, MCValue Target,
532 uint64_t &FixedValue) {
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000533 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbare180fa92010-03-09 21:27:30 +0000534 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
Daniel Dunbareb3804e2010-02-17 23:45:16 +0000535 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000536 unsigned Type = RIT_Vanilla;
537
538 // See <reloc.h>.
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000539 const MCSymbol *A = &Target.getSymA()->getSymbol();
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000540 MCSymbolData *A_SD = &Asm.getSymbolData(*A);
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000541
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000542 if (!A_SD->getFragment())
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000543 llvm_report_error("symbol '" + A->getName() +
544 "' can not be undefined in a subtraction expression");
545
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000546 uint32_t Value = A_SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000547 uint32_t Value2 = 0;
548
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000549 if (const MCSymbolRefExpr *B = Target.getSymB()) {
550 MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000551
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000552 if (!B_SD->getFragment())
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000553 llvm_report_error("symbol '" + B->getSymbol().getName() +
Daniel Dunbar0ce6bd52010-03-08 21:10:39 +0000554 "' can not be undefined in a subtraction expression");
555
Daniel Dunbar07a96412010-03-10 02:10:29 +0000556 // Select the appropriate difference relocation type.
557 //
558 // Note that there is no longer any semantic difference between these two
559 // relocation types from the linkers point of view, this is done solely
560 // for pedantic compatibility with 'as'.
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000561 Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference;
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000562 Value2 = B_SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000563 }
564
Daniel Dunbar17a06502010-03-19 07:09:18 +0000565 // Relocations are written out in reverse order, so the PAIR comes first.
Daniel Dunbara015c1c2010-03-10 00:58:25 +0000566 if (Type == RIT_Difference || Type == RIT_LocalDifference) {
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000567 MachRelocationEntry MRE;
568 MRE.Word0 = ((0 << 0) |
Daniel Dunbaraef9d7a2010-03-09 21:27:47 +0000569 (RIT_Pair << 24) |
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000570 (Log2Size << 28) |
Daniel Dunbaraef9d7a2010-03-09 21:27:47 +0000571 (IsPCRel << 30) |
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000572 RF_Scattered);
573 MRE.Word1 = Value2;
Daniel Dunbar17a06502010-03-19 07:09:18 +0000574 Relocations[Fragment.getParent()].push_back(MRE);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000575 }
Daniel Dunbar17a06502010-03-19 07:09:18 +0000576
577 MachRelocationEntry MRE;
578 MRE.Word0 = ((Address << 0) |
579 (Type << 24) |
580 (Log2Size << 28) |
581 (IsPCRel << 30) |
582 RF_Scattered);
583 MRE.Word1 = Value;
584 Relocations[Fragment.getParent()].push_back(MRE);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000585 }
586
Daniel Dunbar17a06502010-03-19 07:09:18 +0000587 void RecordRelocation(MCAssembler &Asm, MCDataFragment &Fragment,
588 const MCAsmFixup &Fixup, MCValue Target,
589 uint64_t &FixedValue) {
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000590 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
591 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
592
Daniel Dunbarf3a066f2010-03-09 21:27:58 +0000593 // If this is a difference or a defined symbol plus an offset, then we need
594 // a scattered relocation entry.
595 uint32_t Offset = Target.getConstant();
596 if (IsPCRel)
597 Offset += 1 << Log2Size;
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000598 if (Target.getSymB() ||
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000599 (Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() &&
Daniel Dunbar17a06502010-03-19 07:09:18 +0000600 Offset)) {
601 RecordScatteredRelocation(Asm, Fragment, Fixup, Target, FixedValue);
602 return;
603 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000604
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000605 // See <reloc.h>.
Daniel Dunbarcb7d7432010-02-11 21:29:46 +0000606 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000607 uint32_t Value = 0;
608 unsigned Index = 0;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000609 unsigned IsExtern = 0;
610 unsigned Type = 0;
611
Daniel Dunbar1253a6f2009-10-16 01:58:03 +0000612 if (Target.isAbsolute()) { // constant
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000613 // SymbolNum of 0 indicates the absolute section.
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000614 //
Daniel Dunbar979ba5b2010-03-11 05:53:37 +0000615 // FIXME: Currently, these are never generated (see code below). I cannot
616 // find a case where they are actually emitted.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000617 Type = RIT_Vanilla;
618 Value = 0;
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000619 } else {
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000620 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000621 MCSymbolData *SD = &Asm.getSymbolData(*Symbol);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000622
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000623 if (Symbol->isUndefined()) {
624 IsExtern = 1;
625 Index = SD->getIndex();
626 Value = 0;
627 } else {
628 // The index is the section ordinal.
629 //
630 // FIXME: O(N)
631 Index = 1;
Daniel Dunbar591047f2010-02-13 09:45:59 +0000632 MCAssembler::iterator it = Asm.begin(), ie = Asm.end();
633 for (; it != ie; ++it, ++Index)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000634 if (&*it == SD->getFragment()->getParent())
635 break;
Daniel Dunbar591047f2010-02-13 09:45:59 +0000636 assert(it != ie && "Unable to find section index!");
Daniel Dunbar8f0448c2010-03-11 18:22:51 +0000637 Value = SD->getAddress();
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000638 }
639
640 Type = RIT_Vanilla;
641 }
642
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000643 // struct relocation_info (8 bytes)
644 MachRelocationEntry MRE;
645 MRE.Word0 = Address;
646 MRE.Word1 = ((Index << 0) |
647 (IsPCRel << 24) |
648 (Log2Size << 25) |
649 (IsExtern << 27) |
650 (Type << 28));
Daniel Dunbar17a06502010-03-19 07:09:18 +0000651 Relocations[Fragment.getParent()].push_back(MRE);
652 }
653
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000654 void BindIndirectSymbols(MCAssembler &Asm) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000655 // This is the point where 'as' creates actual symbols for indirect symbols
656 // (in the following two passes). It would be easier for us to do this
657 // sooner when we see the attribute, but that makes getting the order in the
658 // symbol table much more complicated than it is worth.
659 //
660 // FIXME: Revisit this when the dust settles.
661
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000662 // Bind non lazy symbol pointers first.
663 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
664 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
665 // FIXME: cast<> support!
666 const MCSectionMachO &Section =
667 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
668
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000669 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000670 continue;
671
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000672 Asm.getOrCreateSymbolData(*it->Symbol);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000673 }
674
675 // Then lazy symbol pointers and symbol stubs.
676 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
677 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
678 // FIXME: cast<> support!
679 const MCSectionMachO &Section =
680 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
681
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000682 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
683 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000684 continue;
685
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000686 // Set the symbol type to undefined lazy, but only on construction.
687 //
688 // FIXME: Do not hardcode.
689 bool Created;
690 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
691 if (Created)
692 Entry.setFlags(Entry.getFlags() | 0x0001);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000693 }
694 }
695
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000696 /// ComputeSymbolTable - Compute the symbol table data
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000697 ///
698 /// \param StringTable [out] - The string table data.
699 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
700 /// string table.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000701 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
702 std::vector<MachSymbolData> &LocalSymbolData,
703 std::vector<MachSymbolData> &ExternalSymbolData,
704 std::vector<MachSymbolData> &UndefinedSymbolData) {
705 // Build section lookup table.
706 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
707 unsigned Index = 1;
708 for (MCAssembler::iterator it = Asm.begin(),
709 ie = Asm.end(); it != ie; ++it, ++Index)
710 SectionIndexMap[&it->getSection()] = Index;
711 assert(Index <= 256 && "Too many sections!");
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000712
713 // Index 0 is always the empty string.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000714 StringMap<uint64_t> StringIndexMap;
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000715 StringTable += '\x00';
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000716
717 // Build the symbol arrays and the string table, but only for non-local
718 // symbols.
719 //
720 // The particular order that we collect the symbols and create the string
721 // table, then sort the symbols is chosen to match 'as'. Even though it
722 // doesn't matter for correctness, this is important for letting us diff .o
723 // files.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000724 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
725 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000726 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000727
Daniel Dunbar23869852010-03-19 03:18:09 +0000728 // Ignore non-linker visible symbols.
729 if (!Asm.isSymbolLinkerVisible(it))
Daniel Dunbar959fd882009-08-26 22:13:22 +0000730 continue;
731
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000732 if (!it->isExternal() && !Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000733 continue;
734
735 uint64_t &Entry = StringIndexMap[Symbol.getName()];
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000736 if (!Entry) {
737 Entry = StringTable.size();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000738 StringTable += Symbol.getName();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000739 StringTable += '\x00';
740 }
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000741
742 MachSymbolData MSD;
743 MSD.SymbolData = it;
744 MSD.StringIndex = Entry;
745
746 if (Symbol.isUndefined()) {
747 MSD.SectionIndex = 0;
748 UndefinedSymbolData.push_back(MSD);
749 } else if (Symbol.isAbsolute()) {
750 MSD.SectionIndex = 0;
751 ExternalSymbolData.push_back(MSD);
752 } else {
753 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
754 assert(MSD.SectionIndex && "Invalid section index!");
755 ExternalSymbolData.push_back(MSD);
756 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000757 }
758
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000759 // Now add the data for local symbols.
760 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
761 ie = Asm.symbol_end(); it != ie; ++it) {
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000762 const MCSymbol &Symbol = it->getSymbol();
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000763
Daniel Dunbar23869852010-03-19 03:18:09 +0000764 // Ignore non-linker visible symbols.
765 if (!Asm.isSymbolLinkerVisible(it))
Daniel Dunbar959fd882009-08-26 22:13:22 +0000766 continue;
767
Daniel Dunbar50e48b32009-08-24 08:39:57 +0000768 if (it->isExternal() || Symbol.isUndefined())
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000769 continue;
770
771 uint64_t &Entry = StringIndexMap[Symbol.getName()];
772 if (!Entry) {
773 Entry = StringTable.size();
774 StringTable += Symbol.getName();
775 StringTable += '\x00';
776 }
777
778 MachSymbolData MSD;
779 MSD.SymbolData = it;
780 MSD.StringIndex = Entry;
781
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000782 if (Symbol.isAbsolute()) {
783 MSD.SectionIndex = 0;
784 LocalSymbolData.push_back(MSD);
785 } else {
786 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
787 assert(MSD.SectionIndex && "Invalid section index!");
788 LocalSymbolData.push_back(MSD);
789 }
790 }
791
792 // External and undefined symbols are required to be in lexicographic order.
793 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
794 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
795
Daniel Dunbarbe963552009-08-26 13:57:54 +0000796 // Set the symbol indices.
797 Index = 0;
798 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
799 LocalSymbolData[i].SymbolData->setIndex(Index++);
800 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
801 ExternalSymbolData[i].SymbolData->setIndex(Index++);
802 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
803 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
804
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000805 // The string table is padded to a multiple of 4.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000806 while (StringTable.size() % 4)
807 StringTable += '\x00';
808 }
809
Daniel Dunbarbacba992010-03-19 07:09:33 +0000810 void ExecutePostLayoutBinding(MCAssembler &Asm) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000811 // Create symbol data for any indirect symbols.
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000812 BindIndirectSymbols(Asm);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000813
Daniel Dunbarbacba992010-03-19 07:09:33 +0000814 // Compute symbol table information and bind symbol indices.
815 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
816 UndefinedSymbolData);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000817 }
818
819 void WriteObject(const MCAssembler &Asm) {
820 unsigned NumSections = Asm.size();
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000821
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000822 // The section data starts after the header, the segment load command (and
823 // section headers) and the symbol table.
824 unsigned NumLoadCommands = 1;
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000825 uint64_t LoadCommandsSize = Is64Bit ?
826 SegmentLoadCommand64Size + NumSections * Section64Size :
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000827 SegmentLoadCommand32Size + NumSections * Section32Size;
828
829 // Add the symbol table load command sizes, if used.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000830 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
831 UndefinedSymbolData.size();
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000832 if (NumSymbols) {
833 NumLoadCommands += 2;
834 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
835 }
836
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000837 // Compute the total size of the section data, as well as its file size and
838 // vm size.
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000839 uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size)
840 + LoadCommandsSize;
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000841 uint64_t SectionDataSize = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000842 uint64_t SectionDataFileSize = 0;
843 uint64_t VMSize = 0;
Daniel Dunbarbacba992010-03-19 07:09:33 +0000844 for (MCAssembler::const_iterator it = Asm.begin(),
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000845 ie = Asm.end(); it != ie; ++it) {
Daniel Dunbarbacba992010-03-19 07:09:33 +0000846 const MCSectionData &SD = *it;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000847
848 VMSize = std::max(VMSize, SD.getAddress() + SD.getSize());
849
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000850 if (isVirtualSection(SD.getSection()))
851 continue;
852
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000853 SectionDataSize = std::max(SectionDataSize,
854 SD.getAddress() + SD.getSize());
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000855 SectionDataFileSize = std::max(SectionDataFileSize,
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000856 SD.getAddress() + SD.getFileSize());
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000857 }
858
Daniel Dunbar6b716532010-02-09 23:00:14 +0000859 // The section data is padded to 4 bytes.
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000860 //
861 // FIXME: Is this machine dependent?
862 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
863 SectionDataFileSize += SectionDataPadding;
864
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000865 // Write the prolog, starting with the header and load command...
Daniel Dunbaree0d8922010-03-13 22:10:17 +0000866 WriteHeader(NumLoadCommands, LoadCommandsSize,
867 Asm.getSubsectionsViaSymbols());
868 WriteSegmentLoadCommand(NumSections, VMSize,
869 SectionDataStart, SectionDataSize);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000870
Daniel Dunbar17a06502010-03-19 07:09:18 +0000871 // ... and then the section headers.
872 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
Daniel Dunbarbacba992010-03-19 07:09:33 +0000873 for (MCAssembler::const_iterator it = Asm.begin(),
Daniel Dunbar17a06502010-03-19 07:09:18 +0000874 ie = Asm.end(); it != ie; ++it) {
875 std::vector<MachRelocationEntry> &Relocs = Relocations[it];
876 unsigned NumRelocs = Relocs.size();
877 uint64_t SectionStart = SectionDataStart + it->getAddress();
878 WriteSection(*it, SectionStart, RelocTableEnd, NumRelocs);
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000879 RelocTableEnd += NumRelocs * RelocationInfoSize;
880 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000881
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000882 // Write the symbol table load command, if used.
883 if (NumSymbols) {
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000884 unsigned FirstLocalSymbol = 0;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000885 unsigned NumLocalSymbols = LocalSymbolData.size();
886 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
887 unsigned NumExternalSymbols = ExternalSymbolData.size();
888 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
889 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000890 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
891 unsigned NumSymTabSymbols =
892 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
893 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
894 uint64_t IndirectSymbolOffset = 0;
895
896 // If used, the indirect symbols are written after the section data.
897 if (NumIndirectSymbols)
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000898 IndirectSymbolOffset = RelocTableEnd;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000899
900 // The symbol table is written after the indirect symbol data.
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000901 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000902
903 // The string table is written after symbol table.
904 uint64_t StringTableOffset =
Daniel Dunbar5691e742010-03-13 22:49:35 +0000905 SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size :
906 Nlist32Size);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000907 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
908 StringTableOffset, StringTable.size());
909
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000910 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
911 FirstExternalSymbol, NumExternalSymbols,
912 FirstUndefinedSymbol, NumUndefinedSymbols,
913 IndirectSymbolOffset, NumIndirectSymbols);
914 }
915
916 // Write the actual section data.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000917 for (MCAssembler::const_iterator it = Asm.begin(),
918 ie = Asm.end(); it != ie; ++it)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000919 WriteFileData(OS, *it, this);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000920
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000921 // Write the extra padding.
922 WriteZeros(SectionDataPadding);
923
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000924 // Write the relocation entries.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000925 for (MCAssembler::const_iterator it = Asm.begin(),
Daniel Dunbar17a06502010-03-19 07:09:18 +0000926 ie = Asm.end(); it != ie; ++it) {
927 // Write the section relocation entries, in reverse order to match 'as'
928 // (approximately, the exact algorithm is more complicated than this).
929 std::vector<MachRelocationEntry> &Relocs = Relocations[it];
930 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
931 Write32(Relocs[e - i - 1].Word0);
932 Write32(Relocs[e - i - 1].Word1);
933 }
Daniel Dunbar3f6a9602009-08-26 13:58:10 +0000934 }
935
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000936 // Write the symbol table data, if used.
937 if (NumSymbols) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000938 // Write the indirect symbol entries.
Daniel Dunbarbacba992010-03-19 07:09:33 +0000939 for (MCAssembler::const_indirect_symbol_iterator
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000940 it = Asm.indirect_symbol_begin(),
941 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
942 // Indirect symbols in the non lazy symbol pointer section have some
943 // special handling.
944 const MCSectionMachO &Section =
945 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
Daniel Dunbar99d22ad2010-03-15 21:56:38 +0000946 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000947 // If this symbol is defined and internal, mark it as such.
948 if (it->Symbol->isDefined() &&
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000949 !Asm.getSymbolData(*it->Symbol).isExternal()) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000950 uint32_t Flags = ISF_Local;
951 if (it->Symbol->isAbsolute())
952 Flags |= ISF_Absolute;
953 Write32(Flags);
954 continue;
955 }
956 }
957
Daniel Dunbar2101d9c2010-03-10 20:58:31 +0000958 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000959 }
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000960
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000961 // FIXME: Check that offsets match computed ones.
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000962
963 // Write the symbol table entries.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000964 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000965 WriteNlist(LocalSymbolData[i]);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000966 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000967 WriteNlist(ExternalSymbolData[i]);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000968 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
Daniel Dunbar5691e742010-03-13 22:49:35 +0000969 WriteNlist(UndefinedSymbolData[i]);
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000970
971 // Write the string table.
972 OS << StringTable.str();
973 }
Daniel Dunbar2ae58f22009-08-22 08:28:27 +0000974 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000975};
976
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000977/* *** */
978
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000979MCFragment::MCFragment() : Kind(FragmentType(~0)) {
980}
981
Daniel Dunbar5e835962009-08-26 02:48:04 +0000982MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000983 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +0000984 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000985 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000986{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000987 if (Parent)
988 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000989}
990
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000991MCFragment::~MCFragment() {
992}
993
Daniel Dunbar5e835962009-08-26 02:48:04 +0000994uint64_t MCFragment::getAddress() const {
995 assert(getParent() && "Missing Section!");
996 return getParent()->getAddress() + Offset;
997}
998
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000999/* *** */
1000
Daniel Dunbar81e40002009-08-27 00:38:04 +00001001MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001002
1003MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +00001004 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001005 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +00001006 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +00001007 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +00001008 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +00001009 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001010{
1011 if (A)
1012 A->getSectionList().push_back(this);
1013}
1014
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001015/* *** */
1016
Daniel Dunbarefbb5332009-09-01 04:09:03 +00001017MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001018
Daniel Dunbarcb579b32009-08-31 08:08:06 +00001019MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001020 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +00001021 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +00001022 IsExternal(false), IsPrivateExtern(false),
1023 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +00001024{
1025 if (A)
1026 A->getSymbolList().push_back(this);
1027}
1028
1029/* *** */
1030
Daniel Dunbar1f3e4452010-03-11 01:34:27 +00001031MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
1032 raw_ostream &_OS)
1033 : Context(_Context), Backend(_Backend), OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +00001034{
1035}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001036
1037MCAssembler::~MCAssembler() {
1038}
1039
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001040static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
1041 const MCAsmFixup &Fixup,
1042 const MCDataFragment *DF,
1043 const MCValue Target,
1044 const MCSection *BaseSection) {
1045 // The effective fixup address is
1046 // addr(atom(A)) + offset(A)
1047 // - addr(atom(B)) - offset(B)
1048 // - addr(<base symbol>) + <fixup offset from base symbol>
1049 // and the offsets are not relocatable, so the fixup is fully resolved when
1050 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
1051 //
1052 // The simple (Darwin, except on x86_64) way of dealing with this was to
1053 // assume that any reference to a temporary symbol *must* be a temporary
1054 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
1055 // relocation to a temporary symbol (in the same section) is fully
1056 // resolved. This also works in conjunction with absolutized .set, which
1057 // requires the compiler to use .set to absolutize the differences between
1058 // symbols which the compiler knows to be assembly time constants, so we don't
1059 // need to worry about consider symbol differences fully resolved.
1060
1061 // Non-relative fixups are only resolved if constant.
1062 if (!BaseSection)
1063 return Target.isAbsolute();
1064
1065 // Otherwise, relative fixups are only resolved if not a difference and the
1066 // target is a temporary in the same section.
1067 if (Target.isAbsolute() || Target.getSymB())
1068 return false;
1069
1070 const MCSymbol *A = &Target.getSymA()->getSymbol();
1071 if (!A->isTemporary() || !A->isInSection() ||
1072 &A->getSection() != BaseSection)
1073 return false;
1074
1075 return true;
1076}
1077
Daniel Dunbar034843a2010-03-19 03:18:18 +00001078static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
1079 const MCAsmFixup &Fixup,
1080 const MCDataFragment *DF,
1081 const MCValue Target,
1082 const MCSymbolData *BaseSymbol) {
1083 // The effective fixup address is
1084 // addr(atom(A)) + offset(A)
1085 // - addr(atom(B)) - offset(B)
1086 // - addr(BaseSymbol) + <fixup offset from base symbol>
1087 // and the offsets are not relocatable, so the fixup is fully resolved when
1088 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
1089 //
1090 // Note that "false" is almost always conservatively correct (it means we emit
1091 // a relocation which is unnecessary), except when it would force us to emit a
1092 // relocation which the target cannot encode.
1093
1094 const MCSymbolData *A_Base = 0, *B_Base = 0;
1095 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1096 // Modified symbol references cannot be resolved.
1097 if (A->getKind() != MCSymbolRefExpr::VK_None)
1098 return false;
1099
1100 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
1101 if (!A_Base)
1102 return false;
1103 }
1104
1105 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1106 // Modified symbol references cannot be resolved.
1107 if (B->getKind() != MCSymbolRefExpr::VK_None)
1108 return false;
1109
1110 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
1111 if (!B_Base)
1112 return false;
1113 }
1114
1115 // If there is no base, A and B have to be the same atom for this fixup to be
1116 // fully resolved.
1117 if (!BaseSymbol)
1118 return A_Base == B_Base;
1119
1120 // Otherwise, B must be missing and A must be the base.
1121 return !B_Base && BaseSymbol == A_Base;
1122}
1123
Daniel Dunbar23869852010-03-19 03:18:09 +00001124bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
1125 // Non-temporary labels should always be visible to the linker.
1126 if (!SD->getSymbol().isTemporary())
1127 return true;
1128
1129 // Absolute temporary labels are never visible.
1130 if (!SD->getFragment())
1131 return false;
1132
1133 // Otherwise, check if the section requires symbols even for temporary labels.
1134 return getBackend().doesSectionRequireSymbols(
1135 SD->getFragment()->getParent()->getSection());
1136}
1137
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +00001138const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section,
1139 uint64_t Address) const {
1140 const MCSymbolData *Best = 0;
1141 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
1142 ie = symbol_end(); it != ie; ++it) {
1143 // Ignore non-linker visible symbols.
1144 if (!isSymbolLinkerVisible(it))
1145 continue;
1146
1147 // Ignore symbols not in the same section.
1148 if (!it->getFragment() || it->getFragment()->getParent() != Section)
1149 continue;
1150
1151 // Otherwise, find the closest symbol preceding this address (ties are
1152 // resolved in favor of the last defined symbol).
1153 if (it->getAddress() <= Address &&
1154 (!Best || it->getAddress() >= Best->getAddress()))
1155 Best = it;
1156 }
1157
1158 return Best;
1159}
1160
1161const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
1162 // Linker visible symbols define atoms.
1163 if (isSymbolLinkerVisible(SD))
1164 return SD;
1165
1166 // Absolute and undefined symbols have no defining atom.
1167 if (!SD->getFragment())
1168 return 0;
1169
1170 // Otherwise, search by address.
1171 return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress());
1172}
1173
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001174bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, MCAsmFixup &Fixup,
1175 MCDataFragment *DF,
1176 MCValue &Target, uint64_t &Value) const {
1177 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
1178 llvm_report_error("expected relocatable expression");
1179
1180 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
1181 // doesn't support small relocations, but then under what criteria does the
1182 // assembler allow symbol differences?
1183
1184 Value = Target.getConstant();
1185
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001186 bool IsResolved = true, IsPCRel = isFixupKindPCRel(Fixup.Kind);
Daniel Dunbar9a1d2002010-03-18 00:59:10 +00001187 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1188 if (A->getSymbol().isDefined())
1189 Value += getSymbolData(A->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001190 else
1191 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001192 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +00001193 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1194 if (B->getSymbol().isDefined())
1195 Value -= getSymbolData(B->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001196 else
1197 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001198 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001199
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001200 // If we are using scattered symbols, determine whether this value is actually
1201 // resolved; scattering may cause atoms to move.
1202 if (IsResolved && getBackend().hasScatteredSymbols()) {
1203 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +00001204 // If this is a PCrel relocation, find the base atom (identified by its
1205 // symbol) that the fixup value is relative to.
1206 const MCSymbolData *BaseSymbol = 0;
1207 if (IsPCRel) {
1208 BaseSymbol = getAtomForAddress(
1209 DF->getParent(), DF->getAddress() + Fixup.Offset);
1210 if (!BaseSymbol)
1211 IsResolved = false;
1212 }
1213
1214 if (IsResolved)
1215 IsResolved = isScatteredFixupFullyResolved(*this, Fixup, DF, Target,
1216 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +00001217 } else {
1218 const MCSection *BaseSection = 0;
1219 if (IsPCRel)
1220 BaseSection = &DF->getParent()->getSection();
1221
1222 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, DF, Target,
1223 BaseSection);
1224 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001225 }
1226
1227 if (IsPCRel)
Daniel Dunbarda3e9f72010-03-13 02:38:00 +00001228 Value -= DF->getAddress() + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +00001229
1230 return IsResolved;
1231}
1232
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001233void MCAssembler::LayoutSection(MCSectionData &SD) {
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001234 MCAsmLayout Layout(*this);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001235 uint64_t Address = SD.getAddress();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001236
1237 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
1238 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001239
Daniel Dunbar6742e342009-08-26 04:13:32 +00001240 F.setOffset(Address - SD.getAddress());
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001241
1242 // Evaluate fragment size.
1243 switch (F.getKind()) {
1244 case MCFragment::FT_Align: {
1245 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001246
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001247 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +00001248 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001249 AF.setFileSize(0);
1250 else
Daniel Dunbar6742e342009-08-26 04:13:32 +00001251 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001252 break;
1253 }
1254
1255 case MCFragment::FT_Data:
Daniel Dunbara4766d72010-02-13 09:28:32 +00001256 case MCFragment::FT_Fill:
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001257 F.setFileSize(F.getMaxFileSize());
1258 break;
1259
1260 case MCFragment::FT_Org: {
1261 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1262
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001263 int64_t TargetLocation;
1264 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
1265 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001266
1267 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001268 int64_t Offset = TargetLocation - F.getOffset();
1269 if (Offset < 0)
1270 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
1271 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001272
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +00001273 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001274 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001275 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001276
1277 case MCFragment::FT_ZeroFill: {
1278 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
1279
1280 // Align the fragment offset; it is safe to adjust the offset freely since
1281 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +00001282 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
1283 F.setOffset(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001284
1285 // FIXME: This is misnamed.
1286 F.setFileSize(ZFF.getSize());
1287 break;
1288 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001289 }
1290
Daniel Dunbar6742e342009-08-26 04:13:32 +00001291 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001292 }
1293
Daniel Dunbar6742e342009-08-26 04:13:32 +00001294 // Set the section sizes.
1295 SD.setSize(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001296 if (isVirtualSection(SD.getSection()))
1297 SD.setFileSize(0);
1298 else
1299 SD.setFileSize(Address - SD.getAddress());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001300}
1301
Kevin Enderby6e720482010-02-23 18:26:34 +00001302/// WriteNopData - Write optimal nops to the output file for the \arg Count
1303/// bytes. This returns the number of bytes written. It may return 0 if
1304/// the \arg Count is more than the maximum optimal nops.
1305///
1306/// FIXME this is X86 32-bit specific and should move to a better place.
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001307static uint64_t WriteNopData(uint64_t Count, MCObjectWriter *OW) {
Kevin Enderby76687082010-02-23 21:41:24 +00001308 static const uint8_t Nops[16][16] = {
1309 // nop
1310 {0x90},
1311 // xchg %ax,%ax
1312 {0x66, 0x90},
1313 // nopl (%[re]ax)
1314 {0x0f, 0x1f, 0x00},
1315 // nopl 0(%[re]ax)
1316 {0x0f, 0x1f, 0x40, 0x00},
1317 // nopl 0(%[re]ax,%[re]ax,1)
1318 {0x0f, 0x1f, 0x44, 0x00, 0x00},
1319 // nopw 0(%[re]ax,%[re]ax,1)
1320 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1321 // nopl 0L(%[re]ax)
1322 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1323 // nopl 0L(%[re]ax,%[re]ax,1)
1324 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1325 // nopw 0L(%[re]ax,%[re]ax,1)
1326 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1327 // nopw %cs:0L(%[re]ax,%[re]ax,1)
1328 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1329 // nopl 0(%[re]ax,%[re]ax,1)
1330 // nopw 0(%[re]ax,%[re]ax,1)
1331 {0x0f, 0x1f, 0x44, 0x00, 0x00,
1332 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1333 // nopw 0(%[re]ax,%[re]ax,1)
1334 // nopw 0(%[re]ax,%[re]ax,1)
1335 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1336 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1337 // nopw 0(%[re]ax,%[re]ax,1)
1338 // nopl 0L(%[re]ax) */
1339 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1340 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1341 // nopl 0L(%[re]ax)
1342 // nopl 0L(%[re]ax)
1343 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1344 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1345 // nopl 0L(%[re]ax)
1346 // nopl 0L(%[re]ax,%[re]ax,1)
1347 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1348 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
1349 };
1350
1351 if (Count > 15)
1352 return 0;
1353
Kevin Enderby6e720482010-02-23 18:26:34 +00001354 for (uint64_t i = 0; i < Count; i++)
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001355 OW->Write8(uint8_t(Nops[Count - 1][i]));
Kevin Enderby6e720482010-02-23 18:26:34 +00001356
1357 return Count;
1358}
1359
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001360/// WriteFileData - Write the \arg F data to the output file.
1361static void WriteFileData(raw_ostream &OS, const MCFragment &F,
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001362 MCObjectWriter *OW) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001363 uint64_t Start = OS.tell();
1364 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001365
Daniel Dunbar0adcd352009-08-25 21:10:45 +00001366 ++EmittedFragments;
1367
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001368 // FIXME: Embed in fragments instead?
1369 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001370 case MCFragment::FT_Align: {
1371 MCAlignFragment &AF = cast<MCAlignFragment>(F);
1372 uint64_t Count = AF.getFileSize() / AF.getValueSize();
1373
1374 // FIXME: This error shouldn't actually occur (the front end should emit
1375 // multiple .align directives to enforce the semantics it wants), but is
1376 // severe enough that we want to report it. How to handle this?
1377 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001378 llvm_report_error("undefined .align directive, value size '" +
1379 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001380 "' is not a divisor of padding size '" +
1381 Twine(AF.getFileSize()) + "'");
1382
Kevin Enderby6e720482010-02-23 18:26:34 +00001383 // See if we are aligning with nops, and if so do that first to try to fill
1384 // the Count bytes. Then if that did not fill any bytes or there are any
1385 // bytes left to fill use the the Value and ValueSize to fill the rest.
1386 if (AF.getEmitNops()) {
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001387 uint64_t NopByteCount = WriteNopData(Count, OW);
Kevin Enderby6e720482010-02-23 18:26:34 +00001388 Count -= NopByteCount;
1389 }
1390
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001391 for (uint64_t i = 0; i != Count; ++i) {
1392 switch (AF.getValueSize()) {
1393 default:
1394 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001395 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
1396 case 2: OW->Write16(uint16_t(AF.getValue())); break;
1397 case 4: OW->Write32(uint32_t(AF.getValue())); break;
1398 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001399 }
1400 }
1401 break;
1402 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001403
Daniel Dunbar3a30b822010-02-13 09:28:15 +00001404 case MCFragment::FT_Data: {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001405 OS << cast<MCDataFragment>(F).getContents().str();
1406 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +00001407 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001408
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001409 case MCFragment::FT_Fill: {
1410 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001411 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
1412 switch (FF.getValueSize()) {
1413 default:
1414 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001415 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
1416 case 2: OW->Write16(uint16_t(FF.getValue())); break;
1417 case 4: OW->Write32(uint32_t(FF.getValue())); break;
1418 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001419 }
1420 }
1421 break;
1422 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001423
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001424 case MCFragment::FT_Org: {
1425 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1426
1427 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001428 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +00001429
1430 break;
1431 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001432
1433 case MCFragment::FT_ZeroFill: {
1434 assert(0 && "Invalid zero fill fragment in concrete section!");
1435 break;
1436 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001437 }
1438
1439 assert(OS.tell() - Start == F.getFileSize());
1440}
1441
1442/// WriteFileData - Write the \arg SD data to the output file.
1443static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001444 MCObjectWriter *OW) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001445 // Ignore virtual sections.
1446 if (isVirtualSection(SD.getSection())) {
1447 assert(SD.getFileSize() == 0);
1448 return;
1449 }
1450
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001451 uint64_t Start = OS.tell();
1452 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +00001453
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001454 for (MCSectionData::const_iterator it = SD.begin(),
1455 ie = SD.end(); it != ie; ++it)
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001456 WriteFileData(OS, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001457
Daniel Dunbar6742e342009-08-26 04:13:32 +00001458 // Add section padding.
1459 assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001460 OW->WriteZeros(SD.getFileSize() - SD.getSize());
Daniel Dunbar6742e342009-08-26 04:13:32 +00001461
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001462 assert(OS.tell() - Start == SD.getFileSize());
1463}
1464
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001465void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001466 DEBUG_WITH_TYPE("mc-dump", {
1467 llvm::errs() << "assembler backend - pre-layout\n--\n";
1468 dump(); });
1469
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001470 // Layout until everything fits.
1471 while (LayoutOnce())
1472 continue;
1473
1474 DEBUG_WITH_TYPE("mc-dump", {
1475 llvm::errs() << "assembler backend - post-layout\n--\n";
1476 dump(); });
1477
Daniel Dunbaree0d8922010-03-13 22:10:17 +00001478 // FIXME: Factor out MCObjectWriter.
1479 bool Is64Bit = StringRef(getBackend().getTarget().getName()) == "x86-64";
1480 MachObjectWriter MOW(OS, Is64Bit);
Daniel Dunbarbacba992010-03-19 07:09:33 +00001481
1482 // Allow the object writer a chance to perform post-layout binding (for
1483 // example, to set the index fields in the symbol data).
1484 MOW.ExecutePostLayoutBinding(*this);
1485
Daniel Dunbarb1e98942010-03-19 07:09:47 +00001486 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarbdd92812010-03-19 09:28:55 +00001487 //
1488 // FIXME: Share layout object.
Daniel Dunbarb1e98942010-03-19 07:09:47 +00001489 MCAsmLayout Layout(*this);
1490 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
1491 for (MCSectionData::iterator it2 = it->begin(),
1492 ie2 = it->end(); it2 != ie2; ++it2) {
1493 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
1494 if (!DF)
1495 continue;
1496
1497 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
1498 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
1499 MCAsmFixup &Fixup = *it3;
1500
1501 // Evaluate the fixup.
1502 MCValue Target;
1503 uint64_t FixedValue;
1504 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
1505 // The fixup was unresolved, we need a relocation. Inform the object
1506 // writer of the relocation, and give it an opportunity to adjust the
1507 // fixup value if need be.
1508 MOW.RecordRelocation(*this, *DF, Fixup, Target, FixedValue);
1509 }
1510
Daniel Dunbar87190c42010-03-19 09:28:12 +00001511 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +00001512 }
1513 }
1514 }
1515
Daniel Dunbarbacba992010-03-19 07:09:33 +00001516 // Write the object file.
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001517 MOW.WriteObject(*this);
1518
1519 OS.flush();
1520}
1521
1522bool MCAssembler::FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF) {
1523 // FIXME: Share layout object.
1524 MCAsmLayout Layout(*this);
1525
1526 // Currently we only need to relax X86::reloc_pcrel_1byte.
1527 if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte)
1528 return false;
1529
1530 // If we cannot resolve the fixup value, it requires relaxation.
1531 MCValue Target;
1532 uint64_t Value;
1533 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
1534 return true;
1535
1536 // Otherwise, relax if the value is too big for a (signed) i8.
1537 return int64_t(Value) != int64_t(int8_t(Value));
1538}
1539
1540bool MCAssembler::LayoutOnce() {
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001541 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +00001542 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001543 MCSectionData *Prev = 0;
1544 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +00001545 MCSectionData &SD = *it;
1546
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001547 // Skip virtual sections.
1548 if (isVirtualSection(SD.getSection()))
1549 continue;
1550
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001551 // Align this section if necessary by adding padding bytes to the previous
1552 // section.
1553 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
1554 assert(Prev && "Missing prev section!");
1555 Prev->setFileSize(Prev->getFileSize() + Pad);
1556 Address += Pad;
1557 }
Daniel Dunbar6742e342009-08-26 04:13:32 +00001558
1559 // Layout the section fragments and its size.
1560 SD.setAddress(Address);
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001561 LayoutSection(SD);
Daniel Dunbar6742e342009-08-26 04:13:32 +00001562 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +00001563
1564 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +00001565 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001566
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001567 // Layout the virtual sections.
1568 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1569 MCSectionData &SD = *it;
1570
1571 if (!isVirtualSection(SD.getSection()))
1572 continue;
1573
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001574 // Align this section if necessary by adding padding bytes to the previous
1575 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +00001576 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001577 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +00001578
Daniel Dunbard5a8e982009-08-28 05:49:21 +00001579 SD.setAddress(Address);
1580 LayoutSection(SD);
1581 Address += SD.getSize();
1582 }
1583
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001584 // Scan the fixups in order and relax any that don't fit.
1585 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1586 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001587
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001588 for (MCSectionData::iterator it2 = SD.begin(),
1589 ie2 = SD.end(); it2 != ie2; ++it2) {
1590 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
1591 if (!DF)
1592 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +00001593
Daniel Dunbarf08fde42010-03-12 22:07:14 +00001594 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
1595 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
1596 MCAsmFixup &Fixup = *it3;
1597
1598 // Check whether we need to relax this fixup.
1599 if (!FixupNeedsRelaxation(Fixup, DF))
1600 continue;
1601
1602 // Relax the instruction.
1603 //
1604 // FIXME: This is a huge temporary hack which just looks for x86
1605 // branches; the only thing we need to relax on x86 is
1606 // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be
1607 // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax
1608 // an individual MCInst.
1609 SmallVectorImpl<char> &C = DF->getContents();
1610 uint64_t PrevOffset = Fixup.Offset;
1611 unsigned Amt = 0;
1612
1613 // jcc instructions
1614 if (unsigned(C[Fixup.Offset-1]) >= 0x70 &&
1615 unsigned(C[Fixup.Offset-1]) <= 0x7f) {
1616 C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10;
1617 C[Fixup.Offset-1] = char(0x0f);
1618 ++Fixup.Offset;
1619 Amt = 4;
1620
1621 // jmp rel8
1622 } else if (C[Fixup.Offset-1] == char(0xeb)) {
1623 C[Fixup.Offset-1] = char(0xe9);
1624 Amt = 3;
1625
1626 } else
1627 llvm_unreachable("unknown 1 byte pcrel instruction!");
1628
1629 Fixup.Value = MCBinaryExpr::Create(
1630 MCBinaryExpr::Sub, Fixup.Value,
1631 MCConstantExpr::Create(3, getContext()),
1632 getContext());
1633 C.insert(C.begin() + Fixup.Offset, Amt, char(0));
1634 Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte);
1635
1636 // Update the remaining fixups, which have slid.
1637 //
1638 // FIXME: This is bad for performance, but will be eliminated by the
1639 // move to MCInst specific fragments.
1640 ++it3;
1641 for (; it3 != ie3; ++it3)
1642 it3->Offset += Amt;
1643
1644 // Update all the symbols for this fragment, which may have slid.
1645 //
1646 // FIXME: This is really really bad for performance, but will be
1647 // eliminated by the move to MCInst specific fragments.
1648 for (MCAssembler::symbol_iterator it = symbol_begin(),
1649 ie = symbol_end(); it != ie; ++it) {
1650 MCSymbolData &SD = *it;
1651
1652 if (it->getFragment() != DF)
1653 continue;
1654
1655 if (SD.getOffset() > PrevOffset)
1656 SD.setOffset(SD.getOffset() + Amt);
1657 }
1658
1659 // Restart layout.
1660 //
1661 // FIXME: This is O(N^2), but will be eliminated once we have a smart
1662 // MCAsmLayout object.
1663 return true;
1664 }
1665 }
1666 }
1667
1668 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001669}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001670
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001671// Debugging methods
1672
1673namespace llvm {
1674
1675raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +00001676 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
1677 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001678 return OS;
1679}
1680
1681}
1682
1683void MCFragment::dump() {
1684 raw_ostream &OS = llvm::errs();
1685
1686 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
1687 << " FileSize:" << FileSize;
1688
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001689 OS << ">";
1690}
1691
1692void MCAlignFragment::dump() {
1693 raw_ostream &OS = llvm::errs();
1694
1695 OS << "<MCAlignFragment ";
1696 this->MCFragment::dump();
1697 OS << "\n ";
1698 OS << " Alignment:" << getAlignment()
1699 << " Value:" << getValue() << " ValueSize:" << getValueSize()
1700 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
1701}
1702
1703void MCDataFragment::dump() {
1704 raw_ostream &OS = llvm::errs();
1705
1706 OS << "<MCDataFragment ";
1707 this->MCFragment::dump();
1708 OS << "\n ";
1709 OS << " Contents:[";
1710 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
1711 if (i) OS << ",";
1712 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1713 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +00001714 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001715
1716 if (!getFixups().empty()) {
1717 OS << ",\n ";
1718 OS << " Fixups:[";
1719 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001720 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001721 OS << *it;
1722 }
1723 OS << "]";
1724 }
1725
1726 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001727}
1728
1729void MCFillFragment::dump() {
1730 raw_ostream &OS = llvm::errs();
1731
1732 OS << "<MCFillFragment ";
1733 this->MCFragment::dump();
1734 OS << "\n ";
1735 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
1736 << " Count:" << getCount() << ">";
1737}
1738
1739void MCOrgFragment::dump() {
1740 raw_ostream &OS = llvm::errs();
1741
1742 OS << "<MCOrgFragment ";
1743 this->MCFragment::dump();
1744 OS << "\n ";
1745 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
1746}
1747
1748void MCZeroFillFragment::dump() {
1749 raw_ostream &OS = llvm::errs();
1750
1751 OS << "<MCZeroFillFragment ";
1752 this->MCFragment::dump();
1753 OS << "\n ";
1754 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
1755}
1756
1757void MCSectionData::dump() {
1758 raw_ostream &OS = llvm::errs();
1759
1760 OS << "<MCSectionData";
1761 OS << " Alignment:" << getAlignment() << " Address:" << Address
1762 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001763 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001764 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1765 if (it != begin()) OS << ",\n ";
1766 it->dump();
1767 }
1768 OS << "]>";
1769}
1770
1771void MCSymbolData::dump() {
1772 raw_ostream &OS = llvm::errs();
1773
1774 OS << "<MCSymbolData Symbol:" << getSymbol()
1775 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1776 << " Flags:" << getFlags() << " Index:" << getIndex();
1777 if (isCommon())
1778 OS << " (common, size:" << getCommonSize()
1779 << " align: " << getCommonAlignment() << ")";
1780 if (isExternal())
1781 OS << " (external)";
1782 if (isPrivateExtern())
1783 OS << " (private extern)";
1784 OS << ">";
1785}
1786
1787void MCAssembler::dump() {
1788 raw_ostream &OS = llvm::errs();
1789
1790 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001791 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001792 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1793 if (it != begin()) OS << ",\n ";
1794 it->dump();
1795 }
1796 OS << "],\n";
1797 OS << " Symbols:[";
1798
1799 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001800 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001801 it->dump();
1802 }
1803 OS << "]>\n";
1804}