summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/Hexagon/HexagonSectionChunks.h
blob: 6157599856d7e2720adee0aa61f063e5ffb8b386 (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
//===- lib/ReaderWriter/ELF/Hexagon/HexagonSectionChunks.h-----------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef HEXAGON_SECTION_CHUNKS_H
#define HEXAGON_SECTION_CHUNKS_H

#include "HexagonTargetHandler.h"

namespace lld {
namespace elf {
template <typename ELFT> class HexagonTargetLayout;
class HexagonLinkingContext;

/// \brief Handle Hexagon SData section
template <class HexagonELFType>
class SDataSection : public AtomSection<HexagonELFType> {
public:
  SDataSection(const HexagonLinkingContext &context)
      : AtomSection<HexagonELFType>(
            context, ".sdata", DefinedAtom::typeDataFast, 0,
            HexagonTargetLayout<HexagonELFType>::ORDER_SDATA) {
    this->_type = SHT_PROGBITS;
    this->_flags = SHF_ALLOC | SHF_WRITE;
    this->_alignment = 4096;
  }

  /// \brief Finalize the section contents before writing
  virtual void doPreFlight();

  /// \brief Does this section have an output segment.
  virtual bool hasOutputSegment() { return true; }

  const lld::AtomLayout *appendAtom(const Atom *atom) {
    const DefinedAtom *definedAtom = cast<DefinedAtom>(atom);
    DefinedAtom::Alignment atomAlign = definedAtom->alignment();
    uint64_t alignment = atomAlign.powerOf2.get();
    this->_atoms.push_back(new (this->_alloc) lld::AtomLayout(atom, 0, 0));
    // Set the section alignment to the largest alignment
    // std::max doesn't support uint64_t
    if (this->_alignment < alignment)
      this->_alignment = alignment;
    return (this->_atoms.back());
  }

}; // SDataSection

template <class HexagonELFType>
void SDataSection<HexagonELFType>::doPreFlight() {
  // sort the atoms on the alignments they have been set
  std::stable_sort(this->_atoms.begin(), this->_atoms.end(),
                                             [](const lld::AtomLayout * A,
                                                const lld::AtomLayout * B) {
    const DefinedAtom *definedAtomA = cast<DefinedAtom>(A->_atom);
    const DefinedAtom *definedAtomB = cast<DefinedAtom>(B->_atom);
    int64_t alignmentA = definedAtomA->alignment().powerOf2.get();
    int64_t alignmentB = definedAtomB->alignment().powerOf2.get();
    if (alignmentA == alignmentB) {
      if (definedAtomA->merge() == DefinedAtom::mergeAsTentative)
        return false;
      if (definedAtomB->merge() == DefinedAtom::mergeAsTentative)
        return true;
    }
    return alignmentA < alignmentB;
  });

  // Set the fileOffset, and the appropriate size of the section
  for (auto &ai : this->_atoms) {
    const DefinedAtom *definedAtom = cast<DefinedAtom>(ai->_atom);
    DefinedAtom::Alignment atomAlign = definedAtom->alignment();
    uint64_t fOffset = this->alignOffset(this->fileSize(), atomAlign);
    uint64_t mOffset = this->alignOffset(this->memSize(), atomAlign);
    ai->_fileOffset = fOffset;
    this->_fsize = fOffset + definedAtom->size();
    this->_msize = mOffset + definedAtom->size();
  }
} // finalize

} // elf
} // lld

#endif // LLD_READER_WRITER_ELF_HEXAGON_HEXAGON_SECTION_CHUNKS_H
OpenPOWER on IntegriCloud