15460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao//===- StaticResolver.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_LD_STATICRESOLVER_H_
1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_LD_STATICRESOLVER_H_
1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/ResolveInfo.h"
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/LD/Resolver.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines
145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao#include <string>
155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1637b74a387bb3993387029859c2d9d051c41c724eStephen Hinesnamespace mcld {
175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
18affc150dc44fab1911775a49636d0ce85333b634Zonr Changclass NamePool;
195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class StaticResolver
215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */
2237b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass StaticResolver : public Resolver {
2337b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /** \enum LinkAction
255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  LinkAction follows BFD:linker.c (binary file descriptor).
265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  List all actions to take in the state table
275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   */
2837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  enum LinkAction {
2937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    FAIL,    // abort.
3037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    NOACT,   // no action.
3137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    UND,     // override by symbol undefined symbol.
3237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    WEAK,    // override by symbol weak undefined.
3337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DEF,     // override by symbol defined.
3437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DEFW,    // override by symbol weak defined.
3537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DEFD,    // override by symbol dynamic defined.
3637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DEFWD,   // override by symbol dynamic weak defined.
3737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    MDEFD,   // mark symbol dynamic defined.
3837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    MDEFWD,  // mark symbol dynamic weak defined.
3937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DUND,    // override dynamic defined symbol by undefined one.
4037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DUNDW,   // oevrride dynamic defined symbol by weak undefined one.
4137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    COM,     // override by symbol common.
4237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    CREF,    // Possibly warn about common reference to defined symbol.
4337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    CDEF,    // redefine existing common symbol.
4437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    BIG,     // override by symbol common using largest size.
4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    MBIG,    // mark common symbol by larger size.
4637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    IND,     // override by indirect symbol.
4737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    CIND,    // mark indirect symbol from existing common symbol.
4837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    MDEF,    // multiple definition error.
4937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    MIND,    // multiple indirect symbols.
5037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    REFC     // Mark indirect symbol referenced and then CYCLE.
515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  };
525460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
5337b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  // These are the values generated by the bit codes.
555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /** Encoding:
565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  D -> define
575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  U -> undefine
585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  d -> dynamic
595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  w -> weak
605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  C -> common
615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   *  I -> indirect
625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao   */
6337b74a387bb3993387029859c2d9d051c41c724eStephen Hines  enum {
6437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    U    = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::undefine_flag,  // NOLINT
6537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    w_U  = ResolveInfo::weak_flag   | ResolveInfo::regular_flag | ResolveInfo::undefine_flag,  // NOLINT
6637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    d_U  = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::undefine_flag,  // NOLINT
6737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    wd_U = ResolveInfo::weak_flag   | ResolveInfo::dynamic_flag | ResolveInfo::undefine_flag,  // NOLINT
6837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    D    = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::define_flag,    // NOLINT
6937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    w_D  = ResolveInfo::weak_flag   | ResolveInfo::regular_flag | ResolveInfo::define_flag,    // NOLINT
7037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    d_D  = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::define_flag,    // NOLINT
7137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    wd_D = ResolveInfo::weak_flag   | ResolveInfo::dynamic_flag | ResolveInfo::define_flag,    // NOLINT
7237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    C    = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::common_flag,    // NOLINT
7337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    w_C  = ResolveInfo::weak_flag   | ResolveInfo::regular_flag | ResolveInfo::common_flag,    // NOLINT
7437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    d_C  = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::common_flag,    // NOLINT
7537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    wd_C = ResolveInfo::weak_flag   | ResolveInfo::dynamic_flag | ResolveInfo::common_flag,    // NOLINT
7637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    I    = ResolveInfo::global_flag | ResolveInfo::regular_flag | ResolveInfo::indirect_flag,  // NOLINT
7737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    w_I  = ResolveInfo::weak_flag   | ResolveInfo::regular_flag | ResolveInfo::indirect_flag,  // NOLINT
7837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    d_I  = ResolveInfo::global_flag | ResolveInfo::dynamic_flag | ResolveInfo::indirect_flag,  // NOLINT
7937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    wd_I = ResolveInfo::weak_flag   | ResolveInfo::dynamic_flag | ResolveInfo::indirect_flag   // NOLINT
805460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  };
815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
8237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  enum ORDINATE {
835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    U_ORD,
845460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    w_U_ORD,
855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    d_U_ORD,
865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    wd_U_ORD,
875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    D_ORD,
885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    w_D_ORD,
895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    d_D_ORD,
905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    wd_D_ORD,
915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    C_ORD,
925460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    w_C_ORD,
935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Cs_ORD,
945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    Is_ORD,
955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    LAST_ORD
965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  };
975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
9837b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  virtual ~StaticResolver();
1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// shouldOverride - Can resolver override the symbol pOld by the symbol pNew?
102affc150dc44fab1911775a49636d0ce85333b634Zonr Chang  /// @return successfully resolved, return true; otherwise, return false.
1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param pOld the symbol which may be overridden.
1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  /// @param pNew the symbol which is used to replace pOld
10537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  virtual bool resolve(ResolveInfo& __restrict__ pOld,
10637b74a387bb3993387029859c2d9d051c41c724eStephen Hines                       const ResolveInfo& __restrict__ pNew,
10737b74a387bb3993387029859c2d9d051c41c724eStephen Hines                       bool& pOverride,
10837b74a387bb3993387029859c2d9d051c41c724eStephen Hines                       LDSymbol::ValueType pValue) const;
1095460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
11037b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
1115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  inline unsigned int getOrdinate(const ResolveInfo& pInfo) const {
1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pInfo.isAbsolute() && pInfo.isDyn())
1135460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return d_D_ORD;
1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pInfo.isAbsolute())
1155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return D_ORD;
1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pInfo.isCommon() && pInfo.isDyn())
1175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return Cs_ORD;
1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pInfo.isCommon() && pInfo.isDefine())
1195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return C_ORD;
1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pInfo.isCommon() && pInfo.isWeak())
1215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return w_C_ORD;
1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    if (pInfo.isIndirect())
1235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao      return Is_ORD;
1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao    return pInfo.info();
1255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao  }
1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao};
1275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao
12837b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
129affc150dc44fab1911775a49636d0ce85333b634Zonr Chang
13037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_LD_STATICRESOLVER_H_
131