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 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 15 | #include "llvm/Object/MachO.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 17 | #include "llvm/Object/MachOFormat.h" |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 20 | #include <cctype> |
| 21 | #include <cstring> |
| 22 | #include <limits> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace object; |
| 26 | |
| 27 | namespace llvm { |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 28 | namespace object { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 29 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 30 | MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, |
| 31 | error_code &ec) |
David Meyer | 6f9489a | 2012-03-09 20:41:57 +0000 | [diff] [blame] | 32 | : ObjectFile(Binary::ID_MachO, Object, ec), |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 33 | MachOObj(MOO), |
| 34 | RegisteredStringTable(std::numeric_limits<uint32_t>::max()) { |
| 35 | DataRefImpl DRI; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 36 | moveToNextSection(DRI); |
| 37 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 38 | while (DRI.d.a < LoadCommandCount) { |
| 39 | Sections.push_back(DRI); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 40 | DRI.d.b++; |
| 41 | moveToNextSection(DRI); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 46 | ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 47 | error_code ec; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 48 | std::string Err; |
| 49 | MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err); |
| 50 | if (!MachOObj) |
| 51 | return NULL; |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 52 | // MachOObject takes ownership of the Buffer we passed to it, and |
| 53 | // MachOObjectFile does, too, so we need to make sure they don't get the |
| 54 | // same object. A MemoryBuffer is cheap (it's just a reference to memory, |
| 55 | // not a copy of the memory itself), so just make a new copy here for |
| 56 | // the MachOObjectFile. |
| 57 | MemoryBuffer *NewBuffer = |
Benjamin Kramer | f56c3e2 | 2012-11-29 20:08:03 +0000 | [diff] [blame] | 58 | MemoryBuffer::getMemBuffer(Buffer->getBuffer(), |
| 59 | Buffer->getBufferIdentifier(), false); |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 60 | return new MachOObjectFile(NewBuffer, MachOObj, ec); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /*===-- Symbols -----------------------------------------------------------===*/ |
| 64 | |
| 65 | void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const { |
| 66 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 67 | while (DRI.d.a < LoadCommandCount) { |
| 68 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 69 | if (LCI.Command.Type == macho::LCT_Symtab) { |
| 70 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 71 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 72 | if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries) |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | DRI.d.a++; |
| 77 | DRI.d.b = 0; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI, |
| 82 | InMemoryStruct<macho::SymbolTableEntry> &Res) const { |
| 83 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 84 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 85 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 86 | |
| 87 | if (RegisteredStringTable != DRI.d.a) { |
| 88 | MachOObj->RegisterStringTable(*SymtabLoadCmd); |
| 89 | RegisteredStringTable = DRI.d.a; |
| 90 | } |
| 91 | |
| 92 | MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, |
| 93 | Res); |
| 94 | } |
| 95 | |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 96 | void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI, |
| 97 | InMemoryStruct<macho::Symbol64TableEntry> &Res) const { |
| 98 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd; |
| 99 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 100 | MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd); |
| 101 | |
| 102 | if (RegisteredStringTable != DRI.d.a) { |
| 103 | MachOObj->RegisterStringTable(*SymtabLoadCmd); |
| 104 | RegisteredStringTable = DRI.d.a; |
| 105 | } |
| 106 | |
| 107 | MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b, |
| 108 | Res); |
| 109 | } |
| 110 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 111 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 112 | error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI, |
| 113 | SymbolRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 114 | DRI.d.b++; |
| 115 | moveToNextSymbol(DRI); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 116 | Result = SymbolRef(DRI, this); |
| 117 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 120 | error_code MachOObjectFile::getSymbolName(DataRefImpl DRI, |
| 121 | StringRef &Result) const { |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 122 | if (MachOObj->is64Bit()) { |
| 123 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 124 | getSymbol64TableEntry(DRI, Entry); |
| 125 | Result = MachOObj->getStringAtIndex(Entry->StringIndex); |
| 126 | } else { |
| 127 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 128 | getSymbolTableEntry(DRI, Entry); |
| 129 | Result = MachOObj->getStringAtIndex(Entry->StringIndex); |
| 130 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 131 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 134 | error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI, |
| 135 | uint64_t &Result) const { |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 136 | if (MachOObj->is64Bit()) { |
| 137 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 138 | getSymbol64TableEntry(DRI, Entry); |
| 139 | Result = Entry->Value; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 140 | if (Entry->SectionIndex) { |
| 141 | InMemoryStruct<macho::Section64> Section; |
| 142 | getSection64(Sections[Entry->SectionIndex-1], Section); |
| 143 | Result += Section->Offset - Section->Address; |
| 144 | } |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 145 | } else { |
| 146 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 147 | getSymbolTableEntry(DRI, Entry); |
| 148 | Result = Entry->Value; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 149 | if (Entry->SectionIndex) { |
| 150 | InMemoryStruct<macho::Section> Section; |
| 151 | getSection(Sections[Entry->SectionIndex-1], Section); |
| 152 | Result += Section->Offset - Section->Address; |
| 153 | } |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 154 | } |
Owen Anderson | 95f8db4 | 2011-10-12 22:37:10 +0000 | [diff] [blame] | 155 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 156 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 159 | error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI, |
| 160 | uint64_t &Result) const { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 161 | if (MachOObj->is64Bit()) { |
| 162 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 163 | getSymbol64TableEntry(DRI, Entry); |
Owen Anderson | 95f8db4 | 2011-10-12 22:37:10 +0000 | [diff] [blame] | 164 | Result = Entry->Value; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 165 | } else { |
| 166 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 167 | getSymbolTableEntry(DRI, Entry); |
Owen Anderson | 95f8db4 | 2011-10-12 22:37:10 +0000 | [diff] [blame] | 168 | Result = Entry->Value; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 169 | } |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 170 | return object_error::success; |
| 171 | } |
| 172 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 173 | error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI, |
| 174 | uint64_t &Result) const { |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 175 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 176 | uint64_t BeginOffset; |
| 177 | uint64_t EndOffset = 0; |
| 178 | uint8_t SectionIndex; |
| 179 | if (MachOObj->is64Bit()) { |
| 180 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 181 | getSymbol64TableEntry(DRI, Entry); |
| 182 | BeginOffset = Entry->Value; |
| 183 | SectionIndex = Entry->SectionIndex; |
| 184 | if (!SectionIndex) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 185 | uint32_t flags = SymbolRef::SF_None; |
| 186 | getSymbolFlags(DRI, flags); |
| 187 | if (flags & SymbolRef::SF_Common) |
| 188 | Result = Entry->Value; |
| 189 | else |
| 190 | Result = UnknownAddressOrSize; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 191 | return object_error::success; |
| 192 | } |
| 193 | // Unfortunately symbols are unsorted so we need to touch all |
| 194 | // symbols from load command |
| 195 | DRI.d.b = 0; |
| 196 | uint32_t Command = DRI.d.a; |
| 197 | while (Command == DRI.d.a) { |
| 198 | moveToNextSymbol(DRI); |
| 199 | if (DRI.d.a < LoadCommandCount) { |
| 200 | getSymbol64TableEntry(DRI, Entry); |
| 201 | if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset) |
| 202 | if (!EndOffset || Entry->Value < EndOffset) |
| 203 | EndOffset = Entry->Value; |
| 204 | } |
| 205 | DRI.d.b++; |
| 206 | } |
| 207 | } else { |
| 208 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 209 | getSymbolTableEntry(DRI, Entry); |
| 210 | BeginOffset = Entry->Value; |
| 211 | SectionIndex = Entry->SectionIndex; |
| 212 | if (!SectionIndex) { |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 213 | uint32_t flags = SymbolRef::SF_None; |
| 214 | getSymbolFlags(DRI, flags); |
| 215 | if (flags & SymbolRef::SF_Common) |
| 216 | Result = Entry->Value; |
| 217 | else |
| 218 | Result = UnknownAddressOrSize; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 219 | return object_error::success; |
| 220 | } |
| 221 | // Unfortunately symbols are unsorted so we need to touch all |
| 222 | // symbols from load command |
| 223 | DRI.d.b = 0; |
| 224 | uint32_t Command = DRI.d.a; |
| 225 | while (Command == DRI.d.a) { |
| 226 | moveToNextSymbol(DRI); |
| 227 | if (DRI.d.a < LoadCommandCount) { |
| 228 | getSymbolTableEntry(DRI, Entry); |
| 229 | if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset) |
| 230 | if (!EndOffset || Entry->Value < EndOffset) |
| 231 | EndOffset = Entry->Value; |
| 232 | } |
| 233 | DRI.d.b++; |
| 234 | } |
| 235 | } |
| 236 | if (!EndOffset) { |
| 237 | uint64_t Size; |
| 238 | getSectionSize(Sections[SectionIndex-1], Size); |
| 239 | getSectionAddress(Sections[SectionIndex-1], EndOffset); |
| 240 | EndOffset += Size; |
| 241 | } |
| 242 | Result = EndOffset - BeginOffset; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 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 | |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 281 | error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI, |
| 282 | uint32_t &Result) const { |
| 283 | uint16_t MachOFlags; |
| 284 | uint8_t MachOType; |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 285 | if (MachOObj->is64Bit()) { |
| 286 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 287 | getSymbol64TableEntry(DRI, Entry); |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 288 | MachOFlags = Entry->Flags; |
| 289 | MachOType = Entry->Type; |
Benjamin Kramer | 32fb2af | 2011-07-15 17:32:45 +0000 | [diff] [blame] | 290 | } else { |
| 291 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 292 | getSymbolTableEntry(DRI, Entry); |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 293 | MachOFlags = Entry->Flags; |
| 294 | MachOType = Entry->Type; |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 297 | // TODO: Correctly set SF_ThreadLocal |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 298 | Result = SymbolRef::SF_None; |
David Meyer | 2c67727 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 299 | |
| 300 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) |
| 301 | Result |= SymbolRef::SF_Undefined; |
| 302 | |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 303 | if (MachOFlags & macho::STF_StabsEntryMask) |
| 304 | Result |= SymbolRef::SF_FormatSpecific; |
| 305 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 306 | if (MachOType & MachO::NlistMaskExternal) { |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 307 | Result |= SymbolRef::SF_Global; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 308 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined) |
| 309 | Result |= SymbolRef::SF_Common; |
| 310 | } |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 311 | |
| 312 | if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef)) |
| 313 | Result |= SymbolRef::SF_Weak; |
| 314 | |
| 315 | if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute) |
| 316 | Result |= SymbolRef::SF_Absolute; |
| 317 | |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 318 | return object_error::success; |
| 319 | } |
| 320 | |
| 321 | error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb, |
| 322 | section_iterator &Res) const { |
| 323 | uint8_t index; |
| 324 | if (MachOObj->is64Bit()) { |
| 325 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 326 | getSymbol64TableEntry(Symb, Entry); |
| 327 | index = Entry->SectionIndex; |
| 328 | } else { |
| 329 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 330 | getSymbolTableEntry(Symb, Entry); |
| 331 | index = Entry->SectionIndex; |
| 332 | } |
| 333 | |
| 334 | if (index == 0) |
| 335 | Res = end_sections(); |
| 336 | else |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 337 | Res = section_iterator(SectionRef(Sections[index-1], this)); |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 338 | |
| 339 | return object_error::success; |
| 340 | } |
| 341 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 342 | error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, |
Michael J. Spencer | 1130a79 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 343 | SymbolRef::Type &Res) const { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 344 | uint8_t n_type; |
| 345 | if (MachOObj->is64Bit()) { |
| 346 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 347 | getSymbol64TableEntry(Symb, Entry); |
| 348 | n_type = Entry->Type; |
| 349 | } else { |
| 350 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 351 | getSymbolTableEntry(Symb, Entry); |
| 352 | n_type = Entry->Type; |
| 353 | } |
| 354 | Res = SymbolRef::ST_Other; |
Owen Anderson | 10a8c62 | 2011-10-12 22:23:12 +0000 | [diff] [blame] | 355 | |
| 356 | // If this is a STAB debugging symbol, we can do nothing more. |
Owen Anderson | a48aab9 | 2011-10-21 19:26:54 +0000 | [diff] [blame] | 357 | if (n_type & MachO::NlistMaskStab) { |
| 358 | Res = SymbolRef::ST_Debug; |
Owen Anderson | 10a8c62 | 2011-10-12 22:23:12 +0000 | [diff] [blame] | 359 | return object_error::success; |
Owen Anderson | a48aab9 | 2011-10-21 19:26:54 +0000 | [diff] [blame] | 360 | } |
Owen Anderson | 10a8c62 | 2011-10-12 22:23:12 +0000 | [diff] [blame] | 361 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 362 | switch (n_type & MachO::NlistMaskType) { |
| 363 | case MachO::NListTypeUndefined : |
David Meyer | 2c67727 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 364 | Res = SymbolRef::ST_Unknown; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 365 | break; |
| 366 | case MachO::NListTypeSection : |
| 367 | Res = SymbolRef::ST_Function; |
| 368 | break; |
| 369 | } |
| 370 | return object_error::success; |
| 371 | } |
| 372 | |
Tim Northover | a41dce3 | 2012-10-29 10:47:00 +0000 | [diff] [blame] | 373 | error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb, |
| 374 | uint64_t &Val) const { |
| 375 | report_fatal_error("getSymbolValue unimplemented in MachOObjectFile"); |
| 376 | } |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 377 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 378 | symbol_iterator MachOObjectFile::begin_symbols() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 379 | // DRI.d.a = segment number; DRI.d.b = symbol index. |
| 380 | DataRefImpl DRI; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 381 | moveToNextSymbol(DRI); |
| 382 | return symbol_iterator(SymbolRef(DRI, this)); |
| 383 | } |
| 384 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 385 | symbol_iterator MachOObjectFile::end_symbols() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 386 | DataRefImpl DRI; |
| 387 | DRI.d.a = MachOObj->getHeader().NumLoadCommands; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 388 | return symbol_iterator(SymbolRef(DRI, this)); |
| 389 | } |
| 390 | |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 391 | symbol_iterator MachOObjectFile::begin_dynamic_symbols() const { |
| 392 | // TODO: implement |
| 393 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); |
| 394 | } |
| 395 | |
| 396 | symbol_iterator MachOObjectFile::end_dynamic_symbols() const { |
| 397 | // TODO: implement |
| 398 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile"); |
| 399 | } |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 400 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 401 | library_iterator MachOObjectFile::begin_libraries_needed() const { |
| 402 | // TODO: implement |
| 403 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 404 | } |
| 405 | |
| 406 | library_iterator MachOObjectFile::end_libraries_needed() const { |
| 407 | // TODO: implement |
| 408 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 409 | } |
| 410 | |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 411 | StringRef MachOObjectFile::getLoadName() const { |
| 412 | // TODO: Implement |
| 413 | report_fatal_error("get_load_name() unimplemented in MachOObjectFile"); |
| 414 | } |
| 415 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 416 | /*===-- Sections ----------------------------------------------------------===*/ |
| 417 | |
| 418 | void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const { |
| 419 | uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands; |
| 420 | while (DRI.d.a < LoadCommandCount) { |
| 421 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 422 | if (LCI.Command.Type == macho::LCT_Segment) { |
| 423 | InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd; |
| 424 | MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd); |
| 425 | if (DRI.d.b < SegmentLoadCmd->NumSections) |
| 426 | return; |
| 427 | } else if (LCI.Command.Type == macho::LCT_Segment64) { |
| 428 | InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd; |
| 429 | MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd); |
| 430 | if (DRI.d.b < Segment64LoadCmd->NumSections) |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | DRI.d.a++; |
| 435 | DRI.d.b = 0; |
| 436 | } |
| 437 | } |
| 438 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 439 | error_code MachOObjectFile::getSectionNext(DataRefImpl DRI, |
| 440 | SectionRef &Result) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 441 | DRI.d.b++; |
| 442 | moveToNextSection(DRI); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 443 | Result = SectionRef(DRI, this); |
| 444 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void |
| 448 | MachOObjectFile::getSection(DataRefImpl DRI, |
| 449 | InMemoryStruct<macho::Section> &Res) const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 450 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 451 | MachOObj->ReadSection(LCI, DRI.d.b, Res); |
| 452 | } |
| 453 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 454 | std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const { |
| 455 | SectionList::const_iterator loc = |
| 456 | std::find(Sections.begin(), Sections.end(), Sec); |
| 457 | assert(loc != Sections.end() && "Sec is not a valid section!"); |
| 458 | return std::distance(Sections.begin(), loc); |
| 459 | } |
| 460 | |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 461 | void |
| 462 | MachOObjectFile::getSection64(DataRefImpl DRI, |
| 463 | InMemoryStruct<macho::Section64> &Res) const { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 464 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 465 | MachOObj->ReadSection64(LCI, DRI.d.b, Res); |
| 466 | } |
| 467 | |
| 468 | static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) { |
| 469 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
| 470 | if (LCI.Command.Type == macho::LCT_Segment64) |
| 471 | return true; |
| 472 | assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type."); |
| 473 | return false; |
| 474 | } |
| 475 | |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 476 | static StringRef parseSegmentOrSectionName(const char *P) { |
| 477 | if (P[15] == 0) |
| 478 | // Null terminated. |
| 479 | return P; |
| 480 | // Not null terminated, so this is a 16 char string. |
| 481 | return StringRef(P, 16); |
| 482 | } |
| 483 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 484 | error_code MachOObjectFile::getSectionName(DataRefImpl DRI, |
| 485 | StringRef &Result) const { |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 486 | if (is64BitLoadCommand(MachOObj.get(), DRI)) { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 487 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 488 | unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) + |
| 489 | DRI.d.b * sizeof(macho::Section64); |
| 490 | StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64)); |
| 491 | const macho::Section64 *sec = |
| 492 | reinterpret_cast<const macho::Section64*>(Data.data()); |
| 493 | Result = parseSegmentOrSectionName(sec->Name); |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 494 | } else { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 495 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a); |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 496 | unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) + |
| 497 | DRI.d.b * sizeof(macho::Section); |
| 498 | StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section)); |
| 499 | const macho::Section *sec = |
| 500 | reinterpret_cast<const macho::Section*>(Data.data()); |
| 501 | Result = parseSegmentOrSectionName(sec->Name); |
Rafael Espindola | f9a6bd8 | 2012-12-19 14:15:04 +0000 | [diff] [blame] | 502 | } |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 503 | return object_error::success; |
| 504 | } |
| 505 | |
| 506 | error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec, |
| 507 | StringRef &Res) const { |
| 508 | if (is64BitLoadCommand(MachOObj.get(), Sec)) { |
| 509 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a); |
| 510 | unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) + |
| 511 | Sec.d.b * sizeof(macho::Section64); |
| 512 | StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64)); |
| 513 | const macho::Section64 *sec = |
| 514 | reinterpret_cast<const macho::Section64*>(Data.data()); |
| 515 | Res = parseSegmentOrSectionName(sec->SegmentName); |
| 516 | } else { |
| 517 | LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a); |
| 518 | unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) + |
| 519 | Sec.d.b * sizeof(macho::Section); |
| 520 | StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section)); |
| 521 | const macho::Section *sec = |
| 522 | reinterpret_cast<const macho::Section*>(Data.data()); |
| 523 | Res = parseSegmentOrSectionName(sec->SegmentName); |
| 524 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 525 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 528 | error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI, |
| 529 | uint64_t &Result) const { |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 530 | if (is64BitLoadCommand(MachOObj.get(), DRI)) { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 531 | InMemoryStruct<macho::Section64> Sect; |
| 532 | getSection64(DRI, Sect); |
| 533 | Result = Sect->Address; |
| 534 | } else { |
| 535 | InMemoryStruct<macho::Section> Sect; |
| 536 | getSection(DRI, Sect); |
| 537 | Result = Sect->Address; |
| 538 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 539 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 542 | error_code MachOObjectFile::getSectionSize(DataRefImpl DRI, |
| 543 | uint64_t &Result) const { |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 544 | if (is64BitLoadCommand(MachOObj.get(), DRI)) { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 545 | InMemoryStruct<macho::Section64> Sect; |
| 546 | getSection64(DRI, Sect); |
| 547 | Result = Sect->Size; |
| 548 | } else { |
| 549 | InMemoryStruct<macho::Section> Sect; |
| 550 | getSection(DRI, Sect); |
| 551 | Result = Sect->Size; |
| 552 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 553 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 556 | error_code MachOObjectFile::getSectionContents(DataRefImpl DRI, |
| 557 | StringRef &Result) const { |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 558 | if (is64BitLoadCommand(MachOObj.get(), DRI)) { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 559 | InMemoryStruct<macho::Section64> Sect; |
| 560 | getSection64(DRI, Sect); |
| 561 | Result = MachOObj->getData(Sect->Offset, Sect->Size); |
| 562 | } else { |
| 563 | InMemoryStruct<macho::Section> Sect; |
| 564 | getSection(DRI, Sect); |
| 565 | Result = MachOObj->getData(Sect->Offset, Sect->Size); |
| 566 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 567 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 570 | error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI, |
| 571 | uint64_t &Result) const { |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 572 | if (is64BitLoadCommand(MachOObj.get(), DRI)) { |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 573 | InMemoryStruct<macho::Section64> Sect; |
| 574 | getSection64(DRI, Sect); |
Michael J. Spencer | 15565ad | 2011-10-10 23:36:56 +0000 | [diff] [blame] | 575 | Result = uint64_t(1) << Sect->Align; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 576 | } else { |
| 577 | InMemoryStruct<macho::Section> Sect; |
| 578 | getSection(DRI, Sect); |
Michael J. Spencer | 15565ad | 2011-10-10 23:36:56 +0000 | [diff] [blame] | 579 | Result = uint64_t(1) << Sect->Align; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 580 | } |
| 581 | return object_error::success; |
| 582 | } |
| 583 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 584 | error_code MachOObjectFile::isSectionText(DataRefImpl DRI, |
| 585 | bool &Result) const { |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 586 | if (is64BitLoadCommand(MachOObj.get(), DRI)) { |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 587 | InMemoryStruct<macho::Section64> Sect; |
| 588 | getSection64(DRI, Sect); |
Tim Northover | 1c2b2f9 | 2012-12-17 17:59:32 +0000 | [diff] [blame] | 589 | Result = Sect->Flags & macho::SF_PureInstructions; |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 590 | } else { |
| 591 | InMemoryStruct<macho::Section> Sect; |
| 592 | getSection(DRI, Sect); |
Tim Northover | 1c2b2f9 | 2012-12-17 17:59:32 +0000 | [diff] [blame] | 593 | Result = Sect->Flags & macho::SF_PureInstructions; |
Benjamin Kramer | 7d14578 | 2011-07-15 00:14:48 +0000 | [diff] [blame] | 594 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 595 | return object_error::success; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 598 | error_code MachOObjectFile::isSectionData(DataRefImpl DRI, |
| 599 | bool &Result) const { |
| 600 | // FIXME: Unimplemented. |
| 601 | Result = false; |
| 602 | return object_error::success; |
| 603 | } |
| 604 | |
| 605 | error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, |
| 606 | bool &Result) const { |
| 607 | // FIXME: Unimplemented. |
| 608 | Result = false; |
| 609 | return object_error::success; |
| 610 | } |
| 611 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 612 | error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec, |
| 613 | bool &Result) const { |
Andrew Kaylor | 30b20eb | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 614 | // FIXME: Unimplemented. |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 615 | Result = true; |
| 616 | return object_error::success; |
| 617 | } |
| 618 | |
| 619 | error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec, |
Andrew Kaylor | 30b20eb | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 620 | bool &Result) const { |
| 621 | // FIXME: Unimplemented. |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 622 | Result = false; |
| 623 | return object_error::success; |
| 624 | } |
| 625 | |
| 626 | error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI, |
| 627 | bool &Result) const { |
| 628 | if (MachOObj->is64Bit()) { |
| 629 | InMemoryStruct<macho::Section64> Sect; |
| 630 | getSection64(DRI, Sect); |
Eli Friedman | 41827f9 | 2012-05-02 02:31:28 +0000 | [diff] [blame] | 631 | unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType; |
| 632 | Result = (SectionType == MachO::SectionTypeZeroFill || |
| 633 | SectionType == MachO::SectionTypeZeroFillLarge); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 634 | } else { |
| 635 | InMemoryStruct<macho::Section> Sect; |
| 636 | getSection(DRI, Sect); |
Eli Friedman | 41827f9 | 2012-05-02 02:31:28 +0000 | [diff] [blame] | 637 | unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType; |
| 638 | Result = (SectionType == MachO::SectionTypeZeroFill || |
| 639 | SectionType == MachO::SectionTypeZeroFillLarge); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return object_error::success; |
| 643 | } |
| 644 | |
Andrew Kaylor | 3a129c8 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 645 | error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec, |
| 646 | bool &Result) const { |
| 647 | // Consider using the code from isSectionText to look for __const sections. |
| 648 | // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS |
| 649 | // to use section attributes to distinguish code from data. |
| 650 | |
| 651 | // FIXME: Unimplemented. |
| 652 | Result = false; |
| 653 | return object_error::success; |
| 654 | } |
| 655 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 656 | error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, |
| 657 | DataRefImpl Symb, |
| 658 | bool &Result) const { |
Michael J. Spencer | 1130a79 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 659 | SymbolRef::Type ST; |
Owen Anderson | cd74988 | 2011-10-12 22:21:32 +0000 | [diff] [blame] | 660 | getSymbolType(Symb, ST); |
David Meyer | 2c67727 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 661 | if (ST == SymbolRef::ST_Unknown) { |
Owen Anderson | cd74988 | 2011-10-12 22:21:32 +0000 | [diff] [blame] | 662 | Result = false; |
| 663 | return object_error::success; |
| 664 | } |
| 665 | |
| 666 | uint64_t SectBegin, SectEnd; |
| 667 | getSectionAddress(Sec, SectBegin); |
| 668 | getSectionSize(Sec, SectEnd); |
| 669 | SectEnd += SectBegin; |
| 670 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 671 | if (MachOObj->is64Bit()) { |
| 672 | InMemoryStruct<macho::Symbol64TableEntry> Entry; |
| 673 | getSymbol64TableEntry(Symb, Entry); |
Owen Anderson | cd74988 | 2011-10-12 22:21:32 +0000 | [diff] [blame] | 674 | uint64_t SymAddr= Entry->Value; |
| 675 | Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 676 | } else { |
| 677 | InMemoryStruct<macho::SymbolTableEntry> Entry; |
| 678 | getSymbolTableEntry(Symb, Entry); |
Owen Anderson | cd74988 | 2011-10-12 22:21:32 +0000 | [diff] [blame] | 679 | uint64_t SymAddr= Entry->Value; |
| 680 | Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd); |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 681 | } |
Owen Anderson | cd74988 | 2011-10-12 22:21:32 +0000 | [diff] [blame] | 682 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 683 | return object_error::success; |
| 684 | } |
| 685 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 686 | relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const { |
| 687 | DataRefImpl ret; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 688 | ret.d.b = getSectionIndex(Sec); |
| 689 | return relocation_iterator(RelocationRef(ret, this)); |
| 690 | } |
| 691 | relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const { |
| 692 | uint32_t last_reloc; |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 693 | if (is64BitLoadCommand(MachOObj.get(), Sec)) { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 694 | InMemoryStruct<macho::Section64> Sect; |
| 695 | getSection64(Sec, Sect); |
| 696 | last_reloc = Sect->NumRelocationTableEntries; |
| 697 | } else { |
| 698 | InMemoryStruct<macho::Section> Sect; |
| 699 | getSection(Sec, Sect); |
| 700 | last_reloc = Sect->NumRelocationTableEntries; |
| 701 | } |
| 702 | DataRefImpl ret; |
| 703 | ret.d.a = last_reloc; |
| 704 | ret.d.b = getSectionIndex(Sec); |
| 705 | return relocation_iterator(RelocationRef(ret, this)); |
| 706 | } |
| 707 | |
| 708 | section_iterator MachOObjectFile::begin_sections() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 709 | DataRefImpl DRI; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 710 | moveToNextSection(DRI); |
| 711 | return section_iterator(SectionRef(DRI, this)); |
| 712 | } |
| 713 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 714 | section_iterator MachOObjectFile::end_sections() const { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 715 | DataRefImpl DRI; |
| 716 | DRI.d.a = MachOObj->getHeader().NumLoadCommands; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 717 | return section_iterator(SectionRef(DRI, this)); |
| 718 | } |
| 719 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 720 | /*===-- Relocations -------------------------------------------------------===*/ |
| 721 | |
| 722 | void MachOObjectFile:: |
| 723 | getRelocation(DataRefImpl Rel, |
| 724 | InMemoryStruct<macho::RelocationEntry> &Res) const { |
| 725 | uint32_t relOffset; |
| 726 | if (MachOObj->is64Bit()) { |
| 727 | InMemoryStruct<macho::Section64> Sect; |
| 728 | getSection64(Sections[Rel.d.b], Sect); |
| 729 | relOffset = Sect->RelocationTableOffset; |
| 730 | } else { |
| 731 | InMemoryStruct<macho::Section> Sect; |
| 732 | getSection(Sections[Rel.d.b], Sect); |
| 733 | relOffset = Sect->RelocationTableOffset; |
| 734 | } |
| 735 | MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res); |
| 736 | } |
| 737 | error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel, |
| 738 | RelocationRef &Res) const { |
| 739 | ++Rel.d.a; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 740 | Res = RelocationRef(Rel, this); |
| 741 | return object_error::success; |
| 742 | } |
| 743 | error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 744 | uint64_t &Res) const { |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 745 | const uint8_t* sectAddress = 0; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 746 | if (MachOObj->is64Bit()) { |
| 747 | InMemoryStruct<macho::Section64> Sect; |
| 748 | getSection64(Sections[Rel.d.b], Sect); |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 749 | sectAddress += Sect->Address; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 750 | } else { |
| 751 | InMemoryStruct<macho::Section> Sect; |
| 752 | getSection(Sections[Rel.d.b], Sect); |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 753 | sectAddress += Sect->Address; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 754 | } |
| 755 | InMemoryStruct<macho::RelocationEntry> RE; |
| 756 | getRelocation(Rel, RE); |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 757 | |
| 758 | unsigned Arch = getArch(); |
| 759 | bool isScattered = (Arch != Triple::x86_64) && |
| 760 | (RE->Word0 & macho::RF_Scattered); |
| 761 | uint64_t RelAddr = 0; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 762 | if (isScattered) |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 763 | RelAddr = RE->Word0 & 0xFFFFFF; |
| 764 | else |
| 765 | RelAddr = RE->Word0; |
| 766 | |
| 767 | Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 768 | return object_error::success; |
| 769 | } |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 770 | error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel, |
| 771 | uint64_t &Res) const { |
| 772 | InMemoryStruct<macho::RelocationEntry> RE; |
| 773 | getRelocation(Rel, RE); |
| 774 | |
| 775 | unsigned Arch = getArch(); |
| 776 | bool isScattered = (Arch != Triple::x86_64) && |
| 777 | (RE->Word0 & macho::RF_Scattered); |
| 778 | if (isScattered) |
| 779 | Res = RE->Word0 & 0xFFFFFF; |
| 780 | else |
| 781 | Res = RE->Word0; |
| 782 | return object_error::success; |
| 783 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 784 | error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, |
| 785 | SymbolRef &Res) const { |
| 786 | InMemoryStruct<macho::RelocationEntry> RE; |
| 787 | getRelocation(Rel, RE); |
| 788 | uint32_t SymbolIdx = RE->Word1 & 0xffffff; |
| 789 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 790 | |
| 791 | DataRefImpl Sym; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 792 | moveToNextSymbol(Sym); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 793 | if (isExtern) { |
| 794 | for (unsigned i = 0; i < SymbolIdx; i++) { |
| 795 | Sym.d.b++; |
| 796 | moveToNextSymbol(Sym); |
Nick Lewycky | 58856ea | 2011-09-09 00:16:50 +0000 | [diff] [blame] | 797 | assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands && |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 798 | "Relocation symbol index out of range!"); |
| 799 | } |
| 800 | } |
| 801 | Res = SymbolRef(Sym, this); |
| 802 | return object_error::success; |
| 803 | } |
| 804 | error_code MachOObjectFile::getRelocationType(DataRefImpl Rel, |
Owen Anderson | 9472b8d | 2011-10-26 17:08:49 +0000 | [diff] [blame] | 805 | uint64_t &Res) const { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 806 | InMemoryStruct<macho::RelocationEntry> RE; |
| 807 | getRelocation(Rel, RE); |
Owen Anderson | f8261e7 | 2011-10-26 17:10:22 +0000 | [diff] [blame] | 808 | Res = RE->Word0; |
| 809 | Res <<= 32; |
| 810 | Res |= RE->Word1; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 811 | return object_error::success; |
| 812 | } |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 813 | error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 814 | SmallVectorImpl<char> &Result) const { |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 815 | // TODO: Support scattered relocations. |
| 816 | StringRef res; |
| 817 | InMemoryStruct<macho::RelocationEntry> RE; |
| 818 | getRelocation(Rel, RE); |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 819 | |
| 820 | unsigned Arch = getArch(); |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 821 | bool isScattered = (Arch != Triple::x86_64) && |
| 822 | (RE->Word0 & macho::RF_Scattered); |
| 823 | |
| 824 | unsigned r_type; |
| 825 | if (isScattered) |
| 826 | r_type = (RE->Word0 >> 24) & 0xF; |
| 827 | else |
| 828 | r_type = (RE->Word1 >> 28) & 0xF; |
| 829 | |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 830 | switch (Arch) { |
| 831 | case Triple::x86: { |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 832 | static const char *const Table[] = { |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 833 | "GENERIC_RELOC_VANILLA", |
| 834 | "GENERIC_RELOC_PAIR", |
| 835 | "GENERIC_RELOC_SECTDIFF", |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 836 | "GENERIC_RELOC_PB_LA_PTR", |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 837 | "GENERIC_RELOC_LOCAL_SECTDIFF", |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 838 | "GENERIC_RELOC_TLV" }; |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 839 | |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 840 | if (r_type > 6) |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 841 | res = "Unknown"; |
| 842 | else |
| 843 | res = Table[r_type]; |
| 844 | break; |
| 845 | } |
| 846 | case Triple::x86_64: { |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 847 | static const char *const Table[] = { |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 848 | "X86_64_RELOC_UNSIGNED", |
| 849 | "X86_64_RELOC_SIGNED", |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 850 | "X86_64_RELOC_BRANCH", |
| 851 | "X86_64_RELOC_GOT_LOAD", |
| 852 | "X86_64_RELOC_GOT", |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 853 | "X86_64_RELOC_SUBTRACTOR", |
| 854 | "X86_64_RELOC_SIGNED_1", |
| 855 | "X86_64_RELOC_SIGNED_2", |
| 856 | "X86_64_RELOC_SIGNED_4", |
| 857 | "X86_64_RELOC_TLV" }; |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 858 | |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 859 | if (r_type > 9) |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 860 | res = "Unknown"; |
| 861 | else |
| 862 | res = Table[r_type]; |
| 863 | break; |
| 864 | } |
| 865 | case Triple::arm: { |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 866 | static const char *const Table[] = { |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 867 | "ARM_RELOC_VANILLA", |
| 868 | "ARM_RELOC_PAIR", |
| 869 | "ARM_RELOC_SECTDIFF", |
| 870 | "ARM_RELOC_LOCAL_SECTDIFF", |
| 871 | "ARM_RELOC_PB_LA_PTR", |
| 872 | "ARM_RELOC_BR24", |
| 873 | "ARM_THUMB_RELOC_BR22", |
| 874 | "ARM_THUMB_32BIT_BRANCH", |
| 875 | "ARM_RELOC_HALF", |
| 876 | "ARM_RELOC_HALF_SECTDIFF" }; |
| 877 | |
| 878 | if (r_type > 9) |
| 879 | res = "Unknown"; |
| 880 | else |
| 881 | res = Table[r_type]; |
| 882 | break; |
| 883 | } |
| 884 | case Triple::ppc: { |
Craig Topper | e329810 | 2012-05-24 06:35:32 +0000 | [diff] [blame] | 885 | static const char *const Table[] = { |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 886 | "PPC_RELOC_VANILLA", |
| 887 | "PPC_RELOC_PAIR", |
| 888 | "PPC_RELOC_BR14", |
| 889 | "PPC_RELOC_BR24", |
| 890 | "PPC_RELOC_HI16", |
| 891 | "PPC_RELOC_LO16", |
| 892 | "PPC_RELOC_HA16", |
| 893 | "PPC_RELOC_LO14", |
| 894 | "PPC_RELOC_SECTDIFF", |
| 895 | "PPC_RELOC_PB_LA_PTR", |
| 896 | "PPC_RELOC_HI16_SECTDIFF", |
| 897 | "PPC_RELOC_LO16_SECTDIFF", |
| 898 | "PPC_RELOC_HA16_SECTDIFF", |
| 899 | "PPC_RELOC_JBSR", |
| 900 | "PPC_RELOC_LO14_SECTDIFF", |
| 901 | "PPC_RELOC_LOCAL_SECTDIFF" }; |
| 902 | |
| 903 | res = Table[r_type]; |
| 904 | break; |
| 905 | } |
| 906 | case Triple::UnknownArch: |
| 907 | res = "Unknown"; |
| 908 | break; |
| 909 | } |
Owen Anderson | 5f4e02c | 2011-10-24 20:19:18 +0000 | [diff] [blame] | 910 | Result.append(res.begin(), res.end()); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 911 | return object_error::success; |
| 912 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 913 | error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 914 | int64_t &Res) const { |
| 915 | InMemoryStruct<macho::RelocationEntry> RE; |
| 916 | getRelocation(Rel, RE); |
| 917 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 918 | Res = 0; |
| 919 | if (!isExtern) { |
| 920 | const uint8_t* sectAddress = base(); |
| 921 | if (MachOObj->is64Bit()) { |
| 922 | InMemoryStruct<macho::Section64> Sect; |
| 923 | getSection64(Sections[Rel.d.b], Sect); |
| 924 | sectAddress += Sect->Offset; |
| 925 | } else { |
| 926 | InMemoryStruct<macho::Section> Sect; |
| 927 | getSection(Sections[Rel.d.b], Sect); |
| 928 | sectAddress += Sect->Offset; |
| 929 | } |
| 930 | Res = reinterpret_cast<uintptr_t>(sectAddress); |
| 931 | } |
| 932 | return object_error::success; |
| 933 | } |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 934 | |
| 935 | // Helper to advance a section or symbol iterator multiple increments at a time. |
| 936 | template<class T> |
| 937 | error_code advance(T &it, size_t Val) { |
| 938 | error_code ec; |
| 939 | while (Val--) { |
| 940 | it.increment(ec); |
| 941 | } |
| 942 | return ec; |
| 943 | } |
| 944 | |
| 945 | template<class T> |
| 946 | void advanceTo(T &it, size_t Val) { |
| 947 | if (error_code ec = advance(it, Val)) |
| 948 | report_fatal_error(ec.message()); |
| 949 | } |
| 950 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 951 | void MachOObjectFile::printRelocationTargetName( |
| 952 | InMemoryStruct<macho::RelocationEntry>& RE, |
| 953 | raw_string_ostream &fmt) const { |
| 954 | unsigned Arch = getArch(); |
| 955 | bool isScattered = (Arch != Triple::x86_64) && |
| 956 | (RE->Word0 & macho::RF_Scattered); |
| 957 | |
| 958 | // Target of a scattered relocation is an address. In the interest of |
| 959 | // generating pretty output, scan through the symbol table looking for a |
| 960 | // symbol that aligns with that address. If we find one, print it. |
| 961 | // Otherwise, we just print the hex address of the target. |
| 962 | if (isScattered) { |
| 963 | uint32_t Val = RE->Word1; |
| 964 | |
| 965 | error_code ec; |
| 966 | for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE; |
| 967 | SI.increment(ec)) { |
| 968 | if (ec) report_fatal_error(ec.message()); |
| 969 | |
| 970 | uint64_t Addr; |
| 971 | StringRef Name; |
| 972 | |
| 973 | if ((ec = SI->getAddress(Addr))) |
| 974 | report_fatal_error(ec.message()); |
| 975 | if (Addr != Val) continue; |
| 976 | if ((ec = SI->getName(Name))) |
| 977 | report_fatal_error(ec.message()); |
| 978 | fmt << Name; |
| 979 | return; |
| 980 | } |
| 981 | |
Owen Anderson | b28bdbf | 2011-10-27 21:53:50 +0000 | [diff] [blame] | 982 | // If we couldn't find a symbol that this relocation refers to, try |
| 983 | // to find a section beginning instead. |
| 984 | for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE; |
| 985 | SI.increment(ec)) { |
| 986 | if (ec) report_fatal_error(ec.message()); |
| 987 | |
| 988 | uint64_t Addr; |
| 989 | StringRef Name; |
| 990 | |
| 991 | if ((ec = SI->getAddress(Addr))) |
| 992 | report_fatal_error(ec.message()); |
| 993 | if (Addr != Val) continue; |
| 994 | if ((ec = SI->getName(Name))) |
| 995 | report_fatal_error(ec.message()); |
| 996 | fmt << Name; |
| 997 | return; |
| 998 | } |
| 999 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1000 | fmt << format("0x%x", Val); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | StringRef S; |
| 1005 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 1006 | uint32_t Val = RE->Word1 & 0xFFFFFF; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1007 | |
| 1008 | if (isExtern) { |
| 1009 | symbol_iterator SI = begin_symbols(); |
| 1010 | advanceTo(SI, Val); |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1011 | SI->getName(S); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1012 | } else { |
| 1013 | section_iterator SI = begin_sections(); |
| 1014 | advanceTo(SI, Val); |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1015 | SI->getName(S); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1018 | fmt << S; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1021 | error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel, |
| 1022 | SmallVectorImpl<char> &Result) const { |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 1023 | InMemoryStruct<macho::RelocationEntry> RE; |
| 1024 | getRelocation(Rel, RE); |
| 1025 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1026 | unsigned Arch = getArch(); |
| 1027 | bool isScattered = (Arch != Triple::x86_64) && |
| 1028 | (RE->Word0 & macho::RF_Scattered); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1029 | |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1030 | std::string fmtbuf; |
| 1031 | raw_string_ostream fmt(fmtbuf); |
| 1032 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1033 | unsigned Type; |
| 1034 | if (isScattered) |
| 1035 | Type = (RE->Word0 >> 24) & 0xF; |
| 1036 | else |
| 1037 | Type = (RE->Word1 >> 28) & 0xF; |
| 1038 | |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 1039 | bool isPCRel; |
| 1040 | if (isScattered) |
| 1041 | isPCRel = ((RE->Word0 >> 30) & 1); |
| 1042 | else |
| 1043 | isPCRel = ((RE->Word1 >> 24) & 1); |
| 1044 | |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1045 | // Determine any addends that should be displayed with the relocation. |
| 1046 | // These require decoding the relocation type, which is triple-specific. |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1047 | |
| 1048 | // X86_64 has entirely custom relocation types. |
| 1049 | if (Arch == Triple::x86_64) { |
Owen Anderson | 929e27c | 2011-10-26 17:05:20 +0000 | [diff] [blame] | 1050 | bool isPCRel = ((RE->Word1 >> 24) & 1); |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1051 | |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1052 | switch (Type) { |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1053 | case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD |
| 1054 | case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT |
| 1055 | printRelocationTargetName(RE, fmt); |
| 1056 | fmt << "@GOT"; |
Owen Anderson | 929e27c | 2011-10-26 17:05:20 +0000 | [diff] [blame] | 1057 | if (isPCRel) fmt << "PCREL"; |
| 1058 | break; |
| 1059 | } |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1060 | case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1061 | InMemoryStruct<macho::RelocationEntry> RENext; |
| 1062 | DataRefImpl RelNext = Rel; |
| 1063 | RelNext.d.a++; |
| 1064 | getRelocation(RelNext, RENext); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1065 | |
| 1066 | // X86_64_SUBTRACTOR must be followed by a relocation of type |
| 1067 | // X86_64_RELOC_UNSIGNED. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1068 | // NOTE: Scattered relocations don't exist on x86_64. |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1069 | unsigned RType = (RENext->Word1 >> 28) & 0xF; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1070 | if (RType != 0) |
| 1071 | report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " |
| 1072 | "X86_64_RELOC_SUBTRACTOR."); |
| 1073 | |
Owen Anderson | ef22f78 | 2011-10-26 17:28:49 +0000 | [diff] [blame] | 1074 | // The X86_64_RELOC_UNSIGNED contains the minuend symbol, |
| 1075 | // X86_64_SUBTRACTOR contains to the subtrahend. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1076 | printRelocationTargetName(RENext, fmt); |
| 1077 | fmt << "-"; |
| 1078 | printRelocationTargetName(RE, fmt); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1079 | } |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 1080 | case macho::RIT_X86_64_TLV: |
| 1081 | printRelocationTargetName(RE, fmt); |
| 1082 | fmt << "@TLV"; |
| 1083 | if (isPCRel) fmt << "P"; |
| 1084 | break; |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1085 | case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1 |
| 1086 | printRelocationTargetName(RE, fmt); |
| 1087 | fmt << "-1"; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1088 | break; |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1089 | case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2 |
| 1090 | printRelocationTargetName(RE, fmt); |
| 1091 | fmt << "-2"; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1092 | break; |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1093 | case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4 |
| 1094 | printRelocationTargetName(RE, fmt); |
| 1095 | fmt << "-4"; |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | default: |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1098 | printRelocationTargetName(RE, fmt); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1099 | break; |
| 1100 | } |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1101 | // X86 and ARM share some relocation types in common. |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1102 | } else if (Arch == Triple::x86 || Arch == Triple::arm) { |
| 1103 | // Generic relocation types... |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1104 | switch (Type) { |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1105 | case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1106 | return object_error::success; |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 1107 | case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1108 | InMemoryStruct<macho::RelocationEntry> RENext; |
| 1109 | DataRefImpl RelNext = Rel; |
| 1110 | RelNext.d.a++; |
| 1111 | getRelocation(RelNext, RENext); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1112 | |
| 1113 | // X86 sect diff's must be followed by a relocation of type |
| 1114 | // GENERIC_RELOC_PAIR. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1115 | bool isNextScattered = (Arch != Triple::x86_64) && |
| 1116 | (RENext->Word0 & macho::RF_Scattered); |
| 1117 | unsigned RType; |
| 1118 | if (isNextScattered) |
| 1119 | RType = (RENext->Word0 >> 24) & 0xF; |
| 1120 | else |
| 1121 | RType = (RENext->Word1 >> 28) & 0xF; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1122 | if (RType != 1) |
| 1123 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 1124 | "GENERIC_RELOC_SECTDIFF."); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1125 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1126 | printRelocationTargetName(RE, fmt); |
| 1127 | fmt << "-"; |
| 1128 | printRelocationTargetName(RENext, fmt); |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1129 | break; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1132 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1133 | if (Arch == Triple::x86) { |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1134 | // All X86 relocations that need special printing were already |
| 1135 | // handled in the generic code. |
Owen Anderson | eb6bd33 | 2011-10-27 20:46:09 +0000 | [diff] [blame] | 1136 | switch (Type) { |
| 1137 | case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF |
| 1138 | InMemoryStruct<macho::RelocationEntry> RENext; |
| 1139 | DataRefImpl RelNext = Rel; |
| 1140 | RelNext.d.a++; |
| 1141 | getRelocation(RelNext, RENext); |
| 1142 | |
| 1143 | // X86 sect diff's must be followed by a relocation of type |
| 1144 | // GENERIC_RELOC_PAIR. |
| 1145 | bool isNextScattered = (Arch != Triple::x86_64) && |
| 1146 | (RENext->Word0 & macho::RF_Scattered); |
| 1147 | unsigned RType; |
| 1148 | if (isNextScattered) |
| 1149 | RType = (RENext->Word0 >> 24) & 0xF; |
| 1150 | else |
| 1151 | RType = (RENext->Word1 >> 28) & 0xF; |
| 1152 | if (RType != 1) |
| 1153 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 1154 | "GENERIC_RELOC_LOCAL_SECTDIFF."); |
| 1155 | |
| 1156 | printRelocationTargetName(RE, fmt); |
| 1157 | fmt << "-"; |
| 1158 | printRelocationTargetName(RENext, fmt); |
| 1159 | break; |
| 1160 | } |
| 1161 | case macho::RIT_Generic_TLV: { |
| 1162 | printRelocationTargetName(RE, fmt); |
| 1163 | fmt << "@TLV"; |
| 1164 | if (isPCRel) fmt << "P"; |
| 1165 | break; |
| 1166 | } |
| 1167 | default: |
| 1168 | printRelocationTargetName(RE, fmt); |
| 1169 | } |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1170 | } else { // ARM-specific relocations |
| 1171 | switch (Type) { |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1172 | case macho::RIT_ARM_Half: // ARM_RELOC_HALF |
| 1173 | case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1174 | // Half relocations steal a bit from the length field to encode |
| 1175 | // whether this is an upper16 or a lower16 relocation. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1176 | bool isUpper; |
| 1177 | if (isScattered) |
| 1178 | isUpper = (RE->Word0 >> 28) & 1; |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1179 | else |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1180 | isUpper = (RE->Word1 >> 25) & 1; |
| 1181 | |
| 1182 | if (isUpper) |
| 1183 | fmt << ":upper16:("; |
| 1184 | else |
| 1185 | fmt << ":lower16:("; |
| 1186 | printRelocationTargetName(RE, fmt); |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1187 | |
| 1188 | InMemoryStruct<macho::RelocationEntry> RENext; |
| 1189 | DataRefImpl RelNext = Rel; |
| 1190 | RelNext.d.a++; |
| 1191 | getRelocation(RelNext, RENext); |
| 1192 | |
| 1193 | // ARM half relocs must be followed by a relocation of type |
| 1194 | // ARM_RELOC_PAIR. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1195 | bool isNextScattered = (Arch != Triple::x86_64) && |
| 1196 | (RENext->Word0 & macho::RF_Scattered); |
| 1197 | unsigned RType; |
| 1198 | if (isNextScattered) |
| 1199 | RType = (RENext->Word0 >> 24) & 0xF; |
| 1200 | else |
| 1201 | RType = (RENext->Word1 >> 28) & 0xF; |
| 1202 | |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1203 | if (RType != 1) |
| 1204 | report_fatal_error("Expected ARM_RELOC_PAIR after " |
| 1205 | "GENERIC_RELOC_HALF"); |
| 1206 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1207 | // NOTE: The half of the target virtual address is stashed in the |
| 1208 | // address field of the secondary relocation, but we can't reverse |
| 1209 | // engineer the constant offset from it without decoding the movw/movt |
| 1210 | // instruction to find the other half in its immediate field. |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1211 | |
| 1212 | // ARM_RELOC_HALF_SECTDIFF encodes the second section in the |
| 1213 | // symbol/section pointer of the follow-on relocation. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1214 | if (Type == macho::RIT_ARM_HalfDifference) { |
| 1215 | fmt << "-"; |
| 1216 | printRelocationTargetName(RENext, fmt); |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1219 | fmt << ")"; |
| 1220 | break; |
| 1221 | } |
| 1222 | default: { |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1223 | printRelocationTargetName(RE, fmt); |
Owen Anderson | 013d756 | 2011-10-25 18:48:41 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1227 | } else |
| 1228 | printRelocationTargetName(RE, fmt); |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 1229 | |
Owen Anderson | 0135fe1 | 2011-10-24 21:44:00 +0000 | [diff] [blame] | 1230 | fmt.flush(); |
| 1231 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1232 | return object_error::success; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1235 | error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel, |
| 1236 | bool &Result) const { |
| 1237 | InMemoryStruct<macho::RelocationEntry> RE; |
| 1238 | getRelocation(Rel, RE); |
| 1239 | |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1240 | unsigned Arch = getArch(); |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1241 | bool isScattered = (Arch != Triple::x86_64) && |
| 1242 | (RE->Word0 & macho::RF_Scattered); |
| 1243 | unsigned Type; |
| 1244 | if (isScattered) |
| 1245 | Type = (RE->Word0 >> 24) & 0xF; |
| 1246 | else |
| 1247 | Type = (RE->Word1 >> 28) & 0xF; |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1248 | |
| 1249 | Result = false; |
| 1250 | |
| 1251 | // On arches that use the generic relocations, GENERIC_RELOC_PAIR |
| 1252 | // is always hidden. |
| 1253 | if (Arch == Triple::x86 || Arch == Triple::arm) { |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1254 | if (Type == macho::RIT_Pair) Result = true; |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1255 | } else if (Arch == Triple::x86_64) { |
| 1256 | // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows |
| 1257 | // an X864_64_RELOC_SUBTRACTOR. |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1258 | if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) { |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1259 | DataRefImpl RelPrev = Rel; |
| 1260 | RelPrev.d.a--; |
| 1261 | InMemoryStruct<macho::RelocationEntry> REPrev; |
| 1262 | getRelocation(RelPrev, REPrev); |
| 1263 | |
| 1264 | unsigned PrevType = (REPrev->Word1 >> 28) & 0xF; |
| 1265 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 1266 | if (PrevType == macho::RIT_X86_64_Subtractor) Result = true; |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | return object_error::success; |
| 1271 | } |
| 1272 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 1273 | error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData, |
| 1274 | LibraryRef &Res) const { |
| 1275 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1276 | } |
| 1277 | |
| 1278 | error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData, |
| 1279 | StringRef &Res) const { |
| 1280 | report_fatal_error("Needed libraries unimplemented in MachOObjectFile"); |
| 1281 | } |
| 1282 | |
| 1283 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1284 | /*===-- Miscellaneous -----------------------------------------------------===*/ |
| 1285 | |
| 1286 | uint8_t MachOObjectFile::getBytesInAddress() const { |
| 1287 | return MachOObj->is64Bit() ? 8 : 4; |
| 1288 | } |
| 1289 | |
| 1290 | StringRef MachOObjectFile::getFileFormatName() const { |
| 1291 | if (!MachOObj->is64Bit()) { |
| 1292 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1293 | case llvm::MachO::CPUTypeI386: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1294 | return "Mach-O 32-bit i386"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1295 | case llvm::MachO::CPUTypeARM: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1296 | return "Mach-O arm"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1297 | case llvm::MachO::CPUTypePowerPC: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1298 | return "Mach-O 32-bit ppc"; |
| 1299 | default: |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1300 | assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 && |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1301 | "64-bit object file when we're not 64-bit?"); |
| 1302 | return "Mach-O 32-bit unknown"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1307 | case llvm::MachO::CPUTypeX86_64: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1308 | return "Mach-O 64-bit x86-64"; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1309 | case llvm::MachO::CPUTypePowerPC64: |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1310 | return "Mach-O 64-bit ppc64"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1311 | default: |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1312 | assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 && |
Eric Christopher | 9ab1d7f | 2011-04-22 04:08:58 +0000 | [diff] [blame] | 1313 | "32-bit object file when we're 64-bit?"); |
| 1314 | return "Mach-O 64-bit unknown"; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | unsigned MachOObjectFile::getArch() const { |
| 1319 | switch (MachOObj->getHeader().CPUType) { |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1320 | case llvm::MachO::CPUTypeI386: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1321 | return Triple::x86; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1322 | case llvm::MachO::CPUTypeX86_64: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1323 | return Triple::x86_64; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1324 | case llvm::MachO::CPUTypeARM: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1325 | return Triple::arm; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1326 | case llvm::MachO::CPUTypePowerPC: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1327 | return Triple::ppc; |
Eric Christopher | f4b2f93 | 2011-04-22 06:34:01 +0000 | [diff] [blame] | 1328 | case llvm::MachO::CPUTypePowerPC64: |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1329 | return Triple::ppc64; |
| 1330 | default: |
| 1331 | return Triple::UnknownArch; |
| 1332 | } |
| 1333 | } |
| 1334 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 1335 | } // end namespace object |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 1336 | } // end namespace llvm |