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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
//===- Core/YamlReader.cpp - Reads YAML -----------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "YamlKeyValues.h"
#include "lld/Core/YamlReader.h"
#include "lld/Core/Atom.h"
#include "lld/Core/File.h"
#include "lld/Core/Reference.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/system_error.h"
#include <vector>
namespace lld {
namespace yaml {
enum yaml_reader_errors {
success = 0,
unknown_keyword,
illegal_value
};
class reader_error_category : public llvm::_do_message {
public:
virtual const char* name() const {
return "lld.yaml.reader";
}
virtual std::string message(int ev) const;
};
const reader_error_category reader_error_category_singleton;
std::string reader_error_category::message(int ev) const {
switch (ev) {
case success:
return "Success";
case unknown_keyword:
return "Unknown keyword found in yaml file";
case illegal_value:
return "Bad value found in yaml file";
default:
llvm_unreachable("An enumerator of yaml_reader_errors does not have a "
"message defined.");
}
}
inline llvm::error_code make_error_code(yaml_reader_errors e) {
return llvm::error_code(static_cast<int>(e), reader_error_category_singleton);
}
class YAML {
public:
struct Entry {
Entry(const char *k, const char *v, int d, bool bd, bool bs)
: key(strdup(k))
, value(strdup(v))
, depth(d)
, beginSequence(bs)
, beginDocument(bd) {}
const char *key;
const char *value;
int depth;
bool beginSequence;
bool beginDocument;
};
static void parse(llvm::MemoryBuffer *mb, std::vector<const Entry *>&);
private:
enum State {
start,
inHeaderComment,
inTripleDash,
inTriplePeriod,
inDocument,
inKey,
inSpaceBeforeValue,
inValue,
inValueSequence,
inValueSequenceEnd
};
};
void YAML::parse(llvm::MemoryBuffer *mb, std::vector<const Entry *> &entries) {
State state = start;
char key[64];
char value[64];
char *p = NULL;
unsigned int lineNumber = 1;
int depth = 0;
bool nextKeyIsStartOfDocument = false;
bool nextKeyIsStartOfSequence = false;
for (const char *s = mb->getBufferStart(); s < mb->getBufferEnd(); ++s) {
char c = *s;
if (c == '\n')
++lineNumber;
switch (state) {
case start:
if (c == '#')
state = inHeaderComment;
else if (c == '-') {
p = &key[0];
*p++ = c;
state = inTripleDash;
}
break;
case inHeaderComment:
if (c == '\n') {
state = start;
}
break;
case inTripleDash:
if (c == '-') {
*p++ = c;
} else if (c == '\n') {
*p = '\0';
if (strcmp(key, "---") != 0)
return;
depth = 0;
state = inDocument;
nextKeyIsStartOfDocument = true;
} else {
return;
}
break;
case inTriplePeriod:
if (c == '.') {
*p++ = c;
} else if (c == '\n') {
*p = '\0';
if (strcmp(key, "...") != 0)
return;
depth = 0;
state = inHeaderComment;
} else {
return;
}
break;
case inDocument:
if (isalnum(c)) {
state = inKey;
p = &key[0];
*p++ = c;
} else if (c == '-') {
if (depth == 0) {
p = &key[0];
*p++ = c;
state = inTripleDash;
} else {
nextKeyIsStartOfSequence = true;
++depth;
}
} else if (c == ' ') {
++depth;
} else if (c == '.') {
p = &key[0];
*p++ = c;
state = inTriplePeriod;
} else if (c == '\n') {
// ignore empty lines
depth = 0;
} else if (c == '\t') {
llvm::report_fatal_error("TAB character found in yaml file");
} else {
return;
}
break;
case inKey:
if (isalnum(c) || (c == '-')) {
*p++ = c;
} else if (c == ':') {
*p = '\0';
state = inSpaceBeforeValue;
} else if (c == '\n') {
*p = '\0';
if (strcmp(key, "---") == 0)
state = inDocument;
else
return;
} else {
return;
}
break;
case inSpaceBeforeValue:
if (isalnum(c) || (c == '-') || (c == '_')) {
p = &value[0];
*p++ = c;
state = inValue;
} else if (c == '\n') {
entries.push_back(new Entry(key, "", depth,
nextKeyIsStartOfDocument,
nextKeyIsStartOfSequence));
nextKeyIsStartOfSequence = false;
nextKeyIsStartOfDocument = false;
state = inDocument;
depth = 0;
} else if (c == '[') {
state = inValueSequence;
} else if (c == ' ') {
// eat space
} else if (c == '\t') {
llvm::report_fatal_error("TAB character found in yaml file");
} else {
return;
}
break;
case inValue:
if (isalnum(c) || (c == '-') || (c == '_')) {
*p++ = c;
} else if (c == '\n') {
*p = '\0';
entries.push_back(new Entry(key, value, depth,
nextKeyIsStartOfDocument,
nextKeyIsStartOfSequence));
nextKeyIsStartOfSequence = false;
nextKeyIsStartOfDocument = false;
state = inDocument;
depth = 0;
}
break;
case inValueSequence:
if (c == ']')
state = inValueSequenceEnd;
break;
case inValueSequenceEnd:
if (c == '\n') {
state = inDocument;
depth = 0;
}
break;
}
}
}
class YAMLFile : public File {
public:
YAMLFile()
: File("path")
, _lastRefIndex(0) {}
virtual bool forEachAtom(File::AtomHandler &) const;
virtual bool justInTimeforEachAtom(llvm::StringRef name,
File::AtomHandler &) const;
std::vector<Atom *> _atoms;
std::vector<Reference> _references;
unsigned int _lastRefIndex;
};
bool YAMLFile::forEachAtom(File::AtomHandler &handler) const {
handler.doFile(*this);
for (std::vector<Atom *>::const_iterator it = _atoms.begin();
it != _atoms.end(); ++it) {
handler.doAtom(**it);
}
return true;
}
bool YAMLFile::justInTimeforEachAtom(llvm::StringRef name,
File::AtomHandler &handler) const {
return false;
}
class YAMLAtom : public Atom {
public:
YAMLAtom( uint64_t ord
, Definition d
, Scope s
, ContentType ct
, SectionChoice sc
, bool intn
, bool md
, bool ah
, DeadStripKind dsk
, bool tb
, bool al
, Alignment a
, YAMLFile& f
, const char *n
, const char* sn
, uint64_t sz)
: Atom(ord, d, s, ct, sc, intn, md, ah, dsk, tb, al, a)
, _file(f)
, _name(n)
, _sectionName(sn)
, _size(sz)
, _refStartIndex(f._lastRefIndex)
, _refEndIndex(f._references.size()) {
f._lastRefIndex = _refEndIndex;
}
virtual const class File& file() const {
return _file;
}
virtual bool translationUnitSource(const char* *dir, const char* *name) const{
return false;
}
virtual llvm::StringRef name() const {
return _name;
}
virtual llvm::StringRef customSectionName() const {
return _sectionName;
}
virtual uint64_t objectAddress() const {
return 0;
}
virtual uint64_t size() const {
return _size;
}
virtual void copyRawContent(uint8_t buffer[]) const { }
virtual Reference::iterator referencesBegin() const;
virtual Reference::iterator referencesEnd() const;
private:
YAMLFile& _file;
const char * _name;
const char * _sectionName;
unsigned long _size;
unsigned int _refStartIndex;
unsigned int _refEndIndex;
};
Reference::iterator YAMLAtom::referencesBegin() const {
if (_file._references.size() < _refStartIndex)
return (Reference::iterator)&_file._references[_refStartIndex];
return 0;
}
Reference::iterator YAMLAtom::referencesEnd() const {
if (_file._references.size() < _refEndIndex)
return (Reference::iterator)&_file._references[_refEndIndex];
return 0;
}
class YAMLAtomState {
public:
YAMLAtomState();
void setName(const char *n);
void setAlign2(const char *n);
void setFixupKind(const char *n);
void setFixupOffset(const char *n);
void setFixupTarget(const char *n);
void addFixup(YAMLFile *f);
void makeAtom(YAMLFile&);
uint64_t _ordinal;
long long _size;
const char *_name;
Atom::Alignment _align;
Atom::ContentType _type;
Atom::Scope _scope;
Atom::Definition _def;
Atom::SectionChoice _sectionChoice;
bool _internalName;
bool _mergeDuplicates;
Atom::DeadStripKind _deadStrip;
bool _thumb;
bool _alias;
bool _autoHide;
const char *_sectionName;
Reference _ref;
};
YAMLAtomState::YAMLAtomState()
: _ordinal(0)
, _size(0)
, _name(NULL)
, _align(0, 0)
, _type(KeyValues::contentTypeDefault)
, _scope(KeyValues::scopeDefault)
, _def(KeyValues::definitionDefault)
, _internalName(KeyValues::internalNameDefault)
, _mergeDuplicates(KeyValues::mergeDuplicatesDefault)
, _deadStrip(KeyValues::deadStripKindDefault)
, _thumb(KeyValues::isThumbDefault)
, _alias(KeyValues::isAliasDefault)
, _autoHide(KeyValues::autoHideDefault)
, _sectionName(NULL) {
_ref.target = NULL;
_ref.addend = 0;
_ref.offsetInAtom = 0;
_ref.kind = 0;
_ref.flags = 0;
}
void YAMLAtomState::makeAtom(YAMLFile& f) {
Atom *a = new YAMLAtom(_ordinal, _def, _scope, _type, _sectionChoice,
_internalName, _mergeDuplicates, _autoHide,
_deadStrip, _thumb, _alias, _align, f,
_name, _sectionName, _size);
f._atoms.push_back(a);
++_ordinal;
// reset state for next atom
_name = NULL;
_align.powerOf2 = 0;
_align.modulus = 0;
_type = KeyValues::contentTypeDefault;
_scope = KeyValues::scopeDefault;
_def = KeyValues::definitionDefault;
_sectionChoice = KeyValues::sectionChoiceDefault;
_internalName = KeyValues::internalNameDefault;
_mergeDuplicates = KeyValues::mergeDuplicatesDefault;
_deadStrip = KeyValues::deadStripKindDefault;
_thumb = KeyValues::isThumbDefault;
_alias = KeyValues::isAliasDefault;
_autoHide = KeyValues::autoHideDefault;
_sectionName = NULL;
_ref.target = NULL;
_ref.addend = 0;
_ref.offsetInAtom = 0;
_ref.kind = 0;
_ref.flags = 0;
}
void YAMLAtomState::setName(const char *n) {
_name = n;
}
void YAMLAtomState::setAlign2(const char *s) {
llvm::StringRef str(s);
uint32_t res;
str.getAsInteger(10, res);
_align.powerOf2 = static_cast<uint16_t>(res);
}
void YAMLAtomState::setFixupKind(const char *s) {
if (strcmp(s, "pcrel32") == 0)
_ref.kind = 1;
else if (strcmp(s, "call32") == 0)
_ref.kind = 2;
else
llvm::report_fatal_error("bad fixup kind value");
}
void YAMLAtomState::setFixupOffset(const char *s) {
if ((s[0] == '0') && (s[1] == 'x'))
llvm::StringRef(s).getAsInteger(16, _ref.offsetInAtom);
else
llvm::StringRef(s).getAsInteger(10, _ref.offsetInAtom);
}
void YAMLAtomState::setFixupTarget(const char *s) {
}
void YAMLAtomState::addFixup(YAMLFile *f) {
f->_references.push_back(_ref);
// clear for next ref
_ref.target = NULL;
_ref.addend = 0;
_ref.offsetInAtom = 0;
_ref.kind = 0;
_ref.flags = 0;
}
llvm::error_code parseObjectText( llvm::MemoryBuffer *mb
, std::vector<File *> &result) {
std::vector<const YAML::Entry *> entries;
YAML::parse(mb, entries);
YAMLFile *file = NULL;
YAMLAtomState atomState;
bool inAtoms = false;
bool inFixups = false;
int depthForAtoms = -1;
int depthForFixups = -1;
int lastDepth = -1;
bool haveAtom = false;
bool haveFixup = false;
for (std::vector<const YAML::Entry *>::iterator it = entries.begin();
it != entries.end(); ++it) {
const YAML::Entry *entry = *it;
if (entry->beginDocument) {
if (file != NULL) {
if (haveAtom) {
atomState.makeAtom(*file);
haveAtom = false;
}
result.push_back(file);
}
file = new YAMLFile();
inAtoms = false;
depthForAtoms = -1;
}
if (lastDepth > entry->depth) {
// end of fixup sequence
if (haveFixup) {
atomState.addFixup(file);
haveFixup = false;
}
}
if (inAtoms && (depthForAtoms == -1)) {
depthForAtoms = entry->depth;
}
if (inFixups && (depthForFixups == -1)) {
depthForFixups = entry->depth;
}
if (strcmp(entry->key, "atoms") == 0) {
inAtoms = true;
}
if (inAtoms) {
if (depthForAtoms == entry->depth) {
if (entry->beginSequence) {
if (haveAtom) {
atomState.makeAtom(*file);
haveAtom = false;
}
}
if (strcmp(entry->key, KeyValues::nameKeyword) == 0) {
atomState.setName(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::internalNameKeyword) == 0) {
atomState._internalName = KeyValues::internalName(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::definitionKeyword) == 0) {
atomState._def = KeyValues::definition(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::scopeKeyword) == 0) {
atomState._scope = KeyValues::scope(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::contentTypeKeyword) == 0) {
atomState._type = KeyValues::contentType(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::deadStripKindKeyword) == 0) {
atomState._deadStrip = KeyValues::deadStripKind(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::sectionChoiceKeyword) == 0) {
atomState._sectionChoice = KeyValues::sectionChoice(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::mergeDuplicatesKeyword) == 0) {
atomState._mergeDuplicates = KeyValues::mergeDuplicates(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::autoHideKeyword) == 0) {
atomState._autoHide = KeyValues::autoHide(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::isThumbKeyword) == 0) {
atomState._thumb = KeyValues::isThumb(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::isAliasKeyword) == 0) {
atomState._alias = KeyValues::isAlias(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::sectionNameKeyword) == 0) {
atomState._sectionName = entry->value;
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::sizeKeyword) == 0) {
llvm::StringRef val = entry->value;
if ( val.getAsInteger(0, atomState._size) )
return make_error_code(illegal_value);
haveAtom = true;
}
else if (strcmp(entry->key, KeyValues::contentKeyword) == 0) {
// TO DO: switch to content mode
haveAtom = true;
}
else if (strcmp(entry->key, "align2") == 0) {
atomState.setAlign2(entry->value);
haveAtom = true;
}
else if (strcmp(entry->key, "fixups") == 0) {
inFixups = true;
}
else {
return make_error_code(unknown_keyword);
}
}
else if (depthForFixups == entry->depth) {
if (entry->beginSequence) {
if (haveFixup) {
atomState.addFixup(file);
haveFixup = false;
}
}
if (strcmp(entry->key, "kind") == 0) {
atomState.setFixupKind(entry->value);
haveFixup = true;
} else if (strcmp(entry->key, "offset") == 0) {
atomState.setFixupOffset(entry->value);
haveFixup = true;
} else if (strcmp(entry->key, "target") == 0) {
atomState.setFixupTarget(entry->value);
haveFixup = true;
}
}
}
lastDepth = entry->depth;
}
if (haveAtom) {
atomState.makeAtom(*file);
}
result.push_back(file);
return make_error_code(success);
}
//
// Fill in vector<File*> from path to input text file.
//
llvm::error_code parseObjectTextFileOrSTDIN(llvm::StringRef path
, std::vector<File*>& result) {
llvm::OwningPtr<llvm::MemoryBuffer> mb;
llvm::error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(path, mb);
if ( ec )
return ec;
return parseObjectText(mb.get(), result);
}
} // namespace yaml
} // namespace lld
|