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" |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Casting.h" |
Rafael Espindola | 8764c89 | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DataExtractor.h" |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 22 | #include <cctype> |
| 23 | #include <cstring> |
| 24 | #include <limits> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace object; |
| 28 | |
| 29 | namespace llvm { |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 30 | namespace object { |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 31 | |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 32 | MachOObjectFileBase::MachOObjectFileBase(MemoryBuffer *Object, |
| 33 | bool IsLittleEndian, bool Is64bits, |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 34 | error_code &ec) |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 35 | : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object) { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 38 | bool MachOObjectFileBase::is64Bit() const { |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 39 | return isa<MachOObjectFileLE64>(this) || isa<MachOObjectFileBE64>(this); |
Rafael Espindola | 3eff318 | 2013-04-07 16:07:35 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 42 | void MachOObjectFileBase::ReadULEB128s(uint64_t Index, |
| 43 | SmallVectorImpl<uint64_t> &Out) const { |
Rafael Espindola | 8764c89 | 2013-04-07 20:01:29 +0000 | [diff] [blame] | 44 | DataExtractor extractor(ObjectFile::getData(), true, 0); |
| 45 | |
| 46 | uint32_t offset = Index; |
| 47 | uint64_t data = 0; |
| 48 | while (uint64_t delta = extractor.getULEB128(&offset)) { |
| 49 | data += delta; |
| 50 | Out.push_back(data); |
| 51 | } |
Rafael Espindola | 3eff318 | 2013-04-07 16:07:35 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 54 | unsigned MachOObjectFileBase::getHeaderSize() const { |
Rafael Espindola | c90cc18 | 2013-04-07 19:31:49 +0000 | [diff] [blame] | 55 | return is64Bit() ? macho::Header64Size : macho::Header32Size; |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 58 | StringRef MachOObjectFileBase::getData(size_t Offset, size_t Size) const { |
Rafael Espindola | f1cc800 | 2013-04-07 19:42:15 +0000 | [diff] [blame] | 59 | return ObjectFile::getData().substr(Offset, Size); |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 62 | ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) { |
Rafael Espindola | 9d55c09 | 2013-04-08 13:25:33 +0000 | [diff] [blame] | 63 | StringRef Magic = Buffer->getBuffer().slice(0, 4); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 64 | error_code ec; |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 65 | ObjectFile *Ret; |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 66 | if (Magic == "\xFE\xED\xFA\xCE") |
| 67 | Ret = new MachOObjectFileBE32(Buffer, ec); |
| 68 | else if (Magic == "\xCE\xFA\xED\xFE") |
| 69 | Ret = new MachOObjectFileLE32(Buffer, ec); |
| 70 | else if (Magic == "\xFE\xED\xFA\xCF") |
| 71 | Ret = new MachOObjectFileBE64(Buffer, ec); |
| 72 | else if (Magic == "\xCF\xFA\xED\xFE") |
| 73 | Ret = new MachOObjectFileLE64(Buffer, ec); |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 74 | else |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | |
Rafael Espindola | 6f1f339 | 2013-04-07 16:58:48 +0000 | [diff] [blame] | 77 | if (ec) |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 78 | return NULL; |
Rafael Espindola | 6f1f339 | 2013-04-07 16:58:48 +0000 | [diff] [blame] | 79 | return Ret; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /*===-- Symbols -----------------------------------------------------------===*/ |
| 83 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 84 | error_code MachOObjectFileBase::getSymbolValue(DataRefImpl Symb, |
| 85 | uint64_t &Val) const { |
| 86 | report_fatal_error("getSymbolValue unimplemented in MachOObjectFileBase"); |
Tim Northover | a41dce3 | 2012-10-29 10:47:00 +0000 | [diff] [blame] | 87 | } |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 88 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 89 | symbol_iterator MachOObjectFileBase::begin_dynamic_symbols() const { |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 90 | // TODO: implement |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 91 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase"); |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 94 | symbol_iterator MachOObjectFileBase::end_dynamic_symbols() const { |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 95 | // TODO: implement |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 96 | report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase"); |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 97 | } |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 98 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 99 | library_iterator MachOObjectFileBase::begin_libraries_needed() const { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 100 | // TODO: implement |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 101 | report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase"); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 104 | library_iterator MachOObjectFileBase::end_libraries_needed() const { |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 105 | // TODO: implement |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 106 | report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase"); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 109 | StringRef MachOObjectFileBase::getLoadName() const { |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 110 | // TODO: Implement |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 111 | report_fatal_error("get_load_name() unimplemented in MachOObjectFileBase"); |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 114 | /*===-- Sections ----------------------------------------------------------===*/ |
| 115 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 116 | std::size_t MachOObjectFileBase::getSectionIndex(DataRefImpl Sec) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 117 | SectionList::const_iterator loc = |
| 118 | std::find(Sections.begin(), Sections.end(), Sec); |
| 119 | assert(loc != Sections.end() && "Sec is not a valid section!"); |
| 120 | return std::distance(Sections.begin(), loc); |
| 121 | } |
| 122 | |
Rafael Espindola | da2a237 | 2013-04-13 01:45:40 +0000 | [diff] [blame] | 123 | StringRef MachOObjectFileBase::parseSegmentOrSectionName(const char *P) const { |
Rafael Espindola | cef81b3 | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 124 | if (P[15] == 0) |
| 125 | // Null terminated. |
| 126 | return P; |
| 127 | // Not null terminated, so this is a 16 char string. |
| 128 | return StringRef(P, 16); |
| 129 | } |
| 130 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 131 | error_code MachOObjectFileBase::isSectionData(DataRefImpl DRI, |
| 132 | bool &Result) const { |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 133 | // FIXME: Unimplemented. |
| 134 | Result = false; |
| 135 | return object_error::success; |
| 136 | } |
| 137 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 138 | error_code MachOObjectFileBase::isSectionBSS(DataRefImpl DRI, |
Andrew Kaylor | 30b20eb | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 139 | bool &Result) const { |
| 140 | // FIXME: Unimplemented. |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 141 | Result = false; |
| 142 | return object_error::success; |
| 143 | } |
| 144 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 145 | error_code |
| 146 | MachOObjectFileBase::isSectionRequiredForExecution(DataRefImpl Sec, |
| 147 | bool &Result) const { |
| 148 | // FIXME: Unimplemented. |
| 149 | Result = true; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 150 | return object_error::success; |
| 151 | } |
| 152 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 153 | error_code MachOObjectFileBase::isSectionVirtual(DataRefImpl Sec, |
| 154 | bool &Result) const { |
| 155 | // FIXME: Unimplemented. |
| 156 | Result = false; |
| 157 | return object_error::success; |
| 158 | } |
| 159 | |
| 160 | error_code MachOObjectFileBase::isSectionReadOnlyData(DataRefImpl Sec, |
| 161 | bool &Result) const { |
Andrew Kaylor | 3a129c8 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 162 | // Consider using the code from isSectionText to look for __const sections. |
| 163 | // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS |
| 164 | // to use section attributes to distinguish code from data. |
| 165 | |
| 166 | // FIXME: Unimplemented. |
| 167 | Result = false; |
| 168 | return object_error::success; |
| 169 | } |
| 170 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 171 | relocation_iterator MachOObjectFileBase::getSectionRelBegin(DataRefImpl Sec) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 172 | DataRefImpl ret; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 173 | ret.d.b = getSectionIndex(Sec); |
| 174 | return relocation_iterator(RelocationRef(ret, this)); |
| 175 | } |
Rafael Espindola | 335f1d4 | 2013-04-08 20:45:01 +0000 | [diff] [blame] | 176 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 177 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 178 | /*===-- Relocations -------------------------------------------------------===*/ |
| 179 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 180 | error_code MachOObjectFileBase::getRelocationNext(DataRefImpl Rel, |
| 181 | RelocationRef &Res) const { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 182 | ++Rel.d.a; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 183 | Res = RelocationRef(Rel, this); |
| 184 | return object_error::success; |
| 185 | } |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 186 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 187 | error_code MachOObjectFileBase::getLibraryNext(DataRefImpl LibData, |
| 188 | LibraryRef &Res) const { |
| 189 | report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase"); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 192 | error_code MachOObjectFileBase::getLibraryPath(DataRefImpl LibData, |
| 193 | StringRef &Res) const { |
| 194 | report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase"); |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Rafael Espindola | 8bf8006 | 2013-04-11 02:21:31 +0000 | [diff] [blame] | 197 | error_code MachOObjectFileBase::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 198 | int64_t &Res) const { |
| 199 | Res = 0; |
| 200 | return object_error::success; |
| 201 | } |
| 202 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 203 | |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 204 | /*===-- Miscellaneous -----------------------------------------------------===*/ |
| 205 | |
Rafael Espindola | f6cfc15 | 2013-04-09 14:49:08 +0000 | [diff] [blame] | 206 | uint8_t MachOObjectFileBase::getBytesInAddress() const { |
Rafael Espindola | 0f08eb1 | 2013-04-07 19:05:30 +0000 | [diff] [blame] | 207 | return is64Bit() ? 8 : 4; |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 210 | } // end namespace object |
Eric Christopher | 6256b03 | 2011-04-22 03:19:48 +0000 | [diff] [blame] | 211 | } // end namespace llvm |