blob: e98d27d73e28307025e4b0dd840e547a920f9ee5 [file] [log] [blame]
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001//===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
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/MachObjectWriter.h"
11#include "llvm/ADT/StringMap.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/MC/MCAssembler.h"
Daniel Dunbar207e06e2010-03-24 03:43:40 +000014#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000015#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCObjectWriter.h"
17#include "llvm/MC/MCSectionMachO.h"
18#include "llvm/MC/MCSymbol.h"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000019#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000020#include "llvm/MC/MCValue.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/MachO.h"
23#include "llvm/Target/TargetAsmBackend.h"
24
25// FIXME: Gross.
26#include "../Target/X86/X86FixupKinds.h"
27
28#include <vector>
29using namespace llvm;
30
Rafael Espindolaa8c02c32010-09-30 03:11:42 +000031// FIXME: this has been copied from (or to) X86AsmBackend.cpp
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000032static unsigned getFixupKindLog2Size(unsigned Kind) {
33 switch (Kind) {
34 default: llvm_unreachable("invalid fixup kind!");
35 case X86::reloc_pcrel_1byte:
36 case FK_Data_1: return 0;
Chris Lattnerda3051a2010-07-07 22:35:13 +000037 case X86::reloc_pcrel_2byte:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000038 case FK_Data_2: return 1;
39 case X86::reloc_pcrel_4byte:
40 case X86::reloc_riprel_4byte:
Daniel Dunbar602b40f2010-03-19 18:07:55 +000041 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +000042 case X86::reloc_signed_4byte:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000043 case FK_Data_4: return 2;
44 case FK_Data_8: return 3;
45 }
46}
47
48static bool isFixupKindPCRel(unsigned Kind) {
49 switch (Kind) {
50 default:
51 return false;
52 case X86::reloc_pcrel_1byte:
Chris Lattnerda3051a2010-07-07 22:35:13 +000053 case X86::reloc_pcrel_2byte:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000054 case X86::reloc_pcrel_4byte:
55 case X86::reloc_riprel_4byte:
Daniel Dunbar602b40f2010-03-19 18:07:55 +000056 case X86::reloc_riprel_4byte_movq_load:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000057 return true;
58 }
59}
60
Daniel Dunbar602b40f2010-03-19 18:07:55 +000061static bool isFixupKindRIPRel(unsigned Kind) {
62 return Kind == X86::reloc_riprel_4byte ||
63 Kind == X86::reloc_riprel_4byte_movq_load;
64}
65
Daniel Dunbare9460ec2010-05-10 23:15:13 +000066static bool doesSymbolRequireExternRelocation(MCSymbolData *SD) {
67 // Undefined symbols are always extern.
68 if (SD->Symbol->isUndefined())
69 return true;
70
71 // References to weak definitions require external relocation entries; the
72 // definition may not always be the one in the same object file.
73 if (SD->getFlags() & SF_WeakDefinition)
74 return true;
75
76 // Otherwise, we can use an internal relocation.
77 return false;
78}
79
Rafael Espindola70703872010-09-30 02:22:20 +000080static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
81 const MCValue Target,
82 const MCSymbolData *BaseSymbol) {
83 // The effective fixup address is
84 // addr(atom(A)) + offset(A)
85 // - addr(atom(B)) - offset(B)
86 // - addr(BaseSymbol) + <fixup offset from base symbol>
87 // and the offsets are not relocatable, so the fixup is fully resolved when
88 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
89 //
90 // Note that "false" is almost always conservatively correct (it means we emit
91 // a relocation which is unnecessary), except when it would force us to emit a
92 // relocation which the target cannot encode.
93
94 const MCSymbolData *A_Base = 0, *B_Base = 0;
95 if (const MCSymbolRefExpr *A = Target.getSymA()) {
96 // Modified symbol references cannot be resolved.
97 if (A->getKind() != MCSymbolRefExpr::VK_None)
98 return false;
99
100 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
101 if (!A_Base)
102 return false;
103 }
104
105 if (const MCSymbolRefExpr *B = Target.getSymB()) {
106 // Modified symbol references cannot be resolved.
107 if (B->getKind() != MCSymbolRefExpr::VK_None)
108 return false;
109
110 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
111 if (!B_Base)
112 return false;
113 }
114
115 // If there is no base, A and B have to be the same atom for this fixup to be
116 // fully resolved.
117 if (!BaseSymbol)
118 return A_Base == B_Base;
119
120 // Otherwise, B must be missing and A must be the base.
121 return !B_Base && BaseSymbol == A_Base;
122}
123
124static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
125 const MCValue Target,
126 const MCSection *BaseSection) {
127 // The effective fixup address is
128 // addr(atom(A)) + offset(A)
129 // - addr(atom(B)) - offset(B)
130 // - addr(<base symbol>) + <fixup offset from base symbol>
131 // and the offsets are not relocatable, so the fixup is fully resolved when
132 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
133 //
134 // The simple (Darwin, except on x86_64) way of dealing with this was to
135 // assume that any reference to a temporary symbol *must* be a temporary
136 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
137 // relocation to a temporary symbol (in the same section) is fully
138 // resolved. This also works in conjunction with absolutized .set, which
139 // requires the compiler to use .set to absolutize the differences between
140 // symbols which the compiler knows to be assembly time constants, so we don't
141 // need to worry about considering symbol differences fully resolved.
142
143 // Non-relative fixups are only resolved if constant.
144 if (!BaseSection)
145 return Target.isAbsolute();
146
147 // Otherwise, relative fixups are only resolved if not a difference and the
148 // target is a temporary in the same section.
149 if (Target.isAbsolute() || Target.getSymB())
150 return false;
151
152 const MCSymbol *A = &Target.getSymA()->getSymbol();
153 if (!A->isTemporary() || !A->isInSection() ||
154 &A->getSection() != BaseSection)
155 return false;
156
157 return true;
158}
159
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000160namespace {
161
162class MachObjectWriterImpl {
163 // See <mach-o/loader.h>.
164 enum {
165 Header_Magic32 = 0xFEEDFACE,
166 Header_Magic64 = 0xFEEDFACF
167 };
168
169 enum {
170 Header32Size = 28,
171 Header64Size = 32,
172 SegmentLoadCommand32Size = 56,
173 SegmentLoadCommand64Size = 72,
174 Section32Size = 68,
175 Section64Size = 80,
176 SymtabLoadCommandSize = 24,
177 DysymtabLoadCommandSize = 80,
178 Nlist32Size = 12,
179 Nlist64Size = 16,
180 RelocationInfoSize = 8
181 };
182
183 enum HeaderFileType {
184 HFT_Object = 0x1
185 };
186
187 enum HeaderFlags {
188 HF_SubsectionsViaSymbols = 0x2000
189 };
190
191 enum LoadCommandType {
192 LCT_Segment = 0x1,
193 LCT_Symtab = 0x2,
194 LCT_Dysymtab = 0xb,
195 LCT_Segment64 = 0x19
196 };
197
198 // See <mach-o/nlist.h>.
199 enum SymbolTypeType {
200 STT_Undefined = 0x00,
201 STT_Absolute = 0x02,
202 STT_Section = 0x0e
203 };
204
205 enum SymbolTypeFlags {
206 // If any of these bits are set, then the entry is a stab entry number (see
207 // <mach-o/stab.h>. Otherwise the other masks apply.
208 STF_StabsEntryMask = 0xe0,
209
210 STF_TypeMask = 0x0e,
211 STF_External = 0x01,
212 STF_PrivateExtern = 0x10
213 };
214
215 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
216 /// symbol entry.
217 enum IndirectSymbolFlags {
218 ISF_Local = 0x80000000,
219 ISF_Absolute = 0x40000000
220 };
221
222 /// RelocationFlags - Special flags for addresses.
223 enum RelocationFlags {
224 RF_Scattered = 0x80000000
225 };
226
227 enum RelocationInfoType {
228 RIT_Vanilla = 0,
229 RIT_Pair = 1,
230 RIT_Difference = 2,
231 RIT_PreboundLazyPointer = 3,
Eric Christopher96ac5152010-05-26 00:02:12 +0000232 RIT_LocalDifference = 4,
233 RIT_TLV = 5
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000234 };
235
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000236 /// X86_64 uses its own relocation types.
237 enum RelocationInfoTypeX86_64 {
238 RIT_X86_64_Unsigned = 0,
239 RIT_X86_64_Signed = 1,
240 RIT_X86_64_Branch = 2,
241 RIT_X86_64_GOTLoad = 3,
242 RIT_X86_64_GOT = 4,
243 RIT_X86_64_Subtractor = 5,
244 RIT_X86_64_Signed1 = 6,
245 RIT_X86_64_Signed2 = 7,
Eric Christopher96ac5152010-05-26 00:02:12 +0000246 RIT_X86_64_Signed4 = 8,
247 RIT_X86_64_TLV = 9
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000248 };
249
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000250 /// MachSymbolData - Helper struct for containing some precomputed information
251 /// on symbols.
252 struct MachSymbolData {
253 MCSymbolData *SymbolData;
254 uint64_t StringIndex;
255 uint8_t SectionIndex;
256
257 // Support lexicographic sorting.
258 bool operator<(const MachSymbolData &RHS) const {
Benjamin Kramerc37791e2010-05-20 14:14:22 +0000259 return SymbolData->getSymbol().getName() <
260 RHS.SymbolData->getSymbol().getName();
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000261 }
262 };
263
264 /// @name Relocation Data
265 /// @{
266
267 struct MachRelocationEntry {
268 uint32_t Word0;
269 uint32_t Word1;
270 };
271
272 llvm::DenseMap<const MCSectionData*,
273 std::vector<MachRelocationEntry> > Relocations;
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000274 llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000275
276 /// @}
277 /// @name Symbol Table Data
278 /// @{
279
280 SmallString<256> StringTable;
281 std::vector<MachSymbolData> LocalSymbolData;
282 std::vector<MachSymbolData> ExternalSymbolData;
283 std::vector<MachSymbolData> UndefinedSymbolData;
284
285 /// @}
286
287 MachObjectWriter *Writer;
288
289 raw_ostream &OS;
290
291 unsigned Is64Bit : 1;
292
Jim Grosbachc9d14392010-11-05 18:48:58 +0000293 uint32_t CPUType;
294 uint32_t CPUSubtype;
295
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000296public:
Jim Grosbachc9d14392010-11-05 18:48:58 +0000297 MachObjectWriterImpl(MachObjectWriter *_Writer, bool _Is64Bit,
298 uint32_t _CPUType, uint32_t _CPUSubtype)
299 : Writer(_Writer), OS(Writer->getStream()), Is64Bit(_Is64Bit),
300 CPUType(_CPUType), CPUSubtype(_CPUSubtype) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000301 }
302
303 void Write8(uint8_t Value) { Writer->Write8(Value); }
304 void Write16(uint16_t Value) { Writer->Write16(Value); }
305 void Write32(uint32_t Value) { Writer->Write32(Value); }
306 void Write64(uint64_t Value) { Writer->Write64(Value); }
307 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
308 void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
309 Writer->WriteBytes(Str, ZeroFillSize);
310 }
311
312 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
313 bool SubsectionsViaSymbols) {
314 uint32_t Flags = 0;
315
316 if (SubsectionsViaSymbols)
317 Flags |= HF_SubsectionsViaSymbols;
318
319 // struct mach_header (28 bytes) or
320 // struct mach_header_64 (32 bytes)
321
322 uint64_t Start = OS.tell();
323 (void) Start;
324
325 Write32(Is64Bit ? Header_Magic64 : Header_Magic32);
326
Jim Grosbachc9d14392010-11-05 18:48:58 +0000327 Write32(CPUType);
328 Write32(CPUSubtype);
329
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000330 Write32(HFT_Object);
331 Write32(NumLoadCommands); // Object files have a single load command, the
332 // segment.
333 Write32(LoadCommandsSize);
334 Write32(Flags);
335 if (Is64Bit)
336 Write32(0); // reserved
337
338 assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size);
339 }
340
341 /// WriteSegmentLoadCommand - Write a segment load command.
342 ///
343 /// \arg NumSections - The number of sections in this segment.
344 /// \arg SectionDataSize - The total size of the sections.
345 void WriteSegmentLoadCommand(unsigned NumSections,
346 uint64_t VMSize,
347 uint64_t SectionDataStartOffset,
348 uint64_t SectionDataSize) {
349 // struct segment_command (56 bytes) or
350 // struct segment_command_64 (72 bytes)
351
352 uint64_t Start = OS.tell();
353 (void) Start;
354
355 unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size :
356 SegmentLoadCommand32Size;
357 Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
358 Write32(SegmentLoadCommandSize +
359 NumSections * (Is64Bit ? Section64Size : Section32Size));
360
361 WriteBytes("", 16);
362 if (Is64Bit) {
363 Write64(0); // vmaddr
364 Write64(VMSize); // vmsize
365 Write64(SectionDataStartOffset); // file offset
366 Write64(SectionDataSize); // file size
367 } else {
368 Write32(0); // vmaddr
369 Write32(VMSize); // vmsize
370 Write32(SectionDataStartOffset); // file offset
371 Write32(SectionDataSize); // file size
372 }
373 Write32(0x7); // maxprot
374 Write32(0x7); // initprot
375 Write32(NumSections);
376 Write32(0); // flags
377
378 assert(OS.tell() - Start == SegmentLoadCommandSize);
379 }
380
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000381 void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
382 const MCSectionData &SD, uint64_t FileOffset,
383 uint64_t RelocationsStart, unsigned NumRelocations) {
Daniel Dunbar5d428512010-03-25 02:00:07 +0000384 uint64_t SectionSize = Layout.getSectionSize(&SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000385
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000386 // The offset is unused for virtual sections.
387 if (Asm.getBackend().isVirtualSection(SD.getSection())) {
Daniel Dunbarb026d642010-03-25 07:10:05 +0000388 assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000389 FileOffset = 0;
390 }
391
392 // struct section (68 bytes) or
393 // struct section_64 (80 bytes)
394
395 uint64_t Start = OS.tell();
396 (void) Start;
397
Daniel Dunbar56279f42010-05-18 17:28:20 +0000398 const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000399 WriteBytes(Section.getSectionName(), 16);
400 WriteBytes(Section.getSegmentName(), 16);
401 if (Is64Bit) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000402 Write64(Layout.getSectionAddress(&SD)); // address
Daniel Dunbar5d428512010-03-25 02:00:07 +0000403 Write64(SectionSize); // size
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000404 } else {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000405 Write32(Layout.getSectionAddress(&SD)); // address
Daniel Dunbar5d428512010-03-25 02:00:07 +0000406 Write32(SectionSize); // size
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000407 }
408 Write32(FileOffset);
409
410 unsigned Flags = Section.getTypeAndAttributes();
411 if (SD.hasInstructions())
412 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
413
414 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
415 Write32(Log2_32(SD.getAlignment()));
416 Write32(NumRelocations ? RelocationsStart : 0);
417 Write32(NumRelocations);
418 Write32(Flags);
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000419 Write32(IndirectSymBase.lookup(&SD)); // reserved1
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000420 Write32(Section.getStubSize()); // reserved2
421 if (Is64Bit)
422 Write32(0); // reserved3
423
424 assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size);
425 }
426
427 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
428 uint32_t StringTableOffset,
429 uint32_t StringTableSize) {
430 // struct symtab_command (24 bytes)
431
432 uint64_t Start = OS.tell();
433 (void) Start;
434
435 Write32(LCT_Symtab);
436 Write32(SymtabLoadCommandSize);
437 Write32(SymbolOffset);
438 Write32(NumSymbols);
439 Write32(StringTableOffset);
440 Write32(StringTableSize);
441
442 assert(OS.tell() - Start == SymtabLoadCommandSize);
443 }
444
445 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
446 uint32_t NumLocalSymbols,
447 uint32_t FirstExternalSymbol,
448 uint32_t NumExternalSymbols,
449 uint32_t FirstUndefinedSymbol,
450 uint32_t NumUndefinedSymbols,
451 uint32_t IndirectSymbolOffset,
452 uint32_t NumIndirectSymbols) {
453 // struct dysymtab_command (80 bytes)
454
455 uint64_t Start = OS.tell();
456 (void) Start;
457
458 Write32(LCT_Dysymtab);
459 Write32(DysymtabLoadCommandSize);
460 Write32(FirstLocalSymbol);
461 Write32(NumLocalSymbols);
462 Write32(FirstExternalSymbol);
463 Write32(NumExternalSymbols);
464 Write32(FirstUndefinedSymbol);
465 Write32(NumUndefinedSymbols);
466 Write32(0); // tocoff
467 Write32(0); // ntoc
468 Write32(0); // modtaboff
469 Write32(0); // nmodtab
470 Write32(0); // extrefsymoff
471 Write32(0); // nextrefsyms
472 Write32(IndirectSymbolOffset);
473 Write32(NumIndirectSymbols);
474 Write32(0); // extreloff
475 Write32(0); // nextrel
476 Write32(0); // locreloff
477 Write32(0); // nlocrel
478
479 assert(OS.tell() - Start == DysymtabLoadCommandSize);
480 }
481
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000482 void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000483 MCSymbolData &Data = *MSD.SymbolData;
484 const MCSymbol &Symbol = Data.getSymbol();
485 uint8_t Type = 0;
486 uint16_t Flags = Data.getFlags();
487 uint32_t Address = 0;
488
489 // Set the N_TYPE bits. See <mach-o/nlist.h>.
490 //
491 // FIXME: Are the prebound or indirect fields possible here?
492 if (Symbol.isUndefined())
493 Type = STT_Undefined;
494 else if (Symbol.isAbsolute())
495 Type = STT_Absolute;
496 else
497 Type = STT_Section;
498
499 // FIXME: Set STAB bits.
500
501 if (Data.isPrivateExtern())
502 Type |= STF_PrivateExtern;
503
504 // Set external bit.
505 if (Data.isExternal() || Symbol.isUndefined())
506 Type |= STF_External;
507
508 // Compute the symbol address.
509 if (Symbol.isDefined()) {
510 if (Symbol.isAbsolute()) {
Daniel Dunbar2d7fd612010-05-05 19:01:05 +0000511 Address = cast<MCConstantExpr>(Symbol.getVariableValue())->getValue();
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000512 } else {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000513 Address = Layout.getSymbolAddress(&Data);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000514 }
515 } else if (Data.isCommon()) {
516 // Common symbols are encoded with the size in the address
517 // field, and their alignment in the flags.
518 Address = Data.getCommonSize();
519
520 // Common alignment is packed into the 'desc' bits.
521 if (unsigned Align = Data.getCommonAlignment()) {
522 unsigned Log2Size = Log2_32(Align);
523 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
524 if (Log2Size > 15)
Chris Lattner75361b62010-04-07 22:58:41 +0000525 report_fatal_error("invalid 'common' alignment '" +
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000526 Twine(Align) + "'");
527 // FIXME: Keep this mask with the SymbolFlags enumeration.
528 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
529 }
530 }
531
532 // struct nlist (12 bytes)
533
534 Write32(MSD.StringIndex);
535 Write8(Type);
536 Write8(MSD.SectionIndex);
537
538 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
539 // value.
540 Write16(Flags);
541 if (Is64Bit)
542 Write64(Address);
543 else
544 Write32(Address);
545 }
546
Daniel Dunbar35b06572010-03-22 23:16:43 +0000547 // FIXME: We really need to improve the relocation validation. Basically, we
548 // want to implement a separate computation which evaluates the relocation
549 // entry as the linker would, and verifies that the resultant fixup value is
550 // exactly what the encoder wanted. This will catch several classes of
551 // problems:
552 //
553 // - Relocation entry bugs, the two algorithms are unlikely to have the same
554 // exact bug.
555 //
556 // - Relaxation issues, where we forget to relax something.
557 //
558 // - Input errors, where something cannot be correctly encoded. 'as' allows
559 // these through in many cases.
560
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000561 void RecordX86_64Relocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Daniel Dunbarb7514182010-03-22 20:35:50 +0000562 const MCFragment *Fragment,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000563 const MCFixup &Fixup, MCValue Target,
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000564 uint64_t &FixedValue) {
Daniel Dunbar482ad802010-05-26 15:18:31 +0000565 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
566 unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind());
567 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000568
569 // See <reloc.h>.
Daniel Dunbar482ad802010-05-26 15:18:31 +0000570 uint32_t FixupOffset =
571 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
572 uint32_t FixupAddress =
573 Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000574 int64_t Value = 0;
575 unsigned Index = 0;
576 unsigned IsExtern = 0;
577 unsigned Type = 0;
578
579 Value = Target.getConstant();
580
581 if (IsPCRel) {
582 // Compensate for the relocation offset, Darwin x86_64 relocations only
583 // have the addend and appear to have attempted to define it to be the
584 // actual expression addend without the PCrel bias. However, instructions
585 // with data following the relocation are not accomodated for (see comment
586 // below regarding SIGNED{1,2,4}), so it isn't exactly that either.
Benjamin Kramer454c4ce2010-04-08 15:25:57 +0000587 Value += 1LL << Log2Size;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000588 }
589
590 if (Target.isAbsolute()) { // constant
591 // SymbolNum of 0 indicates the absolute section.
592 Type = RIT_X86_64_Unsigned;
593 Index = 0;
594
595 // FIXME: I believe this is broken, I don't think the linker can
596 // understand it. I think it would require a local relocation, but I'm not
597 // sure if that would work either. The official way to get an absolute
598 // PCrel relocation is to use an absolute symbol (which we don't support
599 // yet).
600 if (IsPCRel) {
601 IsExtern = 1;
602 Type = RIT_X86_64_Branch;
603 }
604 } else if (Target.getSymB()) { // A - B + constant
605 const MCSymbol *A = &Target.getSymA()->getSymbol();
606 MCSymbolData &A_SD = Asm.getSymbolData(*A);
Rafael Espindolab8141102010-09-27 18:13:03 +0000607 const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000608
609 const MCSymbol *B = &Target.getSymB()->getSymbol();
610 MCSymbolData &B_SD = Asm.getSymbolData(*B);
Rafael Espindolab8141102010-09-27 18:13:03 +0000611 const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000612
613 // Neither symbol can be modified.
614 if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
615 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
Chris Lattner75361b62010-04-07 22:58:41 +0000616 report_fatal_error("unsupported relocation of modified symbol");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000617
618 // We don't support PCrel relocations of differences. Darwin 'as' doesn't
619 // implement most of these correctly.
620 if (IsPCRel)
Chris Lattner75361b62010-04-07 22:58:41 +0000621 report_fatal_error("unsupported pc-relative relocation of difference");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000622
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000623 // The support for the situation where one or both of the symbols would
624 // require a local relocation is handled just like if the symbols were
625 // external. This is certainly used in the case of debug sections where
626 // the section has only temporary symbols and thus the symbols don't have
627 // base symbols. This is encoded using the section ordinal and
628 // non-extern relocation entries.
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000629
630 // Darwin 'as' doesn't emit correct relocations for this (it ends up with
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000631 // a single SIGNED relocation); reject it for now. Except the case where
632 // both symbols don't have a base, equal but both NULL.
633 if (A_Base == B_Base && A_Base)
Chris Lattner75361b62010-04-07 22:58:41 +0000634 report_fatal_error("unsupported relocation with identical base");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000635
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000636 Value += Layout.getSymbolAddress(&A_SD) -
637 (A_Base == NULL ? 0 : Layout.getSymbolAddress(A_Base));
638 Value -= Layout.getSymbolAddress(&B_SD) -
639 (B_Base == NULL ? 0 : Layout.getSymbolAddress(B_Base));
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000640
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000641 if (A_Base) {
642 Index = A_Base->getIndex();
643 IsExtern = 1;
644 }
645 else {
646 Index = A_SD.getFragment()->getParent()->getOrdinal() + 1;
647 IsExtern = 0;
648 }
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000649 Type = RIT_X86_64_Unsigned;
650
651 MachRelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000652 MRE.Word0 = FixupOffset;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000653 MRE.Word1 = ((Index << 0) |
654 (IsPCRel << 24) |
655 (Log2Size << 25) |
656 (IsExtern << 27) |
657 (Type << 28));
Daniel Dunbarb7514182010-03-22 20:35:50 +0000658 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000659
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000660 if (B_Base) {
661 Index = B_Base->getIndex();
662 IsExtern = 1;
663 }
664 else {
665 Index = B_SD.getFragment()->getParent()->getOrdinal() + 1;
666 IsExtern = 0;
667 }
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000668 Type = RIT_X86_64_Subtractor;
669 } else {
670 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
671 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Rafael Espindolab8141102010-09-27 18:13:03 +0000672 const MCSymbolData *Base = Asm.getAtom(&SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000673
Daniel Dunbarae7fb0b2010-05-05 17:22:39 +0000674 // Relocations inside debug sections always use local relocations when
675 // possible. This seems to be done because the debugger doesn't fully
676 // understand x86_64 relocation entries, and expects to find values that
677 // have already been fixed up.
Daniel Dunbar2d7fd612010-05-05 19:01:05 +0000678 if (Symbol->isInSection()) {
Daniel Dunbarae7fb0b2010-05-05 17:22:39 +0000679 const MCSectionMachO &Section = static_cast<const MCSectionMachO&>(
680 Fragment->getParent()->getSection());
681 if (Section.hasAttribute(MCSectionMachO::S_ATTR_DEBUG))
682 Base = 0;
683 }
684
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000685 // x86_64 almost always uses external relocations, except when there is no
686 // symbol to use as a base address (a local symbol with no preceeding
687 // non-local symbol).
688 if (Base) {
689 Index = Base->getIndex();
690 IsExtern = 1;
691
692 // Add the local offset, if needed.
693 if (Base != &SD)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000694 Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
Daniel Dunbaref4591e2010-05-11 23:53:05 +0000695 } else if (Symbol->isInSection()) {
Daniel Dunbar8fb04032010-03-25 08:08:54 +0000696 // The index is the section ordinal (1-based).
697 Index = SD.getFragment()->getParent()->getOrdinal() + 1;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000698 IsExtern = 0;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000699 Value += Layout.getSymbolAddress(&SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000700
701 if (IsPCRel)
Daniel Dunbardb4c7e62010-05-11 23:53:11 +0000702 Value -= FixupAddress + (1 << Log2Size);
Daniel Dunbaref4591e2010-05-11 23:53:05 +0000703 } else {
704 report_fatal_error("unsupported relocation of undefined symbol '" +
705 Symbol->getName() + "'");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000706 }
707
708 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
709 if (IsPCRel) {
710 if (IsRIPRel) {
711 if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
712 // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
713 // rewrite the movq to an leaq at link time if the symbol ends up in
714 // the same linkage unit.
Daniel Dunbar482ad802010-05-26 15:18:31 +0000715 if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000716 Type = RIT_X86_64_GOTLoad;
717 else
718 Type = RIT_X86_64_GOT;
Eric Christopheraeed4d82010-05-27 00:52:31 +0000719 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
Eric Christopher96ac5152010-05-26 00:02:12 +0000720 Type = RIT_X86_64_TLV;
Eric Christopheraeed4d82010-05-27 00:52:31 +0000721 } else if (Modifier != MCSymbolRefExpr::VK_None) {
722 report_fatal_error("unsupported symbol modifier in relocation");
Daniel Dunbarf0f6cdb2010-05-14 18:53:40 +0000723 } else {
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000724 Type = RIT_X86_64_Signed;
Daniel Dunbarf0f6cdb2010-05-14 18:53:40 +0000725
726 // The Darwin x86_64 relocation format has a problem where it cannot
727 // encode an address (L<foo> + <constant>) which is outside the atom
728 // containing L<foo>. Generally, this shouldn't occur but it does
729 // happen when we have a RIPrel instruction with data following the
730 // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
731 // adjustment Darwin x86_64 uses, the offset is still negative and
732 // the linker has no way to recognize this.
733 //
734 // To work around this, Darwin uses several special relocation types
735 // to indicate the offsets. However, the specification or
736 // implementation of these seems to also be incomplete; they should
737 // adjust the addend as well based on the actual encoded instruction
738 // (the additional bias), but instead appear to just look at the
739 // final offset.
740 switch (-(Target.getConstant() + (1LL << Log2Size))) {
741 case 1: Type = RIT_X86_64_Signed1; break;
742 case 2: Type = RIT_X86_64_Signed2; break;
743 case 4: Type = RIT_X86_64_Signed4; break;
744 }
745 }
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000746 } else {
747 if (Modifier != MCSymbolRefExpr::VK_None)
Chris Lattner75361b62010-04-07 22:58:41 +0000748 report_fatal_error("unsupported symbol modifier in branch "
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000749 "relocation");
750
751 Type = RIT_X86_64_Branch;
752 }
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000753 } else {
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000754 if (Modifier == MCSymbolRefExpr::VK_GOT) {
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000755 Type = RIT_X86_64_GOT;
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000756 } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
757 // GOTPCREL is allowed as a modifier on non-PCrel instructions, in
758 // which case all we do is set the PCrel bit in the relocation entry;
759 // this is used with exception handling, for example. The source is
760 // required to include any necessary offset directly.
761 Type = RIT_X86_64_GOT;
762 IsPCRel = 1;
Eric Christopher96ac5152010-05-26 00:02:12 +0000763 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
764 report_fatal_error("TLVP symbol modifier should have been rip-rel");
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000765 } else if (Modifier != MCSymbolRefExpr::VK_None)
Chris Lattner75361b62010-04-07 22:58:41 +0000766 report_fatal_error("unsupported symbol modifier in relocation");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000767 else
768 Type = RIT_X86_64_Unsigned;
769 }
770 }
771
772 // x86_64 always writes custom values into the fixups.
773 FixedValue = Value;
774
775 // struct relocation_info (8 bytes)
776 MachRelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000777 MRE.Word0 = FixupOffset;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000778 MRE.Word1 = ((Index << 0) |
779 (IsPCRel << 24) |
780 (Log2Size << 25) |
781 (IsExtern << 27) |
782 (Type << 28));
Daniel Dunbarb7514182010-03-22 20:35:50 +0000783 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000784 }
785
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000786 void RecordScatteredRelocation(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000787 const MCAsmLayout &Layout,
Daniel Dunbarb7514182010-03-22 20:35:50 +0000788 const MCFragment *Fragment,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000789 const MCFixup &Fixup, MCValue Target,
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000790 uint64_t &FixedValue) {
Daniel Dunbar482ad802010-05-26 15:18:31 +0000791 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
792 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
793 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000794 unsigned Type = RIT_Vanilla;
795
796 // See <reloc.h>.
797 const MCSymbol *A = &Target.getSymA()->getSymbol();
798 MCSymbolData *A_SD = &Asm.getSymbolData(*A);
799
800 if (!A_SD->getFragment())
Chris Lattner75361b62010-04-07 22:58:41 +0000801 report_fatal_error("symbol '" + A->getName() +
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000802 "' can not be undefined in a subtraction expression");
803
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000804 uint32_t Value = Layout.getSymbolAddress(A_SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000805 uint32_t Value2 = 0;
806
807 if (const MCSymbolRefExpr *B = Target.getSymB()) {
808 MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
809
810 if (!B_SD->getFragment())
Chris Lattner75361b62010-04-07 22:58:41 +0000811 report_fatal_error("symbol '" + B->getSymbol().getName() +
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000812 "' can not be undefined in a subtraction expression");
813
814 // Select the appropriate difference relocation type.
815 //
816 // Note that there is no longer any semantic difference between these two
817 // relocation types from the linkers point of view, this is done solely
818 // for pedantic compatibility with 'as'.
819 Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000820 Value2 = Layout.getSymbolAddress(B_SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000821 }
822
823 // Relocations are written out in reverse order, so the PAIR comes first.
824 if (Type == RIT_Difference || Type == RIT_LocalDifference) {
825 MachRelocationEntry MRE;
826 MRE.Word0 = ((0 << 0) |
827 (RIT_Pair << 24) |
828 (Log2Size << 28) |
829 (IsPCRel << 30) |
830 RF_Scattered);
831 MRE.Word1 = Value2;
Daniel Dunbarb7514182010-03-22 20:35:50 +0000832 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000833 }
834
835 MachRelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000836 MRE.Word0 = ((FixupOffset << 0) |
837 (Type << 24) |
838 (Log2Size << 28) |
839 (IsPCRel << 30) |
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000840 RF_Scattered);
841 MRE.Word1 = Value;
Daniel Dunbarb7514182010-03-22 20:35:50 +0000842 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000843 }
844
Eric Christopherc9ada472010-06-15 22:59:05 +0000845 void RecordTLVPRelocation(const MCAssembler &Asm,
Eric Christophere48dbf82010-06-16 00:26:36 +0000846 const MCAsmLayout &Layout,
847 const MCFragment *Fragment,
848 const MCFixup &Fixup, MCValue Target,
849 uint64_t &FixedValue) {
Eric Christopherc9ada472010-06-15 22:59:05 +0000850 assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
851 !Is64Bit &&
852 "Should only be called with a 32-bit TLVP relocation!");
853
Eric Christopherc9ada472010-06-15 22:59:05 +0000854 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
855 uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
856 unsigned IsPCRel = 0;
857
858 // Get the symbol data.
859 MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol());
860 unsigned Index = SD_A->getIndex();
861
Eric Christopherbc067372010-06-16 21:32:38 +0000862 // We're only going to have a second symbol in pic mode and it'll be a
863 // subtraction from the picbase. For 32-bit pic the addend is the difference
Eric Christopher04b8d3c2010-06-17 00:49:46 +0000864 // between the picbase and the next address. For 32-bit static the addend
865 // is zero.
Eric Christopherbc067372010-06-16 21:32:38 +0000866 if (Target.getSymB()) {
Eric Christopher1008d352010-06-22 23:51:47 +0000867 // If this is a subtraction then we're pcrel.
868 uint32_t FixupAddress =
869 Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
870 MCSymbolData *SD_B = &Asm.getSymbolData(Target.getSymB()->getSymbol());
Eric Christopherc9ada472010-06-15 22:59:05 +0000871 IsPCRel = 1;
Eric Christopher1008d352010-06-22 23:51:47 +0000872 FixedValue = (FixupAddress - Layout.getSymbolAddress(SD_B) +
873 Target.getConstant());
Chris Lattnerabf8f9c2010-08-16 16:35:20 +0000874 FixedValue += 1ULL << Log2Size;
Eric Christopherbc067372010-06-16 21:32:38 +0000875 } else {
876 FixedValue = 0;
877 }
Eric Christopherc9ada472010-06-15 22:59:05 +0000878
879 // struct relocation_info (8 bytes)
880 MachRelocationEntry MRE;
881 MRE.Word0 = Value;
882 MRE.Word1 = ((Index << 0) |
883 (IsPCRel << 24) |
884 (Log2Size << 25) |
885 (1 << 27) | // Extern
886 (RIT_TLV << 28)); // Type
887 Relocations[Fragment->getParent()].push_back(MRE);
888 }
889
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000890 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000891 const MCFragment *Fragment, const MCFixup &Fixup,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000892 MCValue Target, uint64_t &FixedValue) {
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000893 if (Is64Bit) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000894 RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000895 return;
896 }
897
Daniel Dunbar482ad802010-05-26 15:18:31 +0000898 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
899 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000900
Eric Christopherc9ada472010-06-15 22:59:05 +0000901 // If this is a 32-bit TLVP reloc it's handled a bit differently.
Daniel Dunbar23bea412010-09-17 15:21:50 +0000902 if (Target.getSymA() &&
903 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
Eric Christopherc9ada472010-06-15 22:59:05 +0000904 RecordTLVPRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
905 return;
906 }
907
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000908 // If this is a difference or a defined symbol plus an offset, then we need
909 // a scattered relocation entry.
Daniel Dunbara8251fa2010-05-10 23:15:20 +0000910 // Differences always require scattered relocations.
911 if (Target.getSymB())
912 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
913 Target, FixedValue);
914
915 // Get the symbol data, if any.
916 MCSymbolData *SD = 0;
917 if (Target.getSymA())
918 SD = &Asm.getSymbolData(Target.getSymA()->getSymbol());
919
920 // If this is an internal relocation with an offset, it also needs a
921 // scattered relocation entry.
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000922 uint32_t Offset = Target.getConstant();
923 if (IsPCRel)
924 Offset += 1 << Log2Size;
Daniel Dunbara8251fa2010-05-10 23:15:20 +0000925 if (Offset && SD && !doesSymbolRequireExternRelocation(SD))
926 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
927 Target, FixedValue);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000928
929 // See <reloc.h>.
Daniel Dunbar482ad802010-05-26 15:18:31 +0000930 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000931 unsigned Index = 0;
932 unsigned IsExtern = 0;
933 unsigned Type = 0;
934
935 if (Target.isAbsolute()) { // constant
936 // SymbolNum of 0 indicates the absolute section.
937 //
938 // FIXME: Currently, these are never generated (see code below). I cannot
939 // find a case where they are actually emitted.
940 Type = RIT_Vanilla;
Michael J. Spencerb0f3b3e2010-08-10 16:00:49 +0000941 } else {
Daniel Dunbare9460ec2010-05-10 23:15:13 +0000942 // Check whether we need an external or internal relocation.
943 if (doesSymbolRequireExternRelocation(SD)) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000944 IsExtern = 1;
945 Index = SD->getIndex();
Daniel Dunbare9460ec2010-05-10 23:15:13 +0000946 // For external relocations, make sure to offset the fixup value to
947 // compensate for the addend of the symbol address, if it was
948 // undefined. This occurs with weak definitions, for example.
949 if (!SD->Symbol->isUndefined())
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +0000950 FixedValue -= Layout.getSymbolAddress(SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000951 } else {
Daniel Dunbar8fb04032010-03-25 08:08:54 +0000952 // The index is the section ordinal (1-based).
953 Index = SD->getFragment()->getParent()->getOrdinal() + 1;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000954 }
955
956 Type = RIT_Vanilla;
957 }
958
959 // struct relocation_info (8 bytes)
960 MachRelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000961 MRE.Word0 = FixupOffset;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000962 MRE.Word1 = ((Index << 0) |
963 (IsPCRel << 24) |
964 (Log2Size << 25) |
965 (IsExtern << 27) |
966 (Type << 28));
Daniel Dunbarb7514182010-03-22 20:35:50 +0000967 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000968 }
969
970 void BindIndirectSymbols(MCAssembler &Asm) {
971 // This is the point where 'as' creates actual symbols for indirect symbols
972 // (in the following two passes). It would be easier for us to do this
973 // sooner when we see the attribute, but that makes getting the order in the
974 // symbol table much more complicated than it is worth.
975 //
976 // FIXME: Revisit this when the dust settles.
977
978 // Bind non lazy symbol pointers first.
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000979 unsigned IndirectIndex = 0;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000980 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000981 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000982 const MCSectionMachO &Section =
Daniel Dunbar56279f42010-05-18 17:28:20 +0000983 cast<MCSectionMachO>(it->SectionData->getSection());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000984
985 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
986 continue;
987
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000988 // Initialize the section indirect symbol base, if necessary.
989 if (!IndirectSymBase.count(it->SectionData))
990 IndirectSymBase[it->SectionData] = IndirectIndex;
991
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000992 Asm.getOrCreateSymbolData(*it->Symbol);
993 }
994
995 // Then lazy symbol pointers and symbol stubs.
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000996 IndirectIndex = 0;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000997 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000998 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000999 const MCSectionMachO &Section =
Daniel Dunbar56279f42010-05-18 17:28:20 +00001000 cast<MCSectionMachO>(it->SectionData->getSection());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001001
1002 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
1003 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
1004 continue;
1005
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +00001006 // Initialize the section indirect symbol base, if necessary.
1007 if (!IndirectSymBase.count(it->SectionData))
1008 IndirectSymBase[it->SectionData] = IndirectIndex;
1009
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001010 // Set the symbol type to undefined lazy, but only on construction.
1011 //
1012 // FIXME: Do not hardcode.
1013 bool Created;
1014 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
1015 if (Created)
1016 Entry.setFlags(Entry.getFlags() | 0x0001);
1017 }
1018 }
1019
1020 /// ComputeSymbolTable - Compute the symbol table data
1021 ///
1022 /// \param StringTable [out] - The string table data.
1023 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
1024 /// string table.
1025 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
1026 std::vector<MachSymbolData> &LocalSymbolData,
1027 std::vector<MachSymbolData> &ExternalSymbolData,
1028 std::vector<MachSymbolData> &UndefinedSymbolData) {
1029 // Build section lookup table.
1030 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
1031 unsigned Index = 1;
1032 for (MCAssembler::iterator it = Asm.begin(),
1033 ie = Asm.end(); it != ie; ++it, ++Index)
1034 SectionIndexMap[&it->getSection()] = Index;
1035 assert(Index <= 256 && "Too many sections!");
1036
1037 // Index 0 is always the empty string.
1038 StringMap<uint64_t> StringIndexMap;
1039 StringTable += '\x00';
1040
1041 // Build the symbol arrays and the string table, but only for non-local
1042 // symbols.
1043 //
1044 // The particular order that we collect the symbols and create the string
1045 // table, then sort the symbols is chosen to match 'as'. Even though it
1046 // doesn't matter for correctness, this is important for letting us diff .o
1047 // files.
1048 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
1049 ie = Asm.symbol_end(); it != ie; ++it) {
1050 const MCSymbol &Symbol = it->getSymbol();
1051
1052 // Ignore non-linker visible symbols.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +00001053 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001054 continue;
1055
1056 if (!it->isExternal() && !Symbol.isUndefined())
1057 continue;
1058
1059 uint64_t &Entry = StringIndexMap[Symbol.getName()];
1060 if (!Entry) {
1061 Entry = StringTable.size();
1062 StringTable += Symbol.getName();
1063 StringTable += '\x00';
1064 }
1065
1066 MachSymbolData MSD;
1067 MSD.SymbolData = it;
1068 MSD.StringIndex = Entry;
1069
1070 if (Symbol.isUndefined()) {
1071 MSD.SectionIndex = 0;
1072 UndefinedSymbolData.push_back(MSD);
1073 } else if (Symbol.isAbsolute()) {
1074 MSD.SectionIndex = 0;
1075 ExternalSymbolData.push_back(MSD);
1076 } else {
1077 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
1078 assert(MSD.SectionIndex && "Invalid section index!");
1079 ExternalSymbolData.push_back(MSD);
1080 }
1081 }
1082
1083 // Now add the data for local symbols.
1084 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
1085 ie = Asm.symbol_end(); it != ie; ++it) {
1086 const MCSymbol &Symbol = it->getSymbol();
1087
1088 // Ignore non-linker visible symbols.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +00001089 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001090 continue;
1091
1092 if (it->isExternal() || Symbol.isUndefined())
1093 continue;
1094
1095 uint64_t &Entry = StringIndexMap[Symbol.getName()];
1096 if (!Entry) {
1097 Entry = StringTable.size();
1098 StringTable += Symbol.getName();
1099 StringTable += '\x00';
1100 }
1101
1102 MachSymbolData MSD;
1103 MSD.SymbolData = it;
1104 MSD.StringIndex = Entry;
1105
1106 if (Symbol.isAbsolute()) {
1107 MSD.SectionIndex = 0;
1108 LocalSymbolData.push_back(MSD);
1109 } else {
1110 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
1111 assert(MSD.SectionIndex && "Invalid section index!");
1112 LocalSymbolData.push_back(MSD);
1113 }
1114 }
1115
1116 // External and undefined symbols are required to be in lexicographic order.
1117 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1118 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1119
1120 // Set the symbol indices.
1121 Index = 0;
1122 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1123 LocalSymbolData[i].SymbolData->setIndex(Index++);
1124 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1125 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1126 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1127 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1128
1129 // The string table is padded to a multiple of 4.
1130 while (StringTable.size() % 4)
1131 StringTable += '\x00';
1132 }
1133
Daniel Dunbar873decb2010-03-20 01:58:40 +00001134 void ExecutePostLayoutBinding(MCAssembler &Asm) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001135 // Create symbol data for any indirect symbols.
1136 BindIndirectSymbols(Asm);
1137
1138 // Compute symbol table information and bind symbol indices.
1139 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
1140 UndefinedSymbolData);
1141 }
1142
Rafael Espindola70703872010-09-30 02:22:20 +00001143
1144 bool IsFixupFullyResolved(const MCAssembler &Asm,
1145 const MCValue Target,
1146 bool IsPCRel,
1147 const MCFragment *DF) const {
1148 // If we are using scattered symbols, determine whether this value is
1149 // actually resolved; scattering may cause atoms to move.
1150 if (Asm.getBackend().hasScatteredSymbols()) {
1151 if (Asm.getBackend().hasReliableSymbolDifference()) {
1152 // If this is a PCrel relocation, find the base atom (identified by its
1153 // symbol) that the fixup value is relative to.
1154 const MCSymbolData *BaseSymbol = 0;
1155 if (IsPCRel) {
1156 BaseSymbol = DF->getAtom();
1157 if (!BaseSymbol)
1158 return false;
1159 }
1160
1161 return isScatteredFixupFullyResolved(Asm, Target, BaseSymbol);
1162 } else {
1163 const MCSection *BaseSection = 0;
1164 if (IsPCRel)
1165 BaseSection = &DF->getParent()->getSection();
1166
1167 return isScatteredFixupFullyResolvedSimple(Asm, Target, BaseSection);
1168 }
1169 }
1170 return true;
1171 }
1172
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001173 void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001174 unsigned NumSections = Asm.size();
1175
1176 // The section data starts after the header, the segment load command (and
1177 // section headers) and the symbol table.
1178 unsigned NumLoadCommands = 1;
1179 uint64_t LoadCommandsSize = Is64Bit ?
1180 SegmentLoadCommand64Size + NumSections * Section64Size :
1181 SegmentLoadCommand32Size + NumSections * Section32Size;
1182
1183 // Add the symbol table load command sizes, if used.
1184 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
1185 UndefinedSymbolData.size();
1186 if (NumSymbols) {
1187 NumLoadCommands += 2;
1188 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
1189 }
1190
1191 // Compute the total size of the section data, as well as its file size and
1192 // vm size.
1193 uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size)
1194 + LoadCommandsSize;
1195 uint64_t SectionDataSize = 0;
1196 uint64_t SectionDataFileSize = 0;
1197 uint64_t VMSize = 0;
1198 for (MCAssembler::const_iterator it = Asm.begin(),
1199 ie = Asm.end(); it != ie; ++it) {
1200 const MCSectionData &SD = *it;
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001201 uint64_t Address = Layout.getSectionAddress(&SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +00001202 uint64_t Size = Layout.getSectionSize(&SD);
1203 uint64_t FileSize = Layout.getSectionFileSize(&SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001204
Daniel Dunbar5d428512010-03-25 02:00:07 +00001205 VMSize = std::max(VMSize, Address + Size);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001206
1207 if (Asm.getBackend().isVirtualSection(SD.getSection()))
1208 continue;
1209
Daniel Dunbar5d428512010-03-25 02:00:07 +00001210 SectionDataSize = std::max(SectionDataSize, Address + Size);
1211 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001212 }
1213
1214 // The section data is padded to 4 bytes.
1215 //
1216 // FIXME: Is this machine dependent?
1217 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
1218 SectionDataFileSize += SectionDataPadding;
1219
1220 // Write the prolog, starting with the header and load command...
1221 WriteHeader(NumLoadCommands, LoadCommandsSize,
1222 Asm.getSubsectionsViaSymbols());
1223 WriteSegmentLoadCommand(NumSections, VMSize,
1224 SectionDataStart, SectionDataSize);
1225
1226 // ... and then the section headers.
1227 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
1228 for (MCAssembler::const_iterator it = Asm.begin(),
1229 ie = Asm.end(); it != ie; ++it) {
1230 std::vector<MachRelocationEntry> &Relocs = Relocations[it];
1231 unsigned NumRelocs = Relocs.size();
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001232 uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it);
1233 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001234 RelocTableEnd += NumRelocs * RelocationInfoSize;
1235 }
1236
1237 // Write the symbol table load command, if used.
1238 if (NumSymbols) {
1239 unsigned FirstLocalSymbol = 0;
1240 unsigned NumLocalSymbols = LocalSymbolData.size();
1241 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
1242 unsigned NumExternalSymbols = ExternalSymbolData.size();
1243 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
1244 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
1245 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
1246 unsigned NumSymTabSymbols =
1247 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
1248 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
1249 uint64_t IndirectSymbolOffset = 0;
1250
1251 // If used, the indirect symbols are written after the section data.
1252 if (NumIndirectSymbols)
1253 IndirectSymbolOffset = RelocTableEnd;
1254
1255 // The symbol table is written after the indirect symbol data.
1256 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
1257
1258 // The string table is written after symbol table.
1259 uint64_t StringTableOffset =
1260 SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size :
1261 Nlist32Size);
1262 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
1263 StringTableOffset, StringTable.size());
1264
1265 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
1266 FirstExternalSymbol, NumExternalSymbols,
1267 FirstUndefinedSymbol, NumUndefinedSymbols,
1268 IndirectSymbolOffset, NumIndirectSymbols);
1269 }
1270
1271 // Write the actual section data.
1272 for (MCAssembler::const_iterator it = Asm.begin(),
1273 ie = Asm.end(); it != ie; ++it)
Daniel Dunbar432cd5f2010-03-25 02:00:02 +00001274 Asm.WriteSectionData(it, Layout, Writer);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001275
1276 // Write the extra padding.
1277 WriteZeros(SectionDataPadding);
1278
1279 // Write the relocation entries.
1280 for (MCAssembler::const_iterator it = Asm.begin(),
1281 ie = Asm.end(); it != ie; ++it) {
1282 // Write the section relocation entries, in reverse order to match 'as'
1283 // (approximately, the exact algorithm is more complicated than this).
1284 std::vector<MachRelocationEntry> &Relocs = Relocations[it];
1285 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1286 Write32(Relocs[e - i - 1].Word0);
1287 Write32(Relocs[e - i - 1].Word1);
1288 }
1289 }
1290
1291 // Write the symbol table data, if used.
1292 if (NumSymbols) {
1293 // Write the indirect symbol entries.
1294 for (MCAssembler::const_indirect_symbol_iterator
1295 it = Asm.indirect_symbol_begin(),
1296 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
1297 // Indirect symbols in the non lazy symbol pointer section have some
1298 // special handling.
1299 const MCSectionMachO &Section =
1300 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
1301 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
1302 // If this symbol is defined and internal, mark it as such.
1303 if (it->Symbol->isDefined() &&
1304 !Asm.getSymbolData(*it->Symbol).isExternal()) {
1305 uint32_t Flags = ISF_Local;
1306 if (it->Symbol->isAbsolute())
1307 Flags |= ISF_Absolute;
1308 Write32(Flags);
1309 continue;
1310 }
1311 }
1312
1313 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
1314 }
1315
1316 // FIXME: Check that offsets match computed ones.
1317
1318 // Write the symbol table entries.
1319 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001320 WriteNlist(LocalSymbolData[i], Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001321 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001322 WriteNlist(ExternalSymbolData[i], Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001323 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001324 WriteNlist(UndefinedSymbolData[i], Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001325
1326 // Write the string table.
1327 OS << StringTable.str();
1328 }
1329 }
1330};
1331
1332}
1333
1334MachObjectWriter::MachObjectWriter(raw_ostream &OS,
1335 bool Is64Bit,
Jim Grosbachc9d14392010-11-05 18:48:58 +00001336 uint32_t CPUType,
1337 uint32_t CPUSubtype,
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001338 bool IsLittleEndian)
1339 : MCObjectWriter(OS, IsLittleEndian)
1340{
Jim Grosbachc9d14392010-11-05 18:48:58 +00001341 Impl = new MachObjectWriterImpl(this, Is64Bit, CPUType, CPUSubtype);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001342}
1343
1344MachObjectWriter::~MachObjectWriter() {
1345 delete (MachObjectWriterImpl*) Impl;
1346}
1347
1348void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1349 ((MachObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1350}
1351
1352void MachObjectWriter::RecordRelocation(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001353 const MCAsmLayout &Layout,
Daniel Dunbarb7514182010-03-22 20:35:50 +00001354 const MCFragment *Fragment,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +00001355 const MCFixup &Fixup, MCValue Target,
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001356 uint64_t &FixedValue) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001357 ((MachObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001358 Target, FixedValue);
1359}
1360
Rafael Espindola70703872010-09-30 02:22:20 +00001361bool MachObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1362 const MCValue Target,
1363 bool IsPCRel,
1364 const MCFragment *DF) const {
1365 return ((MachObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1366 IsPCRel, DF);
1367}
1368
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001369void MachObjectWriter::WriteObject(MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001370 const MCAsmLayout &Layout) {
1371 ((MachObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001372}