1//===- FragmentRef.h ------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_FRAGMENT_FRAGMENTREF_H
10#define MCLD_FRAGMENT_FRAGMENTREF_H
11
12#include <mcld/Config/Config.h>
13#include <mcld/ADT/SizeTraits.h>
14#include <mcld/ADT/TypeTraits.h>
15#include <mcld/Support/Allocators.h>
16
17namespace mcld {
18
19class Fragment;
20class LDSection;
21class Layout;
22
23/** \class FragmentRef
24 *  \brief FragmentRef is a reference of a Fragment's contetnt.
25 *
26 */
27class FragmentRef
28{
29public:
30  typedef uint64_t Offset; // FIXME: use SizeTraits<T>::Offset
31  typedef NonConstTraits<unsigned char>::pointer Address;
32  typedef ConstTraits<unsigned char>::pointer ConstAddress;
33
34public:
35  /// Create - create a fragment reference for a given fragment.
36  ///
37  /// @param pFrag - the given fragment
38  /// @param pOffset - the offset, can be larger than the fragment, but can not
39  ///                  be larger than the section size.
40  /// @return if the offset is legal, return the fragment reference. Otherwise,
41  /// return NULL.
42  static FragmentRef* Create(Fragment& pFrag, uint64_t pOffset);
43
44  static FragmentRef* Create(LDSection& pSection, uint64_t pOffset);
45
46  /// Clear - clear all generated FragmentRef in the system.
47  static void Clear();
48
49  static FragmentRef* Null();
50
51  // -----  modifiers  ----- //
52  FragmentRef& assign(const FragmentRef& pCopy);
53
54  FragmentRef& assign(Fragment& pFrag, Offset pOffset = 0);
55
56  /// memcpy - copy memory
57  /// copy memory from the fragment to the pDesc.
58  /// @pDest - the destination address
59  /// @pNBytes - copies pNBytes from the fragment[offset()+pOffset]
60  /// @pOffset - additional offset.
61  ///            the start address offset from fragment[offset()]
62  void memcpy(void* pDest, size_t pNBytes, Offset pOffset = 0) const;
63
64  // -----  observers  ----- //
65  bool isNull() const { return (this == Null()); }
66
67  Fragment* frag()
68  { return m_pFragment; }
69
70  const Fragment* frag() const
71  { return m_pFragment; }
72
73  Offset offset() const
74  { return m_Offset; }
75
76  Offset getOutputOffset() const;
77
78private:
79  friend FragmentRef& NullFragmentRef();
80  friend class Chunk<FragmentRef, MCLD_SECTIONS_PER_INPUT>;
81  friend class Relocation;
82
83  FragmentRef();
84
85  FragmentRef(Fragment& pFrag, Offset pOffset = 0);
86
87private:
88  Fragment* m_pFragment;
89  Offset m_Offset;
90
91  static FragmentRef g_NullFragmentRef;
92
93};
94
95} // namespace of mcld
96
97#endif
98
99