summaryrefslogtreecommitdiffstats
path: root/lld/lib/Core/YamlReader.cpp
blob: 6af7111b20ddbf276581479e2de775c1d199d6af (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
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
//===- 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 "lld/Core/YamlReader.h"
#include "YamlKeyValues.h"
#include "lld/Core/Atom.h"
#include "lld/Core/AbsoluteAtom.h"
#include "lld/Core/Error.h"
#include "lld/Core/File.h"
#include "lld/Core/LLVM.h"
#include "lld/Core/Platform.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/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/system_error.h"

#include <cstring>
#include <vector>



namespace lld {
namespace yaml {

namespace {

class YAML {
public:
  struct Entry {
    Entry(const char *k, const char *v, std::vector<uint8_t>* vs,
          int d, bool bd, bool bs)
      : key(strdup(k))
      , value(v ? strdup(v) : nullptr)
      , valueSequenceBytes(vs)
      , depth(d)
      , beginSequence(bs)
      , beginDocument(bd) {}

    const char *          key;
    const char *          value;
    std::vector<uint8_t>* valueSequenceBytes;
    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 = nullptr;
  unsigned int lineNumber = 1;
  int depth = 0;
  bool nextKeyIsStartOfDocument = false;
  bool nextKeyIsStartOfSequence = false;
  std::vector<uint8_t>* sequenceBytes = nullptr;
  unsigned contentByte = 0;
  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 == '_') || (c == '/')) {
        p = &value[0];
        *p++ = c;
        state = inValue;
      } else if (c == '\n') {
        entries.push_back(new Entry(key, "", nullptr, depth,
                                    nextKeyIsStartOfDocument,
                                    nextKeyIsStartOfSequence));
        nextKeyIsStartOfSequence = false;
        nextKeyIsStartOfDocument = false;
        state = inDocument;
        depth = 0;
      } else if (c == '[') {
        contentByte = 0;
        sequenceBytes = new std::vector<uint8_t>();
        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 (c == '\n') {
        *p = '\0';
        entries.push_back(new Entry(key, value, nullptr, depth,
                                    nextKeyIsStartOfDocument,
                                    nextKeyIsStartOfSequence));
        nextKeyIsStartOfSequence = false;
        nextKeyIsStartOfDocument = false;
        state = inDocument;
        depth = 0;
      }
      else {
        *p++ = c;
      }
      break;
    case inValueSequence:
      if (c == ']') {
        sequenceBytes->push_back(contentByte);
        state = inValueSequenceEnd;
      }
      else if ( (c == ' ') || (c == '\n') ) {
        // eat white space
      }
      else if (c == ',') {
        sequenceBytes->push_back(contentByte);
      }
      else if ( isdigit(c) ) {
        contentByte = (contentByte << 4) | (c-'0');
      }
      else if ( ('a' <= tolower(c)) && (tolower(c) <= 'f') ) {
        contentByte = (contentByte << 4) | (tolower(c)-'a'+10);
      }
      else {
        llvm::report_fatal_error("non-hex digit found in content [ ]");
      }
      break;
    case inValueSequenceEnd:
      if (c == '\n') {
        entries.push_back(new Entry(key, nullptr, sequenceBytes, depth,
                                    nextKeyIsStartOfDocument,
                                    nextKeyIsStartOfSequence));
        nextKeyIsStartOfSequence = false;
        nextKeyIsStartOfDocument = false;
        state = inDocument;
        depth = 0;
      }
      break;
    }
  }
}



class YAMLReference : public Reference {
public:
                YAMLReference() : _target(nullptr), _targetName(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;
  }

  const Atom*  _target;
  const char*  _targetName;
  uint64_t     _offsetInAtom;
  Addend       _addend;
  Kind         _kind;
};



class YAMLDefinedAtom;

class YAMLFile : public File {
public:
  YAMLFile()
    : File("path")
    , _lastRefIndex(0) {}

  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");
  }

  void bindTargetReferences();
  void addDefinedAtom(YAMLDefinedAtom* atom, const char* refName);
  void addUndefinedAtom(UndefinedAtom* atom);
  void addSharedLibraryAtom(SharedLibraryAtom* atom);
  void addAbsoluteAtom(AbsoluteAtom* atom);
  Atom* findAtom(const char* name);

  struct NameAtomPair {
                 NameAtomPair(const char* n, Atom* a) : name(n), atom(a) {}
    const char*  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;
  unsigned int                                _lastRefIndex;
};



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
          , const char* name
          , const char* 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 {
    if (_name == nullptr)
      return StringRef();
    else
      return _name;
  }

 virtual uint64_t size() const {
    return (_content ? _content->size() : _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 {
    if (_content != nullptr)
      return ArrayRef<uint8_t>(*_content);
    else
      return ArrayRef<uint8_t>();
  }

  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);
  }



  void bindTargetReferences() const {
    for (unsigned int i=_refStartIndex; i < _refEndIndex; ++i) {
      const char* targetName = _file._references[i]._targetName;
      Atom* targetAtom = _file.findAtom(targetName);
      _file._references[i]._target = targetAtom;
    }
  }

private:
  YAMLFile&                   _file;
  const char *                _name;
  const char *                _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;
};


class YAMLUndefinedAtom : public UndefinedAtom {
public:
        YAMLUndefinedAtom(YAMLFile& f, int32_t ord, const char* nm,
                          UndefinedAtom::CanBeNull cbn)
            : _file(f), _name(nm), _ordinal(ord), _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;
  const char *                _name;
  uint32_t                    _ordinal;
  UndefinedAtom::CanBeNull     _canBeNull;
};



class YAMLSharedLibraryAtom : public SharedLibraryAtom {
public:
        YAMLSharedLibraryAtom(YAMLFile& f, int32_t ord, const char* nm,
                                const char* ldnm, bool cbn)
            : _file(f), _name(nm), _ordinal(ord),
              _loadName(ldnm), _canBeNull(cbn) { }

  virtual const class File& file() const {
    return _file;
  }

  virtual StringRef name() const {
    return _name;
  }

  virtual StringRef loadName() const {
    if ( _loadName == nullptr )
      return StringRef();
    else
      return StringRef(_loadName);
  }

  virtual bool canBeNullAtRuntime() const {
    return _canBeNull;
  }


private:
  YAMLFile&                   _file;
  const char *                _name;
  uint32_t                    _ordinal;
  const char *                _loadName;
  bool                        _canBeNull;
};



class YAMLAbsoluteAtom : public AbsoluteAtom {
public:
        YAMLAbsoluteAtom(YAMLFile& f, int32_t ord, const char* nm, uint64_t v)
            : _file(f), _name(nm), _ordinal(ord), _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;
  const char *     _name;
  uint32_t         _ordinal;
  uint64_t         _value;
};



void YAMLFile::bindTargetReferences() {
    for (const DefinedAtom *defAtom : _definedAtoms) {
      const YAMLDefinedAtom* atom = 
                          reinterpret_cast<const YAMLDefinedAtom*>(defAtom);
      atom->bindTargetReferences();
    }
}

Atom* YAMLFile::findAtom(const char* name) {
  for (std::vector<NameAtomPair>::const_iterator it = _nameToAtomMapping.begin();
                                    it != _nameToAtomMapping.end(); ++it) {
    if ( strcmp(name, it->name) == 0 )
      return it->atom;
  }
  llvm::report_fatal_error("reference to atom that does not exist");
}

void YAMLFile::addDefinedAtom(YAMLDefinedAtom* atom, const char* refName) {
  _definedAtoms._atoms.push_back(atom);
  assert(refName != nullptr);
  _nameToAtomMapping.push_back(NameAtomPair(refName, atom));
}

void YAMLFile::addUndefinedAtom(UndefinedAtom* atom) {
  _undefinedAtoms._atoms.push_back(atom);
  _nameToAtomMapping.push_back(NameAtomPair(atom->name().data(), atom));
}

void YAMLFile::addSharedLibraryAtom(SharedLibraryAtom* atom) {
  _sharedLibraryAtoms._atoms.push_back(atom);
  _nameToAtomMapping.push_back(NameAtomPair(atom->name().data(), atom));
}

void YAMLFile::addAbsoluteAtom(AbsoluteAtom* atom) {
  _absoluteAtoms._atoms.push_back(atom);
  _nameToAtomMapping.push_back(NameAtomPair(atom->name().data(), atom));
}


class YAMLAtomState {
public:
  YAMLAtomState(Platform& platform);

  void setName(const char *n);
  void setRefName(const char *n);
  void setAlign2(const char *n);

  void setFixupKind(const char *n);
  void setFixupTarget(const char *n);
  void addFixup(YAMLFile *f);

  void makeAtom(YAMLFile&);

  Platform&                   _platform;
  const char *                _name;
  const char *                _refName;
  const char *                _sectionName;
  const char*                 _loadName;
  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;
};


YAMLAtomState::YAMLAtomState(Platform& platform)
  : _platform(platform)
  , _name(nullptr)
  , _refName(nullptr)
  , _sectionName(nullptr)
  , _loadName(nullptr)
  , _size(0)
  , _value(0)
  , _ordinal(0)
  , _content(nullptr)
  , _alignment(0, 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)
  {
  }


void YAMLAtomState::makeAtom(YAMLFile& f) {
  if ( _definition == Atom::definitionRegular ) {
    YAMLDefinedAtom *a = new YAMLDefinedAtom(_ordinal, f, _scope, _type,
                          _sectionChoice, _interpose, _merge, _deadStrip,
                          _permissions, _isThumb, _isAlias,
                          _alignment, _name, _sectionName, _size, _content);
    f.addDefinedAtom(a, _refName ? _refName : _name);
    ++_ordinal;
  }
  else if ( _definition == Atom::definitionUndefined ) {
    UndefinedAtom *a = new YAMLUndefinedAtom(f, _ordinal, _name, _canBeNull);
    f.addUndefinedAtom(a);
    ++_ordinal;
  }
  else if ( _definition == Atom::definitionSharedLibrary ) {
    bool nullable = (_canBeNull == UndefinedAtom::canBeNullAtRuntime);
    SharedLibraryAtom *a = new YAMLSharedLibraryAtom(f, _ordinal, _name,
                                                      _loadName, nullable);
    f.addSharedLibraryAtom(a);
    ++_ordinal;
  }
   else if ( _definition == Atom::definitionAbsolute ) {
    AbsoluteAtom *a = new YAMLAbsoluteAtom(f, _ordinal, _name, _value);
    f.addAbsoluteAtom(a);
    ++_ordinal;
  }

  // reset state for next atom
  _name             = nullptr;
  _refName          = nullptr;
  _sectionName      = nullptr;
  _loadName         = nullptr;
  _size             = 0;
  _value            = 0;
  _ordinal          = 0;
  _content          = nullptr;
  _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._targetName   = nullptr;
  _ref._addend       = 0;
  _ref._offsetInAtom = 0;
  _ref._kind         = 0;
}

void YAMLAtomState::setName(const char *n) {
  _name = n;
}

void YAMLAtomState::setRefName(const char *n) {
  _refName = n;
}

void YAMLAtomState::setAlign2(const char *s) {
  if (StringRef(s).getAsInteger(10, _alignment.powerOf2))
    _alignment.powerOf2 = 1;
}

void YAMLAtomState::setFixupKind(const char *s) {
  _ref._kind = _platform.kindFromString(StringRef(s));
}

void YAMLAtomState::setFixupTarget(const char *s) {
  _ref._targetName = s;
}


void YAMLAtomState::addFixup(YAMLFile *f) {
  f->_references.push_back(_ref);
  // clear for next ref
  _ref._target       = nullptr;
  _ref._targetName   = nullptr;
  _ref._addend       = 0;
  _ref._offsetInAtom = 0;
  _ref._kind         = 0;
}


} // anonymous namespace





/// parseObjectText - Parse the specified YAML formatted MemoryBuffer
/// into lld::File object(s) and append each to the specified vector<File*>.
error_code parseObjectText( llvm::MemoryBuffer *mb
                          , Platform& platform
                          , std::vector<const File *> &result) {
  std::vector<const YAML::Entry *> entries;
  YAML::parse(mb, entries);

  YAMLFile *file = nullptr;
  YAMLAtomState atomState(platform);
  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 != nullptr) {
        if (haveAtom) {
          atomState.makeAtom(*file);
          haveAtom = false;
        }
        file->bindTargetReferences();
        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::refNameKeyword) == 0) {
          atomState.setRefName(entry->value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::definitionKeyword) == 0) {
          atomState._definition = 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::mergeKeyword) == 0) {
          atomState._merge = KeyValues::merge(entry->value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::interposableKeyword) == 0) {
          atomState._interpose = KeyValues::interposable(entry->value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::isThumbKeyword) == 0) {
          atomState._isThumb = KeyValues::isThumb(entry->value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::isAliasKeyword) == 0) {
          atomState._isAlias = KeyValues::isAlias(entry->value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::canBeNullKeyword) == 0) {
          atomState._canBeNull = KeyValues::canBeNull(entry->value);
          if ( atomState._definition == Atom::definitionSharedLibrary ) {
            if ( atomState._canBeNull == UndefinedAtom::canBeNullAtBuildtime )
              return make_error_code(yaml_reader_error::illegal_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) {
          StringRef val = entry->value;
          if (val.getAsInteger(0, atomState._size))
            return make_error_code(yaml_reader_error::illegal_value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::contentKeyword) == 0) {
          atomState._content = entry->valueSequenceBytes;
          haveAtom = true;
        }
        else if (strcmp(entry->key, "align2") == 0) {
          atomState.setAlign2(entry->value);
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::fixupsKeyword) == 0) {
          inFixups = true;
        }
        else if (strcmp(entry->key, KeyValues::loadNameKeyword) == 0) {
          atomState._loadName = entry->value;
          haveAtom = true;
        }
        else if (strcmp(entry->key, KeyValues::valueKeyword) == 0) {
          llvm::APInt Val;
          StringRef(entry->value).getAsInteger(0, Val);
          atomState._value = Val.getZExtValue();
          haveAtom = true;
        }
        else {
          return make_error_code(yaml_reader_error::unknown_keyword);
        }
      }
      else if (depthForFixups == entry->depth) {
        if (entry->beginSequence) {
          if (haveFixup) {
            atomState.addFixup(file);
            haveFixup = false;
          }
        }
        if (strcmp(entry->key, KeyValues::fixupsKindKeyword) == 0) {
          atomState.setFixupKind(entry->value);
          haveFixup = true;
        }
        else if (strcmp(entry->key, KeyValues::fixupsOffsetKeyword) == 0) {
          if (StringRef(entry->value).getAsInteger(0,
               atomState._ref._offsetInAtom))
            return make_error_code(yaml_reader_error::illegal_value);
          haveFixup = true;
        }
        else if (strcmp(entry->key, KeyValues::fixupsTargetKeyword) == 0) {
          atomState.setFixupTarget(entry->value);
          haveFixup = true;
        }
        else if (strcmp(entry->key, KeyValues::fixupsAddendKeyword) == 0) {
          StringRef Addend(entry->value);
          if (Addend.getAsInteger(0, atomState._ref._addend))
            return make_error_code(yaml_reader_error::illegal_value);
          haveFixup = true;
        }
      }
    }
    lastDepth = entry->depth;
  }
  if (haveAtom) {
    atomState.makeAtom(*file);
  }
  if (file != nullptr) {
    file->bindTargetReferences();
    result.push_back(file);
  }
  return make_error_code(yaml_reader_error::success);
}


//
// Fill in vector<File*> from path to input text file.
//
error_code parseObjectTextFileOrSTDIN( StringRef path
                                     , Platform&  platform
                                     , std::vector<const File*>& result) {
  OwningPtr<llvm::MemoryBuffer> mb;
  if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(path, mb))
    return ec;

  return parseObjectText(mb.get(), platform, result);
}

} // namespace yaml
} // namespace lld
OpenPOWER on IntegriCloud