brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.5 KiB · 07dd3bd Raw
160 lines · cpp
1//===-- llvm-jitlink-macho.cpp -- MachO parsing support for llvm-jitlink --===//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-exception6//7//===----------------------------------------------------------------------===//8//9// MachO parsing support for llvm-jitlink.10//11//===----------------------------------------------------------------------===//12 13#include "llvm-jitlink.h"14 15#include "llvm/Support/Error.h"16#include "llvm/Support/Path.h"17 18#define DEBUG_TYPE "llvm_jitlink"19 20using namespace llvm;21using namespace llvm::jitlink;22 23static bool isMachOGOTSection(Section &S) { return S.getName() == "$__GOT"; }24 25static bool isMachOStubsSection(Section &S) {26  return S.getName() == "$__STUBS";27}28 29static Expected<Edge &> getFirstRelocationEdge(LinkGraph &G, Block &B) {30  auto EItr =31      llvm::find_if(B.edges(), [](Edge &E) { return E.isRelocation(); });32  if (EItr == B.edges().end())33    return make_error<StringError>("GOT entry in " + G.getName() + ", \"" +34                                       B.getSection().getName() +35                                       "\" has no relocations",36                                   inconvertibleErrorCode());37  return *EItr;38}39 40static Expected<Symbol &> getMachOGOTTarget(LinkGraph &G, Block &B) {41  auto E = getFirstRelocationEdge(G, B);42  if (!E)43    return E.takeError();44  auto &TargetSym = E->getTarget();45  if (!TargetSym.hasName())46    return make_error<StringError>(47        "GOT entry in " + G.getName() + ", \"" +48            TargetSym.getBlock().getSection().getName() +49            "\" points to anonymous "50            "symbol",51        inconvertibleErrorCode());52  return TargetSym;53}54 55static Expected<Symbol &> getMachOStubTarget(LinkGraph &G, Block &B) {56  auto E = getFirstRelocationEdge(G, B);57  if (!E)58    return E.takeError();59  auto &GOTSym = E->getTarget();60  if (!GOTSym.isDefined() || !isMachOGOTSection(GOTSym.getBlock().getSection()))61    return make_error<StringError>(62        "Stubs entry in " + G.getName() + ", \"" +63            GOTSym.getBlock().getSection().getName() +64            "\" does not point to GOT entry",65        inconvertibleErrorCode());66  return getMachOGOTTarget(G, GOTSym.getBlock());67}68 69namespace llvm {70 71Error registerMachOGraphInfo(Session &S, LinkGraph &G) {72  std::lock_guard<std::mutex> Lock(S.M);73 74  auto FileName = sys::path::filename(G.getName());75  auto [It, Inserted] = S.FileInfos.try_emplace(FileName);76  if (!Inserted) {77    return make_error<StringError>("When -check is passed, file names must be "78                                   "distinct (duplicate: \"" +79                                       FileName + "\")",80                                   inconvertibleErrorCode());81  }82 83  auto &FileInfo = It->second;84  LLVM_DEBUG({85    dbgs() << "Registering MachO file info for \"" << FileName << "\"\n";86  });87  for (auto &Sec : G.sections()) {88    LLVM_DEBUG({89      dbgs() << "  Section \"" << Sec.getName() << "\": "90             << (Sec.symbols().empty() ? "empty. skipping." : "processing...")91             << "\n";92    });93 94    // Skip empty sections.95    if (Sec.symbols().empty())96      continue;97 98    if (FileInfo.SectionInfos.count(Sec.getName()))99      return make_error<StringError>("Encountered duplicate section name \"" +100                                         Sec.getName() + "\" in \"" + FileName +101                                         "\"",102                                     inconvertibleErrorCode());103 104    bool isGOTSection = isMachOGOTSection(Sec);105    bool isStubsSection = isMachOStubsSection(Sec);106 107    bool SectionContainsContent = false;108    bool SectionContainsZeroFill = false;109 110    auto *FirstSym = *Sec.symbols().begin();111    auto *LastSym = FirstSym;112    for (auto *Sym : Sec.symbols()) {113      if (Sym->getAddress() < FirstSym->getAddress())114        FirstSym = Sym;115      if (Sym->getAddress() > LastSym->getAddress())116        LastSym = Sym;117      if (isGOTSection || isStubsSection) {118        Error Err =119            isGOTSection120                ? FileInfo.registerGOTEntry(G, *Sym, getMachOGOTTarget)121                : FileInfo.registerStubEntry(G, *Sym, getMachOStubTarget);122        if (Err)123          return Err;124        SectionContainsContent = true;125      } else if (Sym->hasName()) {126        if (Sym->isSymbolZeroFill()) {127          S.SymbolInfos[Sym->getName()] = {Sym->getSize(),128                                           Sym->getAddress().getValue()};129          SectionContainsZeroFill = true;130        } else {131          S.SymbolInfos[Sym->getName()] = {Sym->getSymbolContent(),132                                           Sym->getAddress().getValue(),133                                           Sym->getTargetFlags()};134          SectionContainsContent = true;135        }136      }137    }138 139    auto SecAddr = FirstSym->getAddress();140    auto SecSize =141        (LastSym->getBlock().getAddress() + LastSym->getBlock().getSize()) -142        SecAddr;143 144    if (SectionContainsZeroFill && SectionContainsContent)145      return make_error<StringError>("Mixed zero-fill and content sections not "146                                     "supported yet",147                                     inconvertibleErrorCode());148    if (SectionContainsZeroFill)149      FileInfo.SectionInfos[Sec.getName()] = {SecSize, SecAddr.getValue()};150    else151      FileInfo.SectionInfos[Sec.getName()] = {152          ArrayRef<char>(FirstSym->getBlock().getContent().data(), SecSize),153          SecAddr.getValue(), FirstSym->getTargetFlags()};154  }155 156  return Error::success();157}158 159} // end namespace llvm160