summaryrefslogtreecommitdiffstats
path: root/lld/lib/Core/YamlKeyValues.cpp
blob: 19abf4ff12d4caf51cb81fd0df65aa2757d60a12 (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
//===- Core/YamlKeyValues.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 "llvm/Support/ErrorHandling.h"
#include "lld/Core/File.h"

#include <cstring>

namespace lld {
namespace yaml {


const char* const KeyValues::nameKeyword            = "name";
const char* const KeyValues::refNameKeyword         = "ref-name";
const char* const KeyValues::definitionKeyword      = "definition";
const char* const KeyValues::scopeKeyword           = "scope";
const char* const KeyValues::contentTypeKeyword     = "type";
const char* const KeyValues::deadStripKindKeyword   = "dead-strip";
const char* const KeyValues::sectionChoiceKeyword   = "section-choice";
const char* const KeyValues::interposableKeyword    = "interposable";
const char* const KeyValues::mergeKeyword           = "merge";
const char* const KeyValues::isThumbKeyword         = "is-thumb";
const char* const KeyValues::isAliasKeyword         = "is-alias";
const char* const KeyValues::sectionNameKeyword     = "section-name";
const char* const KeyValues::contentKeyword         = "content";
const char* const KeyValues::loadNameKeyword        = "load-name";
const char* const KeyValues::sizeKeyword            = "size";
const char* const KeyValues::valueKeyword           = "value";
const char* const KeyValues::fixupsKeyword          = "fixups";
const char* const KeyValues::permissionsKeyword     = "permissions";
const char* const KeyValues::canBeNullKeyword       = "can-be-null";
const char* const KeyValues::fixupsKindKeyword      = "kind";
const char* const KeyValues::fixupsOffsetKeyword    = "offset";
const char* const KeyValues::fixupsTargetKeyword    = "target";
const char* const KeyValues::fixupsAddendKeyword    = "addend";
const char* const KeyValues::fileAtomsKeyword       = "atoms";
const char* const KeyValues::fileKindKeyword        = "kind";
const char* const KeyValues::fileMembersKeyword     = "members";



const DefinedAtom::Definition         KeyValues::definitionDefault = Atom::definitionRegular;
const DefinedAtom::Scope              KeyValues::scopeDefault = DefinedAtom::scopeTranslationUnit;
const DefinedAtom::ContentType        KeyValues::contentTypeDefault = DefinedAtom::typeData;
const DefinedAtom::DeadStripKind      KeyValues::deadStripKindDefault = DefinedAtom::deadStripNormal;
const DefinedAtom::SectionChoice      KeyValues::sectionChoiceDefault = DefinedAtom::sectionBasedOnContent;
const DefinedAtom::Interposable       KeyValues::interposableDefault = DefinedAtom::interposeNo;
const DefinedAtom::Merge              KeyValues::mergeDefault = DefinedAtom::mergeNo;
const DefinedAtom::ContentPermissions KeyValues::permissionsDefault = DefinedAtom::permR__;
const bool                            KeyValues::isThumbDefault = false;
const bool                            KeyValues::isAliasDefault = false;
const UndefinedAtom::CanBeNull        KeyValues::canBeNullDefault = UndefinedAtom::canBeNullNever;
const File::Kind                      KeyValues::fileKindDefault = File::kindObject;


struct FileKindMapping {
  const char*       string;
  File::Kind        value;
};

static const FileKindMapping fileKindMappings[] = {
  { "object",         File::kindObject },
  { "archive",        File::kindArchiveLibrary },
  { "shared-library", File::kindSharedLibrary },
  { nullptr,          File::kindObject }
};

 File::Kind KeyValues::fileKind(const char* str) {
  for (const FileKindMapping* p = fileKindMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, str) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad file kind value");
}

const char* KeyValues::fileKind(File::Kind k) {
  for (const FileKindMapping* p = fileKindMappings; p->string != nullptr; ++p) {
    if ( p->value == k )
      return p->string;
  }
  llvm::report_fatal_error("bad file kind value");
}


struct DefinitionMapping {
  const char*       string;
  Atom::Definition  value;
};

static const DefinitionMapping defMappings[] = {
  { "regular",        Atom::definitionRegular },
  { "absolute",       Atom::definitionAbsolute },
  { "undefined",      Atom::definitionUndefined },
  { "shared-library", Atom::definitionSharedLibrary },
  { nullptr,          Atom::definitionRegular }
};

Atom::Definition KeyValues::definition(const char* s)
{
  for (const DefinitionMapping* p = defMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad definition value");
}

const char* KeyValues::definition(Atom::Definition s) {
  for (const DefinitionMapping* p = defMappings; p->string != nullptr; ++p) {
    if ( p->value == s )
      return p->string;
  }
  llvm::report_fatal_error("bad definition value");
}





struct ScopeMapping {
  const char* string;
  DefinedAtom::Scope value;
};

static const ScopeMapping scopeMappings[] = {
  { "global", DefinedAtom::scopeGlobal },
  { "hidden", DefinedAtom::scopeLinkageUnit },
  { "static", DefinedAtom::scopeTranslationUnit },
  { nullptr,  DefinedAtom::scopeGlobal }
};

DefinedAtom::Scope KeyValues::scope(const char* s)
{
  for (const ScopeMapping* p = scopeMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad scope value");
}

const char* KeyValues::scope(DefinedAtom::Scope s) {
  for (const ScopeMapping* p = scopeMappings; p->string != nullptr; ++p) {
    if ( p->value == s )
      return p->string;
  }
  llvm::report_fatal_error("bad scope value");
}








struct ContentTypeMapping {
  const char*       string;
  DefinedAtom::ContentType  value;
};

static const ContentTypeMapping typeMappings[] = {
  { "unknown",        DefinedAtom::typeUnknown },
  { "code",           DefinedAtom::typeCode },
  { "stub",           DefinedAtom::typeStub },
  { "stub-helper",    DefinedAtom::typeStubHelper },
  { "resolver",       DefinedAtom::typeResolver },
  { "constant",       DefinedAtom::typeConstant },
  { "c-string",       DefinedAtom::typeCString },
  { "utf16-string",   DefinedAtom::typeUTF16String },
  { "CFI",            DefinedAtom::typeCFI },
  { "LSDA",           DefinedAtom::typeLSDA },
  { "literal-4",      DefinedAtom::typeLiteral4 },
  { "literal-8",      DefinedAtom::typeLiteral8 },
  { "literal-16",     DefinedAtom::typeLiteral16 },
  { "data",           DefinedAtom::typeData },
  { "zero-fill",      DefinedAtom::typeZeroFill },
  { "cf-string",      DefinedAtom::typeCFString },
  { "got",            DefinedAtom::typeGOT },
  { "lazy-pointer",   DefinedAtom::typeLazyPointer },
  { "initializer-ptr",DefinedAtom::typeInitializerPtr },
  { "terminator-ptr", DefinedAtom::typeTerminatorPtr },
  { "c-string-ptr",   DefinedAtom::typeCStringPtr },
  { "objc1-class",    DefinedAtom::typeObjC1Class },
  { "objc1-class-ptr",DefinedAtom::typeObjCClassPtr },
  { "objc2-cat-ptr",  DefinedAtom::typeObjC2CategoryList },
  { "tlv-thunk",      DefinedAtom::typeThunkTLV },
  { "tlv-data",       DefinedAtom::typeTLVInitialData },
  { "tlv-zero-fill",  DefinedAtom::typeTLVInitialZeroFill },
  { "tlv-init-ptr",   DefinedAtom::typeTLVInitializerPtr },
  { nullptr,          DefinedAtom::typeUnknown }
};

DefinedAtom::ContentType KeyValues::contentType(const char* s)
{
  for (const ContentTypeMapping* p = typeMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad content type value");
}

const char* KeyValues::contentType(DefinedAtom::ContentType s) {
  for (const ContentTypeMapping* p = typeMappings; p->string != nullptr; ++p) {
    if ( p->value == s )
      return p->string;
  }
  llvm::report_fatal_error("bad content type value");
}







struct DeadStripMapping {
  const char*           string;
  DefinedAtom::DeadStripKind   value;
};

static const DeadStripMapping deadStripMappings[] = {
  { "normal",         DefinedAtom::deadStripNormal },
  { "never",          DefinedAtom::deadStripNever },
  { "always",         DefinedAtom::deadStripAlways },
  { nullptr,          DefinedAtom::deadStripNormal }
};

DefinedAtom::DeadStripKind KeyValues::deadStripKind(const char* s)
{
  for (const DeadStripMapping* p = deadStripMappings; p->string != nullptr; ++p)
  {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad dead strip value");
}

const char* KeyValues::deadStripKind(DefinedAtom::DeadStripKind dsk) {
  for (const DeadStripMapping* p = deadStripMappings; p->string != nullptr; ++p)
  {
    if ( p->value == dsk )
      return p->string;
  }
  llvm::report_fatal_error("bad dead strip value");
}





struct InterposableMapping {
  const char*           string;
  DefinedAtom::Interposable   value;
};

static const InterposableMapping interMappings[] = {
  { "no",           DefinedAtom::interposeNo },
  { "yes",          DefinedAtom::interposeYes },
  { "yesAndWeak",   DefinedAtom::interposeYesAndRuntimeWeak },
  { nullptr,        DefinedAtom::interposeNo }
};

DefinedAtom::Interposable KeyValues::interposable(const char* s)
{
  for (const InterposableMapping* p = interMappings; p->string != nullptr; ++p){
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad interposable value");
}

const char* KeyValues::interposable(DefinedAtom::Interposable in) {
  for (const InterposableMapping* p = interMappings; p->string != nullptr; ++p){
    if ( p->value == in )
      return p->string;
  }
  llvm::report_fatal_error("bad interposable value");
}






struct MergeMapping {
  const char*          string;
  DefinedAtom::Merge   value;
};

static const MergeMapping mergeMappings[] = {
  { "no",             DefinedAtom::mergeNo },
  { "asTentative",    DefinedAtom::mergeAsTentative },
  { "asWeak",         DefinedAtom::mergeAsWeak },
  { "asAddressedWeak",DefinedAtom::mergeAsWeakAndAddressUsed },
  { nullptr,          DefinedAtom::mergeNo }
};

DefinedAtom::Merge KeyValues::merge(const char* s)
{
  for (const MergeMapping* p = mergeMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad merge value");
}

const char* KeyValues::merge(DefinedAtom::Merge in) {
  for (const MergeMapping* p = mergeMappings; p->string != nullptr; ++p) {
    if ( p->value == in )
      return p->string;
  }
  llvm::report_fatal_error("bad merge value");
}






struct SectionChoiceMapping {
  const char*                 string;
  DefinedAtom::SectionChoice  value;
};

static const SectionChoiceMapping sectMappings[] = {
  { "content",         DefinedAtom::sectionBasedOnContent },
  { "custom",          DefinedAtom::sectionCustomPreferred },
  { "custom-required", DefinedAtom::sectionCustomRequired },
  { nullptr,           DefinedAtom::sectionBasedOnContent }
};

DefinedAtom::SectionChoice KeyValues::sectionChoice(const char* s)
{
  for (const SectionChoiceMapping* p = sectMappings; p->string != nullptr; ++p){
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad dead strip value");
}

const char* KeyValues::sectionChoice(DefinedAtom::SectionChoice s) {
  for (const SectionChoiceMapping* p = sectMappings; p->string != nullptr; ++p){
    if ( p->value == s )
      return p->string;
  }
  llvm::report_fatal_error("bad dead strip value");
}







struct PermissionsMapping {
  const char*                      string;
  DefinedAtom::ContentPermissions  value;
};

static const PermissionsMapping permMappings[] = {
  { "content",         DefinedAtom::perm___ },
  { "custom",          DefinedAtom::permR__ },
  { "custom-required", DefinedAtom::permR_X },
  { "custom-required", DefinedAtom::permRW_ },
  { "custom-required", DefinedAtom::permRW_L },
  { nullptr,           DefinedAtom::perm___ }
};

DefinedAtom::ContentPermissions KeyValues::permissions(const char* s)
{
  for (const PermissionsMapping* p = permMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad permissions value");
}

const char* KeyValues::permissions(DefinedAtom::ContentPermissions s) {
  for (const PermissionsMapping* p = permMappings; p->string != nullptr; ++p) {
    if ( p->value == s )
      return p->string;
  }
  llvm::report_fatal_error("bad permissions value");
}







bool KeyValues::isThumb(const char* s)
{
  if ( strcmp(s, "true") == 0 )
    return true;
  else if ( strcmp(s, "false") == 0 )
    return false;
  llvm::report_fatal_error("bad is-thumb value");
}

const char* KeyValues::isThumb(bool b) {
  return b ? "true" : "false";
}




bool KeyValues::isAlias(const char* s)
{
  if ( strcmp(s, "true") == 0 )
    return true;
  else if ( strcmp(s, "false") == 0 )
    return false;
  llvm::report_fatal_error("bad is-alias value");
}

const char* KeyValues::isAlias(bool b) {
  return b ? "true" : "false";
}




struct CanBeNullMapping {
  const char*               string;
  UndefinedAtom::CanBeNull  value;
};

static const CanBeNullMapping cbnMappings[] = {
  { "never",         UndefinedAtom::canBeNullNever },
  { "at-runtime",    UndefinedAtom::canBeNullAtRuntime },
  { "at-buildtime",  UndefinedAtom::canBeNullAtBuildtime },
  { nullptr,         UndefinedAtom::canBeNullNever }
};


UndefinedAtom::CanBeNull KeyValues::canBeNull(const char* s)
{
  for (const CanBeNullMapping* p = cbnMappings; p->string != nullptr; ++p) {
    if ( strcmp(p->string, s) == 0 )
      return p->value;
  }
  llvm::report_fatal_error("bad can-be-null value");
}

const char* KeyValues::canBeNull(UndefinedAtom::CanBeNull c) {
  for (const CanBeNullMapping* p = cbnMappings; p->string != nullptr; ++p) {
    if ( p->value == c )
      return p->string;
  }
  llvm::report_fatal_error("bad can-be-null value");
}







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