15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- Relocation.h ----------------------------------------------------===//
25460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
35460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//                     The MCLinker Project
45460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
55460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// This file is distributed under the University of Illinois Open Source
65460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// License. See LICENSE.TXT for details.
75460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//
85460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===----------------------------------------------------------------------===//
922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#ifndef MCLD_FRAGMENT_RELOCATION_H
1022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#define MCLD_FRAGMENT_RELOCATION_H
11d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <mcld/Config/Config.h>
1222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <mcld/Fragment/FragmentRef.h>
13d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <mcld/Support/GCFactoryListTraits.h>
14d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
15d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <llvm/ADT/ilist_node.h>
16d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <llvm/Support/DataTypes.h>
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass ResolveInfo;
21d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaoclass Relocator;
2222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass LinkerConfig;
235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
24d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaoclass Relocation : public llvm::ilist_node<Relocation>
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao{
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaofriend class RelocationFactory;
27d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaofriend class GCFactoryListTraits<Relocation>;
28d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaofriend class Chunk<Relocation, MCLD_RELOCATIONS_PER_INPUT>;
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaopublic:
315460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  typedef uint64_t Address; // FIXME: use SizeTrait<T>::Address instead
326f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  typedef uint64_t DWord;   // FIXME: use SizeTrait<T>::Word instead
336f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  typedef int64_t  SWord;   // FIXME: use SizeTrait<T>::SWord instead
3487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef uint32_t Type;
356f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  typedef uint32_t Size;
365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
38d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  Relocation();
39d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Relocation(Type pType,
41cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao             FragmentRef* pTargetRef,
425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao             Address pAddend,
435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao             DWord pTargetData);
445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
455460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ~Relocation();
465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
47d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaopublic:
48d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// Initialize - set up the relocation factory
49d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static void SetUp(const LinkerConfig& pConfig);
50d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
51d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// Clear - Clean up the relocation factory
52d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static void Clear();
53d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
54d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// Create - produce an empty relocation entry
55d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static Relocation* Create();
56d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
57d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// Create - produce a relocation entry
58d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param pType    [in] the type of the relocation entry
59d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param pFragRef [in] the place to apply the relocation
60d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// @param pAddend  [in] the addend of the relocation entry
61d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static Relocation* Create(Type pType, FragmentRef& pFragRef,
62d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                            Address pAddend = 0);
63d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
64d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// Destroy - destroy a relocation entry
65d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static void Destroy(Relocation*& pRelocation);
66d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// type - relocation type
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Type type() const
695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  { return m_Type; }
705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// symValue - S value - the symbol address
725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Address symValue() const;
735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// addend - A value
755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Address addend() const
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  { return m_Addend; }
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// place - P value - address of the place being relocated
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Address place() const;
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
816f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// size - the size of the relocation in bit
826f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Size size(Relocator& pRelocator) const;
836f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// symbol info - binding, type
8522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const ResolveInfo* symInfo() const { return m_pSymInfo; }
8622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ResolveInfo*       symInfo()       { return m_pSymInfo; }
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// target - the target data to relocate
8922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const DWord& target() const { return m_TargetData; }
9022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  DWord&       target()       { return m_TargetData; }
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// targetRef - the reference of the target data
9322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const FragmentRef& targetRef() const { return m_TargetAddress; }
9422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  FragmentRef&       targetRef()       { return m_TargetAddress; }
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
96d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  void apply(Relocator& pRelocator);
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// updateAddend - A relocation with a section symbol must update addend
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// before reading its value.
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void updateAddend();
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// ----- modifiers ----- ///
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void setType(Type pType);
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void setAddend(Address pAddend);
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void setSymInfo(ResolveInfo* pSym);
1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liaoprivate:
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_Type - the type of the relocation entries
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Type m_Type;
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_TargetData - target data of the place being relocated
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DWord m_TargetData;
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_pSymInfo - resolved symbol info of relocation target symbol
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ResolveInfo* m_pSymInfo;
1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
119cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao  /// m_TargetAddress - FragmentRef of the place being relocated
120cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao  FragmentRef m_TargetAddress;
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_Addend - the addend
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Address m_Addend;
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} // namespace of mcld
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#endif
129affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
130