122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===- Attribute.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_MC_ATTRIBUTE_H_ 1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_MC_ATTRIBUTE_H_ 115460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld { 1322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao 1422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass AttributeSet; 155460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class AttributeBase 175460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief AttributeBase provides the real storage for attributes of options. 185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 195460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Attributes are options affecting the link editing of input files. 205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Some options affects the input files mentioned on the command line after 215460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * them. For example, --whole-archive option affects archives mentioned on 225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * the command line after the --whole-archve option. We call such options 235460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * "attributes of input files" 245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 255460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * AttributeBase is the storage for attributes of input files. Each input 265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * file (@see mcld::Input in MCLinker) has a pointer of an attribute. Since 275460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * most attributes of input files are identical, our design lets input files 285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * which have identical attributes share common attribute. AttributeBase is 295460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * the shared storage for attribute. 305460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 3137b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass AttributeBase { 3237b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao AttributeBase() 3437b74a387bb3993387029859c2d9d051c41c724eStephen Hines : m_WholeArchive(false), 3537b74a387bb3993387029859c2d9d051c41c724eStephen Hines m_AsNeeded(false), 3637b74a387bb3993387029859c2d9d051c41c724eStephen Hines m_AddNeeded(true), 3737b74a387bb3993387029859c2d9d051c41c724eStephen Hines m_Static(false) {} 385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao AttributeBase(const AttributeBase& pBase) 4037b74a387bb3993387029859c2d9d051c41c724eStephen Hines : m_WholeArchive(pBase.m_WholeArchive), 4137b74a387bb3993387029859c2d9d051c41c724eStephen Hines m_AsNeeded(pBase.m_AsNeeded), 4237b74a387bb3993387029859c2d9d051c41c724eStephen Hines m_AddNeeded(pBase.m_AddNeeded), 4337b74a387bb3993387029859c2d9d051c41c724eStephen Hines m_Static(pBase.m_Static) {} 445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines virtual ~AttributeBase() {} 465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 475460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- observers ----- // 485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // represent GNU ld --whole-archive/--no-whole-archive options 4937b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isWholeArchive() const { return m_WholeArchive; } 505460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 515460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // represent GNU ld --as-needed/--no-as-needed options 5237b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isAsNeeded() const { return m_AsNeeded; } 535460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // represent GNU ld --add-needed/--no-add-needed options 5537b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isAddNeeded() const { return m_AddNeeded; } 565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // represent GNU ld -static option 5837b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isStatic() const { return m_Static; } 595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // represent GNU ld -call_shared option 6137b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isDynamic() const { return !m_Static; } 6222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao 6337b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool m_WholeArchive : 1; 655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool m_AsNeeded : 1; 665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool m_AddNeeded : 1; 675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool m_Static : 1; 685460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class Attribute 715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief The base class of attributes. Providing the raw operations of an 725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * attributes 735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * For conventience and producing less bugs, we move the stoarges of attributes 755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * onto AttributeBase, and modifiers remains with the class Attribute. 765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 7737b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass Attribute : public AttributeBase { 7837b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- modifiers ----- // 8037b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setWholeArchive() { m_WholeArchive = true; } 815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8237b74a387bb3993387029859c2d9d051c41c724eStephen Hines void unsetWholeArchive() { m_WholeArchive = false; } 835460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8437b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setAsNeeded() { m_AsNeeded = true; } 855460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8637b74a387bb3993387029859c2d9d051c41c724eStephen Hines void unsetAsNeeded() { m_AsNeeded = false; } 875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 8837b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setAddNeeded() { m_AddNeeded = true; } 895460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 9037b74a387bb3993387029859c2d9d051c41c724eStephen Hines void unsetAddNeeded() { m_AddNeeded = false; } 915460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 9237b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setStatic() { m_Static = true; } 935460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 9437b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setDynamic() { m_Static = false; } 955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 965460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class AttrConstraint 985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief AttrConstarint is the constraint of a system. 995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 1005460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Some systems can not enable certain attributes of a input file. 1015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * For example, systems which have no shared libraries can not enable 1025460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * --call_shared options. We call the ability of enabling attributes 1035460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * as the constraint of attributes of a system. 1045460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 1055460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * Systems enable attributes at the target implementation of SectLinker. 1065460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 1075460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * @see SectLinker 1085460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 10937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass AttrConstraint : public AttributeBase { 11037b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 11137b74a387bb3993387029859c2d9d051c41c724eStephen Hines void enableWholeArchive() { m_WholeArchive = true; } 1125460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11337b74a387bb3993387029859c2d9d051c41c724eStephen Hines void disableWholeArchive() { m_WholeArchive = false; } 1145460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11537b74a387bb3993387029859c2d9d051c41c724eStephen Hines void enableAsNeeded() { m_AsNeeded = true; } 1165460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11737b74a387bb3993387029859c2d9d051c41c724eStephen Hines void disableAsNeeded() { m_AsNeeded = false; } 1185460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 11937b74a387bb3993387029859c2d9d051c41c724eStephen Hines void enableAddNeeded() { m_AddNeeded = true; } 1205460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 12137b74a387bb3993387029859c2d9d051c41c724eStephen Hines void disableAddNeeded() { m_AddNeeded = false; } 1225460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 12337b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setSharedSystem() { m_Static = false; } 1245460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 12537b74a387bb3993387029859c2d9d051c41c724eStephen Hines void setStaticSystem() { m_Static = true; } 1265460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 12737b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isSharedSystem() const { return !m_Static; } 1285460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 12937b74a387bb3993387029859c2d9d051c41c724eStephen Hines bool isStaticSystem() const { return m_Static; } 130551ae4ebd3e9d137ea668fb83ae4a55b8cfba451Stephen Hines 131affc150dc44fab1911775a49636d0ce85333b634Zonr Chang bool isLegal(const Attribute& pAttr) const; 1325460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 1335460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1345460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao/** \class AttributeProxy 1355460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * \brief AttributeProxys is the illusion of private attribute of each 1365460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * input file. 1375460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 1385460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * We designers want to hide the details of sharing common attributes 1395460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * between input files. We want input files under the illusion that they 1405460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * have their own private attributes to simplify the linking algorithms. 1415460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * 1425460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * AttributeProxy hides the reality of sharing. An input file can change 1435460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * its attribute without explicit searching of existing attributes 1445460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * as it has a private ownership of the attribute. AttributeProxy does 14522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * the searching in the AttributeSet and changes the pointer of 1465460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao * the attribute of the input file. If the searching fails, AttributeProxy 14722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao * requests a new attribute from the AttributeSet. 1485460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao */ 14937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass AttributeProxy { 15037b74a387bb3993387029859c2d9d051c41c724eStephen Hines public: 15122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao AttributeProxy(AttributeSet& pParent, 15222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao const Attribute& pBase, 15322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao const AttrConstraint& pConstraint); 1545460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1555460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao ~AttributeProxy(); 1565460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1575460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- observers ----- // 1585460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool isWholeArchive() const; 1595460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1605460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool isAsNeeded() const; 1615460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1625460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool isAddNeeded() const; 1635460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1645460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool isStatic() const; 1655460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1665460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao bool isDynamic() const; 1675460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 16837b74a387bb3993387029859c2d9d051c41c724eStephen Hines const Attribute* attr() const { return m_pBase; } 1695460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1705460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao // ----- modifiers ----- // 1715460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void setWholeArchive(); 1725460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void unsetWholeArchive(); 1735460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void setAsNeeded(); 1745460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void unsetAsNeeded(); 1755460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void setAddNeeded(); 1765460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void unsetAddNeeded(); 1775460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void setStatic(); 1785460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao void setDynamic(); 1795460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 18022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao AttributeProxy& assign(Attribute* pBase); 1815460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 18237b74a387bb3993387029859c2d9d051c41c724eStephen Hines private: 18337b74a387bb3993387029859c2d9d051c41c724eStephen Hines AttributeSet& m_AttrPool; 18437b74a387bb3993387029859c2d9d051c41c724eStephen Hines const Attribute* m_pBase; 18522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao const AttrConstraint& m_Constraint; 1865460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao}; 1875460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 1885460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao// ----- comparisons ----- // 18937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline bool operator==(const Attribute& pLHS, const Attribute& pRHS) { 1905460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return ((pLHS.isWholeArchive() == pRHS.isWholeArchive()) && 19137b74a387bb3993387029859c2d9d051c41c724eStephen Hines (pLHS.isAsNeeded() == pRHS.isAsNeeded()) && 19237b74a387bb3993387029859c2d9d051c41c724eStephen Hines (pLHS.isAddNeeded() == pRHS.isAddNeeded()) && 19337b74a387bb3993387029859c2d9d051c41c724eStephen Hines (pLHS.isStatic() == pRHS.isStatic())); 1945460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} 1955460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 19637b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline bool operator!=(const Attribute& pLHS, const Attribute& pRHS) { 1975460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao return !(pLHS == pRHS); 1985460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao} 1995460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 20037b74a387bb3993387029859c2d9d051c41c724eStephen Hines} // namespace mcld 2015460a1f25d9ddecb5c70667267d66d51af177a99Shih-wei Liao 20237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif // MCLD_MC_ATTRIBUTE_H_ 203