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" |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 22 | |
| 23 | #include <cctype> |
| 24 | #include <cstring> |
| 25 | #include <limits> |
| 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace object; |
| 29 | |
| 30 | namespace llvm { |
| 31 | |
| 32 | typedef MachOObject::LoadCommandInfo LoadCommandInfo; |
| 33 | |
| 34 | class MachOObjectFile : public ObjectFile { |
| 35 | public: |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 36 | MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 37 | |
| 38 | virtual symbol_iterator begin_symbols() const; |
| 39 | virtual symbol_iterator end_symbols() const; |
| 40 | virtual section_iterator begin_sections() const; |
| 41 | virtual section_iterator end_sections() const; |
| 42 | |
| 43 | virtual uint8_t getBytesInAddress() const; |
| 44 | virtual StringRef getFileFormatName() const; |
| 45 | virtual unsigned getArch() const; |
| 46 | |
| 47 | protected: |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 48 | virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; |
| 49 | virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 50 | virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 51 | virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; |
| 52 | virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; |
| 53 | virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; |
| 54 | virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 55 | virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const; |
| 56 | virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 57 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 58 | virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; |
| 59 | virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; |
| 60 | virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; |
| 61 | virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; |
| 62 | virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 63 | virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 64 | virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 65 | virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; |
| 66 | virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 67 | virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S, |
| 68 | bool &Result) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 69 | virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; |
| 70 | virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 71 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 72 | virtual error_code getRelocationNext(DataRefImpl Rel, |
| 73 | RelocationRef &Res) const; |
| 74 | virtual error_code getRelocationAddress(DataRefImpl Rel, |
| 75 | uint64_t &Res) const; |
| 76 | virtual error_code getRelocationSymbol(DataRefImpl Rel, |
| 77 | SymbolRef &Res) const; |
| 78 | virtual error_code getRelocationType(DataRefImpl Rel, |
| 79 | uint32_t &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 80 | virtual error_code getRelocationTypeName(DataRefImpl Rel, |
| 81 | SmallVectorImpl<char> &Result) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 82 | virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, |
| 83 | int64_t &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 84 | virtual error_code getRelocationValueString(DataRefImpl Rel, |
| 85 | SmallVectorImpl<char> &Result) const; |
| 86 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 87 | private: |
| 88 | MachOObject *MachOObj; |
| 89 | mutable uint32_t RegisteredStringTable; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 90 | typedef SmallVector<DataRefImpl, 1> SectionList; |
| 91 | SectionList Sections; |
| 92 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 93 | |
| 94 | void moveToNextSection(DataRefImpl &DRI) const; |
| 95 | void getSymbolTableEntry(DataRefImpl DRI, |
| 96 | InMemoryStruct<macho::SymbolTableEntry> &Res) const; |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 97 | void getSymbol64TableEntry(DataRefImpl DRI, |
| 98 | InMemoryStruct<macho::Symbol64TableEntry> &Res) const; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 99 | void moveToNextSymbol(DataRefImpl &DRI) const; |
| 100 | void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const; |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 101 | void getSection64(DataRefImpl DRI, |
| 102 | InMemoryStruct<macho::Section64> &Res) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 103 | void getRelocation(DataRefImpl Rel, |
| 104 | InMemoryStruct<macho::RelocationEntry> &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 105 | std::size_t getSectionIndex(DataRefImpl Sec) const; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 108 | MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, |
| 109 | error_code &ec) |
| 110 | : ObjectFile(Binary::isMachO, Object, ec), |
| 111 | MachOObj(MOO), |
| 112 | RegisteredStringTable(std::numeric_limits<uint32_t>::max()) { |
| 113 | DataRefImpl DRI; |
| 114 | DRI.d.a = DRI.d.b = 0; |
| 115 | moveToNextSection(DRI); |
| 116 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 117 | while (DRI.d.a < LoadCommandCount) { |
| 118 | Sections.push_back(DRI); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 119 | DRI.d.b++; |
| 120 | moveToNextSection(DRI); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 125 | ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 126 | error_code ec; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 127 | std::string Err; |
| 128 | MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err); |
| 129 | if (!MachOObj) |
| 130 | return NULL; |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 131 | return new MachOObjectFile(Buffer, MachOObj, ec); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /*===-- Symbols -----------------------------------------------------------===*/ |
| 135 | |
| 136 | void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const { |
| 137 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 138 | while (DRI.d.a < LoadCommandCount) { |
| 139 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 140 | if (LCI.Command.Type == macho::LCT_Symtab) { |
| 141 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 142 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 143 | if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries) |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | DRI.d.a++; |
| 148 | DRI.d.b = 0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI, |
| 153 | InMemoryStruct<macho::SymbolTableEntry> &Res) const { |
| 154 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 155 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 156 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 157 | |
| 158 | if (RegisteredStringTable != DRI.d.a) { |
| 159 | MachOObj->RegisterStringTable(*SymtabLoadCmd); |
| 160 | RegisteredStringTable = DRI.d.a; |
| 161 | } |
| 162 | |
| 163 | MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, |
| 164 | Res); |
| 165 | } |
| 166 | |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 167 | void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI, |
| 168 | InMemoryStruct<macho::Symbol64TableEntry> &Res) const { |
| 169 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 170 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 171 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 172 | |
| 173 | if (RegisteredStringTable != DRI.d.a) { |
| 174 | MachOObj->RegisterStringTable(*SymtabLoadCmd); |
| 175 | RegisteredStringTable = DRI.d.a; |
| 176 | } |
| 177 | |
| 178 | MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, |
| 179 | Res); |
| 180 | } |
| 181 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 182 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 183 | error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI, |
| 184 | SymbolRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 185 | DRI.d.b++; |
| 186 | moveToNextSymbol(DRI); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 187 | Result = SymbolRef(DRI, this); |
| 188 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 191 | error_code MachOObjectFile::getSymbolName(DataRefImpl DRI, |
| 192 | StringRef &Result) const { |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 193 | if (MachOObj->is64Bit()) { |
| 194 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 195 | getSymbol64TableEntry(DRI, Entry); |
| 196 | Result = MachOObj->getStringAtIndex(Entry->StringIndex); |
| 197 | } else { |
| 198 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 199 | getSymbolTableEntry(DRI, Entry); |
| 200 | Result = MachOObj->getStringAtIndex(Entry->StringIndex); |
| 201 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 202 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 205 | error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI, |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 206 | uint64_t &Result) const { |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 207 | if (MachOObj->is64Bit()) { |
| 208 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 209 | getSymbol64TableEntry(DRI, Entry); |
| 210 | Result = Entry->Value; |
| 211 | } else { |
| 212 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 213 | getSymbolTableEntry(DRI, Entry); |
| 214 | Result = Entry->Value; |
| 215 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 216 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 219 | error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI, |
| 220 | uint64_t &Result) const { |
| 221 | uint64_t SymbolOffset; |
| 222 | uint8_t SectionIndex; |
| 223 | if (MachOObj->is64Bit()) { |
| 224 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 225 | getSymbol64TableEntry(DRI, Entry); |
| 226 | SymbolOffset = Entry->Value; |
| 227 | SectionIndex = Entry->SectionIndex; |
| 228 | } else { |
| 229 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 230 | getSymbolTableEntry(DRI, Entry); |
| 231 | SymbolOffset = Entry->Value; |
| 232 | SectionIndex = Entry->SectionIndex; |
| 233 | } |
| 234 | getSectionAddress(Sections[SectionIndex], Result); |
| 235 | Result += SymbolOffset; |
| 236 | |
| 237 | return object_error::success; |
| 238 | } |
| 239 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 240 | error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, |
| 241 | uint64_t &Result) const { |
| 242 | Result = UnknownAddressOrSize; |
| 243 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 246 | error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI, |
| 247 | char &Result) const { |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 248 | uint8_t Type, Flags; |
| 249 | if (MachOObj->is64Bit()) { |
| 250 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 251 | getSymbol64TableEntry(DRI, Entry); |
| 252 | Type = Entry->Type; |
| 253 | Flags = Entry->Flags; |
| 254 | } else { |
| 255 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 256 | getSymbolTableEntry(DRI, Entry); |
| 257 | Type = Entry->Type; |
| 258 | Flags = Entry->Flags; |
| 259 | } |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 260 | |
| 261 | char Char; |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 262 | switch (Type & macho::STF_TypeMask) { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 263 | case macho::STT_Undefined: |
| 264 | Char = 'u'; |
| 265 | break; |
| 266 | case macho::STT_Absolute: |
| 267 | case macho::STT_Section: |
| 268 | Char = 's'; |
| 269 | break; |
| 270 | default: |
| 271 | Char = '?'; |
| 272 | break; |
| 273 | } |
| 274 | |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 275 | if (Flags & (macho::STF_External | macho::STF_PrivateExtern)) |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 276 | Char = toupper(Char); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 277 | Result = Char; |
| 278 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 281 | error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI, |
| 282 | bool &Result) const { |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 283 | if (MachOObj->is64Bit()) { |
| 284 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 285 | getSymbol64TableEntry(DRI, Entry); |
| 286 | Result = Entry->Flags & macho::STF_StabsEntryMask; |
| 287 | } else { |
| 288 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 289 | getSymbolTableEntry(DRI, Entry); |
| 290 | Result = Entry->Flags & macho::STF_StabsEntryMask; |
| 291 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 292 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 295 | error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const { |
| 296 | |
| 297 | if (MachOObj->is64Bit()) { |
| 298 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 299 | getSymbol64TableEntry(Symb, Entry); |
| 300 | Res = Entry->Type & MachO::NlistMaskExternal; |
| 301 | } else { |
| 302 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 303 | getSymbolTableEntry(Symb, Entry); |
| 304 | Res = Entry->Type & MachO::NlistMaskExternal; |
| 305 | } |
| 306 | return object_error::success; |
| 307 | } |
| 308 | |
| 309 | error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, |
| 310 | SymbolRef::SymbolType &Res) const { |
| 311 | uint8_t n_type; |
| 312 | if (MachOObj->is64Bit()) { |
| 313 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 314 | getSymbol64TableEntry(Symb, Entry); |
| 315 | n_type = Entry->Type; |
| 316 | } else { |
| 317 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 318 | getSymbolTableEntry(Symb, Entry); |
| 319 | n_type = Entry->Type; |
| 320 | } |
| 321 | Res = SymbolRef::ST_Other; |
| 322 | switch (n_type & MachO::NlistMaskType) { |
| 323 | case MachO::NListTypeUndefined : |
| 324 | Res = SymbolRef::ST_External; |
| 325 | break; |
| 326 | case MachO::NListTypeSection : |
| 327 | Res = SymbolRef::ST_Function; |
| 328 | break; |
| 329 | } |
| 330 | return object_error::success; |
| 331 | } |
| 332 | |
| 333 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 334 | symbol_iterator MachOObjectFile::begin_symbols() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 335 | // DRI.d.a = segment number; DRI.d.b = symbol index. |
| 336 | DataRefImpl DRI; |
| 337 | DRI.d.a = DRI.d.b = 0; |
| 338 | moveToNextSymbol(DRI); |
| 339 | return symbol_iterator(SymbolRef(DRI, this)); |
| 340 | } |
| 341 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 342 | symbol_iterator MachOObjectFile::end_symbols() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 343 | DataRefImpl DRI; |
| 344 | DRI.d.a = MachOObj->getHeader().NumLoadCommands; |
| 345 | DRI.d.b = 0; |
| 346 | return symbol_iterator(SymbolRef(DRI, this)); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | /*===-- Sections ----------------------------------------------------------===*/ |
| 351 | |
| 352 | void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { |
| 353 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 354 | while (DRI.d.a < LoadCommandCount) { |
| 355 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 356 | if (LCI.Command.Type == macho::LCT_Segment) { |
| 357 | InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd; |
| 358 | MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd); |
| 359 | if (DRI.d.b < SegmentLoadCmd->NumSections) |
| 360 | return; |
| 361 | } else if (LCI.Command.Type == macho::LCT_Segment64) { |
| 362 | InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd; |
| 363 | MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd); |
| 364 | if (DRI.d.b < Segment64LoadCmd->NumSections) |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | DRI.d.a++; |
| 369 | DRI.d.b = 0; |
| 370 | } |
| 371 | } |
| 372 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 373 | error_code MachOObjectFile::getSectionNext(DataRefImpl DRI, |
| 374 | SectionRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 375 | DRI.d.b++; |
| 376 | moveToNextSection(DRI); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 377 | Result = SectionRef(DRI, this); |
| 378 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void |
| 382 | MachOObjectFile::getSection(DataRefImpl DRI, |
| 383 | InMemoryStruct<macho::Section> &Res) const { |
| 384 | InMemoryStruct<macho::SegmentLoadCommand> SLC; |
| 385 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 386 | MachOObj->ReadSegmentLoadCommand(LCI, SLC); |
| 387 | MachOObj->ReadSection(LCI, DRI.d.b, Res); |
| 388 | } |
| 389 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 390 | std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { |
| 391 | SectionList::const_iterator loc = |
| 392 | std::find(Sections.begin(), Sections.end(), Sec); |
| 393 | assert(loc != Sections.end() && "Sec is not a valid section!"); |
| 394 | return std::distance(Sections.begin(), loc); |
| 395 | } |
| 396 | |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 397 | void |
| 398 | MachOObjectFile::getSection64(DataRefImpl DRI, |
| 399 | InMemoryStruct<macho::Section64> &Res) const { |
| 400 | InMemoryStruct<macho::Segment64LoadCommand> SLC; |
| 401 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 402 | MachOObj->ReadSegment64LoadCommand(LCI, SLC); |
| 403 | MachOObj->ReadSection64(LCI, DRI.d.b, Res); |
| 404 | } |
| 405 | |
| 406 | static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) { |
| 407 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 408 | if (LCI.Command.Type == macho::LCT_Segment64) |
| 409 | return true; |
| 410 | assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type."); |
| 411 | return false; |
| 412 | } |
| 413 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 414 | error_code MachOObjectFile::getSectionName(DataRefImpl DRI, |
| 415 | StringRef &Result) const { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 416 | // FIXME: thread safety. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 417 | static char result[34]; |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 418 | if (is64BitLoadCommand(MachOObj, DRI)) { |
| 419 | InMemoryStruct<macho::Segment64LoadCommand> SLC; |
| 420 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 421 | MachOObj->ReadSegment64LoadCommand(LCI, SLC); |
| 422 | InMemoryStruct<macho::Section64> Sect; |
| 423 | MachOObj->ReadSection64(LCI, DRI.d.b, Sect); |
| 424 | |
Benjamin Kramer | 291e767 | 2011-07-15 00:29:02 +0000 | [diff] [blame] | 425 | strcpy(result, Sect->SegmentName); |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 426 | strcat(result, ","); |
| 427 | strcat(result, Sect->Name); |
| 428 | } else { |
| 429 | InMemoryStruct<macho::SegmentLoadCommand> SLC; |
| 430 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 431 | MachOObj->ReadSegmentLoadCommand(LCI, SLC); |
| 432 | InMemoryStruct<macho::Section> Sect; |
| 433 | MachOObj->ReadSection(LCI, DRI.d.b, Sect); |
| 434 | |
Benjamin Kramer | 291e767 | 2011-07-15 00:29:02 +0000 | [diff] [blame] | 435 | strcpy(result, Sect->SegmentName); |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 436 | strcat(result, ","); |
| 437 | strcat(result, Sect->Name); |
| 438 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 439 | Result = StringRef(result); |
| 440 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 443 | error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, |
| 444 | uint64_t &Result) const { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 445 | if (is64BitLoadCommand(MachOObj, DRI)) { |
| 446 | InMemoryStruct<macho::Section64> Sect; |
| 447 | getSection64(DRI, Sect); |
| 448 | Result = Sect->Address; |
| 449 | } else { |
| 450 | InMemoryStruct<macho::Section> Sect; |
| 451 | getSection(DRI, Sect); |
| 452 | Result = Sect->Address; |
| 453 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 454 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 457 | error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, |
| 458 | uint64_t &Result) const { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 459 | if (is64BitLoadCommand(MachOObj, DRI)) { |
| 460 | InMemoryStruct<macho::Section64> Sect; |
| 461 | getSection64(DRI, Sect); |
| 462 | Result = Sect->Size; |
| 463 | } else { |
| 464 | InMemoryStruct<macho::Section> Sect; |
| 465 | getSection(DRI, Sect); |
| 466 | Result = Sect->Size; |
| 467 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 468 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 471 | error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, |
| 472 | StringRef &Result) const { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 473 | if (is64BitLoadCommand(MachOObj, DRI)) { |
| 474 | InMemoryStruct<macho::Section64> Sect; |
| 475 | getSection64(DRI, Sect); |
| 476 | Result = MachOObj->getData(Sect->Offset, Sect->Size); |
| 477 | } else { |
| 478 | InMemoryStruct<macho::Section> Sect; |
| 479 | getSection(DRI, Sect); |
| 480 | Result = MachOObj->getData(Sect->Offset, Sect->Size); |
| 481 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 482 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 485 | error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI, |
| 486 | uint64_t &Result) const { |
| 487 | if (is64BitLoadCommand(MachOObj, DRI)) { |
| 488 | InMemoryStruct<macho::Section64> Sect; |
| 489 | getSection64(DRI, Sect); |
Michael J. Spencer | 15565ad | 2011-10-10 23:36:56 +0000 | [diff] [blame^] | 490 | Result = uint64_t(1) << Sect->Align; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 491 | } else { |
| 492 | InMemoryStruct<macho::Section> Sect; |
| 493 | getSection(DRI, Sect); |
Michael J. Spencer | 15565ad | 2011-10-10 23:36:56 +0000 | [diff] [blame^] | 494 | Result = uint64_t(1) << Sect->Align; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 495 | } |
| 496 | return object_error::success; |
| 497 | } |
| 498 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 499 | error_code MachOObjectFile::isSectionText(DataRefImpl DRI, |
| 500 | bool &Result) const { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 501 | if (is64BitLoadCommand(MachOObj, DRI)) { |
| 502 | InMemoryStruct<macho::Section64> Sect; |
| 503 | getSection64(DRI, Sect); |
| 504 | Result = !strcmp(Sect->Name, "__text"); |
| 505 | } else { |
| 506 | InMemoryStruct<macho::Section> Sect; |
| 507 | getSection(DRI, Sect); |
| 508 | Result = !strcmp(Sect->Name, "__text"); |
| 509 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 510 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 513 | error_code MachOObjectFile::isSectionData(DataRefImpl DRI, |
| 514 | bool &Result) const { |
| 515 | // FIXME: Unimplemented. |
| 516 | Result = false; |
| 517 | return object_error::success; |
| 518 | } |
| 519 | |
| 520 | error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, |
| 521 | bool &Result) const { |
| 522 | // FIXME: Unimplemented. |
| 523 | Result = false; |
| 524 | return object_error::success; |
| 525 | } |
| 526 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 527 | error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, |
| 528 | DataRefImpl Symb, |
| 529 | bool &Result) const { |
| 530 | if (MachOObj->is64Bit()) { |
| 531 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 532 | getSymbol64TableEntry(Symb, Entry); |
| 533 | Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b; |
| 534 | } else { |
| 535 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 536 | getSymbolTableEntry(Symb, Entry); |
| 537 | Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b; |
| 538 | } |
| 539 | return object_error::success; |
| 540 | } |
| 541 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 542 | relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { |
| 543 | DataRefImpl ret; |
| 544 | ret.d.a = 0; |
| 545 | ret.d.b = getSectionIndex(Sec); |
| 546 | return relocation_iterator(RelocationRef(ret, this)); |
| 547 | } |
| 548 | relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { |
| 549 | uint32_t last_reloc; |
| 550 | if (is64BitLoadCommand(MachOObj, Sec)) { |
| 551 | InMemoryStruct<macho::Section64> Sect; |
| 552 | getSection64(Sec, Sect); |
| 553 | last_reloc = Sect->NumRelocationTableEntries; |
| 554 | } else { |
| 555 | InMemoryStruct<macho::Section> Sect; |
| 556 | getSection(Sec, Sect); |
| 557 | last_reloc = Sect->NumRelocationTableEntries; |
| 558 | } |
| 559 | DataRefImpl ret; |
| 560 | ret.d.a = last_reloc; |
| 561 | ret.d.b = getSectionIndex(Sec); |
| 562 | return relocation_iterator(RelocationRef(ret, this)); |
| 563 | } |
| 564 | |
| 565 | section_iterator MachOObjectFile::begin_sections() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 566 | DataRefImpl DRI; |
| 567 | DRI.d.a = DRI.d.b = 0; |
| 568 | moveToNextSection(DRI); |
| 569 | return section_iterator(SectionRef(DRI, this)); |
| 570 | } |
| 571 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 572 | section_iterator MachOObjectFile::end_sections() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 573 | DataRefImpl DRI; |
| 574 | DRI.d.a = MachOObj->getHeader().NumLoadCommands; |
| 575 | DRI.d.b = 0; |
| 576 | return section_iterator(SectionRef(DRI, this)); |
| 577 | } |
| 578 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 579 | /*===-- Relocations -------------------------------------------------------===*/ |
| 580 | |
| 581 | void MachOObjectFile:: |
| 582 | getRelocation(DataRefImpl Rel, |
| 583 | InMemoryStruct<macho::RelocationEntry> &Res) const { |
| 584 | uint32_t relOffset; |
| 585 | if (MachOObj->is64Bit()) { |
| 586 | InMemoryStruct<macho::Section64> Sect; |
| 587 | getSection64(Sections[Rel.d.b], Sect); |
| 588 | relOffset = Sect->RelocationTableOffset; |
| 589 | } else { |
| 590 | InMemoryStruct<macho::Section> Sect; |
| 591 | getSection(Sections[Rel.d.b], Sect); |
| 592 | relOffset = Sect->RelocationTableOffset; |
| 593 | } |
| 594 | MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res); |
| 595 | } |
| 596 | error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel, |
| 597 | RelocationRef &Res) const { |
| 598 | ++Rel.d.a; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 599 | Res = RelocationRef(Rel, this); |
| 600 | return object_error::success; |
| 601 | } |
| 602 | error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 603 | uint64_t &Res) const { |
| 604 | const uint8_t* sectAddress = base(); |
| 605 | if (MachOObj->is64Bit()) { |
| 606 | InMemoryStruct<macho::Section64> Sect; |
| 607 | getSection64(Sections[Rel.d.b], Sect); |
| 608 | sectAddress += Sect->Offset; |
| 609 | } else { |
| 610 | InMemoryStruct<macho::Section> Sect; |
| 611 | getSection(Sections[Rel.d.b], Sect); |
| 612 | sectAddress += Sect->Offset; |
| 613 | } |
| 614 | InMemoryStruct<macho::RelocationEntry> RE; |
| 615 | getRelocation(Rel, RE); |
| 616 | Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0); |
| 617 | return object_error::success; |
| 618 | } |
| 619 | error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, |
| 620 | SymbolRef &Res) const { |
| 621 | InMemoryStruct<macho::RelocationEntry> RE; |
| 622 | getRelocation(Rel, RE); |
| 623 | uint32_t SymbolIdx = RE->Word1 & 0xffffff; |
| 624 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 625 | |
| 626 | DataRefImpl Sym; |
| 627 | Sym.d.a = Sym.d.b = 0; |
| 628 | moveToNextSymbol(Sym); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 629 | if (isExtern) { |
| 630 | for (unsigned i = 0; i < SymbolIdx; i++) { |
| 631 | Sym.d.b++; |
| 632 | moveToNextSymbol(Sym); |
Nick Lewycky | 58856ea | 2011-09-09 00:16:50 +0000 | [diff] [blame] | 633 | assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands && |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 634 | "Relocation symbol index out of range!"); |
| 635 | } |
| 636 | } |
| 637 | Res = SymbolRef(Sym, this); |
| 638 | return object_error::success; |
| 639 | } |
| 640 | error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, |
| 641 | uint32_t &Res) const { |
| 642 | InMemoryStruct<macho::RelocationEntry> RE; |
| 643 | getRelocation(Rel, RE); |
| 644 | Res = RE->Word1; |
| 645 | return object_error::success; |
| 646 | } |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 647 | error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 648 | SmallVectorImpl<char> &Result) const { |
| 649 | return object_error::success; |
| 650 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 651 | error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 652 | int64_t &Res) const { |
| 653 | InMemoryStruct<macho::RelocationEntry> RE; |
| 654 | getRelocation(Rel, RE); |
| 655 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 656 | Res = 0; |
| 657 | if (!isExtern) { |
| 658 | const uint8_t* sectAddress = base(); |
| 659 | if (MachOObj->is64Bit()) { |
| 660 | InMemoryStruct<macho::Section64> Sect; |
| 661 | getSection64(Sections[Rel.d.b], Sect); |
| 662 | sectAddress += Sect->Offset; |
| 663 | } else { |
| 664 | InMemoryStruct<macho::Section> Sect; |
| 665 | getSection(Sections[Rel.d.b], Sect); |
| 666 | sectAddress += Sect->Offset; |
| 667 | } |
| 668 | Res = reinterpret_cast<uintptr_t>(sectAddress); |
| 669 | } |
| 670 | return object_error::success; |
| 671 | } |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 672 | error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel, |
| 673 | SmallVectorImpl<char> &Result) const { |
| 674 | return object_error::success; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 677 | /*===-- Miscellaneous -----------------------------------------------------===*/ |
| 678 | |
| 679 | uint8_t MachOObjectFile::getBytesInAddress() const { |
| 680 | return MachOObj->is64Bit() ? 8 : 4; |
| 681 | } |
| 682 | |
| 683 | StringRef MachOObjectFile::getFileFormatName() const { |
| 684 | if (!MachOObj->is64Bit()) { |
| 685 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 686 | case llvm::MachO::CPUTypeI386: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 687 | return "Mach-O 32-bit i386"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 688 | case llvm::MachO::CPUTypeARM: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 689 | return "Mach-O arm"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 690 | case llvm::MachO::CPUTypePowerPC: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 691 | return "Mach-O 32-bit ppc"; |
| 692 | default: |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 693 | assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 && |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 694 | "64-bit object file when we're not 64-bit?"); |
| 695 | return "Mach-O 32-bit unknown"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 700 | case llvm::MachO::CPUTypeX86_64: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 701 | return "Mach-O 64-bit x86-64"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 702 | case llvm::MachO::CPUTypePowerPC64: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 703 | return "Mach-O 64-bit ppc64"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 704 | default: |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 705 | assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 && |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 706 | "32-bit object file when we're 64-bit?"); |
| 707 | return "Mach-O 64-bit unknown"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | unsigned MachOObjectFile::getArch() const { |
| 712 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 713 | case llvm::MachO::CPUTypeI386: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 714 | return Triple::x86; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 715 | case llvm::MachO::CPUTypeX86_64: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 716 | return Triple::x86_64; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 717 | case llvm::MachO::CPUTypeARM: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 718 | return Triple::arm; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 719 | case llvm::MachO::CPUTypePowerPC: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 720 | return Triple::ppc; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 721 | case llvm::MachO::CPUTypePowerPC64: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 722 | return Triple::ppc64; |
| 723 | default: |
| 724 | return Triple::UnknownArch; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | } // end namespace llvm |