File: llvm-jitlink-macho.cpp

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,388 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (161 lines) | stat: -rw-r--r-- 6,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//===-- llvm-jitlink-macho.cpp -- MachO parsing support for llvm-jitlink --===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// MachO parsing support for llvm-jitlink.
//
//===----------------------------------------------------------------------===//

#include "llvm-jitlink.h"

#include "llvm/Support/Error.h"
#include "llvm/Support/Path.h"

#define DEBUG_TYPE "llvm-jitlink"

using namespace llvm;
using namespace llvm::jitlink;

static bool isMachOGOTSection(Section &S) { return S.getName() == "$__GOT"; }

static bool isMachOStubsSection(Section &S) {
  return S.getName() == "$__STUBS";
}

static Expected<Edge &> getFirstRelocationEdge(AtomGraph &G, DefinedAtom &DA) {
  auto EItr = std::find_if(DA.edges().begin(), DA.edges().end(),
                           [](Edge &E) { return E.isRelocation(); });
  if (EItr == DA.edges().end())
    return make_error<StringError>("GOT entry in " + G.getName() + ", \"" +
                                       DA.getSection().getName() +
                                       "\" has no relocations",
                                   inconvertibleErrorCode());
  return *EItr;
}

static Expected<Atom &> getMachOGOTTarget(AtomGraph &G, DefinedAtom &DA) {
  auto E = getFirstRelocationEdge(G, DA);
  if (!E)
    return E.takeError();
  auto &TA = E->getTarget();
  if (!TA.hasName())
    return make_error<StringError>("GOT entry in " + G.getName() + ", \"" +
                                       DA.getSection().getName() +
                                       "\" points to anonymous "
                                       "atom",
                                   inconvertibleErrorCode());
  if (TA.isDefined() || TA.isAbsolute())
    return make_error<StringError>(
        "GOT entry \"" + TA.getName() + "\" in " + G.getName() + ", \"" +
            DA.getSection().getName() + "\" does not point to an external atom",
        inconvertibleErrorCode());
  return TA;
}

static Expected<Atom &> getMachOStubTarget(AtomGraph &G, DefinedAtom &DA) {
  auto E = getFirstRelocationEdge(G, DA);
  if (!E)
    return E.takeError();
  auto &GOTA = E->getTarget();
  if (!GOTA.isDefined() ||
      !isMachOGOTSection(static_cast<DefinedAtom &>(GOTA).getSection()))
    return make_error<StringError>("Stubs entry in " + G.getName() + ", \"" +
                                       DA.getSection().getName() +
                                       "\" does not point to GOT entry",
                                   inconvertibleErrorCode());
  return getMachOGOTTarget(G, static_cast<DefinedAtom &>(GOTA));
}

namespace llvm {

Error registerMachOStubsAndGOT(Session &S, AtomGraph &G) {
  auto FileName = sys::path::filename(G.getName());
  if (S.FileInfos.count(FileName)) {
    return make_error<StringError>("When -check is passed, file names must be "
                                   "distinct (duplicate: \"" +
                                       FileName + "\")",
                                   inconvertibleErrorCode());
  }

  auto &FileInfo = S.FileInfos[FileName];
  LLVM_DEBUG({
    dbgs() << "Registering MachO file info for \"" << FileName << "\"\n";
  });
  for (auto &Sec : G.sections()) {
    LLVM_DEBUG({
      dbgs() << "  Section \"" << Sec.getName() << "\": "
             << (Sec.atoms_empty() ? "empty. skipping." : "processing...")
             << "\n";
    });

    // Skip empty sections.
    if (Sec.atoms_empty())
      continue;

    if (FileInfo.SectionInfos.count(Sec.getName()))
      return make_error<StringError>("Encountered duplicate section name \"" +
                                         Sec.getName() + "\" in \"" + FileName +
                                         "\"",
                                     inconvertibleErrorCode());

    bool isGOTSection = isMachOGOTSection(Sec);
    bool isStubsSection = isMachOStubsSection(Sec);

    auto *FirstAtom = *Sec.atoms().begin();
    auto *LastAtom = FirstAtom;
    for (auto *DA : Sec.atoms()) {
      if (DA->getAddress() < FirstAtom->getAddress())
        FirstAtom = DA;
      if (DA->getAddress() > LastAtom->getAddress())
        LastAtom = DA;
      if (isGOTSection) {
        if (Sec.isZeroFill())
          return make_error<StringError>("Content atom in zero-fill section",
                                         inconvertibleErrorCode());

        if (auto TA = getMachOGOTTarget(G, *DA)) {
          FileInfo.GOTEntryInfos[TA->getName()] = {DA->getContent(),
                                                   DA->getAddress()};
        } else
          return TA.takeError();
      } else if (isStubsSection) {
        if (Sec.isZeroFill())
          return make_error<StringError>("Content atom in zero-fill section",
                                         inconvertibleErrorCode());

        if (auto TA = getMachOStubTarget(G, *DA))
          FileInfo.StubInfos[TA->getName()] = {DA->getContent(),
                                               DA->getAddress()};
        else
          return TA.takeError();
      } else if (DA->hasName() && DA->isGlobal()) {
        if (DA->isZeroFill())
          S.SymbolInfos[DA->getName()] = {DA->getSize(), DA->getAddress()};
        else {
          if (Sec.isZeroFill())
            return make_error<StringError>("Content atom in zero-fill section",
                                           inconvertibleErrorCode());
          S.SymbolInfos[DA->getName()] = {DA->getContent(), DA->getAddress()};
        }
      }
    }

    JITTargetAddress SecAddr = FirstAtom->getAddress();
    uint64_t SecSize = (LastAtom->getAddress() + LastAtom->getSize()) -
                       FirstAtom->getAddress();

    if (Sec.isZeroFill())
      FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr};
    else
      FileInfo.SectionInfos[Sec.getName()] = {
          StringRef(FirstAtom->getContent().data(), SecSize), SecAddr};
  }

  return Error::success();
}

} // end namespace llvm