summaryrefslogtreecommitdiff
path: root/lib/LD/GroupReader.cpp
blob: b3e6f9ef584066e0d1d433a25e2b83082e2fc384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//===- GroupReader.cpp ----------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <mcld/LD/Archive.h>
#include <mcld/LD/ArchiveReader.h>
#include <mcld/LD/DynObjReader.h>
#include <mcld/LD/GroupReader.h>
#include <mcld/LD/ObjectReader.h>
#include <mcld/LD/BinaryReader.h>
#include <mcld/LinkerConfig.h>
#include <mcld/MC/Attribute.h>
#include <mcld/Support/MsgHandling.h>

using namespace mcld;

GroupReader::GroupReader(Module& pModule,
                         ObjectReader& pObjectReader,
                         DynObjReader& pDynObjReader,
                         ArchiveReader& pArchiveReader,
                         BinaryReader& pBinaryReader)
  : m_Module(pModule),
    m_ObjectReader(pObjectReader),
    m_DynObjReader(pDynObjReader),
    m_ArchiveReader(pArchiveReader),
    m_BinaryReader(pBinaryReader)
{
}

GroupReader::~GroupReader()
{
}

bool GroupReader::readGroup(Module::input_iterator pRoot,
                            Module::input_iterator pEnd,
                            InputBuilder& pBuilder,
                            const LinkerConfig& pConfig)
{
  // record the number of total objects included in this sub-tree
  size_t cur_obj_cnt = 0;
  size_t last_obj_cnt = 0;
  size_t non_ar_obj_cnt = 0;

  // record the archive files in this sub-tree
  typedef std::vector<ArchiveListEntry*> ArchiveListType;
  ArchiveListType ar_list;

  Module::input_iterator input = --pRoot;

  // first time read the sub-tree
  while (input != pEnd) {
    // already got type - for example, bitcode or external OIR (object
    // intermediate representation)
    if ((*input)->type() == Input::Script ||
        (*input)->type() == Input::Archive ||
        (*input)->type() == Input::External) {
      ++input;
      continue;
    }

    if (Input::Object == (*input)->type()) {
      m_Module.getObjectList().push_back(*input);
      continue;
    }

    if (Input::DynObj == (*input)->type()) {
      m_Module.getLibraryList().push_back(*input);
      continue;
    }

    bool doContinue = false;
    // is an archive
    if (m_ArchiveReader.isMyFormat(**input, doContinue)) {
      (*input)->setType(Input::Archive);
      // record the Archive used by each archive node
      Archive* ar = new Archive(**input, pBuilder);
      ArchiveListEntry* entry = new ArchiveListEntry(*ar, input);
      ar_list.push_back(entry);
      // read archive
      m_ArchiveReader.readArchive(pConfig, *ar);
      cur_obj_cnt += ar->numOfObjectMember();
    }
    // read input as a binary file
    else if (doContinue && m_BinaryReader.isMyFormat(**input, doContinue)) {
      (*input)->setType(Input::Object);
      m_BinaryReader.readBinary(**input);
      m_Module.getObjectList().push_back(*input);
    }
    // is a relocatable object file
    else if (doContinue && m_ObjectReader.isMyFormat(**input, doContinue)) {
      (*input)->setType(Input::Object);
      m_ObjectReader.readHeader(**input);
      m_ObjectReader.readSections(**input);
      m_ObjectReader.readSymbols(**input);
      m_Module.getObjectList().push_back(*input);
      ++cur_obj_cnt;
      ++non_ar_obj_cnt;
    }
    // is a shared object file
    else if (doContinue && m_DynObjReader.isMyFormat(**input, doContinue)) {
      (*input)->setType(Input::DynObj);
      m_DynObjReader.readHeader(**input);
      m_DynObjReader.readSymbols(**input);
      m_Module.getLibraryList().push_back(*input);
    }
    else {
      warning(diag::warn_unrecognized_input_file) << (*input)->path()
        << pConfig.targets().triple().str();
    }
    ++input;
  }

  // after read in all the archives, traverse the archive list in a loop until
  // there is no unresolved symbols added
  ArchiveListType::iterator it = ar_list.begin();
  ArchiveListType::iterator end = ar_list.end();
  while (cur_obj_cnt != last_obj_cnt) {
    last_obj_cnt = cur_obj_cnt;
    cur_obj_cnt = non_ar_obj_cnt;
    for (it = ar_list.begin(); it != end; ++it) {
      Archive& ar = (*it)->archive;
      // if --whole-archive is given to this archive, no need to read it again
      if ( ar.getARFile().attribute()->isWholeArchive())
        continue;
      m_ArchiveReader.readArchive(pConfig, ar);
      cur_obj_cnt += ar.numOfObjectMember();
    }
  }

  // after all needed member included, merge the archive sub-tree to main
  // InputTree
  for (it = ar_list.begin(); it != end; ++it) {
    Archive& ar = (*it)->archive;
    if (ar.numOfObjectMember() > 0) {
      m_Module.getInputTree().merge<InputTree::Inclusive>((*it)->input,
                                                          ar.inputs());
    }
  }

  // cleanup ar_list
  for (it = ar_list.begin(); it != end; ++it) {
    delete &((*it)->archive);
    delete (*it);
  }
  ar_list.clear();

  return true;
}