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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
|
//===- lib/ReaderWriter/YAML/ReaderYAML.cpp - Reads YAML object files -----===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/ReaderWriter/ReaderYAML.h"
#include "lld/Core/AbsoluteAtom.h"
#include "lld/Core/ArchiveLibraryFile.h"
#include "lld/Core/Atom.h"
#include "lld/Core/Error.h"
#include "lld/Core/File.h"
#include "lld/Core/LLVM.h"
#include "lld/Core/Reference.h"
#include "lld/Core/SharedLibraryAtom.h"
#include "lld/Core/UndefinedAtom.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/system_error.h"
#include "llvm/Support/YAMLParser.h"
#include <cstring>
#include <vector>
#include "YamlKeyValues.h"
namespace lld {
namespace yaml {
///
/// Concrete instance of lld::Reference created parsing YAML object files
///
class YAMLReference : public Reference {
public:
YAMLReference()
: _target(nullptr)
, _targetNameNode(nullptr)
, _offsetInAtom(0)
, _addend(0)
, _kind(0)
{}
virtual uint64_t offsetInAtom() const {
return _offsetInAtom;
}
virtual Kind kind() const {
return _kind;
}
virtual void setKind(Kind k) {
_kind = k;
}
virtual const Atom *target() const {
return _target;
}
virtual Addend addend() const {
return _addend;
}
virtual void setAddend(Addend a) {
_addend = a;
}
virtual void setTarget(const Atom *newAtom) {
_target = newAtom;
}
typedef llvm::yaml::ScalarNode ScalarNode;
const Atom *_target;
ScalarNode * _targetNameNode;
uint64_t _offsetInAtom;
Addend _addend;
Kind _kind;
};
///
/// Concrete instance of lld::File created parsing YAML object files.
///
class YAMLFile : public ArchiveLibraryFile {
public:
YAMLFile()
: ArchiveLibraryFile("<anonymous>")
, _lastRefIndex(0)
, _kind(File::kindObject) {
}
~YAMLFile();
// Depending on the YAML description, this file can be either an
// lld::ArchiveLibraryFile or lld::File.
virtual File::Kind kind() const {
return _kind;
}
virtual const atom_collection<DefinedAtom> &defined() const {
return _definedAtoms;
}
virtual const atom_collection<UndefinedAtom> &undefined() const {
return _undefinedAtoms;
}
virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
return _sharedLibraryAtoms;
}
virtual const atom_collection<AbsoluteAtom> &absolute() const {
return _absoluteAtoms;
}
virtual void addAtom(const Atom&) {
assert(0 && "cannot add atoms to YAML files");
}
// Standard way that archives are searched.
virtual const File *find(StringRef name, bool dataSymbolOnly) const;
error_code bindTargetReferences(llvm::yaml::Stream &stream);
void addDefinedAtom(class YAMLDefinedAtom *atom, StringRef refName);
void addUndefinedAtom(UndefinedAtom *atom);
void addSharedLibraryAtom(SharedLibraryAtom *atom);
void addAbsoluteAtom(AbsoluteAtom *atom);
Atom *findAtom(StringRef name);
void addMember(StringRef);
void setName(StringRef);
StringRef copyString(StringRef);
struct NameAtomPair {
NameAtomPair(StringRef n, Atom *a) : name(n), atom(a) {}
StringRef name;
Atom *atom;
};
atom_collection_vector<DefinedAtom> _definedAtoms;
atom_collection_vector<UndefinedAtom> _undefinedAtoms;
atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
std::vector<YAMLReference> _references;
std::vector<NameAtomPair> _nameToAtomMapping;
std::vector<std::unique_ptr<YAMLFile>> _memberFiles;
std::vector<char*> _stringCopies;
unsigned int _lastRefIndex;
File::Kind _kind;
};
///
/// Concrete instance of lld::DefinedAtom created parsing YAML object files.
///
class YAMLDefinedAtom : public DefinedAtom {
public:
YAMLDefinedAtom( uint32_t ord
, YAMLFile &file
, DefinedAtom::Scope scope
, DefinedAtom::ContentType type
, DefinedAtom::SectionChoice sectionChoice
, DefinedAtom::Interposable interpose
, DefinedAtom::Merge merge
, DefinedAtom::DeadStripKind deadStrip
, DefinedAtom::ContentPermissions perms
, bool isThumb
, bool isAlias
, DefinedAtom::Alignment alignment
, StringRef name
, StringRef sectionName
, uint64_t size
, std::vector<uint8_t>& content)
: _file(file)
, _name(name)
, _sectionName(sectionName)
, _size(size)
, _ord(ord)
, _content(content)
, _alignment(alignment)
, _scope(scope)
, _type(type)
, _sectionChoice(sectionChoice)
, _interpose(interpose)
, _merge(merge)
, _deadStrip(deadStrip)
, _permissions(perms)
, _isThumb(isThumb)
, _isAlias(isAlias)
, _refStartIndex(file._lastRefIndex)
, _refEndIndex(file._references.size()) {
file._lastRefIndex = _refEndIndex;
}
virtual const class File &file() const {
return _file;
}
virtual StringRef name() const {
return _name;
}
virtual uint64_t size() const {
return _content.empty() ? _size : _content.size();
}
virtual DefinedAtom::Scope scope() const {
return _scope;
}
virtual DefinedAtom::Interposable interposable() const {
return _interpose;
}
virtual DefinedAtom::Merge merge() const {
return _merge;
}
virtual DefinedAtom::ContentType contentType() const {
return _type;
}
virtual DefinedAtom::Alignment alignment() const {
return _alignment;
}
virtual DefinedAtom::SectionChoice sectionChoice() const {
return _sectionChoice;
}
virtual StringRef customSectionName() const {
return _sectionName;
}
virtual DefinedAtom::DeadStripKind deadStrip() const {
return _deadStrip;
}
virtual DefinedAtom::ContentPermissions permissions() const {
return _permissions;
}
virtual bool isThumb() const {
return _isThumb;
}
virtual bool isAlias() const {
return _isAlias;
}
ArrayRef<uint8_t> rawContent() const {
return ArrayRef<uint8_t>(_content);
}
virtual uint64_t ordinal() const {
return _ord;
}
DefinedAtom::reference_iterator begin() const {
uintptr_t index = _refStartIndex;
const void* it = reinterpret_cast<const void*>(index);
return reference_iterator(*this, it);
}
DefinedAtom::reference_iterator end() const {
uintptr_t index = _refEndIndex;
const void* it = reinterpret_cast<const void*>(index);
return reference_iterator(*this, it);
}
const Reference* derefIterator(const void* it) const {
uintptr_t index = reinterpret_cast<uintptr_t>(it);
assert(index >= _refStartIndex);
assert(index < _refEndIndex);
assert(index < _file._references.size());
return &_file._references[index];
}
void incrementIterator(const void*& it) const {
uintptr_t index = reinterpret_cast<uintptr_t>(it);
++index;
it = reinterpret_cast<const void*>(index);
}
// Convert each target name to a pointer to an atom object
error_code bindTargetReferences(llvm::yaml::Stream &stream) const {
for (unsigned int i=_refStartIndex; i < _refEndIndex; ++i) {
llvm::SmallString<32> storage;
llvm::yaml::ScalarNode *node = _file._references[i]._targetNameNode;
StringRef name = node->getValue(storage);
Atom *targetAtom = _file.findAtom(name);
if ( targetAtom ) {
_file._references[i]._target = targetAtom;
}
else {
stream.printError(node, "Fixup has target '" + name
+ "' which does not exist");
return make_error_code(yaml_reader_error::illegal_value);
}
}
return make_error_code(yaml_reader_error::success);
}
private:
YAMLFile &_file;
StringRef _name;
StringRef _sectionName;
unsigned long _size;
uint32_t _ord;
std::vector<uint8_t> _content;
DefinedAtom::Alignment _alignment;
DefinedAtom::Scope _scope;
DefinedAtom::ContentType _type;
DefinedAtom::SectionChoice _sectionChoice;
DefinedAtom::Interposable _interpose;
DefinedAtom::Merge _merge;
DefinedAtom::DeadStripKind _deadStrip;
DefinedAtom::ContentPermissions _permissions;
bool _isThumb;
bool _isAlias;
unsigned int _refStartIndex;
unsigned int _refEndIndex;
};
///
/// Concrete instance of lld::UndefinedAtom created parsing YAML object files.
///
class YAMLUndefinedAtom : public UndefinedAtom {
public:
YAMLUndefinedAtom( YAMLFile &f
, int32_t
, StringRef name
, UndefinedAtom::CanBeNull cbn)
: _file(f)
, _name(name)
, _canBeNull(cbn) {
}
virtual const class File &file() const {
return _file;
}
virtual StringRef name() const {
return _name;
}
virtual CanBeNull canBeNull() const {
return _canBeNull;
}
private:
YAMLFile &_file;
StringRef _name;
UndefinedAtom::CanBeNull _canBeNull;
};
///
/// Concrete instance of lld::SharedLibraryAtom created parsing YAML files.
///
class YAMLSharedLibraryAtom : public SharedLibraryAtom {
public:
YAMLSharedLibraryAtom( YAMLFile &f
, int32_t
, StringRef name
, StringRef loadName
, bool cbn)
: _file(f)
, _name(name)
, _loadName(loadName)
, _canBeNull(cbn) {
}
virtual const class File &file() const {
return _file;
}
virtual StringRef name() const {
return _name;
}
virtual StringRef loadName() const {
return _loadName;
}
virtual bool canBeNullAtRuntime() const {
return _canBeNull;
}
private:
YAMLFile &_file;
StringRef _name;
StringRef _loadName;
bool _canBeNull;
};
///
/// Concrete instance of lld::AbsoluteAtom created parsing YAML object files.
///
class YAMLAbsoluteAtom : public AbsoluteAtom {
public:
YAMLAbsoluteAtom(YAMLFile &f, int32_t, StringRef name, uint64_t v)
: _file(f)
, _name(name)
, _value(v) {
}
virtual const class File &file() const {
return _file;
}
virtual StringRef name() const {
return _name;
}
virtual uint64_t value() const {
return _value;
}
private:
YAMLFile &_file;
StringRef _name;
uint64_t _value;
};
//===----------------------------------------------------------------------===//
// YAMLFile methods
//===----------------------------------------------------------------------===//
YAMLFile::~YAMLFile() {
for (char *s : _stringCopies) {
delete [] s;
}
}
error_code YAMLFile::bindTargetReferences(llvm::yaml::Stream &stream) {
error_code ec;
for (const DefinedAtom *defAtom : _definedAtoms) {
const YAMLDefinedAtom *atom =
reinterpret_cast<const YAMLDefinedAtom*>(defAtom);
ec = atom->bindTargetReferences(stream);
if ( ec )
return ec;
}
return ec;
}
Atom *YAMLFile::findAtom(StringRef name) {
for (auto &ci : _nameToAtomMapping) {
if (ci.name == name)
return ci.atom;
}
return nullptr;
}
void YAMLFile::addDefinedAtom(YAMLDefinedAtom *atom, StringRef refName) {
_definedAtoms._atoms.push_back(atom);
_nameToAtomMapping.push_back(NameAtomPair(refName, atom));
}
void YAMLFile::addUndefinedAtom(UndefinedAtom *atom) {
_undefinedAtoms._atoms.push_back(atom);
_nameToAtomMapping.push_back(NameAtomPair(atom->name(), atom));
}
void YAMLFile::addSharedLibraryAtom(SharedLibraryAtom *atom) {
_sharedLibraryAtoms._atoms.push_back(atom);
_nameToAtomMapping.push_back(NameAtomPair(atom->name(), atom));
}
void YAMLFile::addAbsoluteAtom(AbsoluteAtom *atom) {
_absoluteAtoms._atoms.push_back(atom);
_nameToAtomMapping.push_back(NameAtomPair(atom->name(), atom));
}
void YAMLFile::setName(StringRef name) {
_path = StringRef(name);
}
// Allocate a new copy of this string and keep track of allocations
// in _stringCopies, so they can be freed when YAMLFile is destroyed.
StringRef YAMLFile::copyString(StringRef str) {
char* s = new char[str.size()];
memcpy(s, str.data(), str.size());
_stringCopies.push_back(s);
return StringRef(s, str.size());
}
const File *YAMLFile::find(StringRef name, bool dataSymbolOnly) const {
for (auto &file : _memberFiles) {
for (const DefinedAtom *atom : file->defined() ) {
if (name == atom->name())
return file.get();
}
}
return nullptr;
}
///
/// The state machine that drives the YAMLParser stream and instantiates
/// Files and Atoms. This class also buffers all the attribures for the
/// current atom and current fixup. Once all attributes are accumulated,
/// a new atom or fixup instance is instantiated.
///
class YAMLState {
public:
YAMLState(const ReaderOptionsYAML &opts, llvm::yaml::Stream *s, YAMLFile *f);
void parse(llvm::yaml::Node *node, StringRef keyword,
llvm::yaml::Node *keywordNode=nullptr);
error_code error() { return _error; }
private:
typedef llvm::yaml::Node Node;
typedef llvm::yaml::ScalarNode ScalarNode;
typedef llvm::yaml::SequenceNode SequenceNode;
typedef llvm::yaml::MappingNode MappingNode;
typedef llvm::yaml::Stream Stream;
void resetState();
void setAlign2(StringRef n);
void makeReference();
void makeAtom(Node *node);
void makeDefinedAtom(Node *node);
void makeUndefinedAtom(Node *node);
void makeSharedLibraryAtom(Node *node);
void makeAbsoluteAtom(Node *node);
void parseMemberName(ScalarNode *node);
void parseAtomName(ScalarNode *node);
void parseAtomRefName(ScalarNode *node);
void parseAtomType(ScalarNode *node);
void parseAtomScope(ScalarNode *node);
void parseAtomDefinition(ScalarNode *node);
void parseAtomDeadStrip(ScalarNode *node);
void parseAtomSectionChoice(ScalarNode *node);
void parseAtomInterposable(ScalarNode *node);
void parseAtomMerge(ScalarNode *node);
void parseAtomIsThumb(ScalarNode *node);
void parseAtomIsAlias(ScalarNode *node);
void parseAtomSectionName(ScalarNode *node);
void parseAtomSize(ScalarNode *node);
void parseAtomPermissions(ScalarNode *node);
void parseAtomCanBeNull(ScalarNode *node);
void parseFixUpOffset(ScalarNode *node);
void parseFixUpKind(ScalarNode *node);
void parseFixUpTarget(ScalarNode *node);
void parseFixUpAddend(ScalarNode *node);
void parseAtomContentByte(ScalarNode *node);
void parseAtomLoadName(ScalarNode *node);
void parseAtomValue(ScalarNode *node);
StringRef extractString(ScalarNode *node);
typedef void (YAMLState:: *ParseScalar)(ScalarNode *node);
typedef void (YAMLState:: *ParseSeq)(SequenceNode *node);
typedef void (YAMLState:: *ParseMap)(MappingNode *node);
enum State { inError, inTop, inDoc, inArch, inMemb,
inAtoms, inAtom, inFixUps, inFixUp, inBytes };
struct Transistion {
State state;
const char* keyword;
State newState;
ParseScalar customAction;
};
static const char* stateName(State);
void moveToState(State s);
void returnToState(State s, Node *node);
static const Transistion _s_transistions[];
const ReaderOptionsYAML &_options;
error_code _error;
llvm::yaml::Stream *_stream;
YAMLFile *_file;
YAMLFile *_archiveFile;
State _state;
StringRef _name;
StringRef _refName;
StringRef _sectionName;
StringRef _loadName;
StringRef _memberName;
unsigned long long _size;
uint64_t _value;
uint32_t _ordinal;
std::vector<uint8_t> _content;
DefinedAtom::Alignment _alignment;
Atom::Definition _definition;
DefinedAtom::Scope _scope;
DefinedAtom::ContentType _type;
DefinedAtom::SectionChoice _sectionChoice;
DefinedAtom::Interposable _interpose;
DefinedAtom::Merge _merge;
DefinedAtom::DeadStripKind _deadStrip;
DefinedAtom::ContentPermissions _permissions;
bool _isThumb;
bool _isAlias;
UndefinedAtom::CanBeNull _canBeNull;
YAMLReference _ref;
bool _hasDefinedAtomAttributes;
bool _hasUndefinedAtomAttributes;
bool _hasSharedLibraryAtomAttributes;
bool _hasAbsoluteAtomAttributes;
};
//
// This transition table is the heart of the state machine.
// The table is read left-to-right columns A,B,C,D as:
// If the state is A and key B is seen switch to state C then
// if D is not nullptr call that method with the key's value,
// if D is nullptr, recursively parse in the new state.
//
const YAMLState::Transistion YAMLState::_s_transistions[] = {
{ inTop, "<root>", inDoc, nullptr },
{ inDoc, "archive", inArch, nullptr },
{ inArch, "<any-seq-item>", inMemb, nullptr },
{ inMemb, "atoms", inAtoms, nullptr },
{ inMemb, "name", inMemb, &YAMLState::parseMemberName },
{ inDoc, "atoms", inAtoms, nullptr },
{ inAtoms, "<any-seq-item>", inAtom, nullptr },
{ inAtom, "name", inAtom, &YAMLState::parseAtomName },
{ inAtom, "ref-name", inAtom, &YAMLState::parseAtomRefName },
{ inAtom, "type", inAtom, &YAMLState::parseAtomType },
{ inAtom, "scope", inAtom, &YAMLState::parseAtomScope },
{ inAtom, "definition", inAtom, &YAMLState::parseAtomDefinition },
{ inAtom, "dead-strip", inAtom, &YAMLState::parseAtomDeadStrip },
{ inAtom, "section-choice", inAtom, &YAMLState::parseAtomSectionChoice },
{ inAtom, "interposable", inAtom, &YAMLState::parseAtomInterposable },
{ inAtom, "merge", inAtom, &YAMLState::parseAtomMerge },
{ inAtom, "is-thumb", inAtom, &YAMLState::parseAtomIsThumb },
{ inAtom, "is-alias", inAtom, &YAMLState::parseAtomIsAlias },
{ inAtom, "section-name", inAtom, &YAMLState::parseAtomSectionName },
{ inAtom, "size", inAtom, &YAMLState::parseAtomSize },
{ inAtom, "permissions", inAtom, &YAMLState::parseAtomPermissions },
{ inAtom, "can-be-null", inAtom, &YAMLState::parseAtomCanBeNull },
{ inAtom, "content", inBytes, nullptr },
{ inAtom, "fixups", inFixUps,nullptr },
{ inBytes, "<any-seq-item>", inBytes, &YAMLState::parseAtomContentByte },
{ inFixUps,"<any-seq-item>", inFixUp, nullptr },
{ inFixUp, "offset", inFixUp, &YAMLState::parseFixUpOffset },
{ inFixUp, "kind", inFixUp, &YAMLState::parseFixUpKind },
{ inFixUp, "target", inFixUp, &YAMLState::parseFixUpTarget },
{ inFixUp, "addend", inFixUp, &YAMLState::parseFixUpAddend },
{ inAtom, "load-name", inAtom, &YAMLState::parseAtomLoadName },
{ inAtom, "value", inAtom, &YAMLState::parseAtomValue },
{ inError, nullptr, inAtom, nullptr },
};
YAMLState::YAMLState(const ReaderOptionsYAML &opts, Stream *stream,
YAMLFile *file)
: _options(opts)
, _error(make_error_code(yaml_reader_error::success))
, _stream(stream)
, _file(file)
, _archiveFile(nullptr)
, _state(inTop)
, _alignment(0, 0) {
this->resetState();
}
void YAMLState::makeAtom(Node *node) {
switch (_definition ) {
case Atom::definitionRegular:
this->makeDefinedAtom(node);
break;
case Atom::definitionUndefined:
this->makeUndefinedAtom(node);
break;
case Atom::definitionSharedLibrary:
this->makeSharedLibraryAtom(node);
break;
case Atom::definitionAbsolute:
this->makeAbsoluteAtom(node);
break;
}
++_ordinal;
// reset state for next atom
this->resetState();
}
void YAMLState::makeDefinedAtom(Node *node) {
if ( _hasAbsoluteAtomAttributes ) {
_stream->printError(node, "Defined atom '" + _name
+ "' has attributes only allowed on absolute atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
if ( _hasSharedLibraryAtomAttributes ) {
_stream->printError(node, "Defined atom '" + _name
+ "' has attributes only allowed on shared library atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
YAMLDefinedAtom *a = new YAMLDefinedAtom(_ordinal, *_file, _scope, _type
, _sectionChoice, _interpose, _merge, _deadStrip
, _permissions, _isThumb, _isAlias, _alignment
, _name, _sectionName, _size, _content);
_file->addDefinedAtom(a, !_refName.empty() ? _refName : _name);
}
void YAMLState::makeUndefinedAtom(Node *node) {
if ( _hasDefinedAtomAttributes ) {
_stream->printError(node, "Undefined atom '" + _name
+ "' has attributes only allowed on defined atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
if ( _hasAbsoluteAtomAttributes ) {
_stream->printError(node, "Defined atom '" + _name
+ "' has attributes only allowed on absolute atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
UndefinedAtom *a = new YAMLUndefinedAtom(*_file, _ordinal, _name, _canBeNull);
_file->addUndefinedAtom(a);
}
void YAMLState::makeSharedLibraryAtom(Node *node) {
if ( _hasDefinedAtomAttributes ) {
_stream->printError(node, "SharedLibrary atom '" + _name
+ "' has attributes only allowed on defined atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
if ( _hasAbsoluteAtomAttributes ) {
_stream->printError(node, "Defined atom '" + _name
+ "' has attributes only allowed on absolute atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
bool nullable = (_canBeNull == UndefinedAtom::canBeNullAtRuntime);
SharedLibraryAtom *a = new YAMLSharedLibraryAtom(*_file, _ordinal, _name,
_loadName, nullable);
_file->addSharedLibraryAtom(a);
}
void YAMLState::makeAbsoluteAtom(Node *node) {
if ( _hasDefinedAtomAttributes ) {
_stream->printError(node, "Absolute atom '" + _name
+ "' has attributes only allowed on defined atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
if ( _hasSharedLibraryAtomAttributes ) {
_stream->printError(node, "Absolute atom '" + _name
+ "' has attributes only allowed on shared library atoms");
_error = make_error_code(yaml_reader_error::illegal_value);
}
AbsoluteAtom *a = new YAMLAbsoluteAtom(*_file, _ordinal, _name, _value);
_file->addAbsoluteAtom(a);
}
void YAMLState::resetState() {
_name = StringRef();
_refName = StringRef();
_sectionName = StringRef();
_loadName = StringRef();
_memberName = StringRef();
_size = 0;
_value = 0;
_ordinal = 0;
_content.clear();
_alignment.powerOf2 = 0;
_alignment.modulus = 0;
_definition = KeyValues::definitionDefault;
_scope = KeyValues::scopeDefault;
_type = KeyValues::contentTypeDefault;
_sectionChoice = KeyValues::sectionChoiceDefault;
_interpose = KeyValues::interposableDefault;
_merge = KeyValues::mergeDefault;
_deadStrip = KeyValues::deadStripKindDefault;
_permissions = KeyValues::permissionsDefault;
_isThumb = KeyValues::isThumbDefault;
_isAlias = KeyValues::isAliasDefault;
_canBeNull = KeyValues::canBeNullDefault;
_ref._target = nullptr;
_ref._targetNameNode= nullptr;
_ref._addend = 0;
_ref._offsetInAtom = 0;
_ref._kind = 0;
_hasDefinedAtomAttributes = false;
_hasUndefinedAtomAttributes = false;
_hasSharedLibraryAtomAttributes = false;
_hasAbsoluteAtomAttributes = false;
}
void YAMLState::makeReference() {
_file->_references.push_back(_ref);
// clear for next ref
_ref._target = nullptr;
_ref._targetNameNode= nullptr;
_ref._addend = 0;
_ref._offsetInAtom = 0;
_ref._kind = 0;
}
void YAMLState::setAlign2(StringRef s) {
if (StringRef(s).getAsInteger(10, _alignment.powerOf2))
_alignment.powerOf2 = 1;
}
// For debug logging
const char* YAMLState::stateName(State s) {
switch ( s ) {
case inError:
return "inError";
case inTop:
return "inTop";
case inDoc:
return "inDoc";
case inArch:
return "inArch";
case inMemb:
return "inMemb";
case inAtoms:
return "inAtoms";
case inAtom:
return "inAtom";
case inFixUps:
return "inFixUps";
case inFixUp:
return "inFixUp";
case inBytes:
return "inBytes";
}
return "unknown case";
}
// Called by parse() when recursing and switching to a new state.
void YAMLState::moveToState(State newState) {
if ( newState == _state )
return;
DEBUG_WITH_TYPE("objtxt", llvm::dbgs() << "moveToState(" << stateName(newState)
<< "), _state=" << stateName(_state) << "\n");
if ( newState == inArch ) {
// Seen "archive:", repurpose existing YAMLFile to be archive file
_file->_kind = File::kindArchiveLibrary;
_archiveFile = _file;
_file = nullptr;
}
if ( newState == inMemb ) {
assert(_state == inArch);
// Make new YAMLFile for this member
std::unique_ptr<YAMLFile> memberFile(new YAMLFile);
_file = memberFile.get();
assert(_archiveFile != nullptr);
_archiveFile->_memberFiles.emplace_back(memberFile.release());
}
_state = newState;
}
// Called by parse() when returning from recursion and restoring the old state.
void YAMLState::returnToState(State prevState, Node *node) {
if ( prevState == _state )
return;
DEBUG_WITH_TYPE("objtxt", llvm::dbgs()
<< "returnToState(" << stateName(prevState)
<< "), _state=" << stateName(_state) << "\n");
// If done with an atom, instantiate an object for it.
if ( (_state == inAtom) && (prevState == inAtoms) )
this->makeAtom(node);
// If done wit a fixup, instantiate an object for it.
if ( (_state == inFixUp) && (prevState == inFixUps) )
this->makeReference();
_state = prevState;
}
// If a string in the yaml document is quoted in a way that there is no
// contiguous range of bytes that a StringRef can point to, then we make
// a copy of the string and have the StringRef point to that.
StringRef YAMLState::extractString(ScalarNode *node) {
llvm::SmallString<32> storage;
StringRef str = node->getValue(storage);
//if ( str.data() == storage.begin() ) {
str = _file->copyString(str);
//}
return str;
}
void YAMLState::parseMemberName(ScalarNode *node) {
_memberName = extractString(node);
}
void YAMLState::parseAtomName(ScalarNode *node) {
_name = extractString(node);
}
void YAMLState::parseAtomRefName(ScalarNode *node) {
_refName = extractString(node);
}
void YAMLState::parseAtomScope(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::scope(node->getValue(storage), _scope) ) {
_stream->printError(node, "Invalid value for 'scope:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomDefinition(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::definition(node->getValue(storage), _definition) ) {
_stream->printError(node, "Invalid value for 'definition:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
}
void YAMLState::parseAtomType(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::contentType(node->getValue(storage), _type) ) {
_stream->printError(node, "Invalid value for 'type:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomDeadStrip(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::deadStripKind(node->getValue(storage), _deadStrip) ) {
_stream->printError(node, "Invalid value for 'dead-strip:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomSectionChoice(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::sectionChoice(node->getValue(storage), _sectionChoice) ) {
_stream->printError(node, "Invalid value for 'section-choice:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomInterposable(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::interposable(node->getValue(storage), _interpose) ) {
_stream->printError(node, "Invalid value for 'interposable:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomMerge(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::merge(node->getValue(storage), _merge) ) {
_stream->printError(node, "Invalid value for 'merge:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomIsThumb(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::isThumb(node->getValue(storage), _isThumb) ) {
_stream->printError(node, "Invalid value for 'thumb:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomIsAlias(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::isAlias(node->getValue(storage), _isAlias) ) {
_stream->printError(node, "Invalid value for 'is-alias:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomSectionName(ScalarNode *node) {
_sectionName = extractString(node);
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomSize(ScalarNode *node) {
llvm::SmallString<32> storage;
StringRef offsetStr = node->getValue(storage);
if ( offsetStr.getAsInteger(0, _size) ) {
_stream->printError(node, "Invalid value for atom 'size:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomPermissions(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::permissions(node->getValue(storage), _permissions) ) {
_stream->printError(node, "Invalid value for 'permissions:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomCanBeNull(ScalarNode *node) {
llvm::SmallString<32> storage;
if ( KeyValues::canBeNull(node->getValue(storage), _canBeNull) ) {
_stream->printError(node, "Invalid value for 'can-be-null:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
}
void YAMLState::parseFixUpOffset(ScalarNode *node) {
llvm::SmallString<32> storage;
StringRef offsetStr = node->getValue(storage);
if ( offsetStr.getAsInteger(0, _ref._offsetInAtom) ) {
_stream->printError(node, "Invalid value for fixup 'offset:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseFixUpKind(ScalarNode *node) {
llvm::SmallString<32> storage;
_ref._kind = _options.kindFromString(node->getValue(storage));
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseFixUpTarget(ScalarNode *node) {
_ref._targetNameNode = node;
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseFixUpAddend(ScalarNode *node) {
llvm::SmallString<32> storage;
StringRef offsetStr = node->getValue(storage);
if ( offsetStr.getAsInteger(0, _ref._addend) ) {
_stream->printError(node, "Invalid value for fixup 'addend:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomContentByte(ScalarNode *node) {
llvm::SmallString<32> storage;
StringRef str = node->getValue(storage);
unsigned int contentByte;
if ( str.getAsInteger(16, contentByte) ) {
_stream->printError(node, "Invalid content hex byte '0x" + str + "'");
_error = make_error_code(yaml_reader_error::illegal_value);
return;
}
if (contentByte > 0xFF) {
_stream->printError(node, "Content hex byte out of range (0x"
+ str + " > 0xFF)");
_error = make_error_code(yaml_reader_error::illegal_value);
return;
}
_content.push_back(contentByte & 0xFF);
_hasDefinedAtomAttributes = true;
}
void YAMLState::parseAtomLoadName(ScalarNode *node) {
_loadName = extractString(node);
_hasSharedLibraryAtomAttributes = true;
}
void YAMLState::parseAtomValue(ScalarNode *node) {
llvm::SmallString<32> storage;
StringRef offsetStr = node->getValue(storage);
if ( offsetStr.getAsInteger(0, _value) ) {
_stream->printError(node, "Invalid value for fixup 'addend:'");
_error = make_error_code(yaml_reader_error::illegal_value);
}
_hasAbsoluteAtomAttributes = true;
}
//
// This is the parsing engine that walks the nodes in the yaml document
// stream. It is table driven. See _s_transistions.
//
void YAMLState::parse(Node *node, StringRef keyword, Node *keywordNode) {
using namespace llvm::yaml;
DEBUG_WITH_TYPE("objtxt", llvm::dbgs() << "parse(" << keyword << "), _state="
<< stateName(_state) << "\n");
if ( _error )
return;
State savedState = _state;
for(const Transistion* t=_s_transistions; t->state != inError; ++t) {
if ( t->state != _state )
continue;
if ( ! keyword.equals(t->keyword) )
continue;
ParseScalar action = t->customAction;
this->moveToState(t->newState);
if ( ScalarNode *sc = llvm::dyn_cast<ScalarNode>(node) ) {
if ( action ) {
(*this.*action)(sc);
}
else {
_stream->printError(node, "unexpected scalar");
_error = make_error_code(yaml_reader_error::illegal_value);
}
}
else if ( SequenceNode *seq = llvm::dyn_cast<SequenceNode>(node) ) {
if ( action ) {
_stream->printError(node, "unexpected sequence");
_error = make_error_code(yaml_reader_error::illegal_value);
}
else {
for (Node &seqEntry : *seq ) {
this->parse(&seqEntry, StringRef("<any-seq-item>"));
if ( _error )
break;
}
}
}
else if ( MappingNode *map = llvm::dyn_cast<MappingNode>(node) ) {
if ( action ) {
_stream->printError(node, "unexpected map");
_error = make_error_code(yaml_reader_error::illegal_value);
}
else {
llvm::SmallString<32> storage;
for (auto &keyVal : *map) {
ScalarNode *keyScalar = llvm::dyn_cast<ScalarNode>(keyVal.getKey());
llvm::StringRef keyStr = keyScalar->getValue(storage);
this->parse(keyVal.getValue(), keyStr, keyScalar);
if ( _error )
break;
}
}
}
else {
_stream->printError(node, "unexpected node type");
_error = make_error_code(yaml_reader_error::illegal_value);
}
this->returnToState(savedState, node);
return;
}
switch (_state) {
case inAtom:
_stream->printError(keywordNode, "Unknown atom attribute '"
+ keyword + ":'");
break;
case inFixUp:
_stream->printError(keywordNode, "Unknown fixup attribute '"
+ keyword + ":'");
break;
case inDoc:
_stream->printError(keywordNode, "Unknown file attribute '"
+ keyword + ":'");
break;
default:
_stream->printError(keywordNode, "Unknown keyword '"
+ keyword + ":'");
}
_error = make_error_code(yaml_reader_error::illegal_value);
}
/// parseFile - Parse the specified YAML formatted MemoryBuffer
/// into lld::File object(s) and append each to the specified vector<File*>.
static error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
const ReaderOptionsYAML &options,
std::vector<std::unique_ptr<File>> &result) {
llvm::SourceMgr srcMgr;
llvm::yaml::Stream stream(mb->getBuffer(), srcMgr);
for (llvm::yaml::Document &d : stream) {
std::unique_ptr<yaml::YAMLFile> curFile(new yaml::YAMLFile);
if (llvm::isa<llvm::yaml::NullNode>(d.getRoot()))
continue; // Empty files are allowed.
yaml::YAMLState yamlState(options, &stream, curFile.get());
yamlState.parse(d.getRoot(), StringRef("<root>"));
if ( stream.failed() )
return make_error_code(yaml_reader_error::illegal_value);
if ( yamlState.error() )
return yamlState.error();
error_code ec = curFile->bindTargetReferences(stream);
if ( ec )
return ec;
result.emplace_back(curFile.release());
}
return make_error_code(yaml_reader_error::success);
}
} // namespace yaml
class ReaderYAML: public Reader {
public:
ReaderYAML(const ReaderOptionsYAML &options) : _options(options) {
}
error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
std::vector<std::unique_ptr<File>> &result) {
return lld::yaml::parseFile(mb, _options, result);
}
private:
const ReaderOptionsYAML &_options;
};
Reader* createReaderYAML(const ReaderOptionsYAML &options) {
return new ReaderYAML(options);
}
ReaderOptionsYAML::ReaderOptionsYAML() {
}
ReaderOptionsYAML::~ReaderOptionsYAML() {
}
} // namespace lld
|