Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1 | //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===// |
| 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 | // This file defines the MachOObjectFile class, which binds the MachOObject |
| 11 | // class to the generic ObjectFile wrapper. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/Triple.h" |
| 16 | #include "llvm/Object/MachOFormat.h" |
| 17 | #include "llvm/Object/MachOObject.h" |
| 18 | #include "llvm/Object/ObjectFile.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MachO.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 21 | |
| 22 | #include <cctype> |
| 23 | #include <cstring> |
| 24 | #include <limits> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace object; |
| 28 | |
| 29 | namespace llvm { |
| 30 | |
| 31 | typedef MachOObject::LoadCommandInfo LoadCommandInfo; |
| 32 | |
| 33 | class MachOObjectFile : public ObjectFile { |
| 34 | public: |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 35 | MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec) |
| 36 | : ObjectFile(Binary::isMachO, Object, ec), |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 37 | MachOObj(MOO), |
| 38 | RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {} |
| 39 | |
| 40 | virtual symbol_iterator begin_symbols() const; |
| 41 | virtual symbol_iterator end_symbols() const; |
| 42 | virtual section_iterator begin_sections() const; |
| 43 | virtual section_iterator end_sections() const; |
| 44 | |
| 45 | virtual uint8_t getBytesInAddress() const; |
| 46 | virtual StringRef getFileFormatName() const; |
| 47 | virtual unsigned getArch() const; |
| 48 | |
| 49 | protected: |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 50 | virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; |
| 51 | virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; |
| 52 | virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; |
| 53 | virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; |
| 54 | virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; |
| 55 | virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 56 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 57 | virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; |
| 58 | virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; |
| 59 | virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; |
| 60 | virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; |
| 61 | virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; |
| 62 | virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | MachOObject *MachOObj; |
| 66 | mutable uint32_t RegisteredStringTable; |
| 67 | |
| 68 | void moveToNextSection(DataRefImpl &DRI) const; |
| 69 | void getSymbolTableEntry(DataRefImpl DRI, |
| 70 | InMemoryStruct<macho::SymbolTableEntry> &Res) const; |
| 71 | void moveToNextSymbol(DataRefImpl &DRI) const; |
| 72 | void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const; |
| 73 | }; |
| 74 | |
| 75 | ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 76 | error_code ec; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 77 | std::string Err; |
| 78 | MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err); |
| 79 | if (!MachOObj) |
| 80 | return NULL; |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 81 | return new MachOObjectFile(Buffer, MachOObj, ec); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /*===-- Symbols -----------------------------------------------------------===*/ |
| 85 | |
| 86 | void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const { |
| 87 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 88 | while (DRI.d.a < LoadCommandCount) { |
| 89 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 90 | if (LCI.Command.Type == macho::LCT_Symtab) { |
| 91 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 92 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 93 | if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries) |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | DRI.d.a++; |
| 98 | DRI.d.b = 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI, |
| 103 | InMemoryStruct<macho::SymbolTableEntry> &Res) const { |
| 104 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 105 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 106 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 107 | |
| 108 | if (RegisteredStringTable != DRI.d.a) { |
| 109 | MachOObj->RegisterStringTable(*SymtabLoadCmd); |
| 110 | RegisteredStringTable = DRI.d.a; |
| 111 | } |
| 112 | |
| 113 | MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, |
| 114 | Res); |
| 115 | } |
| 116 | |
| 117 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 118 | error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI, |
| 119 | SymbolRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 120 | DRI.d.b++; |
| 121 | moveToNextSymbol(DRI); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 122 | Result = SymbolRef(DRI, this); |
| 123 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 126 | error_code MachOObjectFile::getSymbolName(DataRefImpl DRI, |
| 127 | StringRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 128 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 129 | getSymbolTableEntry(DRI, Entry); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 130 | Result = MachOObj->getStringAtIndex(Entry->StringIndex); |
| 131 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 134 | error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI, |
| 135 | uint64_t &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 136 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 137 | getSymbolTableEntry(DRI, Entry); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 138 | Result = Entry->Value; |
| 139 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 142 | error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, |
| 143 | uint64_t &Result) const { |
| 144 | Result = UnknownAddressOrSize; |
| 145 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 148 | error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI, |
| 149 | char &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 150 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 151 | getSymbolTableEntry(DRI, Entry); |
| 152 | |
| 153 | char Char; |
| 154 | switch (Entry->Type & macho::STF_TypeMask) { |
| 155 | case macho::STT_Undefined: |
| 156 | Char = 'u'; |
| 157 | break; |
| 158 | case macho::STT_Absolute: |
| 159 | case macho::STT_Section: |
| 160 | Char = 's'; |
| 161 | break; |
| 162 | default: |
| 163 | Char = '?'; |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern)) |
| 168 | Char = toupper(Char); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 169 | Result = Char; |
| 170 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 173 | error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI, |
| 174 | bool &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 175 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 176 | getSymbolTableEntry(DRI, Entry); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 177 | Result = Entry->Flags & macho::STF_StabsEntryMask; |
| 178 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const { |
| 182 | // DRI.d.a = segment number; DRI.d.b = symbol index. |
| 183 | DataRefImpl DRI; |
| 184 | DRI.d.a = DRI.d.b = 0; |
| 185 | moveToNextSymbol(DRI); |
| 186 | return symbol_iterator(SymbolRef(DRI, this)); |
| 187 | } |
| 188 | |
| 189 | ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const { |
| 190 | DataRefImpl DRI; |
| 191 | DRI.d.a = MachOObj->getHeader().NumLoadCommands; |
| 192 | DRI.d.b = 0; |
| 193 | return symbol_iterator(SymbolRef(DRI, this)); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /*===-- Sections ----------------------------------------------------------===*/ |
| 198 | |
| 199 | void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { |
| 200 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 201 | while (DRI.d.a < LoadCommandCount) { |
| 202 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 203 | if (LCI.Command.Type == macho::LCT_Segment) { |
| 204 | InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd; |
| 205 | MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd); |
| 206 | if (DRI.d.b < SegmentLoadCmd->NumSections) |
| 207 | return; |
| 208 | } else if (LCI.Command.Type == macho::LCT_Segment64) { |
| 209 | InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd; |
| 210 | MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd); |
| 211 | if (DRI.d.b < Segment64LoadCmd->NumSections) |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | DRI.d.a++; |
| 216 | DRI.d.b = 0; |
| 217 | } |
| 218 | } |
| 219 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 220 | error_code MachOObjectFile::getSectionNext(DataRefImpl DRI, |
| 221 | SectionRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 222 | DRI.d.b++; |
| 223 | moveToNextSection(DRI); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 224 | Result = SectionRef(DRI, this); |
| 225 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
| 229 | MachOObjectFile::getSection(DataRefImpl DRI, |
| 230 | InMemoryStruct<macho::Section> &Res) const { |
| 231 | InMemoryStruct<macho::SegmentLoadCommand> SLC; |
| 232 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 233 | MachOObj->ReadSegmentLoadCommand(LCI, SLC); |
| 234 | MachOObj->ReadSection(LCI, DRI.d.b, Res); |
| 235 | } |
| 236 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 237 | error_code MachOObjectFile::getSectionName(DataRefImpl DRI, |
| 238 | StringRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 239 | InMemoryStruct<macho::SegmentLoadCommand> SLC; |
| 240 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 241 | MachOObj->ReadSegmentLoadCommand(LCI, SLC); |
| 242 | InMemoryStruct<macho::Section> Sect; |
| 243 | MachOObj->ReadSection(LCI, DRI.d.b, Sect); |
| 244 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 245 | static char result[34]; |
| 246 | strcpy(result, SLC->Name); |
| 247 | strcat(result, ","); |
| 248 | strcat(result, Sect->Name); |
| 249 | Result = StringRef(result); |
| 250 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 253 | error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, |
| 254 | uint64_t &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 255 | InMemoryStruct<macho::Section> Sect; |
| 256 | getSection(DRI, Sect); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 257 | Result = Sect->Address; |
| 258 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 261 | error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, |
| 262 | uint64_t &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 263 | InMemoryStruct<macho::Section> Sect; |
| 264 | getSection(DRI, Sect); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 265 | Result = Sect->Size; |
| 266 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 269 | error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, |
| 270 | StringRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 271 | InMemoryStruct<macho::Section> Sect; |
| 272 | getSection(DRI, Sect); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 273 | Result = MachOObj->getData(Sect->Offset, Sect->Size); |
| 274 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 277 | error_code MachOObjectFile::isSectionText(DataRefImpl DRI, |
| 278 | bool &Result) const { |
Benjamin Kramer | 82ba5b7 | 2011-07-15 00:14:46 +0000 | [diff] [blame^] | 279 | InMemoryStruct<macho::Section> Sect; |
| 280 | getSection(DRI, Sect); |
| 281 | Result = !strcmp(Sect->Name, "__text"); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 282 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | ObjectFile::section_iterator MachOObjectFile::begin_sections() const { |
| 286 | DataRefImpl DRI; |
| 287 | DRI.d.a = DRI.d.b = 0; |
| 288 | moveToNextSection(DRI); |
| 289 | return section_iterator(SectionRef(DRI, this)); |
| 290 | } |
| 291 | |
| 292 | ObjectFile::section_iterator MachOObjectFile::end_sections() const { |
| 293 | DataRefImpl DRI; |
| 294 | DRI.d.a = MachOObj->getHeader().NumLoadCommands; |
| 295 | DRI.d.b = 0; |
| 296 | return section_iterator(SectionRef(DRI, this)); |
| 297 | } |
| 298 | |
| 299 | /*===-- Miscellaneous -----------------------------------------------------===*/ |
| 300 | |
| 301 | uint8_t MachOObjectFile::getBytesInAddress() const { |
| 302 | return MachOObj->is64Bit() ? 8 : 4; |
| 303 | } |
| 304 | |
| 305 | StringRef MachOObjectFile::getFileFormatName() const { |
| 306 | if (!MachOObj->is64Bit()) { |
| 307 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 308 | case llvm::MachO::CPUTypeI386: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 309 | return "Mach-O 32-bit i386"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 310 | case llvm::MachO::CPUTypeARM: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 311 | return "Mach-O arm"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 312 | case llvm::MachO::CPUTypePowerPC: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 313 | return "Mach-O 32-bit ppc"; |
| 314 | default: |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 315 | assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 && |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 316 | "64-bit object file when we're not 64-bit?"); |
| 317 | return "Mach-O 32-bit unknown"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 322 | case llvm::MachO::CPUTypeX86_64: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 323 | return "Mach-O 64-bit x86-64"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 324 | case llvm::MachO::CPUTypePowerPC64: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 325 | return "Mach-O 64-bit ppc64"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 326 | default: |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 327 | assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 && |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 328 | "32-bit object file when we're 64-bit?"); |
| 329 | return "Mach-O 64-bit unknown"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | unsigned MachOObjectFile::getArch() const { |
| 334 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 335 | case llvm::MachO::CPUTypeI386: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 336 | return Triple::x86; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 337 | case llvm::MachO::CPUTypeX86_64: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 338 | return Triple::x86_64; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 339 | case llvm::MachO::CPUTypeARM: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 340 | return Triple::arm; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 341 | case llvm::MachO::CPUTypePowerPC: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 342 | return Triple::ppc; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 343 | case llvm::MachO::CPUTypePowerPC64: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 344 | return Triple::ppc64; |
| 345 | default: |
| 346 | return Triple::UnknownArch; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | } // end namespace llvm |
| 351 | |