summaryrefslogtreecommitdiff
path: root/lib/CodeGen/MCLinker.cpp
blob: 6b7196ba26f550dc53e6e276bf80b99180e91220 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//===- MCLinker.cpp -------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the MCLinker class.
//
//===----------------------------------------------------------------------===//
#include <mcld/CodeGen/MCLinker.h>

#include <mcld/Module.h>
#include <mcld/LinkerConfig.h>
#include <mcld/InputTree.h>
#include <mcld/Linker.h>
#include <mcld/IRBuilder.h>
#include <mcld/MC/InputBuilder.h>
#include <mcld/MC/FileAction.h>
#include <mcld/MC/CommandAction.h>
#include <mcld/Object/ObjectLinker.h>
#include <mcld/Support/CommandLine.h>
#include <mcld/Support/FileSystem.h>
#include <mcld/Support/MsgHandling.h>
#include <mcld/Support/FileHandle.h>
#include <mcld/Support/raw_ostream.h>
#include <mcld/Support/MemoryArea.h>

#include <llvm/IR/Module.h>
#include <llvm/Support/CommandLine.h>

#include <algorithm>
#include <vector>
#include <string>

using namespace mcld;
using namespace llvm;

char MCLinker::m_ID = 0;

//===----------------------------------------------------------------------===//
// Help Functions
//===----------------------------------------------------------------------===//
static inline bool CompareAction(const InputAction* X, const InputAction* Y)
{
  return (X->position() < Y->position());
}

//===----------------------------------------------------------------------===//
// Positional Options
// There are four kinds of positional options:
//   1. Inputs, object files, such as /tmp/XXXX.o
//   2. Namespecs, short names of libraries. A namespec may refer to an archive
//      or a shared library. For example, -lm.
//   3. Attributes of inputs. Attributes describe inputs appears after them.
//      For example, --as-needed and --whole-archive.
//   4. Groups. A Group is a set of archives. Linkers repeatedly read archives
//      in groups until there is no new undefined symbols.
//   5. Bitcode. Bitcode is a kind of object files. MCLinker compiles it to
//      object file first, then link it as a object file. (Bitcode is recorded
//      in BitcodeOption, not be read by LLVM Command Line library.)
//===----------------------------------------------------------------------===//
// Inputs
//===----------------------------------------------------------------------===//
static cl::list<mcld::sys::fs::Path>
ArgInputObjectFiles(cl::Positional,
                    cl::desc("[input object files]"),
                    cl::ZeroOrMore);

//===----------------------------------------------------------------------===//
// Namespecs
//===----------------------------------------------------------------------===//
static cl::list<std::string>
ArgNameSpecList("l",
            cl::ZeroOrMore,
            cl::desc("Add the archive or object file specified by namespec to "
                     "the list of files to link."),
            cl::value_desc("namespec"),
            cl::Prefix);

static cl::alias
ArgNameSpecListAlias("library",
                 cl::desc("alias for -l"),
                 cl::aliasopt(ArgNameSpecList));

//===----------------------------------------------------------------------===//
// Attributes
//===----------------------------------------------------------------------===//
static cl::list<bool>
ArgWholeArchiveList("whole-archive",
               cl::ValueDisallowed,
               cl::desc("For each archive mentioned on the command line after "
                        "the --whole-archive option, include all object files "
                        "in the archive."));

static cl::list<bool>
ArgNoWholeArchiveList("no-whole-archive",
               cl::ValueDisallowed,
               cl::desc("Turn off the effect of the --whole-archive option for "
                        "subsequent archive files."));

static cl::list<bool>
ArgAsNeededList("as-needed",
               cl::ValueDisallowed,
               cl::desc("This option affects ELF DT_NEEDED tags for dynamic "
                        "libraries mentioned on the command line after the "
                        "--as-needed option."));

static cl::list<bool>
ArgNoAsNeededList("no-as-needed",
               cl::ValueDisallowed,
               cl::desc("Turn off the effect of the --as-needed option for "
                        "subsequent dynamic libraries"));

static cl::list<bool>
ArgAddNeededList("add-needed",
                cl::ValueDisallowed,
                cl::desc("--add-needed causes DT_NEEDED tags are always "
                         "emitted for those libraries from DT_NEEDED tags. "
                         "This is the default behavior."));

static cl::list<bool>
ArgNoAddNeededList("no-add-needed",
                cl::ValueDisallowed,
                cl::desc("--no-add-needed causes DT_NEEDED tags will never be "
                         "emitted for those libraries from DT_NEEDED tags"));

static cl::list<bool>
ArgBDynamicList("Bdynamic",
                cl::ValueDisallowed,
                cl::desc("Link against dynamic library"));

static cl::alias
ArgBDynamicListAlias1("dy",
                cl::desc("alias for --Bdynamic"),
                cl::aliasopt(ArgBDynamicList));

static cl::alias
ArgBDynamicListAlias2("call_shared",
                cl::desc("alias for --Bdynamic"),
                cl::aliasopt(ArgBDynamicList));

static cl::list<bool>
ArgBStaticList("Bstatic",
                cl::ValueDisallowed,
                cl::desc("Link against static library"));

static cl::alias
ArgBStaticListAlias1("dn",
                cl::desc("alias for --Bstatic"),
                cl::aliasopt(ArgBStaticList));

static cl::alias
ArgBStaticListAlias2("static",
                cl::desc("alias for --Bstatic"),
                cl::aliasopt(ArgBStaticList));

static cl::alias
ArgBStaticListAlias3("non_shared",
                cl::desc("alias for --Bstatic"),
                cl::aliasopt(ArgBStaticList));

//===----------------------------------------------------------------------===//
// Groups
//===----------------------------------------------------------------------===//
static cl::list<bool>
ArgStartGroupList("start-group",
                  cl::ValueDisallowed,
                  cl::desc("start to record a group of archives"));

static cl::alias
ArgStartGroupListAlias("(",
                       cl::desc("alias for --start-group"),
                       cl::aliasopt(ArgStartGroupList));

static cl::list<bool>
ArgEndGroupList("end-group",
                cl::ValueDisallowed,
                cl::desc("stop recording a group of archives"));

static cl::alias
ArgEndGroupListAlias(")",
                     cl::desc("alias for --end-group"),
                     cl::aliasopt(ArgEndGroupList));

//===----------------------------------------------------------------------===//
// MCLinker
//===----------------------------------------------------------------------===//
MCLinker::MCLinker(LinkerConfig& pConfig,
                   mcld::Module& pModule,
                   MemoryArea& pOutput)
  : MachineFunctionPass(m_ID),
    m_Config(pConfig),
    m_Module(pModule),
    m_Output(pOutput),
    m_pBuilder(NULL),
    m_pLinker(NULL) {
}

MCLinker::~MCLinker()
{
  delete m_pLinker;
  delete m_pBuilder;
}

bool MCLinker::doInitialization(llvm::Module &pM)
{
  // Now, all input arguments are prepared well, send it into ObjectLinker
  m_pLinker = new Linker();

  if (!m_pLinker->config(m_Config))
    return false;

  m_pBuilder = new IRBuilder(m_Module, m_Config);

  initializeInputTree(*m_pBuilder);

  return true;
}

bool MCLinker::doFinalization(llvm::Module &pM)
{
  if (!m_pLinker->link(m_Module, *m_pBuilder))
    return true;

  if (!m_pLinker->emit(m_Output))
    return true;

  return false;
}

bool MCLinker::runOnMachineFunction(MachineFunction& pF)
{
  // basically, linkers do nothing during function is generated.
  return false;
}

void MCLinker::initializeInputTree(IRBuilder& pBuilder)
{
  if (0 == ArgInputObjectFiles.size() &&
      0 == ArgNameSpecList.size() &&
      !m_Config.bitcode().hasDefined()) {
    fatal(diag::err_no_inputs);
    return;
  }

  size_t num_actions = ArgInputObjectFiles.size() +
                       ArgNameSpecList.size() +
                       ArgWholeArchiveList.size() +
                       ArgNoWholeArchiveList.size() +
                       ArgAsNeededList.size() +
                       ArgNoAsNeededList.size() +
                       ArgAddNeededList.size() +
                       ArgNoAddNeededList.size() +
                       ArgBDynamicList.size() +
                       ArgBStaticList.size() +
                       ArgStartGroupList.size() +
                       ArgEndGroupList.size() +
                       1; // bitcode
  std::vector<InputAction*> actions;
  actions.reserve(num_actions);

  // -----  inputs  ----- //
  cl::list<mcld::sys::fs::Path>::iterator input, inBegin, inEnd;
  inBegin = ArgInputObjectFiles.begin();
  inEnd = ArgInputObjectFiles.end();
  for (input = inBegin; input != inEnd; ++input) {
    unsigned int pos = ArgInputObjectFiles.getPosition(input - inBegin);
    actions.push_back(new InputFileAction(pos, *input));
    actions.push_back(new ContextAction(pos));
    actions.push_back(new MemoryAreaAction(pos, FileHandle::ReadOnly));
  }

  // -----  namespecs  ----- //
  cl::list<std::string>::iterator namespec, nsBegin, nsEnd;
  nsBegin = ArgNameSpecList.begin();
  nsEnd = ArgNameSpecList.end();
  for (namespec = nsBegin; namespec != nsEnd; ++namespec) {
    unsigned int pos = ArgNameSpecList.getPosition(namespec - nsBegin);
    actions.push_back(new NamespecAction(pos, *namespec,
                                         m_Config.options().directories()));
    actions.push_back(new ContextAction(pos));
    actions.push_back(new MemoryAreaAction(pos, FileHandle::ReadOnly));
  }

  // -----  attributes  ----- //
  /// --whole-archive
  cl::list<bool>::iterator attr, attrBegin, attrEnd;
  attrBegin = ArgWholeArchiveList.begin();
  attrEnd   = ArgWholeArchiveList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgWholeArchiveList.getPosition(attr - attrBegin);
    actions.push_back(new WholeArchiveAction(pos));
  }

  /// --no-whole-archive
  attrBegin = ArgNoWholeArchiveList.begin();
  attrEnd   = ArgNoWholeArchiveList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgNoWholeArchiveList.getPosition(attr - attrBegin);
    actions.push_back(new NoWholeArchiveAction(pos));
  }

  /// --as-needed
  attrBegin = ArgAsNeededList.begin();
  attrEnd   = ArgAsNeededList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgAsNeededList.getPosition(attr - attrBegin);
    actions.push_back(new AsNeededAction(pos));
  }

  /// --no-as-needed
  attrBegin = ArgNoAsNeededList.begin();
  attrEnd   = ArgNoAsNeededList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgNoAsNeededList.getPosition(attr - attrBegin);
    actions.push_back(new NoAsNeededAction(pos));
  }

  /// --add--needed
  attrBegin = ArgAddNeededList.begin();
  attrEnd   = ArgAddNeededList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgAddNeededList.getPosition(attr - attrBegin);
    actions.push_back(new AddNeededAction(pos));
  }

  /// --no-add--needed
  attrBegin = ArgNoAddNeededList.begin();
  attrEnd   = ArgNoAddNeededList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgNoAddNeededList.getPosition(attr - attrBegin);
    actions.push_back(new NoAddNeededAction(pos));
  }

  /// --Bdynamic
  attrBegin = ArgBDynamicList.begin();
  attrEnd   = ArgBDynamicList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgBDynamicList.getPosition(attr - attrBegin);
    actions.push_back(new BDynamicAction(pos));
  }

  /// --Bstatic
  attrBegin = ArgBStaticList.begin();
  attrEnd   = ArgBStaticList.end();
  for (attr = attrBegin; attr != attrEnd; ++attr) {
    unsigned int pos = ArgBStaticList.getPosition(attr - attrBegin);
    actions.push_back(new BStaticAction(pos));
  }

  // -----  groups  ----- //
  /// --start-group
  cl::list<bool>::iterator group, gsBegin, gsEnd;
  gsBegin = ArgStartGroupList.begin();
  gsEnd   = ArgStartGroupList.end();
  for (group = gsBegin; group != gsEnd; ++group) {
    unsigned int pos = ArgStartGroupList.getPosition(group - gsBegin);
    actions.push_back(new StartGroupAction(pos));
  }

  /// --end-group
  gsBegin = ArgEndGroupList.begin();
  gsEnd   = ArgEndGroupList.end();
  for (group = gsBegin; group != gsEnd; ++group) {
    unsigned int pos = ArgEndGroupList.getPosition(group - gsBegin);
    actions.push_back(new EndGroupAction(pos));
  }

  // -----  bitcode  ----- //
  if (m_Config.bitcode().hasDefined()) {
    actions.push_back(new BitcodeAction(m_Config.bitcode().getPosition(),
                                        m_Config.bitcode().getPath()));
  }

  // stable sort
  std::stable_sort(actions.begin(), actions.end(), CompareAction);

  // build up input tree
  std::vector<InputAction*>::iterator action, actionEnd = actions.end();
  for (action = actions.begin(); action != actionEnd; ++action) {
    (*action)->activate(pBuilder.getInputBuilder());
  }

  if (pBuilder.getInputBuilder().isInGroup())
    report_fatal_error("no matched --start-group and --end-group");
}