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 return mbref; 78 } 79 80 static const load_command *findCommand(const mach_header_64 *hdr, 81 uint32_t type) { 82 const uint8_t *p = 83 reinterpret_cast<const uint8_t *>(hdr) + sizeof(mach_header_64); 84 85 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) { 86 auto *cmd = reinterpret_cast<const load_command *>(p); 87 if (cmd->cmd == type) 88 return cmd; 89 p += cmd->cmdsize; 90 } 91 return nullptr; 92 } 93 94 std::vector<InputSection *> 95 InputFile::parseSections(ArrayRef<section_64> sections) { 96 std::vector<InputSection *> ret; 97 ret.reserve(sections.size()); 98 99 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 100 101 for (const section_64 &sec : sections) { 102 InputSection *isec = make<InputSection>(); 103 isec->file = this; 104 isec->name = StringRef(sec.sectname, strnlen(sec.sectname, 16)); 105 isec->segname = StringRef(sec.segname, strnlen(sec.segname, 16)); 106 isec->data = {buf + sec.offset, static_cast<size_t>(sec.size)}; 107 if (sec.align >= 32) 108 error("alignment " + std::to_string(sec.align) + " of section " + 109 isec->name + " is too large"); 110 else 111 isec->align = 1 << sec.align; 112 isec->flags = sec.flags; 113 ret.push_back(isec); 114 } 115 116 return ret; 117 } 118 119 void InputFile::parseRelocations(const section_64 &sec, 120 std::vector<Reloc> &relocs) { 121 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 122 ArrayRef<any_relocation_info> relInfos( 123 reinterpret_cast<const any_relocation_info *>(buf + sec.reloff), 124 sec.nreloc); 125 126 for (const any_relocation_info &anyRel : relInfos) { 127 Reloc r; 128 if (anyRel.r_word0 & R_SCATTERED) { 129 error("TODO: Scattered relocations not supported"); 130 } else { 131 auto rel = reinterpret_cast<const relocation_info &>(anyRel); 132 r.type = rel.r_type; 133 r.offset = rel.r_address; 134 r.addend = target->getImplicitAddend(buf + sec.offset + r.offset, r.type); 135 if (rel.r_extern) 136 r.target = symbols[rel.r_symbolnum]; 137 else { 138 error("TODO: Non-extern relocations are not supported"); 139 continue; 140 } 141 } 142 relocs.push_back(r); 143 } 144 } 145 146 ObjFile::ObjFile(MemoryBufferRef mb) : InputFile(ObjKind, mb) { 147 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart()); 148 auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart()); 149 ArrayRef<section_64> objSections; 150 151 if (const load_command *cmd = findCommand(hdr, LC_SEGMENT_64)) { 152 auto *c = reinterpret_cast<const segment_command_64 *>(cmd); 153 objSections = ArrayRef<section_64>{ 154 reinterpret_cast<const section_64 *>(c + 1), c->nsects}; 155 sections = parseSections(objSections); 156 } 157 158 if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) { 159 auto *c = reinterpret_cast<const symtab_command *>(cmd); 160 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff; 161 ArrayRef<const nlist_64> nList( 162 reinterpret_cast<const nlist_64 *>(buf + c->symoff), c->nsyms); 163 164 symbols.reserve(c->nsyms); 165 166 for (const nlist_64 &sym : nList) { 167 StringRef name = strtab + sym.n_strx; 168 169 // Undefined symbol 170 if (!sym.n_sect) { 171 error("TODO: Support undefined symbols"); 172 continue; 173 } 174 175 InputSection *isec = sections[sym.n_sect - 1]; 176 const section_64 &objSec = objSections[sym.n_sect - 1]; 177 uint64_t value = sym.n_value - objSec.addr; 178 179 // Global defined symbol 180 if (sym.n_type & N_EXT) { 181 symbols.push_back(symtab->addDefined(name, isec, value)); 182 continue; 183 } 184 185 // Local defined symbol 186 symbols.push_back(make<Defined>(name, isec, value)); 187 } 188 } 189 190 // The relocations may refer to the symbols, so we parse them after we have 191 // the symbols loaded. 192 if (!sections.empty()) { 193 auto it = sections.begin(); 194 for (const section_64 &sec : objSections) { 195 parseRelocations(sec, (*it)->relocs); 196 ++it; 197 } 198 } 199 } 200 201 // Returns "<internal>" or "baz.o". 202 std::string lld::toString(const InputFile *file) { 203 return file ? std::string(file->getName()) : "<internal>"; 204 } 205