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//===----------------------------------------------------------------------===//
937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_FRAGMENT_RELOCATION_H_
1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_FRAGMENT_RELOCATION_H_
1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Config/Config.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Fragment/FragmentRef.h"
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/GCFactoryListTraits.h"
15d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
16d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <llvm/ADT/ilist_node.h>
17d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao#include <llvm/Support/DataTypes.h>
185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass ResolveInfo;
22d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liaoclass Relocator;
2322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass LinkerConfig;
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
2537b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass Relocation : public llvm::ilist_node<Relocation> {
2637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  friend class RelocationFactory;
2737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  friend class GCFactoryListTraits<Relocation>;
2837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  friend class Chunk<Relocation, MCLD_RELOCATIONS_PER_INPUT>;
295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
3037b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
3137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef uint64_t Address;  // FIXME: use SizeTrait<T>::Address instead
3237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  typedef uint64_t DWord;    // FIXME: use SizeTrait<T>::Word instead
3337b74a387bb3993387029859c2d9d051c41c724eStephen 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
3737b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
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
4737b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
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
6137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  static Relocation* Create(Type pType,
6237b74a387bb3993387029859c2d9d051c41c724eStephen Hines                            FragmentRef& pFragRef,
63d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao                            Address pAddend = 0);
64d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
65d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  /// Destroy - destroy a relocation entry
66d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  static void Destroy(Relocation*& pRelocation);
67d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao
685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// type - relocation type
6937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  Type type() const { 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
7537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  Address addend() const { return m_Addend; }
765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// place - P value - address of the place being relocated
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Address place() const;
795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
806f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  /// size - the size of the relocation in bit
816f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines  Size size(Relocator& pRelocator) const;
826f75755c9204b1d8817ae5a65a2f7e5af0ec3f70Stephen Hines
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// symbol info - binding, type
8422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const ResolveInfo* symInfo() const { return m_pSymInfo; }
8537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  ResolveInfo* symInfo() { return m_pSymInfo; }
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// target - the target data to relocate
8822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const DWord& target() const { return m_TargetData; }
8937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  DWord& target() { return m_TargetData; }
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// targetRef - the reference of the target data
9222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const FragmentRef& targetRef() const { return m_TargetAddress; }
9337b74a387bb3993387029859c2d9d051c41c724eStephen Hines  FragmentRef& targetRef() { return m_TargetAddress; }
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
95d0fbbb227051be16931a1aa9b4a7722ac039c698Shih-wei Liao  void apply(Relocator& pRelocator);
9622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// updateAddend - A relocation with a section symbol must update addend
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  /// before reading its value.
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void updateAddend();
1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// ----- modifiers ----- ///
1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void setType(Type pType);
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void setAddend(Address pAddend);
1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  void setSymInfo(ResolveInfo* pSym);
1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
10837b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_Type - the type of the relocation entries
1105460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Type m_Type;
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_TargetData - target data of the place being relocated
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  DWord m_TargetData;
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_pSymInfo - resolved symbol info of relocation target symbol
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  ResolveInfo* m_pSymInfo;
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
118cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao  /// m_TargetAddress - FragmentRef of the place being relocated
119cedee4b38f4786845183be7f5916dd520a170ae0Shih-wei Liao  FragmentRef m_TargetAddress;
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// m_Addend - the addend
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  Address m_Addend;
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
12537b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
126affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
12737b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_FRAGMENT_RELOCATION_H_
128