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