Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 1 | //===- MachOObject.cpp - Mach-O Object File Wrapper -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Object/MachOObject.h" |
Daniel Dunbar | 9536916 | 2010-11-27 06:39:22 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 12 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 13 | #include "llvm/System/Host.h" |
| 14 | #include "llvm/System/SwapByteOrder.h" |
Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 17 | using namespace llvm::object; |
| 18 | |
Daniel Dunbar | 4ba1f5e | 2010-11-27 08:22:29 +0000 | [diff] [blame] | 19 | /* Translation Utilities */ |
| 20 | |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 21 | template<typename T> |
| 22 | static void SwapValue(T &Value) { |
| 23 | Value = sys::SwapByteOrder(Value); |
| 24 | } |
Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 25 | |
Daniel Dunbar | 4ba1f5e | 2010-11-27 08:22:29 +0000 | [diff] [blame] | 26 | template<typename T> |
| 27 | static void SwapStruct(T &Value); |
| 28 | |
| 29 | template<typename T> |
| 30 | static void ReadInMemoryStruct(const MachOObject &MOO, |
| 31 | StringRef Buffer, uint64_t Base, |
| 32 | InMemoryStruct<T> &Res) { |
| 33 | typedef T struct_type; |
| 34 | uint64_t Size = sizeof(struct_type); |
| 35 | |
| 36 | // Check that the buffer contains the expected data. |
| 37 | if (Base + Size > Buffer.size()) { |
| 38 | Res = 0; |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Check whether we can return a direct pointer. |
| 43 | struct_type *Ptr = (struct_type *) (Buffer.data() + Base); |
| 44 | if (!MOO.isSwappedEndian()) { |
| 45 | Res = Ptr; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Otherwise, copy the struct and translate the values. |
| 50 | Res = *Ptr; |
| 51 | SwapStruct(*Res); |
| 52 | } |
| 53 | |
| 54 | /* *** */ |
| 55 | |
Daniel Dunbar | 9536916 | 2010-11-27 06:39:22 +0000 | [diff] [blame] | 56 | MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_, |
| 57 | bool Is64Bit_) |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 58 | : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_), |
| 59 | IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()), |
Daniel Dunbar | f2e2a5f | 2010-11-27 13:46:11 +0000 | [diff] [blame^] | 60 | HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) { |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 61 | // Load the common header. |
| 62 | memcpy(&Header, Buffer->getBuffer().data(), sizeof(Header)); |
| 63 | if (IsSwappedEndian) { |
| 64 | SwapValue(Header.Magic); |
| 65 | SwapValue(Header.CPUType); |
| 66 | SwapValue(Header.CPUSubtype); |
| 67 | SwapValue(Header.FileType); |
| 68 | SwapValue(Header.NumLoadCommands); |
| 69 | SwapValue(Header.SizeOfLoadCommands); |
| 70 | SwapValue(Header.Flags); |
| 71 | } |
| 72 | |
| 73 | if (is64Bit()) { |
| 74 | memcpy(&Header64Ext, Buffer->getBuffer().data() + sizeof(Header), |
| 75 | sizeof(Header64Ext)); |
| 76 | if (IsSwappedEndian) { |
| 77 | SwapValue(Header64Ext.Reserved); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Create the load command array if sane. |
| 82 | if (getHeader().NumLoadCommands < (1 << 20)) |
| 83 | LoadCommands = new LoadCommandInfo[getHeader().NumLoadCommands]; |
| 84 | } |
| 85 | |
| 86 | MachOObject::~MachOObject() { |
| 87 | delete LoadCommands; |
Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | MachOObject *MachOObject::LoadFromBuffer(MemoryBuffer *Buffer, |
| 91 | std::string *ErrorStr) { |
Daniel Dunbar | 9536916 | 2010-11-27 06:39:22 +0000 | [diff] [blame] | 92 | // First, check the magic value and initialize the basic object info. |
| 93 | bool IsLittleEndian = false, Is64Bit = false; |
| 94 | StringRef Magic = Buffer->getBuffer().slice(0, 4); |
| 95 | if (Magic == "\xFE\xED\xFA\xCE") { |
| 96 | } else if (Magic == "\xCE\xFA\xED\xFE") { |
| 97 | IsLittleEndian = true; |
| 98 | } else if (Magic == "\xFE\xED\xFA\xCF") { |
| 99 | Is64Bit = true; |
| 100 | } else if (Magic == "\xCF\xFA\xED\xFE") { |
| 101 | IsLittleEndian = true; |
| 102 | Is64Bit = true; |
| 103 | } else { |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 104 | if (ErrorStr) *ErrorStr = "not a Mach object file (invalid magic)"; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | // Ensure that the at least the full header is present. |
| 109 | unsigned HeaderSize = Is64Bit ? macho::Header64Size : macho::Header32Size; |
| 110 | if (Buffer->getBufferSize() < HeaderSize) { |
| 111 | if (ErrorStr) *ErrorStr = "not a Mach object file (invalid header)"; |
Daniel Dunbar | 9536916 | 2010-11-27 06:39:22 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian, |
| 116 | Is64Bit)); |
| 117 | |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 118 | // Check for bogus number of load commands. |
| 119 | if (Object->getHeader().NumLoadCommands >= (1 << 20)) { |
| 120 | if (ErrorStr) *ErrorStr = "not a Mach object file (unreasonable header)"; |
| 121 | return 0; |
| 122 | } |
| 123 | |
Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 124 | if (ErrorStr) *ErrorStr = ""; |
Daniel Dunbar | 9536916 | 2010-11-27 06:39:22 +0000 | [diff] [blame] | 125 | return Object.take(); |
Daniel Dunbar | fbd25b7 | 2010-11-27 05:38:50 +0000 | [diff] [blame] | 126 | } |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | f2e2a5f | 2010-11-27 13:46:11 +0000 | [diff] [blame^] | 128 | void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) { |
| 129 | HasStringTable = true; |
| 130 | StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset, |
| 131 | SLC.StringTableSize); |
| 132 | } |
| 133 | |
Daniel Dunbar | a956d8b | 2010-11-27 07:19:41 +0000 | [diff] [blame] | 134 | const MachOObject::LoadCommandInfo & |
| 135 | MachOObject::getLoadCommandInfo(unsigned Index) const { |
| 136 | assert(Index < getHeader().NumLoadCommands && "Invalid index!"); |
| 137 | |
| 138 | // Load the command, if necessary. |
| 139 | if (Index >= NumLoadedCommands) { |
| 140 | uint64_t Offset; |
| 141 | if (Index == 0) { |
| 142 | Offset = getHeaderSize(); |
| 143 | } else { |
| 144 | const LoadCommandInfo &Prev = getLoadCommandInfo(Index - 1); |
| 145 | Offset = Prev.Offset + Prev.Command.Size; |
| 146 | } |
| 147 | |
| 148 | LoadCommandInfo &Info = LoadCommands[Index]; |
| 149 | memcpy(&Info.Command, Buffer->getBuffer().data() + Offset, |
| 150 | sizeof(macho::LoadCommand)); |
| 151 | if (IsSwappedEndian) { |
| 152 | SwapValue(Info.Command.Type); |
| 153 | SwapValue(Info.Command.Size); |
| 154 | } |
| 155 | Info.Offset = Offset; |
| 156 | NumLoadedCommands = Index + 1; |
| 157 | } |
| 158 | |
| 159 | return LoadCommands[Index]; |
| 160 | } |
Daniel Dunbar | 4ba1f5e | 2010-11-27 08:22:29 +0000 | [diff] [blame] | 161 | |
| 162 | template<> |
Benjamin Kramer | 3946e3b | 2010-11-27 09:08:25 +0000 | [diff] [blame] | 163 | void SwapStruct(macho::SegmentLoadCommand &Value) { |
Daniel Dunbar | 4ba1f5e | 2010-11-27 08:22:29 +0000 | [diff] [blame] | 164 | SwapValue(Value.Type); |
| 165 | SwapValue(Value.Size); |
| 166 | SwapValue(Value.VMAddress); |
| 167 | SwapValue(Value.VMSize); |
| 168 | SwapValue(Value.FileOffset); |
| 169 | SwapValue(Value.FileSize); |
| 170 | SwapValue(Value.MaxVMProtection); |
| 171 | SwapValue(Value.InitialVMProtection); |
| 172 | SwapValue(Value.NumSections); |
| 173 | SwapValue(Value.Flags); |
| 174 | } |
| 175 | void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI, |
| 176 | InMemoryStruct<macho::SegmentLoadCommand> &Res) const { |
| 177 | ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res); |
| 178 | } |
| 179 | |
| 180 | template<> |
Benjamin Kramer | 3946e3b | 2010-11-27 09:08:25 +0000 | [diff] [blame] | 181 | void SwapStruct(macho::Segment64LoadCommand &Value) { |
Daniel Dunbar | 4ba1f5e | 2010-11-27 08:22:29 +0000 | [diff] [blame] | 182 | SwapValue(Value.Type); |
| 183 | SwapValue(Value.Size); |
| 184 | SwapValue(Value.VMAddress); |
| 185 | SwapValue(Value.VMSize); |
| 186 | SwapValue(Value.FileOffset); |
| 187 | SwapValue(Value.FileSize); |
| 188 | SwapValue(Value.MaxVMProtection); |
| 189 | SwapValue(Value.InitialVMProtection); |
| 190 | SwapValue(Value.NumSections); |
| 191 | SwapValue(Value.Flags); |
| 192 | } |
| 193 | void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI, |
| 194 | InMemoryStruct<macho::Segment64LoadCommand> &Res) const { |
| 195 | ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res); |
| 196 | } |
Daniel Dunbar | f879f14 | 2010-11-27 08:33:44 +0000 | [diff] [blame] | 197 | |
| 198 | template<> |
Benjamin Kramer | 3946e3b | 2010-11-27 09:08:25 +0000 | [diff] [blame] | 199 | void SwapStruct(macho::SymtabLoadCommand &Value) { |
Daniel Dunbar | f879f14 | 2010-11-27 08:33:44 +0000 | [diff] [blame] | 200 | SwapValue(Value.Type); |
| 201 | SwapValue(Value.Size); |
| 202 | SwapValue(Value.SymbolTableOffset); |
| 203 | SwapValue(Value.NumSymbolTableEntries); |
| 204 | SwapValue(Value.StringTableOffset); |
| 205 | SwapValue(Value.StringTableSize); |
| 206 | } |
| 207 | void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI, |
| 208 | InMemoryStruct<macho::SymtabLoadCommand> &Res) const { |
| 209 | ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res); |
| 210 | } |
| 211 | |
| 212 | template<> |
Benjamin Kramer | 3946e3b | 2010-11-27 09:08:25 +0000 | [diff] [blame] | 213 | void SwapStruct(macho::DysymtabLoadCommand &Value) { |
Daniel Dunbar | f879f14 | 2010-11-27 08:33:44 +0000 | [diff] [blame] | 214 | SwapValue(Value.Type); |
| 215 | SwapValue(Value.Size); |
| 216 | SwapValue(Value.LocalSymbolIndex); |
| 217 | SwapValue(Value.NumLocalSymbols); |
| 218 | SwapValue(Value.ExternalSymbolsIndex); |
| 219 | SwapValue(Value.NumExternalSymbols); |
| 220 | SwapValue(Value.UndefinedSymbolsIndex); |
| 221 | SwapValue(Value.NumUndefinedSymbols); |
| 222 | SwapValue(Value.TOCOffset); |
| 223 | SwapValue(Value.NumTOCEntries); |
| 224 | SwapValue(Value.ModuleTableOffset); |
| 225 | SwapValue(Value.NumModuleTableEntries); |
| 226 | SwapValue(Value.ReferenceSymbolTableOffset); |
| 227 | SwapValue(Value.NumReferencedSymbolTableEntries); |
| 228 | SwapValue(Value.IndirectSymbolTableOffset); |
| 229 | SwapValue(Value.NumIndirectSymbolTableEntries); |
| 230 | SwapValue(Value.ExternalRelocationTableOffset); |
| 231 | SwapValue(Value.NumExternalRelocationTableEntries); |
| 232 | SwapValue(Value.LocalRelocationTableOffset); |
| 233 | SwapValue(Value.NumLocalRelocationTableEntries); |
| 234 | } |
| 235 | void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI, |
| 236 | InMemoryStruct<macho::DysymtabLoadCommand> &Res) const { |
| 237 | ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res); |
| 238 | } |
Daniel Dunbar | 4c55e0d | 2010-11-27 13:26:12 +0000 | [diff] [blame] | 239 | |
| 240 | template<> |
| 241 | void SwapStruct(macho::IndirectSymbolTableEntry &Value) { |
| 242 | SwapValue(Value.Index); |
| 243 | } |
| 244 | void |
| 245 | MachOObject::ReadIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC, |
| 246 | unsigned Index, |
| 247 | InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const { |
| 248 | uint64_t Offset = (DLC.IndirectSymbolTableOffset + |
| 249 | Index * sizeof(macho::IndirectSymbolTableEntry)); |
| 250 | ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res); |
| 251 | } |
Daniel Dunbar | 2acadbd | 2010-11-27 13:33:15 +0000 | [diff] [blame] | 252 | |
| 253 | |
| 254 | template<> |
| 255 | void SwapStruct(macho::Section &Value) { |
| 256 | SwapValue(Value.Address); |
| 257 | SwapValue(Value.Size); |
| 258 | SwapValue(Value.Offset); |
| 259 | SwapValue(Value.Align); |
| 260 | SwapValue(Value.RelocationTableOffset); |
| 261 | SwapValue(Value.NumRelocationTableEntries); |
| 262 | SwapValue(Value.Flags); |
| 263 | SwapValue(Value.Reserved1); |
| 264 | SwapValue(Value.Reserved2); |
| 265 | } |
| 266 | void MachOObject::ReadSection(const LoadCommandInfo &LCI, |
| 267 | unsigned Index, |
| 268 | InMemoryStruct<macho::Section> &Res) const { |
| 269 | assert(LCI.Command.Type == macho::LCT_Segment && |
| 270 | "Unexpected load command info!"); |
| 271 | uint64_t Offset = (LCI.Offset + sizeof(macho::SegmentLoadCommand) + |
| 272 | Index * sizeof(macho::Section)); |
| 273 | ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res); |
| 274 | } |
| 275 | |
| 276 | template<> |
| 277 | void SwapStruct(macho::Section64 &Value) { |
| 278 | SwapValue(Value.Address); |
| 279 | SwapValue(Value.Size); |
| 280 | SwapValue(Value.Offset); |
| 281 | SwapValue(Value.Align); |
| 282 | SwapValue(Value.RelocationTableOffset); |
| 283 | SwapValue(Value.NumRelocationTableEntries); |
| 284 | SwapValue(Value.Flags); |
| 285 | SwapValue(Value.Reserved1); |
| 286 | SwapValue(Value.Reserved2); |
| 287 | SwapValue(Value.Reserved3); |
| 288 | } |
| 289 | void MachOObject::ReadSection64(const LoadCommandInfo &LCI, |
| 290 | unsigned Index, |
| 291 | InMemoryStruct<macho::Section64> &Res) const { |
| 292 | assert(LCI.Command.Type == macho::LCT_Segment64 && |
| 293 | "Unexpected load command info!"); |
| 294 | uint64_t Offset = (LCI.Offset + sizeof(macho::Segment64LoadCommand) + |
| 295 | Index * sizeof(macho::Section64)); |
| 296 | ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res); |
| 297 | } |
Daniel Dunbar | 90e3e3a | 2010-11-27 13:39:48 +0000 | [diff] [blame] | 298 | |
| 299 | template<> |
| 300 | void SwapStruct(macho::RelocationEntry &Value) { |
| 301 | SwapValue(Value.Word0); |
| 302 | SwapValue(Value.Word1); |
| 303 | } |
| 304 | void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset, |
| 305 | unsigned Index, |
| 306 | InMemoryStruct<macho::RelocationEntry> &Res) const { |
| 307 | uint64_t Offset = (RelocationTableOffset + |
| 308 | Index * sizeof(macho::RelocationEntry)); |
| 309 | ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res); |
| 310 | } |