reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
//===- lib/ReaderWriter/MachO/Atoms.h ---------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_MACHO_ATOMS_H
#define LLD_READER_WRITER_MACHO_ATOMS_H

#include "lld/Core/Atom.h"
#include "lld/Core/DefinedAtom.h"
#include "lld/Core/SharedLibraryAtom.h"
#include "lld/Core/Simple.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include <cstdint>
#include <string>

namespace lld {

class File;

namespace mach_o {

class MachODefinedAtom : public SimpleDefinedAtom {
public:
  MachODefinedAtom(const File &f, const StringRef name, Scope scope,
                   ContentType type, Merge merge, bool thumb, bool noDeadStrip,
                   const ArrayRef<uint8_t> content, Alignment align)
      : SimpleDefinedAtom(f), _name(name), _content(content),
        _align(align), _contentType(type), _scope(scope), _merge(merge),
        _thumb(thumb), _noDeadStrip(noDeadStrip) {}

  // Constructor for zero-fill content
  MachODefinedAtom(const File &f, const StringRef name, Scope scope,
                   ContentType type, uint64_t size, bool noDeadStrip,
                   Alignment align)
      : SimpleDefinedAtom(f), _name(name),
        _content(ArrayRef<uint8_t>(nullptr, size)), _align(align),
        _contentType(type), _scope(scope), _merge(mergeNo), _thumb(false),
        _noDeadStrip(noDeadStrip) {}

  ~MachODefinedAtom() override = default;

  uint64_t size() const override { return _content.size(); }

  ContentType contentType() const override { return _contentType; }

  Alignment alignment() const override { return _align; }

  StringRef name() const override { return _name; }

  Scope scope() const override { return _scope; }

  Merge merge() const override { return _merge; }

  DeadStripKind deadStrip() const override {
    if (_contentType == DefinedAtom::typeInitializerPtr)
      return deadStripNever;
    if (_contentType == DefinedAtom::typeTerminatorPtr)
      return deadStripNever;
    if (_noDeadStrip)
      return deadStripNever;
    return deadStripNormal;
  }

  ArrayRef<uint8_t> rawContent() const override {
    // Note: Zerofill atoms have a content pointer which is null.
    return _content;
  }

  bool isThumb() const { return _thumb; }

private:
  const StringRef _name;
  const ArrayRef<uint8_t> _content;
  const DefinedAtom::Alignment _align;
  const ContentType _contentType;
  const Scope _scope;
  const Merge _merge;
  const bool _thumb;
  const bool _noDeadStrip;
};

class MachODefinedCustomSectionAtom : public MachODefinedAtom {
public:
  MachODefinedCustomSectionAtom(const File &f, const StringRef name,
                                Scope scope, ContentType type, Merge merge,
                                bool thumb, bool noDeadStrip,
                                const ArrayRef<uint8_t> content,
                                StringRef sectionName, Alignment align)
      : MachODefinedAtom(f, name, scope, type, merge, thumb, noDeadStrip,
                         content, align),
        _sectionName(sectionName) {}

  ~MachODefinedCustomSectionAtom() override = default;

  SectionChoice sectionChoice() const override {
    return DefinedAtom::sectionCustomRequired;
  }

  StringRef customSectionName() const override {
    return _sectionName;
  }
private:
  StringRef _sectionName;
};

class MachOTentativeDefAtom : public SimpleDefinedAtom {
public:
  MachOTentativeDefAtom(const File &f, const StringRef name, Scope scope,
                        uint64_t size, DefinedAtom::Alignment align)
      : SimpleDefinedAtom(f), _name(name), _scope(scope), _size(size),
        _align(align) {}

  ~MachOTentativeDefAtom() override = default;

  uint64_t size() const override { return _size; }

  Merge merge() const override { return DefinedAtom::mergeAsTentative; }

  ContentType contentType() const override { return DefinedAtom::typeZeroFill; }

  Alignment alignment() const override { return _align; }

  StringRef name() const override { return _name; }

  Scope scope() const override { return _scope; }

  ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }

private:
  const std::string _name;
  const Scope _scope;
  const uint64_t _size;
  const DefinedAtom::Alignment _align;
};

class MachOSharedLibraryAtom : public SharedLibraryAtom {
public:
  MachOSharedLibraryAtom(const File &file, StringRef name,
                         StringRef dylibInstallName, bool weakDef)
      : SharedLibraryAtom(), _file(file), _name(name),
        _dylibInstallName(dylibInstallName) {}
  ~MachOSharedLibraryAtom() override = default;

  StringRef loadName() const override { return _dylibInstallName; }

  bool canBeNullAtRuntime() const override {
    // FIXME: this may actually be changeable. For now, all symbols are strongly
    // defined though.
    return false;
  }

  const File &file() const override { return _file; }

  StringRef name() const override { return _name; }

  Type type() const override {
    // Unused in MachO (I think).
    return Type::Unknown;
  }

  uint64_t size() const override {
    // Unused in MachO (I think)
    return 0;
  }

private:
  const File &_file;
  StringRef _name;
  StringRef _dylibInstallName;
};

} // end namespace mach_o
} // end namespace lld

#endif // LLD_READER_WRITER_MACHO_ATOMS_H