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 uint64_t addend = r.addend; 36 if (auto *s = r.target.dyn_cast<Symbol *>()) { 37 if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s)) { 38 va = target->getDylibSymbolVA(*dylibSymbol, r.type); 39 } else { 40 va = s->getVA(); 41 } 42 } else if (auto *isec = r.target.dyn_cast<InputSection *>()) { 43 va = isec->getVA(); 44 // The implicit addend for pcrel section relocations is the pcrel offset 45 // in terms of the addresses in the input file. Here we adjust it so that 46 // it describes the offset from the start of the target section. 47 // TODO: Figure out what to do for non-pcrel section relocations. 48 // TODO: The offset of 4 is probably not right for ARM64. 49 addend -= isec->header->addr - (header->addr + r.offset + 4); 50 } 51 52 uint64_t val = va + addend; 53 if (1) // TODO: handle non-pcrel relocations 54 val -= getVA() + r.offset; 55 target->relocateOne(buf + r.offset, r.type, val); 56 } 57 } 58