| Nick Kledzik | 40b6855 | 2012-06-11 22:53:15 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/WriterOptionsMachO.cpp ----------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 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 "lld/ReaderWriter/WriterMachO.h" |
| 11 | |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "llvm/Support/ErrorHandling.h" |
| 14 | #include "llvm/Support/system_error.h" |
| 15 | |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
| 18 | |
| 19 | #include "MachOFormat.hpp" |
| 20 | |
| 21 | namespace lld { |
| 22 | |
| 23 | WriterOptionsMachO::WriterOptionsMachO() |
| 24 | : _outputkind(outputDynamicExecutable), |
| 25 | _architecture(arch_x86), |
| 26 | _pageZeroSize(0x1000), |
| 27 | _noTextRelocations(true) { |
| 28 | } |
| 29 | |
| 30 | WriterOptionsMachO::~WriterOptionsMachO() { |
| 31 | } |
| 32 | |
| 33 | StringRef WriterOptionsMachO::archName() const { |
| 34 | switch ( _architecture ) { |
| 35 | case arch_x86_64: |
| 36 | return StringRef("x86_64"); |
| 37 | case arch_x86: |
| 38 | return StringRef("i386"); |
| 39 | case arch_armv6: |
| 40 | return StringRef("armv6"); |
| 41 | case arch_armv7: |
| 42 | return StringRef("armv7"); |
| 43 | } |
| 44 | assert(0 && "unknown arch"); |
| 45 | return StringRef("???"); |
| 46 | } |
| 47 | |
| 48 | uint32_t WriterOptionsMachO::cpuType() const { |
| 49 | switch ( _architecture ) { |
| 50 | case arch_x86_64: |
| 51 | return mach_o::CPU_TYPE_X86_64; |
| 52 | case arch_x86: |
| 53 | return mach_o::CPU_TYPE_I386; |
| 54 | case arch_armv6: |
| 55 | case arch_armv7: |
| 56 | return mach_o::CPU_TYPE_ARM; |
| 57 | } |
| 58 | assert(0 && "unknown arch"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | uint32_t WriterOptionsMachO::cpuSubtype() const { |
| 63 | switch ( _architecture ) { |
| 64 | case arch_x86_64: |
| 65 | return mach_o::CPU_SUBTYPE_X86_64_ALL; |
| 66 | case arch_x86: |
| 67 | return mach_o::CPU_SUBTYPE_X86_ALL; |
| 68 | case arch_armv6: |
| 69 | return mach_o::CPU_SUBTYPE_ARM_V6; |
| 70 | case arch_armv7: |
| 71 | return mach_o::CPU_SUBTYPE_ARM_V7; |
| 72 | } |
| 73 | assert(0 && "unknown arch"); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | uint64_t WriterOptionsMachO::pageZeroSize() const { |
| 78 | switch ( _outputkind ) { |
| 79 | case outputDynamicExecutable: |
| 80 | return _pageZeroSize; |
| 81 | case outputDylib: |
| 82 | case outputBundle: |
| 83 | case outputObjectFile: |
| 84 | assert(_pageZeroSize == 0); |
| 85 | return 0; |
| 86 | } |
| 87 | assert(0 && "unknown outputkind"); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | bool WriterOptionsMachO::addEntryPointLoadCommand() const { |
| 92 | switch ( _outputkind ) { |
| 93 | case outputDynamicExecutable: |
| 94 | // Only main executables have an entry point |
| 95 | return false; |
| 96 | case outputDylib: |
| 97 | case outputBundle: |
| 98 | case outputObjectFile: |
| 99 | return false; |
| 100 | } |
| 101 | assert(0 && "unknown outputkind"); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool WriterOptionsMachO::addUnixThreadLoadCommand() const { |
| 106 | switch ( _outputkind ) { |
| 107 | case outputDynamicExecutable: |
| 108 | // Only main executables have an entry point |
| 109 | return true; |
| 110 | case outputDylib: |
| 111 | case outputBundle: |
| 112 | case outputObjectFile: |
| 113 | return false; |
| 114 | } |
| 115 | assert(0 && "unknown outputkind"); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | StringRef WriterOptionsMachO::entryPointName() const { |
| 120 | switch ( _outputkind ) { |
| 121 | case outputDynamicExecutable: |
| 122 | // Only main executables have an entry point |
| 123 | if ( ! _customEntryPointName.empty() ) { |
| 124 | return _customEntryPointName; |
| 125 | } |
| 126 | else { |
| 127 | if ( true || this->addEntryPointLoadCommand() ) |
| 128 | return StringRef("_main"); |
| 129 | else |
| 130 | return StringRef("start"); |
| 131 | } |
| 132 | break; |
| 133 | case outputDylib: |
| 134 | case outputBundle: |
| 135 | case outputObjectFile: |
| 136 | return StringRef(); |
| 137 | } |
| 138 | assert(0 && "unknown outputkind"); |
| 139 | return StringRef(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | } // namespace lld |
| 144 | |