aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/ExecutionEngine/JITLink/ELF_systemz.cpp
blob: 42cf926b3c34b35a0030421930e40bfeecd351c5 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//===----- ELF_systemz.cpp - JIT linker implementation for ELF/systemz ----===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// ELF/systemz jit-link implementation.
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/JITLink/DWARFRecordSectionSplitter.h"
#include "llvm/ExecutionEngine/JITLink/systemz.h"
#include "llvm/Object/ELFObjectFile.h"

#include "DefineExternalSectionStartAndEndSymbols.h"
#include "EHFrameSupportImpl.h"
#include "ELFLinkGraphBuilder.h"
#include "JITLinkGeneric.h"

#define DEBUG_TYPE "jitlink"

using namespace llvm;
using namespace llvm::jitlink;

namespace {

constexpr StringRef ELFGOTSymbolName = "_GLOBAL_OFFSET_TABLE_";
constexpr StringRef ELFTLSInfoSectionName = "$__TLSINFO";

// TLS Info Builder.
class TLSInfoTableManager_ELF_systemz
    : public TableManager<TLSInfoTableManager_ELF_systemz> {
public:
  static StringRef getSectionName() { return ELFTLSInfoSectionName; }

  static const uint8_t TLSInfoEntryContent[16];

  bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
    if (E.getKind() ==
        systemz::RequestTLSDescInGOTAndTransformToDelta64FromGOT) {
      LLVM_DEBUG({
        dbgs() << "  Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
               << formatv("{0:x}", B->getFixupAddress(E)) << " ("
               << formatv("{0:x}", B->getAddress()) << " + "
               << formatv("{0:x}", E.getOffset()) << ")\n";
      });
      E.setKind(systemz::Delta64FromGOT);
      E.setTarget(getEntryForTarget(G, E.getTarget()));
      return true;
    }
    return false;
  }

  Symbol &createEntry(LinkGraph &G, Symbol &Target) {
    // the TLS Info entry's key value will be written by the fixTLVSectionByName
    // pass, so create mutable content.
    auto &TLSInfoEntry = G.createMutableContentBlock(
        getTLSInfoSection(G), G.allocateContent(getTLSInfoEntryContent()),
        orc::ExecutorAddr(), 8, 0);
    TLSInfoEntry.addEdge(systemz::Pointer64, 8, Target, 0);
    return G.addAnonymousSymbol(TLSInfoEntry, 0, 16, false, false);
  }

private:
  Section &getTLSInfoSection(LinkGraph &G) {
    if (!TLSInfoTable)
      TLSInfoTable = &G.createSection(getSectionName(), orc::MemProt::Read);
    return *TLSInfoTable;
  }

  ArrayRef<char> getTLSInfoEntryContent() const {
    return {reinterpret_cast<const char *>(TLSInfoEntryContent),
            sizeof(TLSInfoEntryContent)};
  }

  Section *TLSInfoTable = nullptr;
};

const uint8_t TLSInfoTableManager_ELF_systemz::TLSInfoEntryContent[16] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

Error buildTables_ELF_systemz(LinkGraph &G) {
  LLVM_DEBUG(dbgs() << "Visiting edges in graph:\n");
  systemz::GOTTableManager GOT;
  systemz::PLTTableManager PLT(GOT);
  TLSInfoTableManager_ELF_systemz TLSInfo;
  visitExistingEdges(G, GOT, PLT, TLSInfo);
  return Error::success();
}

} // namespace

namespace llvm {
namespace jitlink {
class ELFJITLinker_systemz : public JITLinker<ELFJITLinker_systemz> {
  friend class JITLinker<ELFJITLinker_systemz>;

public:
  ELFJITLinker_systemz(std::unique_ptr<JITLinkContext> Ctx,
                       std::unique_ptr<LinkGraph> G,
                       PassConfiguration PassConfig)
      : JITLinker(std::move(Ctx), std::move(G), std::move(PassConfig)) {
    if (shouldAddDefaultTargetPasses(getGraph().getTargetTriple()))
      getPassConfig().PostAllocationPasses.push_back(
          [this](LinkGraph &G) { return getOrCreateGOTSymbol(G); });
  }

private:
  Symbol *GOTSymbol = nullptr;

  Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
    return systemz::applyFixup(G, B, E, GOTSymbol);
  }

  Error getOrCreateGOTSymbol(LinkGraph &G) {
    auto DefineExternalGOTSymbolIfPresent =
        createDefineExternalSectionStartAndEndSymbolsPass(
            [&](LinkGraph &LG, Symbol &Sym) -> SectionRangeSymbolDesc {
              if (Sym.getName() != nullptr &&
                  *Sym.getName() == ELFGOTSymbolName)
                if (auto *GOTSection = G.findSectionByName(
                        systemz::GOTTableManager::getSectionName())) {
                  GOTSymbol = &Sym;
                  return {*GOTSection, true};
                }
              return {};
            });

    // Try to attach _GLOBAL_OFFSET_TABLE_ to the GOT if it's defined as an
    // external.
    if (auto Err = DefineExternalGOTSymbolIfPresent(G))
      return Err;

    // If we succeeded then we're done.
    if (GOTSymbol)
      return Error::success();

    // Otherwise look for a GOT section: If it already has a start symbol we'll
    // record it, otherwise we'll create our own.
    // If there's a GOT section but we didn't find an external GOT symbol...
    if (auto *GOTSection =
            G.findSectionByName(systemz::GOTTableManager::getSectionName())) {

      // Check for an existing defined symbol.
      for (auto *Sym : GOTSection->symbols())
        if (Sym->getName() != nullptr && *Sym->getName() == ELFGOTSymbolName) {
          GOTSymbol = Sym;
          return Error::success();
        }

      // If there's no defined symbol then create one.
      SectionRange SR(*GOTSection);
      if (SR.empty())
        GOTSymbol =
            &G.addAbsoluteSymbol(ELFGOTSymbolName, orc::ExecutorAddr(), 0,
                                 Linkage::Strong, Scope::Local, true);
      else
        GOTSymbol =
            &G.addDefinedSymbol(*SR.getFirstBlock(), 0, ELFGOTSymbolName, 0,
                                Linkage::Strong, Scope::Local, false, true);
    }

    // If we still haven't found a GOT symbol then double check the externals.
    // We may have a GOT-relative reference but no GOT section, in which case
    // we just need to point the GOT symbol at some address in this graph.
    if (!GOTSymbol) {
      for (auto *Sym : G.external_symbols()) {
        if (Sym->getName() != nullptr && *Sym->getName() == ELFGOTSymbolName) {
          auto Blocks = G.blocks();
          if (!Blocks.empty()) {
            G.makeAbsolute(*Sym, (*Blocks.begin())->getAddress());
            GOTSymbol = Sym;
            break;
          }
        }
      }
    }

    return Error::success();
  }
};

class ELFLinkGraphBuilder_systemz
    : public ELFLinkGraphBuilder<object::ELF64BE> {
private:
  using ELFT = object::ELF64BE;
  using Base = ELFLinkGraphBuilder<ELFT>;
  using Base::G; // Use LinkGraph pointer from base class.

  Error addRelocations() override {
    LLVM_DEBUG(dbgs() << "Processing relocations:\n");

    using Base = ELFLinkGraphBuilder<ELFT>;
    using Self = ELFLinkGraphBuilder_systemz;
    for (const auto &RelSect : Base::Sections) {
      if (RelSect.sh_type == ELF::SHT_REL)
        // Validate the section to read relocation entries from.
        return make_error<StringError>("No SHT_REL in valid " +
                                           G->getTargetTriple().getArchName() +
                                           " ELF object files",
                                       inconvertibleErrorCode());

      if (Error Err = Base::forEachRelaRelocation(RelSect, this,
                                                  &Self::addSingleRelocation))
        return Err;
    }

    return Error::success();
  }

  Error addSingleRelocation(const typename ELFT::Rela &Rel,
                            const typename ELFT::Shdr &FixupSect,
                            Block &BlockToFix) {
    using support::big32_t;
    using Base = ELFLinkGraphBuilder<ELFT>;
    auto ELFReloc = Rel.getType(false);

    // No reloc.
    if (LLVM_UNLIKELY(ELFReloc == ELF::R_390_NONE))
      return Error::success();

    uint32_t SymbolIndex = Rel.getSymbol(false);
    auto ObjSymbol = Base::Obj.getRelocationSymbol(Rel, Base::SymTabSec);
    if (!ObjSymbol)
      return ObjSymbol.takeError();

    Symbol *GraphSymbol = Base::getGraphSymbol(SymbolIndex);
    if (!GraphSymbol)
      return make_error<StringError>(
          formatv("Could not find symbol at given index, did you add it to "
                  "JITSymbolTable? index: {0}, shndx: {1} Size of table: {2}",
                  SymbolIndex, (*ObjSymbol)->st_shndx,
                  Base::GraphSymbols.size()),
          inconvertibleErrorCode());

    // Validate the relocation kind.
    int64_t Addend = Rel.r_addend;
    Edge::Kind Kind = Edge::Invalid;

    switch (ELFReloc) {
    case ELF::R_390_PC64: {
      Kind = systemz::Delta64;
      break;
    }
    case ELF::R_390_PC32: {
      Kind = systemz::Delta32;
      break;
    }
    case ELF::R_390_PC16: {
      Kind = systemz::Delta16;
      break;
    }
    case ELF::R_390_PC32DBL: {
      Kind = systemz::Delta32dbl;
      break;
    }
    case ELF::R_390_PC24DBL: {
      Kind = systemz::Delta24dbl;
      break;
    }
    case ELF::R_390_PC16DBL: {
      Kind = systemz::Delta16dbl;
      break;
    }
    case ELF::R_390_PC12DBL: {
      Kind = systemz::Delta12dbl;
      break;
    }
    case ELF::R_390_64: {
      Kind = systemz::Pointer64;
      break;
    }
    case ELF::R_390_32: {
      Kind = systemz::Pointer32;
      break;
    }
    case ELF::R_390_20: {
      Kind = systemz::Pointer20;
      break;
    }
    case ELF::R_390_16: {
      Kind = systemz::Pointer16;
      break;
    }
    case ELF::R_390_12: {
      Kind = systemz::Pointer12;
      break;
    }
    case ELF::R_390_8: {
      Kind = systemz::Pointer8;
      break;
    }
    // Relocations targeting the PLT associated with the symbol.
    case ELF::R_390_PLT64: {
      Kind = systemz::DeltaPLT64;
      break;
    }
    case ELF::R_390_PLT32: {
      Kind = systemz::DeltaPLT32;
      break;
    }
    case ELF::R_390_PLT32DBL: {
      Kind = systemz::DeltaPLT32dbl;
      break;
    }
    case ELF::R_390_PLT24DBL: {
      Kind = systemz::DeltaPLT24dbl;
      break;
    }
    case ELF::R_390_PLT16DBL: {
      Kind = systemz::DeltaPLT16dbl;
      break;
    }
    case ELF::R_390_PLT12DBL: {
      Kind = systemz::DeltaPLT12dbl;
      break;
    }
    case ELF::R_390_PLTOFF64: {
      Kind = systemz::Delta64PLTFromGOT;
      break;
    }
    case ELF::R_390_PLTOFF32: {
      Kind = systemz::Delta32PLTFromGOT;
      break;
    }
    case ELF::R_390_PLTOFF16: {
      Kind = systemz::Delta16PLTFromGOT;
      break;
    }
    // Relocations targeting the actual symbol (just relative to the GOT).
    case ELF::R_390_GOTOFF64: {
      Kind = systemz::Delta64FromGOT;
      break;
    }
    case ELF::R_390_GOTOFF: {
      Kind = systemz::Delta32FromGOT;
      break;
    }
    case ELF::R_390_GOTOFF16: {
      Kind = systemz::Delta16FromGOT;
      break;
    }
    // Relocations targeting the GOT entry associated with the symbol.
    case ELF::R_390_GOT64:
    case ELF::R_390_GOTPLT64: {
      Kind = systemz::RequestGOTAndTransformToDelta64FromGOT;
      break;
    }
    case ELF::R_390_GOT32:
    case ELF::R_390_GOTPLT32: {
      Kind = systemz::RequestGOTAndTransformToDelta32FromGOT;
      break;
    }
    case ELF::R_390_GOT20:
    case ELF::R_390_GOTPLT20: {
      Kind = systemz::RequestGOTAndTransformToDelta20FromGOT;
      break;
    }
    case ELF::R_390_GOT16:
    case ELF::R_390_GOTPLT16: {
      Kind = systemz::RequestGOTAndTransformToDelta16FromGOT;
      break;
    }
    case ELF::R_390_GOT12:
    case ELF::R_390_GOTPLT12: {
      Kind = systemz::RequestGOTAndTransformToDelta12FromGOT;
      break;
    }
    case ELF::R_390_GOTENT:
    case ELF::R_390_GOTPLTENT: {
      Kind = systemz::RequestGOTAndTransformToDelta32dbl;
      break;
    }
    // R_390_GOTPC and R_390_GOTPCDBL don't create GOT entry, they don't even
    // have symbol.
    case ELF::R_390_GOTPC: {
      Kind = systemz::Delta32GOTBase;
      break;
    }
    case ELF::R_390_GOTPCDBL: {
      Kind = systemz::Delta32dblGOTBase;
      break;
    }
    // Tag for function call in general dynamic TLS code.
    case ELF::R_390_TLS_GDCALL: {
      break;
    }
    // Direct 64 bit for general dynamic thread local data.
    case ELF::R_390_TLS_GD64: {
      Kind = systemz::RequestTLSDescInGOTAndTransformToDelta64FromGOT;
      break;
    }
    default:
      return make_error<JITLinkError>(
          "In " + G->getName() + ": Unsupported systemz relocation type " +
          object::getELFRelocationTypeName(ELF::EM_S390, ELFReloc));
    }
    auto FixupAddress = orc::ExecutorAddr(FixupSect.sh_addr) + Rel.r_offset;
    Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
    Edge GE(Kind, Offset, *GraphSymbol, Addend);
    LLVM_DEBUG({
      dbgs() << "    ";
      printEdge(dbgs(), BlockToFix, GE, systemz::getEdgeKindName(Kind));
      dbgs() << "\n";
    });

    BlockToFix.addEdge(std::move(GE));

    return Error::success();
  }

public:
  ELFLinkGraphBuilder_systemz(StringRef FileName,
                              const object::ELFFile<ELFT> &Obj,
                              std::shared_ptr<orc::SymbolStringPool> SSP,
                              Triple TT, SubtargetFeatures Features)
      : ELFLinkGraphBuilder<ELFT>(Obj, std::move(SSP), std::move(TT),
                                  std::move(Features), FileName,
                                  systemz::getEdgeKindName) {}
};

Expected<std::unique_ptr<LinkGraph>> createLinkGraphFromELFObject_systemz(
    MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
  LLVM_DEBUG({
    dbgs() << "Building jitlink graph for new input "
           << ObjectBuffer.getBufferIdentifier() << "...\n";
  });

  auto ELFObj = object::ObjectFile::createELFObjectFile(ObjectBuffer);
  if (!ELFObj)
    return ELFObj.takeError();

  auto Features = (*ELFObj)->getFeatures();
  if (!Features)
    return Features.takeError();

  assert((*ELFObj)->getArch() == Triple::systemz &&
         "Only SystemZ is supported");

  auto &ELFObjFile = cast<object::ELFObjectFile<object::ELF64BE>>(**ELFObj);
  return ELFLinkGraphBuilder_systemz(
             (*ELFObj)->getFileName(), ELFObjFile.getELFFile(), std::move(SSP),
             (*ELFObj)->makeTriple(), std::move(*Features))
      .buildGraph();
}

void link_ELF_systemz(std::unique_ptr<LinkGraph> G,
                      std::unique_ptr<JITLinkContext> Ctx) {
  PassConfiguration Config;
  const Triple &TT = G->getTargetTriple();
  if (Ctx->shouldAddDefaultTargetPasses(TT)) {
    // Add eh-frame passes.
    Config.PrePrunePasses.push_back(DWARFRecordSectionSplitter(".eh_frame"));
    Config.PrePrunePasses.push_back(
        EHFrameEdgeFixer(".eh_frame", G->getPointerSize(), systemz::Pointer32,
                         systemz::Pointer64, systemz::Delta32, systemz::Delta64,
                         systemz::NegDelta32));
    Config.PrePrunePasses.push_back(EHFrameNullTerminator(".eh_frame"));

    // Add a mark-live pass.
    if (auto MarkLive = Ctx->getMarkLivePass(TT))
      Config.PrePrunePasses.push_back(std::move(MarkLive));
    else
      Config.PrePrunePasses.push_back(markAllSymbolsLive);

    // Add an in-place GOT/Stubs build pass.
    Config.PostPrunePasses.push_back(buildTables_ELF_systemz);

    // Resolve any external section start / end symbols.
    Config.PostAllocationPasses.push_back(
        createDefineExternalSectionStartAndEndSymbolsPass(
            identifyELFSectionStartAndEndSymbols));

    // Add GOT/Stubs optimizer pass.
    Config.PreFixupPasses.push_back(systemz::optimizeGOTAndStubAccesses);
  }

  if (auto Err = Ctx->modifyPassConfig(*G, Config))
    return Ctx->notifyFailed(std::move(Err));

  ELFJITLinker_systemz::link(std::move(Ctx), std::move(G), std::move(Config));
}

} // namespace jitlink
} // namespace llvm