122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===- SectionMap.h -------------------------------------------------------===//
222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//                     The MCLinker Project
422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// This file is distributed under the University of Illinois Open Source
622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// License. See LICENSE.TXT for details.
722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===----------------------------------------------------------------------===//
937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_OBJECT_SECTIONMAP_H_
1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_OBJECT_SECTIONMAP_H_
1137b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Script/Assignment.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Script/InputSectDesc.h"
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Script/OutputSectDesc.h"
1522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
1687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines#include <llvm/Support/DataTypes.h>
1737b74a387bb3993387029859c2d9d051c41c724eStephen Hines
1822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao#include <string>
1937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include <vector>
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
2222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2387f34658dec9097d987d254a990ea7f311bfc95fStephen Hinesclass Fragment;
2487f34658dec9097d987d254a990ea7f311bfc95fStephen Hinesclass LDSection;
2587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
2622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/** \class SectionMap
2787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines *  \brief descirbe how to map input sections into output sections
2822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao */
2937b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass SectionMap {
3037b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
3187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  class Input {
3237b74a387bb3993387029859c2d9d051c41c724eStephen Hines   public:
3387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef std::vector<std::pair<Fragment*, Assignment> > DotAssignments;
3487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef DotAssignments::const_iterator const_dot_iterator;
3587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef DotAssignments::iterator dot_iterator;
3687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
3787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    Input(const std::string& pName, InputSectDesc::KeepPolicy pPolicy);
3837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    explicit Input(const InputSectDesc& pInputDesc);
3987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
4087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    InputSectDesc::KeepPolicy policy() const { return m_Policy; }
4187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
4287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const InputSectDesc::Spec& spec() const { return m_Spec; }
4387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
4487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const LDSection* getSection() const { return m_pSection; }
4537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    LDSection* getSection() { return m_pSection; }
4687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
4787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const_dot_iterator dot_begin() const { return m_DotAssignments.begin(); }
4837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    dot_iterator dot_begin() { return m_DotAssignments.begin(); }
4937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const_dot_iterator dot_end() const { return m_DotAssignments.end(); }
5037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    dot_iterator dot_end() { return m_DotAssignments.end(); }
5187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
5287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const DotAssignments& dotAssignments() const { return m_DotAssignments; }
5337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DotAssignments& dotAssignments() { return m_DotAssignments; }
5422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5537b74a387bb3993387029859c2d9d051c41c724eStephen Hines   private:
5687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    InputSectDesc::KeepPolicy m_Policy;
5787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    InputSectDesc::Spec m_Spec;
5887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    LDSection* m_pSection;
5987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    DotAssignments m_DotAssignments;
6087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  };
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  class Output {
6337b74a387bb3993387029859c2d9d051c41c724eStephen Hines   public:
6487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef std::vector<Input*> InputList;
6587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef InputList::const_iterator const_iterator;
6687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef InputList::iterator iterator;
6787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef InputList::const_reference const_reference;
6887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef InputList::reference reference;
6987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
7087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef std::vector<Assignment> DotAssignments;
7187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef DotAssignments::const_iterator const_dot_iterator;
7287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    typedef DotAssignments::iterator dot_iterator;
7387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
7437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    explicit Output(const std::string& pName);
7537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    explicit Output(const OutputSectDesc& pOutputDesc);
7687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
7787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const std::string& name() const { return m_Name; }
7887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
7987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const OutputSectDesc::Prolog& prolog() const { return m_Prolog; }
8037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    OutputSectDesc::Prolog& prolog() { return m_Prolog; }
8187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
8287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const OutputSectDesc::Epilog& epilog() const { return m_Epilog; }
8337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    OutputSectDesc::Epilog& epilog() { return m_Epilog; }
8487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
8587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    size_t order() const { return m_Order; }
8687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
8787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    void setOrder(size_t pOrder) { m_Order = pOrder; }
8887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
8987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    bool hasContent() const;
9087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
9187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const LDSection* getSection() const { return m_pSection; }
9237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    LDSection* getSection() { return m_pSection; }
9387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
9487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    void setSection(LDSection* pSection) { m_pSection = pSection; }
9587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
9687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const_iterator begin() const { return m_InputList.begin(); }
9737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    iterator begin() { return m_InputList.begin(); }
9837b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const_iterator end() const { return m_InputList.end(); }
9937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    iterator end() { return m_InputList.end(); }
10087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
10187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const_reference front() const { return m_InputList.front(); }
10237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    reference front() { return m_InputList.front(); }
10337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const_reference back() const { return m_InputList.back(); }
10437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    reference back() { return m_InputList.back(); }
10587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
10687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    size_t size() const { return m_InputList.size(); }
10787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
10887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    bool empty() const { return m_InputList.empty(); }
10987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
11087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    bool isDiscard() const { return m_bIsDiscard; }
11187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
11287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    void append(Input* pInput) { m_InputList.push_back(pInput); }
11387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
11487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const_dot_iterator dot_begin() const { return m_DotAssignments.begin(); }
11537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    dot_iterator dot_begin() { return m_DotAssignments.begin(); }
11637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const_dot_iterator dot_end() const { return m_DotAssignments.end(); }
11737b74a387bb3993387029859c2d9d051c41c724eStephen Hines    dot_iterator dot_end() { return m_DotAssignments.end(); }
11887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
11987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const_dot_iterator find_first_explicit_dot() const;
12037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    dot_iterator find_first_explicit_dot();
12187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
12287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const_dot_iterator find_last_explicit_dot() const;
12337b74a387bb3993387029859c2d9d051c41c724eStephen Hines    dot_iterator find_last_explicit_dot();
12487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
12587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    const DotAssignments& dotAssignments() const { return m_DotAssignments; }
12637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    DotAssignments& dotAssignments() { return m_DotAssignments; }
12787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
12837b74a387bb3993387029859c2d9d051c41c724eStephen Hines   private:
12987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    std::string m_Name;
13087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    OutputSectDesc::Prolog m_Prolog;
13187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    OutputSectDesc::Epilog m_Epilog;
13287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    LDSection* m_pSection;
13387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    size_t m_Order;
13487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    bool m_bIsDiscard;
13587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    InputList m_InputList;
13687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines    DotAssignments m_DotAssignments;
13722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  };
13822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  struct SHOCompare {
14037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    bool operator()(const Output* LHS, const Output* RHS) const {
14137b74a387bb3993387029859c2d9d051c41c724eStephen Hines      return LHS->order() < RHS->order();
14237b74a387bb3993387029859c2d9d051c41c724eStephen Hines    }
14387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  };
14487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
14587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef std::pair<const Output*, const Input*> const_mapping;
14687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef std::pair<Output*, Input*> mapping;
14722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
14887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef std::vector<Output*> OutputDescList;
14987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef OutputDescList::const_iterator const_iterator;
15087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef OutputDescList::iterator iterator;
15187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef OutputDescList::const_reference const_reference;
15287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef OutputDescList::reference reference;
15387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
15487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef OutputDescList::const_reverse_iterator const_reverse_iterator;
15587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  typedef OutputDescList::reverse_iterator reverse_iterator;
15622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
15737b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
15887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  ~SectionMap();
15987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
16087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  const_mapping find(const std::string& pInputFile,
16187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines                     const std::string& pInputSection) const;
16237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  mapping find(const std::string& pInputFile, const std::string& pInputSection);
16322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
16487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  const_iterator find(const std::string& pOutputSection) const;
16537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  iterator find(const std::string& pOutputSection);
16622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
16737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  std::pair<mapping, bool> insert(
16837b74a387bb3993387029859c2d9d051c41c724eStephen Hines      const std::string& pInputSection,
16937b74a387bb3993387029859c2d9d051c41c724eStephen Hines      const std::string& pOutputSection,
17037b74a387bb3993387029859c2d9d051c41c724eStephen Hines      InputSectDesc::KeepPolicy pPolicy = InputSectDesc::NoKeep);
17137b74a387bb3993387029859c2d9d051c41c724eStephen Hines  std::pair<mapping, bool> insert(const InputSectDesc& pInputDesc,
17237b74a387bb3993387029859c2d9d051c41c724eStephen Hines                                  const OutputSectDesc& pOutputDesc);
17322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
17437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  bool empty() const { return m_OutputDescList.empty(); }
17537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  size_t size() const { return m_OutputDescList.size(); }
17622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
17787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  const_iterator begin() const { return m_OutputDescList.begin(); }
17837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  iterator begin() { return m_OutputDescList.begin(); }
17937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  const_iterator end() const { return m_OutputDescList.end(); }
18037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  iterator end() { return m_OutputDescList.end(); }
18122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
18287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  const_reference front() const { return m_OutputDescList.front(); }
18337b74a387bb3993387029859c2d9d051c41c724eStephen Hines  reference front() { return m_OutputDescList.front(); }
18437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  const_reference back() const { return m_OutputDescList.back(); }
18537b74a387bb3993387029859c2d9d051c41c724eStephen Hines  reference back() { return m_OutputDescList.back(); }
18687f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
18787f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  const_reverse_iterator rbegin() const { return m_OutputDescList.rbegin(); }
18837b74a387bb3993387029859c2d9d051c41c724eStephen Hines  reverse_iterator rbegin() { return m_OutputDescList.rbegin(); }
18937b74a387bb3993387029859c2d9d051c41c724eStephen Hines  const_reverse_iterator rend() const { return m_OutputDescList.rend(); }
19037b74a387bb3993387029859c2d9d051c41c724eStephen Hines  reverse_iterator rend() { return m_OutputDescList.rend(); }
19187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
19287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  iterator insert(iterator pPosition, LDSection* pSection);
19387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
19487f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  // fixupDotSymbols - ensure the dot assignments are valid
19587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  void fixupDotSymbols();
19622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
19737b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
19887f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  bool matched(const Input& pInput,
19987f34658dec9097d987d254a990ea7f311bfc95fStephen Hines               const std::string& pInputFile,
20087f34658dec9097d987d254a990ea7f311bfc95fStephen Hines               const std::string& pInputSection) const;
20187f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
20287f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  bool matched(const WildcardPattern& pPattern, const std::string& pName) const;
20387f34658dec9097d987d254a990ea7f311bfc95fStephen Hines
20437b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
20587f34658dec9097d987d254a990ea7f311bfc95fStephen Hines  OutputDescList m_OutputDescList;
20622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao};
20722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
20837b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
20922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
21037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_OBJECT_SECTIONMAP_H_
211