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 "Symbols.h"
11 #include "SyntheticSections.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 void InputSection::writeTo(uint8_t *buf) {
24   memcpy(buf, data.data(), data.size());
25 
26   for (Reloc &r : relocs) {
27     uint64_t va = 0;
28     if (auto *s = r.target.dyn_cast<Symbol *>()) {
29       if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s)) {
30         va = in.got->addr - ImageBase + dylibSymbol->gotIndex * WordSize;
31       } else {
32         va = s->getVA();
33       }
34     } else if (auto *isec = r.target.dyn_cast<InputSection *>()) {
35       va = isec->addr;
36     } else {
37       llvm_unreachable("Unknown relocation target");
38     }
39 
40     uint64_t val = va + r.addend;
41     if (1) // TODO: handle non-pcrel relocations
42       val -= addr - ImageBase + r.offset;
43     target->relocateOne(buf + r.offset, r.type, val);
44   }
45 }
46