summaryrefslogtreecommitdiff
path: root/lib/Object/SectionMap.cpp
blob: 9cba89559975a381918fd7efc044e4e24e06126d (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
//===- SectionMap.cpp -----------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "mcld/Object/SectionMap.h"

#include "mcld/Fragment/NullFragment.h"
#include "mcld/LD/LDSection.h"
#include "mcld/LD/SectionData.h"
#include "mcld/Script/Assignment.h"
#include "mcld/Script/Operand.h"
#include "mcld/Script/Operator.h"
#include "mcld/Script/RpnExpr.h"
#include "mcld/Script/StringList.h"
#include "mcld/Script/WildcardPattern.h"

#include <llvm/Support/Casting.h>

#include <cassert>
#include <cstring>
#include <climits>
#if !defined(MCLD_ON_WIN32)
#include <fnmatch.h>
#define fnmatch0(pattern, string) (fnmatch(pattern, string, 0) == 0)
#else
#include <windows.h>
#include <shlwapi.h>
#define fnmatch0(pattern, string) (PathMatchSpec(string, pattern) == true)
#endif

namespace mcld {

//===----------------------------------------------------------------------===//
// SectionMap::Input
//===----------------------------------------------------------------------===//
SectionMap::Input::Input(const std::string& pName,
                         InputSectDesc::KeepPolicy pPolicy)
    : m_Policy(pPolicy) {
  m_Spec.m_pWildcardFile =
      WildcardPattern::create("*", WildcardPattern::SORT_NONE);
  m_Spec.m_pExcludeFiles = NULL;

  StringList* sections = StringList::create();
  sections->push_back(
      WildcardPattern::create(pName, WildcardPattern::SORT_NONE));
  m_Spec.m_pWildcardSections = sections;

  m_pSection = LDSection::Create(pName, LDFileFormat::TEXT, 0, 0);
  SectionData* sd = SectionData::Create(*m_pSection);
  m_pSection->setSectionData(sd);
  new NullFragment(sd);
  new NullFragment(sd);
}

SectionMap::Input::Input(const InputSectDesc& pInputDesc)
    : m_Policy(pInputDesc.policy()) {
  m_Spec.m_pWildcardFile = pInputDesc.spec().m_pWildcardFile;
  m_Spec.m_pExcludeFiles = pInputDesc.spec().m_pExcludeFiles;
  m_Spec.m_pWildcardSections = pInputDesc.spec().m_pWildcardSections;
  m_pSection = LDSection::Create("", LDFileFormat::TEXT, 0, 0);
  SectionData* sd = SectionData::Create(*m_pSection);
  m_pSection->setSectionData(sd);
  new NullFragment(sd);
  new NullFragment(sd);
}

//===----------------------------------------------------------------------===//
// SectionMap::Output
//===----------------------------------------------------------------------===//
SectionMap::Output::Output(const std::string& pName)
    : m_Name(pName), m_Order(UINT_MAX) {
  m_Prolog.m_pVMA = NULL;
  m_Prolog.m_Type = OutputSectDesc::LOAD;
  m_Prolog.m_pLMA = NULL;
  m_Prolog.m_pAlign = NULL;
  m_Prolog.m_pSubAlign = NULL;
  m_Prolog.m_Constraint = OutputSectDesc::NO_CONSTRAINT;

  m_Epilog.m_pRegion = NULL;
  m_Epilog.m_pLMARegion = NULL;
  m_Epilog.m_pPhdrs = NULL;
  m_Epilog.m_pFillExp = NULL;

  m_pSection = LDSection::Create(pName, LDFileFormat::TEXT, 0, 0);
  SectionData* sd = SectionData::Create(*m_pSection);
  m_pSection->setSectionData(sd);

  m_bIsDiscard = pName.compare("/DISCARD/") == 0;
}

SectionMap::Output::Output(const OutputSectDesc& pOutputDesc)
    : m_Name(pOutputDesc.name()),
      m_Prolog(pOutputDesc.prolog()),
      m_Epilog(pOutputDesc.epilog()),
      m_Order(UINT_MAX) {
  m_pSection = LDSection::Create(m_Name, LDFileFormat::TEXT, 0, 0);
  SectionData* sd = SectionData::Create(*m_pSection);
  m_pSection->setSectionData(sd);

  m_bIsDiscard = m_Name.compare("/DISCARD/") == 0;
}

bool SectionMap::Output::hasContent() const {
  return m_pSection != NULL && m_pSection->size() != 0;
}

SectionMap::Output::const_dot_iterator
SectionMap::Output::find_first_explicit_dot() const {
  for (const_dot_iterator it = dot_begin(), ie = dot_end(); it != ie; ++it) {
    if ((*it).type() == Assignment::DEFAULT)
      return it;
  }
  return dot_end();
}

SectionMap::Output::dot_iterator SectionMap::Output::find_first_explicit_dot() {
  for (dot_iterator it = dot_begin(), ie = dot_end(); it != ie; ++it) {
    if ((*it).type() == Assignment::DEFAULT)
      return it;
  }
  return dot_end();
}

SectionMap::Output::const_dot_iterator
SectionMap::Output::find_last_explicit_dot() const {
  typedef DotAssignments::const_reverse_iterator CONST_RIT;
  for (CONST_RIT rit = dotAssignments().rbegin(), rie = dotAssignments().rend();
       rit != rie;
       ++rit) {
    if ((*rit).type() == Assignment::DEFAULT) {
      return dot_begin() +
             (dotAssignments().size() - (rit - dotAssignments().rbegin()) - 1);
    }
  }
  return dot_end();
}

SectionMap::Output::dot_iterator SectionMap::Output::find_last_explicit_dot() {
  typedef DotAssignments::reverse_iterator RIT;
  for (RIT rit = dotAssignments().rbegin(), rie = dotAssignments().rend();
       rit != rie;
       ++rit) {
    if ((*rit).type() == Assignment::DEFAULT) {
      return dot_begin() +
             (dotAssignments().size() - (rit - dotAssignments().rbegin()) - 1);
    }
  }
  return dot_end();
}

//===----------------------------------------------------------------------===//
// SectionMap
//===----------------------------------------------------------------------===//
SectionMap::~SectionMap() {
  iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    if (*out != NULL) {
      Output::iterator in, inBegin = (*out)->begin(), inEnd = (*out)->end();
      for (in = inBegin; in != inEnd; ++in) {
        if (*in != NULL)
          delete *in;
      }
      delete *out;
    }
  }
}

SectionMap::const_mapping SectionMap::find(
    const std::string& pInputFile,
    const std::string& pInputSection) const {
  const_iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    Output::const_iterator in, inBegin = (*out)->begin(), inEnd = (*out)->end();
    for (in = inBegin; in != inEnd; ++in) {
      if (matched(**in, pInputFile, pInputSection))
        return std::make_pair(*out, *in);
    }
  }
  return std::make_pair((const Output*)NULL, (const Input*)NULL);
}

SectionMap::mapping SectionMap::find(const std::string& pInputFile,
                                     const std::string& pInputSection) {
  iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    Output::iterator in, inBegin = (*out)->begin(), inEnd = (*out)->end();
    for (in = inBegin; in != inEnd; ++in) {
      if (matched(**in, pInputFile, pInputSection))
        return std::make_pair(*out, *in);
    }
  }
  return std::make_pair(nullptr, nullptr);
}

SectionMap::const_iterator SectionMap::find(
    const std::string& pOutputSection) const {
  const_iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    if ((*out)->name().compare(pOutputSection) == 0)
      return out;
  }
  return outEnd;
}

SectionMap::iterator SectionMap::find(const std::string& pOutputSection) {
  iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    if ((*out)->name().compare(pOutputSection) == 0)
      return out;
  }
  return outEnd;
}

std::pair<SectionMap::mapping, bool> SectionMap::insert(
    const std::string& pInputSection,
    const std::string& pOutputSection,
    InputSectDesc::KeepPolicy pPolicy) {
  iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    if ((*out)->name().compare(pOutputSection) == 0)
      break;
  }
  if (out != end()) {
    Output::iterator in, inBegin = (*out)->begin(), inEnd = (*out)->end();
    for (in = inBegin; in != inEnd; ++in) {
      if ((*in)->getSection()->name().compare(pInputSection) == 0)
        break;
    }

    if (in != (*out)->end()) {
      return std::make_pair(std::make_pair(*out, *in), false);
    } else {
      Input* input = new Input(pInputSection, pPolicy);
      (*out)->append(input);
      return std::make_pair(std::make_pair(*out, input), true);
    }
  }

  Output* output = new Output(pOutputSection);
  m_OutputDescList.push_back(output);
  Input* input = new Input(pInputSection, pPolicy);
  output->append(input);

  return std::make_pair(std::make_pair(output, input), true);
}

std::pair<SectionMap::mapping, bool> SectionMap::insert(
    const InputSectDesc& pInputDesc,
    const OutputSectDesc& pOutputDesc) {
  iterator out, outBegin = begin(), outEnd = end();
  for (out = outBegin; out != outEnd; ++out) {
    if ((*out)->name().compare(pOutputDesc.name()) == 0 &&
        (*out)->prolog() == pOutputDesc.prolog() &&
        (*out)->epilog() == pOutputDesc.epilog())
      break;
  }

  if (out != end()) {
    Output::iterator in, inBegin = (*out)->begin(), inEnd = (*out)->end();
    for (in = inBegin; in != inEnd; ++in) {
      if ((*in)->policy() == pInputDesc.policy() &&
          (*in)->spec() == pInputDesc.spec())
        break;
    }

    if (in != (*out)->end()) {
      return std::make_pair(std::make_pair(*out, *in), false);
    } else {
      Input* input = new Input(pInputDesc);
      (*out)->append(input);
      return std::make_pair(std::make_pair(*out, input), true);
    }
  }

  Output* output = new Output(pOutputDesc);
  m_OutputDescList.push_back(output);
  Input* input = new Input(pInputDesc);
  output->append(input);

  return std::make_pair(std::make_pair(output, input), true);
}

SectionMap::iterator SectionMap::insert(iterator pPosition,
                                        LDSection* pSection) {
  Output* output = new Output(pSection->name());
  output->append(new Input(pSection->name(), InputSectDesc::NoKeep));
  output->setSection(pSection);
  return m_OutputDescList.insert(pPosition, output);
}

bool SectionMap::matched(const SectionMap::Input& pInput,
                         const std::string& pInputFile,
                         const std::string& pInputSection) const {
  if (pInput.spec().hasFile() && !matched(pInput.spec().file(), pInputFile))
    return false;

  if (pInput.spec().hasExcludeFiles()) {
    StringList::const_iterator file, fileEnd;
    fileEnd = pInput.spec().excludeFiles().end();
    for (file = pInput.spec().excludeFiles().begin(); file != fileEnd; ++file) {
      if (matched(llvm::cast<WildcardPattern>(**file), pInputFile)) {
        return false;
      }
    }
  }

  if (pInput.spec().hasSections()) {
    StringList::const_iterator sect, sectEnd = pInput.spec().sections().end();
    for (sect = pInput.spec().sections().begin(); sect != sectEnd; ++sect) {
      if (matched(llvm::cast<WildcardPattern>(**sect), pInputSection)) {
        return true;
      }
    }
  }

  return false;
}

bool SectionMap::matched(const WildcardPattern& pPattern,
                         const std::string& pName) const {
  if (pPattern.isPrefix()) {
    llvm::StringRef name(pName);
    return name.startswith(pPattern.prefix());
  } else {
    return fnmatch0(pPattern.name().c_str(), pName.c_str());
  }
}

// fixupDotSymbols - ensure the dot symbols are valid
void SectionMap::fixupDotSymbols() {
  for (iterator it = begin() + 1, ie = end(); it != ie; ++it) {
    // fixup the 1st explicit dot assignment if needed
    if (!(*it)->dotAssignments().empty()) {
      Output::dot_iterator dot = (*it)->find_first_explicit_dot();
      if (dot != (*it)->dot_end() && (*dot).symbol().isDot() &&
          (*dot).getRpnExpr().hasDot()) {
        Assignment assign(Assignment::OUTPUT_SECTION,
                          Assignment::DEFAULT,
                          *SymOperand::create("."),
                          *RpnExpr::buildHelperExpr(it - 1));
        Output::dot_iterator ref = (*it)->dotAssignments().insert(dot, assign);
        for (RpnExpr::iterator tok = (*dot).getRpnExpr().begin(),
                               tokEnd = (*dot).getRpnExpr().end();
             tok != tokEnd;
             ++tok) {
          if ((*tok)->kind() == ExprToken::OPERAND &&
              llvm::cast<Operand>(*tok)->isDot())
            *tok = &((*ref).symbol());
        }  // for each token in the RHS expr of the dot assignment
      }
    }

    // fixup dot in output VMA if needed
    if ((*it)->prolog().hasVMA() && (*it)->prolog().vma().hasDot()) {
      Output::dot_iterator dot = (*it)->find_last_explicit_dot();
      if (dot == (*it)->dot_end()) {
        Assignment assign(Assignment::OUTPUT_SECTION,
                          Assignment::DEFAULT,
                          *SymOperand::create("."),
                          *RpnExpr::buildHelperExpr(it - 1));
        dot = (*it)->dotAssignments().insert(dot, assign);
      }
      for (RpnExpr::iterator tok = (*it)->prolog().vma().begin(),
                             tokEnd = (*it)->prolog().vma().end();
           tok != tokEnd;
           ++tok) {
        if ((*tok)->kind() == ExprToken::OPERAND &&
            llvm::cast<Operand>(*tok)->isDot())
          *tok = &((*dot).symbol());
      }  // for each token in the RHS expr of the dot assignment
    }
  }  // for each output section
}

}  // namespace mcld