1 //===- InputSection.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 #include "InputSection.h" 10 #include "OutputSegment.h" 11 #include "Symbols.h" 12 #include "Target.h" 13 #include "lld/Common/Memory.h" 14 #include "llvm/Support/Endian.h" 15 16 using namespace llvm::MachO; 17 using namespace llvm::support; 18 using namespace lld; 19 using namespace lld::macho; 20 21 std::vector<InputSection *> macho::inputSections; 22 23 uint64_t InputSection::getFileOffset() const { 24 return parent->fileOff + outSecFileOff; 25 } 26 27 uint64_t InputSection::getVA() const { return parent->addr + outSecOff; } 28 29 void InputSection::writeTo(uint8_t *buf) { 30 if (!data.empty()) 31 memcpy(buf, data.data(), data.size()); 32 33 for (Reloc &r : relocs) { 34 uint64_t va = 0; 35 if (auto *s = r.target.dyn_cast<Symbol *>()) { 36 if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s)) { 37 va = target->getDylibSymbolVA(*dylibSymbol, r.type); 38 } else { 39 va = s->getVA(); 40 } 41 } else if (auto *isec = r.target.dyn_cast<InputSection *>()) { 42 va = isec->getVA(); 43 } 44 45 uint64_t val = va + r.addend; 46 if (r.pcrel) 47 val -= getVA() + r.offset; 48 target->relocateOne(buf + r.offset, r.type, val); 49 } 50 } 51