1 //===- InputFiles.cpp -----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains functions to parse Mach-O object files. In this comment, 10 // we describe the Mach-O file structure and how we parse it. 11 // 12 // Mach-O is not very different from ELF or COFF. The notion of symbols, 13 // sections and relocations exists in Mach-O as it does in ELF and COFF. 14 // 15 // Perhaps the notion that is new to those who know ELF/COFF is "subsections". 16 // In ELF/COFF, sections are an atomic unit of data copied from input files to 17 // output files. When we merge or garbage-collect sections, we treat each 18 // section as an atomic unit. In Mach-O, that's not the case. Sections can 19 // consist of multiple subsections, and subsections are a unit of merging and 20 // garbage-collecting. Therefore, Mach-O's subsections are more similar to 21 // ELF/COFF's sections than Mach-O's sections are. 22 // 23 // A section can have multiple symbols. A symbol that does not have the 24 // N_ALT_ENTRY attribute indicates a beginning of a subsection. Therefore, by 25 // definition, a symbol is always present at the beginning of each subsection. A 26 // symbol with N_ALT_ENTRY attribute does not start a new subsection and can 27 // point to a middle of a subsection. 28 // 29 // The notion of subsections also affects how relocations are represented in 30 // Mach-O. All references within a section need to be explicitly represented as 31 // relocations if they refer to different subsections, because we obviously need 32 // to fix up addresses if subsections are laid out in an output file differently 33 // than they were in object files. To represent that, Mach-O relocations can 34 // refer to an unnamed location via its address. Scattered relocations (those 35 // with the R_SCATTERED bit set) always refer to unnamed locations. 36 // Non-scattered relocations refer to an unnamed location if r_extern is not set 37 // and r_symbolnum is zero. 38 // 39 // Without the above differences, I think you can use your knowledge about ELF 40 // and COFF for Mach-O. 41 // 42 //===----------------------------------------------------------------------===// 43 44 #include "InputFiles.h" 45 #include "InputSection.h" 46 #include "OutputSegment.h" 47 #include "SymbolTable.h" 48 #include "Symbols.h" 49 #include "Target.h" 50 51 #include "lld/Common/ErrorHandler.h" 52 #include "lld/Common/Memory.h" 53 #include "llvm/BinaryFormat/MachO.h" 54 #include "llvm/Support/Endian.h" 55 #include "llvm/Support/MemoryBuffer.h" 56 57 using namespace llvm; 58 using namespace llvm::MachO; 59 using namespace llvm::support::endian; 60 using namespace lld; 61 using namespace lld::macho; 62 63 std::vector<InputFile *> macho::inputFiles; 64 65 // Open a given file path and return it as a memory-mapped file. 66 Optional<MemoryBufferRef> macho::readFile(StringRef path) { 67 // Open a file. 68 auto mbOrErr = MemoryBuffer::getFile(path); 69 if (auto ec = mbOrErr.getError()) { 70 error("cannot open " + path + ": " + ec.message()); 71 return None; 72 } 73 74 std::unique_ptr<MemoryBuffer> &mb = *mbOrErr; 75 MemoryBufferRef mbref = mb->getMemBufferRef(); 76 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership 77 78 // If this is a regular non-fat file, return it. 79 const char *buf = mbref.getBufferStart(); 80 auto *hdr = reinterpret_cast<const MachO::fat_header *>(buf); 81 if (read32be(&hdr->magic) != MachO::FAT_MAGIC) 82 return mbref; 83 84 error("TODO: Add support for universal binaries"); 85 return None; 86 } 87 88 static const load_command *findCommand(const mach_header_64 *hdr, 89 uint32_t type) { 90 const uint8_t *p = 91 reinterpret_cast<const uint8_t *>(hdr) + sizeof(mach_header_64); 92 93 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { 94 auto *cmd = reinterpret_cast<const load_command *>(p); 95 if (cmd->cmd == type) 96 return cmd; 97 p += cmd->cmdsize; 98 } 99 return nullptr; 100 } 101 102 std::vector<InputSection *> 103 InputFile::parseSections(ArrayRef<section_64> sections) { 104 std::vector<InputSection *> ret; 105 ret.reserve(sections.size()); 106 107 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 108 109 for (const section_64 &sec : sections) { 110 InputSection *isec = make<InputSection>(); 111 isec->file = this; 112 isec->name = StringRef(sec.sectname, strnlen(sec.sectname, 16)); 113 isec->segname = StringRef(sec.segname, strnlen(sec.segname, 16)); 114 isec->data = {buf + sec.offset, static_cast<size_t>(sec.size)}; 115 if (sec.align >= 32) 116 error("alignment " + std::to_string(sec.align) + " of section " + 117 isec->name + " is too large"); 118 else 119 isec->align = 1 << sec.align; 120 isec->flags = sec.flags; 121 ret.push_back(isec); 122 } 123 124 return ret; 125 } 126 127 void InputFile::parseRelocations(const section_64 &sec, 128 std::vector<Reloc> &relocs) { 129 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 130 ArrayRef<any_relocation_info> relInfos( 131 reinterpret_cast<const any_relocation_info *>(buf + sec.reloff), 132 sec.nreloc); 133 134 for (const any_relocation_info &anyRel : relInfos) { 135 Reloc r; 136 if (anyRel.r_word0 & R_SCATTERED) { 137 error("TODO: Scattered relocations not supported"); 138 } else { 139 auto rel = reinterpret_cast<const relocation_info &>(anyRel); 140 r.type = rel.r_type; 141 r.offset = rel.r_address; 142 r.addend = target->getImplicitAddend(buf + sec.offset + r.offset, r.type); 143 if (rel.r_extern) 144 r.target = symbols[rel.r_symbolnum]; 145 else { 146 error("TODO: Non-extern relocations are not supported"); 147 continue; 148 } 149 } 150 relocs.push_back(r); 151 } 152 } 153 154 ObjFile::ObjFile(MemoryBufferRef mb) : InputFile(ObjKind, mb) { 155 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 156 auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart()); 157 ArrayRef<section_64> objSections; 158 159 if (const load_command *cmd = findCommand(hdr, LC_SEGMENT_64)) { 160 auto *c = reinterpret_cast<const segment_command_64 *>(cmd); 161 objSections = ArrayRef<section_64>{ 162 reinterpret_cast<const section_64 *>(c + 1), c->nsects}; 163 sections = parseSections(objSections); 164 } 165 166 // TODO: Error on missing LC_SYMTAB? 167 if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) { 168 auto *c = reinterpret_cast<const symtab_command *>(cmd); 169 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff; 170 ArrayRef<const nlist_64> nList( 171 reinterpret_cast<const nlist_64 *>(buf + c->symoff), c->nsyms); 172 173 symbols.reserve(c->nsyms); 174 175 for (const nlist_64 &sym : nList) { 176 StringRef name = strtab + sym.n_strx; 177 178 // Undefined symbol 179 if (!sym.n_sect) { 180 symbols.push_back(symtab->addUndefined(name)); 181 continue; 182 } 183 184 InputSection *isec = sections[sym.n_sect - 1]; 185 const section_64 &objSec = objSections[sym.n_sect - 1]; 186 uint64_t value = sym.n_value - objSec.addr; 187 188 // Global defined symbol 189 if (sym.n_type & N_EXT) { 190 symbols.push_back(symtab->addDefined(name, isec, value)); 191 continue; 192 } 193 194 // Local defined symbol 195 symbols.push_back(make<Defined>(name, isec, value)); 196 } 197 } 198 199 // The relocations may refer to the symbols, so we parse them after we have 200 // the symbols loaded. 201 if (!sections.empty()) { 202 auto it = sections.begin(); 203 for (const section_64 &sec : objSections) { 204 parseRelocations(sec, (*it)->relocs); 205 ++it; 206 } 207 } 208 } 209 210 DylibFile::DylibFile(MemoryBufferRef mb) : InputFile(DylibKind, mb) { 211 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 212 auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart()); 213 214 // Initialize dylibName. 215 if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) { 216 auto *c = reinterpret_cast<const dylib_command *>(cmd); 217 dylibName = reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name); 218 } else { 219 error("dylib " + getName() + " missing LC_ID_DYLIB load command"); 220 return; 221 } 222 223 // Initialize symbols. 224 if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) { 225 auto *c = reinterpret_cast<const symtab_command *>(cmd); 226 const char *strtab = reinterpret_cast<const char *>(buf + c->stroff); 227 ArrayRef<const nlist_64> nList( 228 reinterpret_cast<const nlist_64 *>(buf + c->symoff), c->nsyms); 229 230 symbols.reserve(c->nsyms); 231 232 for (const nlist_64 &sym : nList) { 233 StringRef name = strtab + sym.n_strx; 234 // TODO: Figure out what to do about undefined symbols: ignore or warn 235 // if unsatisfied? Also make sure we handle re-exported symbols 236 // correctly. 237 symbols.push_back(symtab->addDylib(name, this)); 238 } 239 } 240 } 241 242 // Returns "<internal>" or "baz.o". 243 std::string lld::toString(const InputFile *file) { 244 return file ? std::string(file->getName()) : "<internal>"; 245 } 246