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 "Target.h"
12 #include "lld/Common/Memory.h"
13 #include "llvm/Support/Endian.h"
14 
15 using namespace llvm::MachO;
16 using namespace llvm::support;
17 using namespace lld;
18 using namespace lld::macho;
19 
20 std::vector<InputSection *> macho::inputSections;
21 
22 void InputSection::writeTo(uint8_t *buf) {
23   memcpy(buf, data.data(), data.size());
24 
25   for (Reloc &r : relocs) {
26     uint64_t va = 0;
27     if (auto *s = r.target.dyn_cast<Symbol *>())
28       va = s->getVA();
29     else if (auto *isec = r.target.dyn_cast<InputSection *>())
30       va = isec->addr;
31     else
32       llvm_unreachable("Unknown relocation target");
33 
34     uint64_t val = va + r.addend;
35     if (1) // TODO: handle non-pcrel relocations
36       val -= addr - ImageBase + r.offset;
37     target->relocateOne(buf + r.offset, r.type, val);
38   }
39 }
40