File: | build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include/llvm/ObjectYAML/ELFYAML.h |
Warning: | line 55, column 1 The left operand of '==' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file defines classes for handling the YAML representation of ELF. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "llvm/ObjectYAML/ELFYAML.h" | |||
14 | #include "llvm/ADT/APInt.h" | |||
15 | #include "llvm/ADT/MapVector.h" | |||
16 | #include "llvm/ADT/StringRef.h" | |||
17 | #include "llvm/BinaryFormat/ELF.h" | |||
18 | #include "llvm/Support/ARMEHABI.h" | |||
19 | #include "llvm/Support/Casting.h" | |||
20 | #include "llvm/Support/ErrorHandling.h" | |||
21 | #include "llvm/Support/MipsABIFlags.h" | |||
22 | #include "llvm/Support/YAMLTraits.h" | |||
23 | #include "llvm/Support/WithColor.h" | |||
24 | #include <cassert> | |||
25 | #include <cstdint> | |||
26 | ||||
27 | namespace llvm { | |||
28 | ||||
29 | ELFYAML::Chunk::~Chunk() = default; | |||
30 | ||||
31 | namespace ELFYAML { | |||
32 | ELF_ELFOSABI Object::getOSAbi() const { return Header.OSABI; } | |||
33 | ||||
34 | unsigned Object::getMachine() const { | |||
35 | if (Header.Machine) | |||
36 | return *Header.Machine; | |||
37 | return llvm::ELF::EM_NONE; | |||
38 | } | |||
39 | ||||
40 | constexpr StringRef SectionHeaderTable::TypeStr; | |||
41 | } // namespace ELFYAML | |||
42 | ||||
43 | namespace yaml { | |||
44 | ||||
45 | void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration( | |||
46 | IO &IO, ELFYAML::ELF_ET &Value) { | |||
47 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
48 | ECase(ET_NONE); | |||
49 | ECase(ET_REL); | |||
50 | ECase(ET_EXEC); | |||
51 | ECase(ET_DYN); | |||
52 | ECase(ET_CORE); | |||
53 | #undef ECase | |||
54 | IO.enumFallback<Hex16>(Value); | |||
55 | } | |||
56 | ||||
57 | void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration( | |||
58 | IO &IO, ELFYAML::ELF_PT &Value) { | |||
59 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
60 | ECase(PT_NULL); | |||
61 | ECase(PT_LOAD); | |||
62 | ECase(PT_DYNAMIC); | |||
63 | ECase(PT_INTERP); | |||
64 | ECase(PT_NOTE); | |||
65 | ECase(PT_SHLIB); | |||
66 | ECase(PT_PHDR); | |||
67 | ECase(PT_TLS); | |||
68 | ECase(PT_GNU_EH_FRAME); | |||
69 | ECase(PT_GNU_STACK); | |||
70 | ECase(PT_GNU_RELRO); | |||
71 | ECase(PT_GNU_PROPERTY); | |||
72 | #undef ECase | |||
73 | IO.enumFallback<Hex32>(Value); | |||
74 | } | |||
75 | ||||
76 | void ScalarEnumerationTraits<ELFYAML::ELF_NT>::enumeration( | |||
77 | IO &IO, ELFYAML::ELF_NT &Value) { | |||
78 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
79 | // Generic note types. | |||
80 | ECase(NT_VERSION); | |||
81 | ECase(NT_ARCH); | |||
82 | ECase(NT_GNU_BUILD_ATTRIBUTE_OPEN); | |||
83 | ECase(NT_GNU_BUILD_ATTRIBUTE_FUNC); | |||
84 | // Core note types. | |||
85 | ECase(NT_PRSTATUS); | |||
86 | ECase(NT_FPREGSET); | |||
87 | ECase(NT_PRPSINFO); | |||
88 | ECase(NT_TASKSTRUCT); | |||
89 | ECase(NT_AUXV); | |||
90 | ECase(NT_PSTATUS); | |||
91 | ECase(NT_FPREGS); | |||
92 | ECase(NT_PSINFO); | |||
93 | ECase(NT_LWPSTATUS); | |||
94 | ECase(NT_LWPSINFO); | |||
95 | ECase(NT_WIN32PSTATUS); | |||
96 | ECase(NT_PPC_VMX); | |||
97 | ECase(NT_PPC_VSX); | |||
98 | ECase(NT_PPC_TAR); | |||
99 | ECase(NT_PPC_PPR); | |||
100 | ECase(NT_PPC_DSCR); | |||
101 | ECase(NT_PPC_EBB); | |||
102 | ECase(NT_PPC_PMU); | |||
103 | ECase(NT_PPC_TM_CGPR); | |||
104 | ECase(NT_PPC_TM_CFPR); | |||
105 | ECase(NT_PPC_TM_CVMX); | |||
106 | ECase(NT_PPC_TM_CVSX); | |||
107 | ECase(NT_PPC_TM_SPR); | |||
108 | ECase(NT_PPC_TM_CTAR); | |||
109 | ECase(NT_PPC_TM_CPPR); | |||
110 | ECase(NT_PPC_TM_CDSCR); | |||
111 | ECase(NT_386_TLS); | |||
112 | ECase(NT_386_IOPERM); | |||
113 | ECase(NT_X86_XSTATE); | |||
114 | ECase(NT_S390_HIGH_GPRS); | |||
115 | ECase(NT_S390_TIMER); | |||
116 | ECase(NT_S390_TODCMP); | |||
117 | ECase(NT_S390_TODPREG); | |||
118 | ECase(NT_S390_CTRS); | |||
119 | ECase(NT_S390_PREFIX); | |||
120 | ECase(NT_S390_LAST_BREAK); | |||
121 | ECase(NT_S390_SYSTEM_CALL); | |||
122 | ECase(NT_S390_TDB); | |||
123 | ECase(NT_S390_VXRS_LOW); | |||
124 | ECase(NT_S390_VXRS_HIGH); | |||
125 | ECase(NT_S390_GS_CB); | |||
126 | ECase(NT_S390_GS_BC); | |||
127 | ECase(NT_ARM_VFP); | |||
128 | ECase(NT_ARM_TLS); | |||
129 | ECase(NT_ARM_HW_BREAK); | |||
130 | ECase(NT_ARM_HW_WATCH); | |||
131 | ECase(NT_ARM_SVE); | |||
132 | ECase(NT_ARM_PAC_MASK); | |||
133 | ECase(NT_FILE); | |||
134 | ECase(NT_PRXFPREG); | |||
135 | ECase(NT_SIGINFO); | |||
136 | // LLVM-specific notes. | |||
137 | ECase(NT_LLVM_HWASAN_GLOBALS); | |||
138 | // GNU note types | |||
139 | ECase(NT_GNU_ABI_TAG); | |||
140 | ECase(NT_GNU_HWCAP); | |||
141 | ECase(NT_GNU_BUILD_ID); | |||
142 | ECase(NT_GNU_GOLD_VERSION); | |||
143 | ECase(NT_GNU_PROPERTY_TYPE_0); | |||
144 | // FreeBSD note types. | |||
145 | ECase(NT_FREEBSD_ABI_TAG); | |||
146 | ECase(NT_FREEBSD_NOINIT_TAG); | |||
147 | ECase(NT_FREEBSD_ARCH_TAG); | |||
148 | ECase(NT_FREEBSD_FEATURE_CTL); | |||
149 | // FreeBSD core note types. | |||
150 | ECase(NT_FREEBSD_THRMISC); | |||
151 | ECase(NT_FREEBSD_PROCSTAT_PROC); | |||
152 | ECase(NT_FREEBSD_PROCSTAT_FILES); | |||
153 | ECase(NT_FREEBSD_PROCSTAT_VMMAP); | |||
154 | ECase(NT_FREEBSD_PROCSTAT_GROUPS); | |||
155 | ECase(NT_FREEBSD_PROCSTAT_UMASK); | |||
156 | ECase(NT_FREEBSD_PROCSTAT_RLIMIT); | |||
157 | ECase(NT_FREEBSD_PROCSTAT_OSREL); | |||
158 | ECase(NT_FREEBSD_PROCSTAT_PSSTRINGS); | |||
159 | ECase(NT_FREEBSD_PROCSTAT_AUXV); | |||
160 | // NetBSD core note types. | |||
161 | ECase(NT_NETBSDCORE_PROCINFO); | |||
162 | ECase(NT_NETBSDCORE_AUXV); | |||
163 | ECase(NT_NETBSDCORE_LWPSTATUS); | |||
164 | // OpenBSD core note types. | |||
165 | ECase(NT_OPENBSD_PROCINFO); | |||
166 | ECase(NT_OPENBSD_AUXV); | |||
167 | ECase(NT_OPENBSD_REGS); | |||
168 | ECase(NT_OPENBSD_FPREGS); | |||
169 | ECase(NT_OPENBSD_XFPREGS); | |||
170 | ECase(NT_OPENBSD_WCOOKIE); | |||
171 | // AMD specific notes. (Code Object V2) | |||
172 | ECase(NT_AMD_HSA_CODE_OBJECT_VERSION); | |||
173 | ECase(NT_AMD_HSA_HSAIL); | |||
174 | ECase(NT_AMD_HSA_ISA_VERSION); | |||
175 | ECase(NT_AMD_HSA_METADATA); | |||
176 | ECase(NT_AMD_HSA_ISA_NAME); | |||
177 | ECase(NT_AMD_PAL_METADATA); | |||
178 | // AMDGPU specific notes. (Code Object V3) | |||
179 | ECase(NT_AMDGPU_METADATA); | |||
180 | // Android specific notes. | |||
181 | ECase(NT_ANDROID_TYPE_IDENT); | |||
182 | ECase(NT_ANDROID_TYPE_KUSER); | |||
183 | ECase(NT_ANDROID_TYPE_MEMTAG); | |||
184 | #undef ECase | |||
185 | IO.enumFallback<Hex32>(Value); | |||
186 | } | |||
187 | ||||
188 | void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration( | |||
189 | IO &IO, ELFYAML::ELF_EM &Value) { | |||
190 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
191 | ECase(EM_NONE); | |||
192 | ECase(EM_M32); | |||
193 | ECase(EM_SPARC); | |||
194 | ECase(EM_386); | |||
195 | ECase(EM_68K); | |||
196 | ECase(EM_88K); | |||
197 | ECase(EM_IAMCU); | |||
198 | ECase(EM_860); | |||
199 | ECase(EM_MIPS); | |||
200 | ECase(EM_S370); | |||
201 | ECase(EM_MIPS_RS3_LE); | |||
202 | ECase(EM_PARISC); | |||
203 | ECase(EM_VPP500); | |||
204 | ECase(EM_SPARC32PLUS); | |||
205 | ECase(EM_960); | |||
206 | ECase(EM_PPC); | |||
207 | ECase(EM_PPC64); | |||
208 | ECase(EM_S390); | |||
209 | ECase(EM_SPU); | |||
210 | ECase(EM_V800); | |||
211 | ECase(EM_FR20); | |||
212 | ECase(EM_RH32); | |||
213 | ECase(EM_RCE); | |||
214 | ECase(EM_ARM); | |||
215 | ECase(EM_ALPHA); | |||
216 | ECase(EM_SH); | |||
217 | ECase(EM_SPARCV9); | |||
218 | ECase(EM_TRICORE); | |||
219 | ECase(EM_ARC); | |||
220 | ECase(EM_H8_300); | |||
221 | ECase(EM_H8_300H); | |||
222 | ECase(EM_H8S); | |||
223 | ECase(EM_H8_500); | |||
224 | ECase(EM_IA_64); | |||
225 | ECase(EM_MIPS_X); | |||
226 | ECase(EM_COLDFIRE); | |||
227 | ECase(EM_68HC12); | |||
228 | ECase(EM_MMA); | |||
229 | ECase(EM_PCP); | |||
230 | ECase(EM_NCPU); | |||
231 | ECase(EM_NDR1); | |||
232 | ECase(EM_STARCORE); | |||
233 | ECase(EM_ME16); | |||
234 | ECase(EM_ST100); | |||
235 | ECase(EM_TINYJ); | |||
236 | ECase(EM_X86_64); | |||
237 | ECase(EM_PDSP); | |||
238 | ECase(EM_PDP10); | |||
239 | ECase(EM_PDP11); | |||
240 | ECase(EM_FX66); | |||
241 | ECase(EM_ST9PLUS); | |||
242 | ECase(EM_ST7); | |||
243 | ECase(EM_68HC16); | |||
244 | ECase(EM_68HC11); | |||
245 | ECase(EM_68HC08); | |||
246 | ECase(EM_68HC05); | |||
247 | ECase(EM_SVX); | |||
248 | ECase(EM_ST19); | |||
249 | ECase(EM_VAX); | |||
250 | ECase(EM_CRIS); | |||
251 | ECase(EM_JAVELIN); | |||
252 | ECase(EM_FIREPATH); | |||
253 | ECase(EM_ZSP); | |||
254 | ECase(EM_MMIX); | |||
255 | ECase(EM_HUANY); | |||
256 | ECase(EM_PRISM); | |||
257 | ECase(EM_AVR); | |||
258 | ECase(EM_FR30); | |||
259 | ECase(EM_D10V); | |||
260 | ECase(EM_D30V); | |||
261 | ECase(EM_V850); | |||
262 | ECase(EM_M32R); | |||
263 | ECase(EM_MN10300); | |||
264 | ECase(EM_MN10200); | |||
265 | ECase(EM_PJ); | |||
266 | ECase(EM_OPENRISC); | |||
267 | ECase(EM_ARC_COMPACT); | |||
268 | ECase(EM_XTENSA); | |||
269 | ECase(EM_VIDEOCORE); | |||
270 | ECase(EM_TMM_GPP); | |||
271 | ECase(EM_NS32K); | |||
272 | ECase(EM_TPC); | |||
273 | ECase(EM_SNP1K); | |||
274 | ECase(EM_ST200); | |||
275 | ECase(EM_IP2K); | |||
276 | ECase(EM_MAX); | |||
277 | ECase(EM_CR); | |||
278 | ECase(EM_F2MC16); | |||
279 | ECase(EM_MSP430); | |||
280 | ECase(EM_BLACKFIN); | |||
281 | ECase(EM_SE_C33); | |||
282 | ECase(EM_SEP); | |||
283 | ECase(EM_ARCA); | |||
284 | ECase(EM_UNICORE); | |||
285 | ECase(EM_EXCESS); | |||
286 | ECase(EM_DXP); | |||
287 | ECase(EM_ALTERA_NIOS2); | |||
288 | ECase(EM_CRX); | |||
289 | ECase(EM_XGATE); | |||
290 | ECase(EM_C166); | |||
291 | ECase(EM_M16C); | |||
292 | ECase(EM_DSPIC30F); | |||
293 | ECase(EM_CE); | |||
294 | ECase(EM_M32C); | |||
295 | ECase(EM_TSK3000); | |||
296 | ECase(EM_RS08); | |||
297 | ECase(EM_SHARC); | |||
298 | ECase(EM_ECOG2); | |||
299 | ECase(EM_SCORE7); | |||
300 | ECase(EM_DSP24); | |||
301 | ECase(EM_VIDEOCORE3); | |||
302 | ECase(EM_LATTICEMICO32); | |||
303 | ECase(EM_SE_C17); | |||
304 | ECase(EM_TI_C6000); | |||
305 | ECase(EM_TI_C2000); | |||
306 | ECase(EM_TI_C5500); | |||
307 | ECase(EM_MMDSP_PLUS); | |||
308 | ECase(EM_CYPRESS_M8C); | |||
309 | ECase(EM_R32C); | |||
310 | ECase(EM_TRIMEDIA); | |||
311 | ECase(EM_HEXAGON); | |||
312 | ECase(EM_8051); | |||
313 | ECase(EM_STXP7X); | |||
314 | ECase(EM_NDS32); | |||
315 | ECase(EM_ECOG1); | |||
316 | ECase(EM_ECOG1X); | |||
317 | ECase(EM_MAXQ30); | |||
318 | ECase(EM_XIMO16); | |||
319 | ECase(EM_MANIK); | |||
320 | ECase(EM_CRAYNV2); | |||
321 | ECase(EM_RX); | |||
322 | ECase(EM_METAG); | |||
323 | ECase(EM_MCST_ELBRUS); | |||
324 | ECase(EM_ECOG16); | |||
325 | ECase(EM_CR16); | |||
326 | ECase(EM_ETPU); | |||
327 | ECase(EM_SLE9X); | |||
328 | ECase(EM_L10M); | |||
329 | ECase(EM_K10M); | |||
330 | ECase(EM_AARCH64); | |||
331 | ECase(EM_AVR32); | |||
332 | ECase(EM_STM8); | |||
333 | ECase(EM_TILE64); | |||
334 | ECase(EM_TILEPRO); | |||
335 | ECase(EM_MICROBLAZE); | |||
336 | ECase(EM_CUDA); | |||
337 | ECase(EM_TILEGX); | |||
338 | ECase(EM_CLOUDSHIELD); | |||
339 | ECase(EM_COREA_1ST); | |||
340 | ECase(EM_COREA_2ND); | |||
341 | ECase(EM_ARC_COMPACT2); | |||
342 | ECase(EM_OPEN8); | |||
343 | ECase(EM_RL78); | |||
344 | ECase(EM_VIDEOCORE5); | |||
345 | ECase(EM_78KOR); | |||
346 | ECase(EM_56800EX); | |||
347 | ECase(EM_AMDGPU); | |||
348 | ECase(EM_RISCV); | |||
349 | ECase(EM_LANAI); | |||
350 | ECase(EM_BPF); | |||
351 | ECase(EM_VE); | |||
352 | ECase(EM_CSKY); | |||
353 | ECase(EM_LOONGARCH); | |||
354 | #undef ECase | |||
355 | IO.enumFallback<Hex16>(Value); | |||
356 | } | |||
357 | ||||
358 | void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration( | |||
359 | IO &IO, ELFYAML::ELF_ELFCLASS &Value) { | |||
360 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
361 | // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it | |||
362 | // here. | |||
363 | ECase(ELFCLASS32); | |||
364 | ECase(ELFCLASS64); | |||
365 | #undef ECase | |||
366 | } | |||
367 | ||||
368 | void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration( | |||
369 | IO &IO, ELFYAML::ELF_ELFDATA &Value) { | |||
370 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
371 | // ELFDATANONE is an invalid data encoding, but we accept it because | |||
372 | // we want to be able to produce invalid binaries for the tests. | |||
373 | ECase(ELFDATANONE); | |||
374 | ECase(ELFDATA2LSB); | |||
375 | ECase(ELFDATA2MSB); | |||
376 | #undef ECase | |||
377 | } | |||
378 | ||||
379 | void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration( | |||
380 | IO &IO, ELFYAML::ELF_ELFOSABI &Value) { | |||
381 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
382 | ECase(ELFOSABI_NONE); | |||
383 | ECase(ELFOSABI_HPUX); | |||
384 | ECase(ELFOSABI_NETBSD); | |||
385 | ECase(ELFOSABI_GNU); | |||
386 | ECase(ELFOSABI_LINUX); | |||
387 | ECase(ELFOSABI_HURD); | |||
388 | ECase(ELFOSABI_SOLARIS); | |||
389 | ECase(ELFOSABI_AIX); | |||
390 | ECase(ELFOSABI_IRIX); | |||
391 | ECase(ELFOSABI_FREEBSD); | |||
392 | ECase(ELFOSABI_TRU64); | |||
393 | ECase(ELFOSABI_MODESTO); | |||
394 | ECase(ELFOSABI_OPENBSD); | |||
395 | ECase(ELFOSABI_OPENVMS); | |||
396 | ECase(ELFOSABI_NSK); | |||
397 | ECase(ELFOSABI_AROS); | |||
398 | ECase(ELFOSABI_FENIXOS); | |||
399 | ECase(ELFOSABI_CLOUDABI); | |||
400 | ECase(ELFOSABI_AMDGPU_HSA); | |||
401 | ECase(ELFOSABI_AMDGPU_PAL); | |||
402 | ECase(ELFOSABI_AMDGPU_MESA3D); | |||
403 | ECase(ELFOSABI_ARM); | |||
404 | ECase(ELFOSABI_C6000_ELFABI); | |||
405 | ECase(ELFOSABI_C6000_LINUX); | |||
406 | ECase(ELFOSABI_STANDALONE); | |||
407 | #undef ECase | |||
408 | IO.enumFallback<Hex8>(Value); | |||
409 | } | |||
410 | ||||
411 | void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO, | |||
412 | ELFYAML::ELF_EF &Value) { | |||
413 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
414 | assert(Object && "The IO context is not initialized")(static_cast <bool> (Object && "The IO context is not initialized" ) ? void (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 414, __extension__ __PRETTY_FUNCTION__ )); | |||
415 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
416 | #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M) | |||
417 | switch (Object->getMachine()) { | |||
418 | case ELF::EM_ARM: | |||
419 | BCase(EF_ARM_SOFT_FLOAT); | |||
420 | BCase(EF_ARM_VFP_FLOAT); | |||
421 | BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK); | |||
422 | BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK); | |||
423 | BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK); | |||
424 | BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK); | |||
425 | BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK); | |||
426 | BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK); | |||
427 | break; | |||
428 | case ELF::EM_MIPS: | |||
429 | BCase(EF_MIPS_NOREORDER); | |||
430 | BCase(EF_MIPS_PIC); | |||
431 | BCase(EF_MIPS_CPIC); | |||
432 | BCase(EF_MIPS_ABI2); | |||
433 | BCase(EF_MIPS_32BITMODE); | |||
434 | BCase(EF_MIPS_FP64); | |||
435 | BCase(EF_MIPS_NAN2008); | |||
436 | BCase(EF_MIPS_MICROMIPS); | |||
437 | BCase(EF_MIPS_ARCH_ASE_M16); | |||
438 | BCase(EF_MIPS_ARCH_ASE_MDMX); | |||
439 | BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI); | |||
440 | BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI); | |||
441 | BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI); | |||
442 | BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI); | |||
443 | BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH); | |||
444 | BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH); | |||
445 | BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH); | |||
446 | BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH); | |||
447 | BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH); | |||
448 | BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH); | |||
449 | BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH); | |||
450 | BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH); | |||
451 | BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH); | |||
452 | BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH); | |||
453 | BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH); | |||
454 | BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH); | |||
455 | BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH); | |||
456 | BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH); | |||
457 | BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH); | |||
458 | BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH); | |||
459 | BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH); | |||
460 | BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH); | |||
461 | BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH); | |||
462 | BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH); | |||
463 | BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH); | |||
464 | BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH); | |||
465 | BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH); | |||
466 | BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH); | |||
467 | BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH); | |||
468 | BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH); | |||
469 | BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH); | |||
470 | BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH); | |||
471 | BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH); | |||
472 | break; | |||
473 | case ELF::EM_HEXAGON: | |||
474 | BCaseMask(EF_HEXAGON_MACH_V2, EF_HEXAGON_MACH); | |||
475 | BCaseMask(EF_HEXAGON_MACH_V3, EF_HEXAGON_MACH); | |||
476 | BCaseMask(EF_HEXAGON_MACH_V4, EF_HEXAGON_MACH); | |||
477 | BCaseMask(EF_HEXAGON_MACH_V5, EF_HEXAGON_MACH); | |||
478 | BCaseMask(EF_HEXAGON_MACH_V55, EF_HEXAGON_MACH); | |||
479 | BCaseMask(EF_HEXAGON_MACH_V60, EF_HEXAGON_MACH); | |||
480 | BCaseMask(EF_HEXAGON_MACH_V62, EF_HEXAGON_MACH); | |||
481 | BCaseMask(EF_HEXAGON_MACH_V65, EF_HEXAGON_MACH); | |||
482 | BCaseMask(EF_HEXAGON_MACH_V66, EF_HEXAGON_MACH); | |||
483 | BCaseMask(EF_HEXAGON_MACH_V67, EF_HEXAGON_MACH); | |||
484 | BCaseMask(EF_HEXAGON_MACH_V67T, EF_HEXAGON_MACH); | |||
485 | BCaseMask(EF_HEXAGON_MACH_V68, EF_HEXAGON_MACH); | |||
486 | BCaseMask(EF_HEXAGON_MACH_V69, EF_HEXAGON_MACH); | |||
487 | BCaseMask(EF_HEXAGON_ISA_V2, EF_HEXAGON_ISA); | |||
488 | BCaseMask(EF_HEXAGON_ISA_V3, EF_HEXAGON_ISA); | |||
489 | BCaseMask(EF_HEXAGON_ISA_V4, EF_HEXAGON_ISA); | |||
490 | BCaseMask(EF_HEXAGON_ISA_V5, EF_HEXAGON_ISA); | |||
491 | BCaseMask(EF_HEXAGON_ISA_V55, EF_HEXAGON_ISA); | |||
492 | BCaseMask(EF_HEXAGON_ISA_V60, EF_HEXAGON_ISA); | |||
493 | BCaseMask(EF_HEXAGON_ISA_V62, EF_HEXAGON_ISA); | |||
494 | BCaseMask(EF_HEXAGON_ISA_V65, EF_HEXAGON_ISA); | |||
495 | BCaseMask(EF_HEXAGON_ISA_V66, EF_HEXAGON_ISA); | |||
496 | BCaseMask(EF_HEXAGON_ISA_V67, EF_HEXAGON_ISA); | |||
497 | BCaseMask(EF_HEXAGON_ISA_V68, EF_HEXAGON_ISA); | |||
498 | BCaseMask(EF_HEXAGON_ISA_V69, EF_HEXAGON_ISA); | |||
499 | break; | |||
500 | case ELF::EM_AVR: | |||
501 | BCaseMask(EF_AVR_ARCH_AVR1, EF_AVR_ARCH_MASK); | |||
502 | BCaseMask(EF_AVR_ARCH_AVR2, EF_AVR_ARCH_MASK); | |||
503 | BCaseMask(EF_AVR_ARCH_AVR25, EF_AVR_ARCH_MASK); | |||
504 | BCaseMask(EF_AVR_ARCH_AVR3, EF_AVR_ARCH_MASK); | |||
505 | BCaseMask(EF_AVR_ARCH_AVR31, EF_AVR_ARCH_MASK); | |||
506 | BCaseMask(EF_AVR_ARCH_AVR35, EF_AVR_ARCH_MASK); | |||
507 | BCaseMask(EF_AVR_ARCH_AVR4, EF_AVR_ARCH_MASK); | |||
508 | BCaseMask(EF_AVR_ARCH_AVR5, EF_AVR_ARCH_MASK); | |||
509 | BCaseMask(EF_AVR_ARCH_AVR51, EF_AVR_ARCH_MASK); | |||
510 | BCaseMask(EF_AVR_ARCH_AVR6, EF_AVR_ARCH_MASK); | |||
511 | BCaseMask(EF_AVR_ARCH_AVRTINY, EF_AVR_ARCH_MASK); | |||
512 | BCaseMask(EF_AVR_ARCH_XMEGA1, EF_AVR_ARCH_MASK); | |||
513 | BCaseMask(EF_AVR_ARCH_XMEGA2, EF_AVR_ARCH_MASK); | |||
514 | BCaseMask(EF_AVR_ARCH_XMEGA3, EF_AVR_ARCH_MASK); | |||
515 | BCaseMask(EF_AVR_ARCH_XMEGA4, EF_AVR_ARCH_MASK); | |||
516 | BCaseMask(EF_AVR_ARCH_XMEGA5, EF_AVR_ARCH_MASK); | |||
517 | BCaseMask(EF_AVR_ARCH_XMEGA6, EF_AVR_ARCH_MASK); | |||
518 | BCaseMask(EF_AVR_ARCH_XMEGA7, EF_AVR_ARCH_MASK); | |||
519 | BCase(EF_AVR_LINKRELAX_PREPARED); | |||
520 | break; | |||
521 | case ELF::EM_RISCV: | |||
522 | BCase(EF_RISCV_RVC); | |||
523 | BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI); | |||
524 | BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI); | |||
525 | BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI); | |||
526 | BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI); | |||
527 | BCase(EF_RISCV_RVE); | |||
528 | BCase(EF_RISCV_TSO); | |||
529 | break; | |||
530 | case ELF::EM_AMDGPU: | |||
531 | BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH); | |||
532 | BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH); | |||
533 | BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH); | |||
534 | BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH); | |||
535 | BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH); | |||
536 | BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH); | |||
537 | BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH); | |||
538 | BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH); | |||
539 | BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH); | |||
540 | BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH); | |||
541 | BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH); | |||
542 | BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH); | |||
543 | BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH); | |||
544 | BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH); | |||
545 | BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH); | |||
546 | BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH); | |||
547 | BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH); | |||
548 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH); | |||
549 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH); | |||
550 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX602, EF_AMDGPU_MACH); | |||
551 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH); | |||
552 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH); | |||
553 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH); | |||
554 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH); | |||
555 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH); | |||
556 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX705, EF_AMDGPU_MACH); | |||
557 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH); | |||
558 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH); | |||
559 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH); | |||
560 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX805, EF_AMDGPU_MACH); | |||
561 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH); | |||
562 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH); | |||
563 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH); | |||
564 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH); | |||
565 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH); | |||
566 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH); | |||
567 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH); | |||
568 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90A, EF_AMDGPU_MACH); | |||
569 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90C, EF_AMDGPU_MACH); | |||
570 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX940, EF_AMDGPU_MACH); | |||
571 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH); | |||
572 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH); | |||
573 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH); | |||
574 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1013, EF_AMDGPU_MACH); | |||
575 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1030, EF_AMDGPU_MACH); | |||
576 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1031, EF_AMDGPU_MACH); | |||
577 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1032, EF_AMDGPU_MACH); | |||
578 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1033, EF_AMDGPU_MACH); | |||
579 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1034, EF_AMDGPU_MACH); | |||
580 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1035, EF_AMDGPU_MACH); | |||
581 | BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1036, EF_AMDGPU_MACH); | |||
582 | switch (Object->Header.ABIVersion) { | |||
583 | default: | |||
584 | // ELFOSABI_AMDGPU_PAL, ELFOSABI_AMDGPU_MESA3D support *_V3 flags. | |||
585 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
586 | case ELF::ELFABIVERSION_AMDGPU_HSA_V3: | |||
587 | BCase(EF_AMDGPU_FEATURE_XNACK_V3); | |||
588 | BCase(EF_AMDGPU_FEATURE_SRAMECC_V3); | |||
589 | break; | |||
590 | case ELF::ELFABIVERSION_AMDGPU_HSA_V4: | |||
591 | case ELF::ELFABIVERSION_AMDGPU_HSA_V5: | |||
592 | BCaseMask(EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4, | |||
593 | EF_AMDGPU_FEATURE_XNACK_V4); | |||
594 | BCaseMask(EF_AMDGPU_FEATURE_XNACK_ANY_V4, | |||
595 | EF_AMDGPU_FEATURE_XNACK_V4); | |||
596 | BCaseMask(EF_AMDGPU_FEATURE_XNACK_OFF_V4, | |||
597 | EF_AMDGPU_FEATURE_XNACK_V4); | |||
598 | BCaseMask(EF_AMDGPU_FEATURE_XNACK_ON_V4, | |||
599 | EF_AMDGPU_FEATURE_XNACK_V4); | |||
600 | BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4, | |||
601 | EF_AMDGPU_FEATURE_SRAMECC_V4); | |||
602 | BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ANY_V4, | |||
603 | EF_AMDGPU_FEATURE_SRAMECC_V4); | |||
604 | BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_OFF_V4, | |||
605 | EF_AMDGPU_FEATURE_SRAMECC_V4); | |||
606 | BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ON_V4, | |||
607 | EF_AMDGPU_FEATURE_SRAMECC_V4); | |||
608 | break; | |||
609 | } | |||
610 | break; | |||
611 | default: | |||
612 | break; | |||
613 | } | |||
614 | #undef BCase | |||
615 | #undef BCaseMask | |||
616 | } | |||
617 | ||||
618 | void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration( | |||
619 | IO &IO, ELFYAML::ELF_SHT &Value) { | |||
620 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
621 | assert(Object && "The IO context is not initialized")(static_cast <bool> (Object && "The IO context is not initialized" ) ? void (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 621, __extension__ __PRETTY_FUNCTION__ )); | |||
622 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
623 | ECase(SHT_NULL); | |||
624 | ECase(SHT_PROGBITS); | |||
625 | ECase(SHT_SYMTAB); | |||
626 | // FIXME: Issue a diagnostic with this information. | |||
627 | ECase(SHT_STRTAB); | |||
628 | ECase(SHT_RELA); | |||
629 | ECase(SHT_HASH); | |||
630 | ECase(SHT_DYNAMIC); | |||
631 | ECase(SHT_NOTE); | |||
632 | ECase(SHT_NOBITS); | |||
633 | ECase(SHT_REL); | |||
634 | ECase(SHT_SHLIB); | |||
635 | ECase(SHT_DYNSYM); | |||
636 | ECase(SHT_INIT_ARRAY); | |||
637 | ECase(SHT_FINI_ARRAY); | |||
638 | ECase(SHT_PREINIT_ARRAY); | |||
639 | ECase(SHT_GROUP); | |||
640 | ECase(SHT_SYMTAB_SHNDX); | |||
641 | ECase(SHT_RELR); | |||
642 | ECase(SHT_ANDROID_REL); | |||
643 | ECase(SHT_ANDROID_RELA); | |||
644 | ECase(SHT_ANDROID_RELR); | |||
645 | ECase(SHT_LLVM_ODRTAB); | |||
646 | ECase(SHT_LLVM_LINKER_OPTIONS); | |||
647 | ECase(SHT_LLVM_CALL_GRAPH_PROFILE); | |||
648 | ECase(SHT_LLVM_ADDRSIG); | |||
649 | ECase(SHT_LLVM_DEPENDENT_LIBRARIES); | |||
650 | ECase(SHT_LLVM_SYMPART); | |||
651 | ECase(SHT_LLVM_PART_EHDR); | |||
652 | ECase(SHT_LLVM_PART_PHDR); | |||
653 | ECase(SHT_LLVM_BB_ADDR_MAP); | |||
654 | ECase(SHT_GNU_ATTRIBUTES); | |||
655 | ECase(SHT_GNU_HASH); | |||
656 | ECase(SHT_GNU_verdef); | |||
657 | ECase(SHT_GNU_verneed); | |||
658 | ECase(SHT_GNU_versym); | |||
659 | switch (Object->getMachine()) { | |||
660 | case ELF::EM_ARM: | |||
661 | ECase(SHT_ARM_EXIDX); | |||
662 | ECase(SHT_ARM_PREEMPTMAP); | |||
663 | ECase(SHT_ARM_ATTRIBUTES); | |||
664 | ECase(SHT_ARM_DEBUGOVERLAY); | |||
665 | ECase(SHT_ARM_OVERLAYSECTION); | |||
666 | break; | |||
667 | case ELF::EM_HEXAGON: | |||
668 | ECase(SHT_HEX_ORDERED); | |||
669 | break; | |||
670 | case ELF::EM_X86_64: | |||
671 | ECase(SHT_X86_64_UNWIND); | |||
672 | break; | |||
673 | case ELF::EM_MIPS: | |||
674 | ECase(SHT_MIPS_REGINFO); | |||
675 | ECase(SHT_MIPS_OPTIONS); | |||
676 | ECase(SHT_MIPS_DWARF); | |||
677 | ECase(SHT_MIPS_ABIFLAGS); | |||
678 | break; | |||
679 | case ELF::EM_RISCV: | |||
680 | ECase(SHT_RISCV_ATTRIBUTES); | |||
681 | break; | |||
682 | case ELF::EM_MSP430: | |||
683 | ECase(SHT_MSP430_ATTRIBUTES); | |||
684 | break; | |||
685 | default: | |||
686 | // Nothing to do. | |||
687 | break; | |||
688 | } | |||
689 | #undef ECase | |||
690 | IO.enumFallback<Hex32>(Value); | |||
691 | } | |||
692 | ||||
693 | void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO, | |||
694 | ELFYAML::ELF_PF &Value) { | |||
695 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
696 | BCase(PF_X); | |||
697 | BCase(PF_W); | |||
698 | BCase(PF_R); | |||
699 | } | |||
700 | ||||
701 | void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO, | |||
702 | ELFYAML::ELF_SHF &Value) { | |||
703 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
704 | #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) | |||
705 | BCase(SHF_WRITE); | |||
706 | BCase(SHF_ALLOC); | |||
707 | BCase(SHF_EXCLUDE); | |||
708 | BCase(SHF_EXECINSTR); | |||
709 | BCase(SHF_MERGE); | |||
710 | BCase(SHF_STRINGS); | |||
711 | BCase(SHF_INFO_LINK); | |||
712 | BCase(SHF_LINK_ORDER); | |||
713 | BCase(SHF_OS_NONCONFORMING); | |||
714 | BCase(SHF_GROUP); | |||
715 | BCase(SHF_TLS); | |||
716 | BCase(SHF_COMPRESSED); | |||
717 | switch (Object->getOSAbi()) { | |||
718 | case ELF::ELFOSABI_SOLARIS: | |||
719 | BCase(SHF_SUNW_NODISCARD); | |||
720 | break; | |||
721 | default: | |||
722 | BCase(SHF_GNU_RETAIN); | |||
723 | break; | |||
724 | } | |||
725 | switch (Object->getMachine()) { | |||
726 | case ELF::EM_ARM: | |||
727 | BCase(SHF_ARM_PURECODE); | |||
728 | break; | |||
729 | case ELF::EM_HEXAGON: | |||
730 | BCase(SHF_HEX_GPREL); | |||
731 | break; | |||
732 | case ELF::EM_MIPS: | |||
733 | BCase(SHF_MIPS_NODUPES); | |||
734 | BCase(SHF_MIPS_NAMES); | |||
735 | BCase(SHF_MIPS_LOCAL); | |||
736 | BCase(SHF_MIPS_NOSTRIP); | |||
737 | BCase(SHF_MIPS_GPREL); | |||
738 | BCase(SHF_MIPS_MERGE); | |||
739 | BCase(SHF_MIPS_ADDR); | |||
740 | BCase(SHF_MIPS_STRING); | |||
741 | break; | |||
742 | case ELF::EM_X86_64: | |||
743 | BCase(SHF_X86_64_LARGE); | |||
744 | break; | |||
745 | default: | |||
746 | // Nothing to do. | |||
747 | break; | |||
748 | } | |||
749 | #undef BCase | |||
750 | } | |||
751 | ||||
752 | void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration( | |||
753 | IO &IO, ELFYAML::ELF_SHN &Value) { | |||
754 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
755 | ECase(SHN_UNDEF); | |||
756 | ECase(SHN_LORESERVE); | |||
757 | ECase(SHN_LOPROC); | |||
758 | ECase(SHN_HIPROC); | |||
759 | ECase(SHN_LOOS); | |||
760 | ECase(SHN_HIOS); | |||
761 | ECase(SHN_ABS); | |||
762 | ECase(SHN_COMMON); | |||
763 | ECase(SHN_XINDEX); | |||
764 | ECase(SHN_HIRESERVE); | |||
765 | ECase(SHN_AMDGPU_LDS); | |||
766 | ECase(SHN_HEXAGON_SCOMMON); | |||
767 | ECase(SHN_HEXAGON_SCOMMON_1); | |||
768 | ECase(SHN_HEXAGON_SCOMMON_2); | |||
769 | ECase(SHN_HEXAGON_SCOMMON_4); | |||
770 | ECase(SHN_HEXAGON_SCOMMON_8); | |||
771 | #undef ECase | |||
772 | IO.enumFallback<Hex16>(Value); | |||
773 | } | |||
774 | ||||
775 | void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration( | |||
776 | IO &IO, ELFYAML::ELF_STB &Value) { | |||
777 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
778 | ECase(STB_LOCAL); | |||
779 | ECase(STB_GLOBAL); | |||
780 | ECase(STB_WEAK); | |||
781 | ECase(STB_GNU_UNIQUE); | |||
782 | #undef ECase | |||
783 | IO.enumFallback<Hex8>(Value); | |||
784 | } | |||
785 | ||||
786 | void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration( | |||
787 | IO &IO, ELFYAML::ELF_STT &Value) { | |||
788 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
789 | ECase(STT_NOTYPE); | |||
790 | ECase(STT_OBJECT); | |||
791 | ECase(STT_FUNC); | |||
792 | ECase(STT_SECTION); | |||
793 | ECase(STT_FILE); | |||
794 | ECase(STT_COMMON); | |||
795 | ECase(STT_TLS); | |||
796 | ECase(STT_GNU_IFUNC); | |||
797 | #undef ECase | |||
798 | IO.enumFallback<Hex8>(Value); | |||
799 | } | |||
800 | ||||
801 | ||||
802 | void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration( | |||
803 | IO &IO, ELFYAML::ELF_RSS &Value) { | |||
804 | #define ECase(X) IO.enumCase(Value, #X, ELF::X) | |||
805 | ECase(RSS_UNDEF); | |||
806 | ECase(RSS_GP); | |||
807 | ECase(RSS_GP0); | |||
808 | ECase(RSS_LOC); | |||
809 | #undef ECase | |||
810 | } | |||
811 | ||||
812 | void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration( | |||
813 | IO &IO, ELFYAML::ELF_REL &Value) { | |||
814 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
815 | assert(Object && "The IO context is not initialized")(static_cast <bool> (Object && "The IO context is not initialized" ) ? void (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 815, __extension__ __PRETTY_FUNCTION__ )); | |||
816 | #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X); | |||
817 | switch (Object->getMachine()) { | |||
818 | case ELF::EM_X86_64: | |||
819 | #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" | |||
820 | break; | |||
821 | case ELF::EM_MIPS: | |||
822 | #include "llvm/BinaryFormat/ELFRelocs/Mips.def" | |||
823 | break; | |||
824 | case ELF::EM_HEXAGON: | |||
825 | #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" | |||
826 | break; | |||
827 | case ELF::EM_386: | |||
828 | case ELF::EM_IAMCU: | |||
829 | #include "llvm/BinaryFormat/ELFRelocs/i386.def" | |||
830 | break; | |||
831 | case ELF::EM_AARCH64: | |||
832 | #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" | |||
833 | break; | |||
834 | case ELF::EM_ARM: | |||
835 | #include "llvm/BinaryFormat/ELFRelocs/ARM.def" | |||
836 | break; | |||
837 | case ELF::EM_ARC: | |||
838 | #include "llvm/BinaryFormat/ELFRelocs/ARC.def" | |||
839 | break; | |||
840 | case ELF::EM_RISCV: | |||
841 | #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" | |||
842 | break; | |||
843 | case ELF::EM_LANAI: | |||
844 | #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" | |||
845 | break; | |||
846 | case ELF::EM_AMDGPU: | |||
847 | #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" | |||
848 | break; | |||
849 | case ELF::EM_BPF: | |||
850 | #include "llvm/BinaryFormat/ELFRelocs/BPF.def" | |||
851 | break; | |||
852 | case ELF::EM_VE: | |||
853 | #include "llvm/BinaryFormat/ELFRelocs/VE.def" | |||
854 | break; | |||
855 | case ELF::EM_CSKY: | |||
856 | #include "llvm/BinaryFormat/ELFRelocs/CSKY.def" | |||
857 | break; | |||
858 | case ELF::EM_PPC: | |||
859 | #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" | |||
860 | break; | |||
861 | case ELF::EM_PPC64: | |||
862 | #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" | |||
863 | break; | |||
864 | case ELF::EM_68K: | |||
865 | #include "llvm/BinaryFormat/ELFRelocs/M68k.def" | |||
866 | break; | |||
867 | case ELF::EM_LOONGARCH: | |||
868 | #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def" | |||
869 | break; | |||
870 | default: | |||
871 | // Nothing to do. | |||
872 | break; | |||
873 | } | |||
874 | #undef ELF_RELOC | |||
875 | IO.enumFallback<Hex32>(Value); | |||
876 | } | |||
877 | ||||
878 | void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration( | |||
879 | IO &IO, ELFYAML::ELF_DYNTAG &Value) { | |||
880 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
881 | assert(Object && "The IO context is not initialized")(static_cast <bool> (Object && "The IO context is not initialized" ) ? void (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 881, __extension__ __PRETTY_FUNCTION__ )); | |||
882 | ||||
883 | // Disable architecture specific tags by default. We might enable them below. | |||
884 | #define AARCH64_DYNAMIC_TAG(name, value) | |||
885 | #define MIPS_DYNAMIC_TAG(name, value) | |||
886 | #define HEXAGON_DYNAMIC_TAG(name, value) | |||
887 | #define PPC_DYNAMIC_TAG(name, value) | |||
888 | #define PPC64_DYNAMIC_TAG(name, value) | |||
889 | // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. | |||
890 | #define DYNAMIC_TAG_MARKER(name, value) | |||
891 | ||||
892 | #define STRINGIFY(X) (#X) | |||
893 | #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X); | |||
894 | switch (Object->getMachine()) { | |||
895 | case ELF::EM_AARCH64: | |||
896 | #undef AARCH64_DYNAMIC_TAG | |||
897 | #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) | |||
898 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
899 | #undef AARCH64_DYNAMIC_TAG | |||
900 | #define AARCH64_DYNAMIC_TAG(name, value) | |||
901 | break; | |||
902 | case ELF::EM_MIPS: | |||
903 | #undef MIPS_DYNAMIC_TAG | |||
904 | #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) | |||
905 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
906 | #undef MIPS_DYNAMIC_TAG | |||
907 | #define MIPS_DYNAMIC_TAG(name, value) | |||
908 | break; | |||
909 | case ELF::EM_HEXAGON: | |||
910 | #undef HEXAGON_DYNAMIC_TAG | |||
911 | #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) | |||
912 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
913 | #undef HEXAGON_DYNAMIC_TAG | |||
914 | #define HEXAGON_DYNAMIC_TAG(name, value) | |||
915 | break; | |||
916 | case ELF::EM_PPC: | |||
917 | #undef PPC_DYNAMIC_TAG | |||
918 | #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) | |||
919 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
920 | #undef PPC_DYNAMIC_TAG | |||
921 | #define PPC_DYNAMIC_TAG(name, value) | |||
922 | break; | |||
923 | case ELF::EM_PPC64: | |||
924 | #undef PPC64_DYNAMIC_TAG | |||
925 | #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) | |||
926 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
927 | #undef PPC64_DYNAMIC_TAG | |||
928 | #define PPC64_DYNAMIC_TAG(name, value) | |||
929 | break; | |||
930 | case ELF::EM_RISCV: | |||
931 | #undef RISCV_DYNAMIC_TAG | |||
932 | #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) | |||
933 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
934 | #undef RISCV_DYNAMIC_TAG | |||
935 | #define RISCV_DYNAMIC_TAG(name, value) | |||
936 | break; | |||
937 | default: | |||
938 | #include "llvm/BinaryFormat/DynamicTags.def" | |||
939 | break; | |||
940 | } | |||
941 | #undef AARCH64_DYNAMIC_TAG | |||
942 | #undef MIPS_DYNAMIC_TAG | |||
943 | #undef HEXAGON_DYNAMIC_TAG | |||
944 | #undef PPC_DYNAMIC_TAG | |||
945 | #undef PPC64_DYNAMIC_TAG | |||
946 | #undef DYNAMIC_TAG_MARKER | |||
947 | #undef STRINGIFY | |||
948 | #undef DYNAMIC_TAG | |||
949 | ||||
950 | IO.enumFallback<Hex64>(Value); | |||
951 | } | |||
952 | ||||
953 | void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration( | |||
954 | IO &IO, ELFYAML::MIPS_AFL_REG &Value) { | |||
955 | #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) | |||
956 | ECase(REG_NONE); | |||
957 | ECase(REG_32); | |||
958 | ECase(REG_64); | |||
959 | ECase(REG_128); | |||
960 | #undef ECase | |||
961 | } | |||
962 | ||||
963 | void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration( | |||
964 | IO &IO, ELFYAML::MIPS_ABI_FP &Value) { | |||
965 | #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X) | |||
966 | ECase(FP_ANY); | |||
967 | ECase(FP_DOUBLE); | |||
968 | ECase(FP_SINGLE); | |||
969 | ECase(FP_SOFT); | |||
970 | ECase(FP_OLD_64); | |||
971 | ECase(FP_XX); | |||
972 | ECase(FP_64); | |||
973 | ECase(FP_64A); | |||
974 | #undef ECase | |||
975 | } | |||
976 | ||||
977 | void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration( | |||
978 | IO &IO, ELFYAML::MIPS_AFL_EXT &Value) { | |||
979 | #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) | |||
980 | ECase(EXT_NONE); | |||
981 | ECase(EXT_XLR); | |||
982 | ECase(EXT_OCTEON2); | |||
983 | ECase(EXT_OCTEONP); | |||
984 | ECase(EXT_LOONGSON_3A); | |||
985 | ECase(EXT_OCTEON); | |||
986 | ECase(EXT_5900); | |||
987 | ECase(EXT_4650); | |||
988 | ECase(EXT_4010); | |||
989 | ECase(EXT_4100); | |||
990 | ECase(EXT_3900); | |||
991 | ECase(EXT_10000); | |||
992 | ECase(EXT_SB1); | |||
993 | ECase(EXT_4111); | |||
994 | ECase(EXT_4120); | |||
995 | ECase(EXT_5400); | |||
996 | ECase(EXT_5500); | |||
997 | ECase(EXT_LOONGSON_2E); | |||
998 | ECase(EXT_LOONGSON_2F); | |||
999 | ECase(EXT_OCTEON3); | |||
1000 | #undef ECase | |||
1001 | } | |||
1002 | ||||
1003 | void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration( | |||
1004 | IO &IO, ELFYAML::MIPS_ISA &Value) { | |||
1005 | IO.enumCase(Value, "MIPS1", 1); | |||
1006 | IO.enumCase(Value, "MIPS2", 2); | |||
1007 | IO.enumCase(Value, "MIPS3", 3); | |||
1008 | IO.enumCase(Value, "MIPS4", 4); | |||
1009 | IO.enumCase(Value, "MIPS5", 5); | |||
1010 | IO.enumCase(Value, "MIPS32", 32); | |||
1011 | IO.enumCase(Value, "MIPS64", 64); | |||
1012 | IO.enumFallback<Hex32>(Value); | |||
1013 | } | |||
1014 | ||||
1015 | void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset( | |||
1016 | IO &IO, ELFYAML::MIPS_AFL_ASE &Value) { | |||
1017 | #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X) | |||
1018 | BCase(DSP); | |||
1019 | BCase(DSPR2); | |||
1020 | BCase(EVA); | |||
1021 | BCase(MCU); | |||
1022 | BCase(MDMX); | |||
1023 | BCase(MIPS3D); | |||
1024 | BCase(MT); | |||
1025 | BCase(SMARTMIPS); | |||
1026 | BCase(VIRT); | |||
1027 | BCase(MSA); | |||
1028 | BCase(MIPS16); | |||
1029 | BCase(MICROMIPS); | |||
1030 | BCase(XPA); | |||
1031 | BCase(CRC); | |||
1032 | BCase(GINV); | |||
1033 | #undef BCase | |||
1034 | } | |||
1035 | ||||
1036 | void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset( | |||
1037 | IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) { | |||
1038 | #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X) | |||
1039 | BCase(ODDSPREG); | |||
1040 | #undef BCase | |||
1041 | } | |||
1042 | ||||
1043 | void MappingTraits<ELFYAML::SectionHeader>::mapping( | |||
1044 | IO &IO, ELFYAML::SectionHeader &SHdr) { | |||
1045 | IO.mapRequired("Name", SHdr.Name); | |||
1046 | } | |||
1047 | ||||
1048 | void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO, | |||
1049 | ELFYAML::FileHeader &FileHdr) { | |||
1050 | IO.mapRequired("Class", FileHdr.Class); | |||
1051 | IO.mapRequired("Data", FileHdr.Data); | |||
1052 | IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0)); | |||
1053 | IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0)); | |||
1054 | IO.mapRequired("Type", FileHdr.Type); | |||
1055 | IO.mapOptional("Machine", FileHdr.Machine); | |||
1056 | IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0)); | |||
1057 | IO.mapOptional("Entry", FileHdr.Entry, Hex64(0)); | |||
1058 | IO.mapOptional("SectionHeaderStringTable", FileHdr.SectionHeaderStringTable); | |||
1059 | ||||
1060 | // obj2yaml does not dump these fields. | |||
1061 | assert(!IO.outputting() ||(static_cast <bool> (!IO.outputting() || (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum)) ? void (0) : __assert_fail ("!IO.outputting() || (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum)" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1062, __extension__ __PRETTY_FUNCTION__ )) | |||
1062 | (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum))(static_cast <bool> (!IO.outputting() || (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum)) ? void (0) : __assert_fail ("!IO.outputting() || (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum)" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1062, __extension__ __PRETTY_FUNCTION__ )); | |||
1063 | IO.mapOptional("EPhOff", FileHdr.EPhOff); | |||
1064 | IO.mapOptional("EPhEntSize", FileHdr.EPhEntSize); | |||
1065 | IO.mapOptional("EPhNum", FileHdr.EPhNum); | |||
1066 | IO.mapOptional("EShEntSize", FileHdr.EShEntSize); | |||
1067 | IO.mapOptional("EShOff", FileHdr.EShOff); | |||
1068 | IO.mapOptional("EShNum", FileHdr.EShNum); | |||
1069 | IO.mapOptional("EShStrNdx", FileHdr.EShStrNdx); | |||
1070 | } | |||
1071 | ||||
1072 | void MappingTraits<ELFYAML::ProgramHeader>::mapping( | |||
1073 | IO &IO, ELFYAML::ProgramHeader &Phdr) { | |||
1074 | IO.mapRequired("Type", Phdr.Type); | |||
1075 | IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0)); | |||
1076 | IO.mapOptional("FirstSec", Phdr.FirstSec); | |||
1077 | IO.mapOptional("LastSec", Phdr.LastSec); | |||
1078 | IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0)); | |||
1079 | IO.mapOptional("PAddr", Phdr.PAddr, Phdr.VAddr); | |||
1080 | IO.mapOptional("Align", Phdr.Align); | |||
1081 | IO.mapOptional("FileSize", Phdr.FileSize); | |||
1082 | IO.mapOptional("MemSize", Phdr.MemSize); | |||
1083 | IO.mapOptional("Offset", Phdr.Offset); | |||
1084 | } | |||
1085 | ||||
1086 | std::string MappingTraits<ELFYAML::ProgramHeader>::validate( | |||
1087 | IO &IO, ELFYAML::ProgramHeader &FileHdr) { | |||
1088 | if (!FileHdr.FirstSec && FileHdr.LastSec) | |||
1089 | return "the \"LastSec\" key can't be used without the \"FirstSec\" key"; | |||
1090 | if (FileHdr.FirstSec && !FileHdr.LastSec) | |||
1091 | return "the \"FirstSec\" key can't be used without the \"LastSec\" key"; | |||
1092 | return ""; | |||
1093 | } | |||
1094 | ||||
1095 | LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece)struct StOtherPiece { StOtherPiece() = default; StOtherPiece( const StringRef v) : value(v) {} StOtherPiece(const StOtherPiece &v) = default; StOtherPiece &operator=(const StOtherPiece &rhs) = default; StOtherPiece &operator=(const StringRef &rhs) { value = rhs; return *this; } operator const StringRef & () const { return value; } bool operator==(const StOtherPiece &rhs) const { return value == rhs.value; } bool operator ==(const StringRef &rhs) const { return value == rhs; } bool operator<(const StOtherPiece &rhs) const { return value < rhs.value; } StringRef value; using BaseType = StringRef ; }; | |||
1096 | ||||
1097 | template <> struct ScalarTraits<StOtherPiece> { | |||
1098 | static void output(const StOtherPiece &Val, void *, raw_ostream &Out) { | |||
1099 | Out << Val; | |||
1100 | } | |||
1101 | static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) { | |||
1102 | Val = Scalar; | |||
1103 | return {}; | |||
1104 | } | |||
1105 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | |||
1106 | }; | |||
1107 | template <> struct SequenceElementTraits<StOtherPiece> { | |||
1108 | static const bool flow = true; | |||
1109 | }; | |||
1110 | ||||
1111 | template <> struct ScalarTraits<ELFYAML::YAMLFlowString> { | |||
1112 | static void output(const ELFYAML::YAMLFlowString &Val, void *, | |||
1113 | raw_ostream &Out) { | |||
1114 | Out << Val; | |||
1115 | } | |||
1116 | static StringRef input(StringRef Scalar, void *, | |||
1117 | ELFYAML::YAMLFlowString &Val) { | |||
1118 | Val = Scalar; | |||
1119 | return {}; | |||
1120 | } | |||
1121 | static QuotingType mustQuote(StringRef S) { | |||
1122 | return ScalarTraits<StringRef>::mustQuote(S); | |||
1123 | } | |||
1124 | }; | |||
1125 | template <> struct SequenceElementTraits<ELFYAML::YAMLFlowString> { | |||
1126 | static const bool flow = true; | |||
1127 | }; | |||
1128 | ||||
1129 | namespace { | |||
1130 | ||||
1131 | struct NormalizedOther { | |||
1132 | NormalizedOther(IO &IO) : YamlIO(IO) {} | |||
1133 | NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) { | |||
1134 | assert(Original && "This constructor is only used for outputting YAML and "(static_cast <bool> (Original && "This constructor is only used for outputting YAML and " "assumes a non-empty Original") ? void (0) : __assert_fail ( "Original && \"This constructor is only used for outputting YAML and \" \"assumes a non-empty Original\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1135, __extension__ __PRETTY_FUNCTION__ )) | |||
1135 | "assumes a non-empty Original")(static_cast <bool> (Original && "This constructor is only used for outputting YAML and " "assumes a non-empty Original") ? void (0) : __assert_fail ( "Original && \"This constructor is only used for outputting YAML and \" \"assumes a non-empty Original\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1135, __extension__ __PRETTY_FUNCTION__ )); | |||
1136 | std::vector<StOtherPiece> Ret; | |||
1137 | const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext()); | |||
1138 | for (std::pair<StringRef, uint8_t> &P : | |||
1139 | getFlags(Object->getMachine()).takeVector()) { | |||
1140 | uint8_t FlagValue = P.second; | |||
1141 | if ((*Original & FlagValue) != FlagValue) | |||
1142 | continue; | |||
1143 | *Original &= ~FlagValue; | |||
1144 | Ret.push_back({P.first}); | |||
1145 | } | |||
1146 | ||||
1147 | if (*Original != 0) { | |||
1148 | UnknownFlagsHolder = std::to_string(*Original); | |||
1149 | Ret.push_back({UnknownFlagsHolder}); | |||
1150 | } | |||
1151 | ||||
1152 | if (!Ret.empty()) | |||
1153 | Other = std::move(Ret); | |||
1154 | } | |||
1155 | ||||
1156 | uint8_t toValue(StringRef Name) { | |||
1157 | const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext()); | |||
1158 | MapVector<StringRef, uint8_t> Flags = getFlags(Object->getMachine()); | |||
1159 | ||||
1160 | auto It = Flags.find(Name); | |||
1161 | if (It != Flags.end()) | |||
1162 | return It->second; | |||
1163 | ||||
1164 | uint8_t Val; | |||
1165 | if (to_integer(Name, Val)) | |||
1166 | return Val; | |||
1167 | ||||
1168 | YamlIO.setError("an unknown value is used for symbol's 'Other' field: " + | |||
1169 | Name); | |||
1170 | return 0; | |||
1171 | } | |||
1172 | ||||
1173 | Optional<uint8_t> denormalize(IO &) { | |||
1174 | if (!Other) | |||
1175 | return None; | |||
1176 | uint8_t Ret = 0; | |||
1177 | for (StOtherPiece &Val : *Other) | |||
1178 | Ret |= toValue(Val); | |||
1179 | return Ret; | |||
1180 | } | |||
1181 | ||||
1182 | // st_other field is used to encode symbol visibility and platform-dependent | |||
1183 | // flags and values. This method returns a name to value map that is used for | |||
1184 | // parsing and encoding this field. | |||
1185 | MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) { | |||
1186 | MapVector<StringRef, uint8_t> Map; | |||
1187 | // STV_* values are just enumeration values. We add them in a reversed order | |||
1188 | // because when we convert the st_other to named constants when printing | |||
1189 | // YAML we want to use a maximum number of bits on each step: | |||
1190 | // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but | |||
1191 | // not as STV_HIDDEN (2) + STV_INTERNAL (1). | |||
1192 | Map["STV_PROTECTED"] = ELF::STV_PROTECTED; | |||
1193 | Map["STV_HIDDEN"] = ELF::STV_HIDDEN; | |||
1194 | Map["STV_INTERNAL"] = ELF::STV_INTERNAL; | |||
1195 | // STV_DEFAULT is used to represent the default visibility and has a value | |||
1196 | // 0. We want to be able to read it from YAML documents, but there is no | |||
1197 | // reason to print it. | |||
1198 | if (!YamlIO.outputting()) | |||
1199 | Map["STV_DEFAULT"] = ELF::STV_DEFAULT; | |||
1200 | ||||
1201 | // MIPS is not consistent. All of the STO_MIPS_* values are bit flags, | |||
1202 | // except STO_MIPS_MIPS16 which overlaps them. It should be checked and | |||
1203 | // consumed first when we print the output, because we do not want to print | |||
1204 | // any other flags that have the same bits instead. | |||
1205 | if (EMachine == ELF::EM_MIPS) { | |||
1206 | Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16; | |||
1207 | Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS; | |||
1208 | Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC; | |||
1209 | Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT; | |||
1210 | Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL; | |||
1211 | } | |||
1212 | ||||
1213 | if (EMachine == ELF::EM_AARCH64) | |||
1214 | Map["STO_AARCH64_VARIANT_PCS"] = ELF::STO_AARCH64_VARIANT_PCS; | |||
1215 | if (EMachine == ELF::EM_RISCV) | |||
1216 | Map["STO_RISCV_VARIANT_CC"] = ELF::STO_RISCV_VARIANT_CC; | |||
1217 | return Map; | |||
1218 | } | |||
1219 | ||||
1220 | IO &YamlIO; | |||
1221 | Optional<std::vector<StOtherPiece>> Other; | |||
1222 | std::string UnknownFlagsHolder; | |||
1223 | }; | |||
1224 | ||||
1225 | } // end anonymous namespace | |||
1226 | ||||
1227 | void ScalarTraits<ELFYAML::YAMLIntUInt>::output(const ELFYAML::YAMLIntUInt &Val, | |||
1228 | void *Ctx, raw_ostream &Out) { | |||
1229 | Out << Val; | |||
1230 | } | |||
1231 | ||||
1232 | StringRef ScalarTraits<ELFYAML::YAMLIntUInt>::input(StringRef Scalar, void *Ctx, | |||
1233 | ELFYAML::YAMLIntUInt &Val) { | |||
1234 | const bool Is64 = static_cast<ELFYAML::Object *>(Ctx)->Header.Class == | |||
1235 | ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); | |||
1236 | StringRef ErrMsg = "invalid number"; | |||
1237 | // We do not accept negative hex numbers because their meaning is ambiguous. | |||
1238 | // For example, would -0xfffffffff mean 1 or INT32_MIN? | |||
1239 | if (Scalar.empty() || Scalar.startswith("-0x")) | |||
1240 | return ErrMsg; | |||
1241 | ||||
1242 | if (Scalar.startswith("-")) { | |||
1243 | const int64_t MinVal = Is64 ? INT64_MIN(-9223372036854775807L -1) : INT32_MIN(-2147483647-1); | |||
1244 | long long Int; | |||
1245 | if (getAsSignedInteger(Scalar, /*Radix=*/0, Int) || (Int < MinVal)) | |||
1246 | return ErrMsg; | |||
1247 | Val = Int; | |||
1248 | return ""; | |||
1249 | } | |||
1250 | ||||
1251 | const uint64_t MaxVal = Is64 ? UINT64_MAX(18446744073709551615UL) : UINT32_MAX(4294967295U); | |||
1252 | unsigned long long UInt; | |||
1253 | if (getAsUnsignedInteger(Scalar, /*Radix=*/0, UInt) || (UInt > MaxVal)) | |||
1254 | return ErrMsg; | |||
1255 | Val = UInt; | |||
1256 | return ""; | |||
1257 | } | |||
1258 | ||||
1259 | void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) { | |||
1260 | IO.mapOptional("Name", Symbol.Name, StringRef()); | |||
1261 | IO.mapOptional("StName", Symbol.StName); | |||
1262 | IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0)); | |||
1263 | IO.mapOptional("Section", Symbol.Section); | |||
1264 | IO.mapOptional("Index", Symbol.Index); | |||
1265 | IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0)); | |||
1266 | IO.mapOptional("Value", Symbol.Value); | |||
1267 | IO.mapOptional("Size", Symbol.Size); | |||
1268 | ||||
1269 | // Symbol's Other field is a bit special. It is usually a field that | |||
1270 | // represents st_other and holds the symbol visibility. However, on some | |||
1271 | // platforms, it can contain bit fields and regular values, or even sometimes a | |||
1272 | // crazy mix of them (see comments for NormalizedOther). Because of this, we | |||
1273 | // need special handling. | |||
1274 | MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO, | |||
1275 | Symbol.Other); | |||
1276 | IO.mapOptional("Other", Keys->Other); | |||
1277 | } | |||
1278 | ||||
1279 | std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO, | |||
1280 | ELFYAML::Symbol &Symbol) { | |||
1281 | if (Symbol.Index && Symbol.Section) | |||
1282 | return "Index and Section cannot both be specified for Symbol"; | |||
1283 | return ""; | |||
1284 | } | |||
1285 | ||||
1286 | static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) { | |||
1287 | IO.mapOptional("Name", Section.Name, StringRef()); | |||
1288 | IO.mapRequired("Type", Section.Type); | |||
1289 | IO.mapOptional("Flags", Section.Flags); | |||
1290 | IO.mapOptional("Address", Section.Address); | |||
1291 | IO.mapOptional("Link", Section.Link); | |||
1292 | IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0)); | |||
1293 | IO.mapOptional("EntSize", Section.EntSize); | |||
1294 | IO.mapOptional("Offset", Section.Offset); | |||
1295 | ||||
1296 | IO.mapOptional("Content", Section.Content); | |||
1297 | IO.mapOptional("Size", Section.Size); | |||
1298 | ||||
1299 | // obj2yaml does not dump these fields. They are expected to be empty when we | |||
1300 | // are producing YAML, because yaml2obj sets appropriate values for them | |||
1301 | // automatically when they are not explicitly defined. | |||
1302 | assert(!IO.outputting() ||(static_cast <bool> (!IO.outputting() || (!Section.ShOffset && !Section.ShSize && !Section.ShName && !Section.ShFlags && !Section.ShType && !Section .ShAddrAlign)) ? void (0) : __assert_fail ("!IO.outputting() || (!Section.ShOffset && !Section.ShSize && !Section.ShName && !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign)" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1304, __extension__ __PRETTY_FUNCTION__ )) | |||
1303 | (!Section.ShOffset && !Section.ShSize && !Section.ShName &&(static_cast <bool> (!IO.outputting() || (!Section.ShOffset && !Section.ShSize && !Section.ShName && !Section.ShFlags && !Section.ShType && !Section .ShAddrAlign)) ? void (0) : __assert_fail ("!IO.outputting() || (!Section.ShOffset && !Section.ShSize && !Section.ShName && !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign)" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1304, __extension__ __PRETTY_FUNCTION__ )) | |||
1304 | !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign))(static_cast <bool> (!IO.outputting() || (!Section.ShOffset && !Section.ShSize && !Section.ShName && !Section.ShFlags && !Section.ShType && !Section .ShAddrAlign)) ? void (0) : __assert_fail ("!IO.outputting() || (!Section.ShOffset && !Section.ShSize && !Section.ShName && !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign)" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1304, __extension__ __PRETTY_FUNCTION__ )); | |||
1305 | IO.mapOptional("ShAddrAlign", Section.ShAddrAlign); | |||
1306 | IO.mapOptional("ShName", Section.ShName); | |||
1307 | IO.mapOptional("ShOffset", Section.ShOffset); | |||
1308 | IO.mapOptional("ShSize", Section.ShSize); | |||
1309 | IO.mapOptional("ShFlags", Section.ShFlags); | |||
1310 | IO.mapOptional("ShType", Section.ShType); | |||
1311 | } | |||
1312 | ||||
1313 | static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) { | |||
1314 | commonSectionMapping(IO, Section); | |||
1315 | IO.mapOptional("Entries", Section.Entries); | |||
1316 | } | |||
1317 | ||||
1318 | static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { | |||
1319 | commonSectionMapping(IO, Section); | |||
1320 | ||||
1321 | // We also support reading a content as array of bytes using the ContentArray | |||
1322 | // key. obj2yaml never prints this field. | |||
1323 | assert(!IO.outputting() || !Section.ContentBuf.hasValue())(static_cast <bool> (!IO.outputting() || !Section.ContentBuf .hasValue()) ? void (0) : __assert_fail ("!IO.outputting() || !Section.ContentBuf.hasValue()" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1323, __extension__ __PRETTY_FUNCTION__ )); | |||
1324 | IO.mapOptional("ContentArray", Section.ContentBuf); | |||
1325 | if (Section.ContentBuf) { | |||
1326 | if (Section.Content) | |||
1327 | IO.setError("Content and ContentArray can't be used together"); | |||
1328 | Section.Content = yaml::BinaryRef(*Section.ContentBuf); | |||
1329 | } | |||
1330 | ||||
1331 | IO.mapOptional("Info", Section.Info); | |||
1332 | } | |||
1333 | ||||
1334 | static void sectionMapping(IO &IO, ELFYAML::BBAddrMapSection &Section) { | |||
1335 | commonSectionMapping(IO, Section); | |||
1336 | IO.mapOptional("Content", Section.Content); | |||
1337 | IO.mapOptional("Entries", Section.Entries); | |||
1338 | } | |||
1339 | ||||
1340 | static void sectionMapping(IO &IO, ELFYAML::StackSizesSection &Section) { | |||
1341 | commonSectionMapping(IO, Section); | |||
1342 | IO.mapOptional("Entries", Section.Entries); | |||
1343 | } | |||
1344 | ||||
1345 | static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) { | |||
1346 | commonSectionMapping(IO, Section); | |||
1347 | IO.mapOptional("Bucket", Section.Bucket); | |||
1348 | IO.mapOptional("Chain", Section.Chain); | |||
1349 | ||||
1350 | // obj2yaml does not dump these fields. They can be used to override nchain | |||
1351 | // and nbucket values for creating broken sections. | |||
1352 | assert(!IO.outputting() ||(static_cast <bool> (!IO.outputting() || (!Section.NBucket .hasValue() && !Section.NChain.hasValue())) ? void (0 ) : __assert_fail ("!IO.outputting() || (!Section.NBucket.hasValue() && !Section.NChain.hasValue())" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1353, __extension__ __PRETTY_FUNCTION__ )) | |||
1353 | (!Section.NBucket.hasValue() && !Section.NChain.hasValue()))(static_cast <bool> (!IO.outputting() || (!Section.NBucket .hasValue() && !Section.NChain.hasValue())) ? void (0 ) : __assert_fail ("!IO.outputting() || (!Section.NBucket.hasValue() && !Section.NChain.hasValue())" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1353, __extension__ __PRETTY_FUNCTION__ )); | |||
1354 | IO.mapOptional("NChain", Section.NChain); | |||
1355 | IO.mapOptional("NBucket", Section.NBucket); | |||
1356 | } | |||
1357 | ||||
1358 | static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) { | |||
1359 | commonSectionMapping(IO, Section); | |||
1360 | IO.mapOptional("Notes", Section.Notes); | |||
1361 | } | |||
1362 | ||||
1363 | ||||
1364 | static void sectionMapping(IO &IO, ELFYAML::GnuHashSection &Section) { | |||
1365 | commonSectionMapping(IO, Section); | |||
1366 | IO.mapOptional("Header", Section.Header); | |||
1367 | IO.mapOptional("BloomFilter", Section.BloomFilter); | |||
1368 | IO.mapOptional("HashBuckets", Section.HashBuckets); | |||
1369 | IO.mapOptional("HashValues", Section.HashValues); | |||
1370 | } | |||
1371 | static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) { | |||
1372 | commonSectionMapping(IO, Section); | |||
1373 | } | |||
1374 | ||||
1375 | static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) { | |||
1376 | commonSectionMapping(IO, Section); | |||
1377 | IO.mapOptional("Info", Section.Info); | |||
1378 | IO.mapOptional("Entries", Section.Entries); | |||
1379 | } | |||
1380 | ||||
1381 | static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) { | |||
1382 | commonSectionMapping(IO, Section); | |||
1383 | IO.mapOptional("Entries", Section.Entries); | |||
1384 | } | |||
1385 | ||||
1386 | static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) { | |||
1387 | commonSectionMapping(IO, Section); | |||
1388 | IO.mapOptional("Info", Section.Info); | |||
1389 | IO.mapOptional("Dependencies", Section.VerneedV); | |||
1390 | } | |||
1391 | ||||
1392 | static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) { | |||
1393 | commonSectionMapping(IO, Section); | |||
1394 | IO.mapOptional("Info", Section.RelocatableSec, StringRef()); | |||
1395 | IO.mapOptional("Relocations", Section.Relocations); | |||
1396 | } | |||
1397 | ||||
1398 | static void sectionMapping(IO &IO, ELFYAML::RelrSection &Section) { | |||
1399 | commonSectionMapping(IO, Section); | |||
1400 | IO.mapOptional("Entries", Section.Entries); | |||
1401 | } | |||
1402 | ||||
1403 | static void groupSectionMapping(IO &IO, ELFYAML::GroupSection &Group) { | |||
1404 | commonSectionMapping(IO, Group); | |||
1405 | IO.mapOptional("Info", Group.Signature); | |||
1406 | IO.mapOptional("Members", Group.Members); | |||
1407 | } | |||
1408 | ||||
1409 | static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) { | |||
1410 | commonSectionMapping(IO, Section); | |||
1411 | IO.mapOptional("Entries", Section.Entries); | |||
1412 | } | |||
1413 | ||||
1414 | static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) { | |||
1415 | commonSectionMapping(IO, Section); | |||
1416 | IO.mapOptional("Symbols", Section.Symbols); | |||
1417 | } | |||
1418 | ||||
1419 | static void fillMapping(IO &IO, ELFYAML::Fill &Fill) { | |||
1420 | IO.mapOptional("Name", Fill.Name, StringRef()); | |||
1421 | IO.mapOptional("Pattern", Fill.Pattern); | |||
1422 | IO.mapOptional("Offset", Fill.Offset); | |||
1423 | IO.mapRequired("Size", Fill.Size); | |||
1424 | } | |||
1425 | ||||
1426 | static void sectionHeaderTableMapping(IO &IO, | |||
1427 | ELFYAML::SectionHeaderTable &SHT) { | |||
1428 | IO.mapOptional("Offset", SHT.Offset); | |||
1429 | IO.mapOptional("Sections", SHT.Sections); | |||
1430 | IO.mapOptional("Excluded", SHT.Excluded); | |||
1431 | IO.mapOptional("NoHeaders", SHT.NoHeaders); | |||
1432 | } | |||
1433 | ||||
1434 | static void sectionMapping(IO &IO, ELFYAML::LinkerOptionsSection &Section) { | |||
1435 | commonSectionMapping(IO, Section); | |||
1436 | IO.mapOptional("Options", Section.Options); | |||
1437 | } | |||
1438 | ||||
1439 | static void sectionMapping(IO &IO, | |||
1440 | ELFYAML::DependentLibrariesSection &Section) { | |||
1441 | commonSectionMapping(IO, Section); | |||
1442 | IO.mapOptional("Libraries", Section.Libs); | |||
1443 | } | |||
1444 | ||||
1445 | static void sectionMapping(IO &IO, ELFYAML::CallGraphProfileSection &Section) { | |||
1446 | commonSectionMapping(IO, Section); | |||
1447 | IO.mapOptional("Entries", Section.Entries); | |||
1448 | } | |||
1449 | ||||
1450 | void MappingTraits<ELFYAML::SectionOrType>::mapping( | |||
1451 | IO &IO, ELFYAML::SectionOrType §ionOrType) { | |||
1452 | IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType); | |||
1453 | } | |||
1454 | ||||
1455 | static void sectionMapping(IO &IO, ELFYAML::ARMIndexTableSection &Section) { | |||
1456 | commonSectionMapping(IO, Section); | |||
1457 | IO.mapOptional("Entries", Section.Entries); | |||
1458 | } | |||
1459 | ||||
1460 | static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) { | |||
1461 | commonSectionMapping(IO, Section); | |||
1462 | IO.mapOptional("Version", Section.Version, Hex16(0)); | |||
1463 | IO.mapRequired("ISA", Section.ISALevel); | |||
1464 | IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0)); | |||
1465 | IO.mapOptional("ISAExtension", Section.ISAExtension, | |||
1466 | ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE)); | |||
1467 | IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0)); | |||
1468 | IO.mapOptional("FpABI", Section.FpABI, | |||
1469 | ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY)); | |||
1470 | IO.mapOptional("GPRSize", Section.GPRSize, | |||
1471 | ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); | |||
1472 | IO.mapOptional("CPR1Size", Section.CPR1Size, | |||
1473 | ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); | |||
1474 | IO.mapOptional("CPR2Size", Section.CPR2Size, | |||
1475 | ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); | |||
1476 | IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0)); | |||
1477 | IO.mapOptional("Flags2", Section.Flags2, Hex32(0)); | |||
1478 | } | |||
1479 | ||||
1480 | static StringRef getStringValue(IO &IO, const char *Key) { | |||
1481 | StringRef Val; | |||
1482 | IO.mapRequired(Key, Val); | |||
1483 | return Val; | |||
1484 | } | |||
1485 | ||||
1486 | static void setStringValue(IO &IO, const char *Key, StringRef Val) { | |||
1487 | IO.mapRequired(Key, Val); | |||
1488 | } | |||
1489 | ||||
1490 | static bool isInteger(StringRef Val) { | |||
1491 | APInt Tmp; | |||
1492 | return !Val.getAsInteger(0, Tmp); | |||
1493 | } | |||
1494 | ||||
1495 | void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping( | |||
1496 | IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) { | |||
1497 | ELFYAML::ELF_SHT Type; | |||
| ||||
1498 | StringRef TypeStr; | |||
1499 | if (IO.outputting()) { | |||
1500 | if (auto *S = dyn_cast<ELFYAML::Section>(Section.get())) | |||
1501 | Type = S->Type; | |||
1502 | else if (auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(Section.get())) | |||
1503 | TypeStr = SHT->TypeStr; | |||
1504 | } else { | |||
1505 | // When the Type string does not have a "SHT_" prefix, we know it is not a | |||
1506 | // description of a regular ELF output section. | |||
1507 | TypeStr = getStringValue(IO, "Type"); | |||
1508 | if (TypeStr.startswith("SHT_") || isInteger(TypeStr)) | |||
1509 | IO.mapRequired("Type", Type); | |||
1510 | } | |||
1511 | ||||
1512 | if (TypeStr == "Fill") { | |||
1513 | assert(!IO.outputting())(static_cast <bool> (!IO.outputting()) ? void (0) : __assert_fail ("!IO.outputting()", "llvm/lib/ObjectYAML/ELFYAML.cpp", 1513 , __extension__ __PRETTY_FUNCTION__)); // We don't dump fills currently. | |||
1514 | Section.reset(new ELFYAML::Fill()); | |||
1515 | fillMapping(IO, *cast<ELFYAML::Fill>(Section.get())); | |||
1516 | return; | |||
1517 | } | |||
1518 | ||||
1519 | if (TypeStr == ELFYAML::SectionHeaderTable::TypeStr) { | |||
1520 | if (IO.outputting()) | |||
1521 | setStringValue(IO, "Type", TypeStr); | |||
1522 | else | |||
1523 | Section.reset(new ELFYAML::SectionHeaderTable(/*IsImplicit=*/false)); | |||
1524 | ||||
1525 | sectionHeaderTableMapping( | |||
1526 | IO, *cast<ELFYAML::SectionHeaderTable>(Section.get())); | |||
1527 | return; | |||
1528 | } | |||
1529 | ||||
1530 | const auto &Obj = *static_cast<ELFYAML::Object *>(IO.getContext()); | |||
1531 | if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) { | |||
1532 | if (!IO.outputting()) | |||
1533 | Section.reset(new ELFYAML::MipsABIFlags()); | |||
1534 | sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get())); | |||
1535 | return; | |||
1536 | } | |||
1537 | ||||
1538 | if (Obj.getMachine() == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX) { | |||
1539 | if (!IO.outputting()) | |||
1540 | Section.reset(new ELFYAML::ARMIndexTableSection()); | |||
1541 | sectionMapping(IO, *cast<ELFYAML::ARMIndexTableSection>(Section.get())); | |||
1542 | return; | |||
1543 | } | |||
1544 | ||||
1545 | switch (Type) { | |||
1546 | case ELF::SHT_DYNAMIC: | |||
1547 | if (!IO.outputting()) | |||
1548 | Section.reset(new ELFYAML::DynamicSection()); | |||
1549 | sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get())); | |||
1550 | break; | |||
1551 | case ELF::SHT_REL: | |||
1552 | case ELF::SHT_RELA: | |||
1553 | if (!IO.outputting()) | |||
1554 | Section.reset(new ELFYAML::RelocationSection()); | |||
1555 | sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get())); | |||
1556 | break; | |||
1557 | case ELF::SHT_RELR: | |||
1558 | if (!IO.outputting()) | |||
1559 | Section.reset(new ELFYAML::RelrSection()); | |||
1560 | sectionMapping(IO, *cast<ELFYAML::RelrSection>(Section.get())); | |||
1561 | break; | |||
1562 | case ELF::SHT_GROUP: | |||
1563 | if (!IO.outputting()) | |||
1564 | Section.reset(new ELFYAML::GroupSection()); | |||
1565 | groupSectionMapping(IO, *cast<ELFYAML::GroupSection>(Section.get())); | |||
1566 | break; | |||
1567 | case ELF::SHT_NOBITS: | |||
1568 | if (!IO.outputting()) | |||
1569 | Section.reset(new ELFYAML::NoBitsSection()); | |||
1570 | sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get())); | |||
1571 | break; | |||
1572 | case ELF::SHT_HASH: | |||
1573 | if (!IO.outputting()) | |||
1574 | Section.reset(new ELFYAML::HashSection()); | |||
1575 | sectionMapping(IO, *cast<ELFYAML::HashSection>(Section.get())); | |||
1576 | break; | |||
1577 | case ELF::SHT_NOTE: | |||
1578 | if (!IO.outputting()) | |||
1579 | Section.reset(new ELFYAML::NoteSection()); | |||
1580 | sectionMapping(IO, *cast<ELFYAML::NoteSection>(Section.get())); | |||
1581 | break; | |||
1582 | case ELF::SHT_GNU_HASH: | |||
1583 | if (!IO.outputting()) | |||
1584 | Section.reset(new ELFYAML::GnuHashSection()); | |||
1585 | sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get())); | |||
1586 | break; | |||
1587 | case ELF::SHT_GNU_verdef: | |||
1588 | if (!IO.outputting()) | |||
1589 | Section.reset(new ELFYAML::VerdefSection()); | |||
1590 | sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get())); | |||
1591 | break; | |||
1592 | case ELF::SHT_GNU_versym: | |||
1593 | if (!IO.outputting()) | |||
1594 | Section.reset(new ELFYAML::SymverSection()); | |||
1595 | sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get())); | |||
1596 | break; | |||
1597 | case ELF::SHT_GNU_verneed: | |||
1598 | if (!IO.outputting()) | |||
1599 | Section.reset(new ELFYAML::VerneedSection()); | |||
1600 | sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get())); | |||
1601 | break; | |||
1602 | case ELF::SHT_SYMTAB_SHNDX: | |||
1603 | if (!IO.outputting()) | |||
1604 | Section.reset(new ELFYAML::SymtabShndxSection()); | |||
1605 | sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get())); | |||
1606 | break; | |||
1607 | case ELF::SHT_LLVM_ADDRSIG: | |||
1608 | if (!IO.outputting()) | |||
1609 | Section.reset(new ELFYAML::AddrsigSection()); | |||
1610 | sectionMapping(IO, *cast<ELFYAML::AddrsigSection>(Section.get())); | |||
1611 | break; | |||
1612 | case ELF::SHT_LLVM_LINKER_OPTIONS: | |||
1613 | if (!IO.outputting()) | |||
1614 | Section.reset(new ELFYAML::LinkerOptionsSection()); | |||
1615 | sectionMapping(IO, *cast<ELFYAML::LinkerOptionsSection>(Section.get())); | |||
1616 | break; | |||
1617 | case ELF::SHT_LLVM_DEPENDENT_LIBRARIES: | |||
1618 | if (!IO.outputting()) | |||
1619 | Section.reset(new ELFYAML::DependentLibrariesSection()); | |||
1620 | sectionMapping(IO, | |||
1621 | *cast<ELFYAML::DependentLibrariesSection>(Section.get())); | |||
1622 | break; | |||
1623 | case ELF::SHT_LLVM_CALL_GRAPH_PROFILE: | |||
1624 | if (!IO.outputting()) | |||
1625 | Section.reset(new ELFYAML::CallGraphProfileSection()); | |||
1626 | sectionMapping(IO, *cast<ELFYAML::CallGraphProfileSection>(Section.get())); | |||
1627 | break; | |||
1628 | case ELF::SHT_LLVM_BB_ADDR_MAP: | |||
1629 | if (!IO.outputting()) | |||
1630 | Section.reset(new ELFYAML::BBAddrMapSection()); | |||
1631 | sectionMapping(IO, *cast<ELFYAML::BBAddrMapSection>(Section.get())); | |||
1632 | break; | |||
1633 | default: | |||
1634 | if (!IO.outputting()) { | |||
1635 | StringRef Name; | |||
1636 | IO.mapOptional("Name", Name, StringRef()); | |||
1637 | Name = ELFYAML::dropUniqueSuffix(Name); | |||
1638 | ||||
1639 | if (ELFYAML::StackSizesSection::nameMatches(Name)) | |||
1640 | Section = std::make_unique<ELFYAML::StackSizesSection>(); | |||
1641 | else | |||
1642 | Section = std::make_unique<ELFYAML::RawContentSection>(); | |||
1643 | } | |||
1644 | ||||
1645 | if (auto S = dyn_cast<ELFYAML::RawContentSection>(Section.get())) | |||
1646 | sectionMapping(IO, *S); | |||
1647 | else | |||
1648 | sectionMapping(IO, *cast<ELFYAML::StackSizesSection>(Section.get())); | |||
1649 | } | |||
1650 | } | |||
1651 | ||||
1652 | std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate( | |||
1653 | IO &io, std::unique_ptr<ELFYAML::Chunk> &C) { | |||
1654 | if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) { | |||
1655 | if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size) | |||
1656 | return "\"Size\" can't be 0 when \"Pattern\" is not empty"; | |||
1657 | return ""; | |||
1658 | } | |||
1659 | ||||
1660 | if (const auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) { | |||
1661 | if (SHT->NoHeaders && (SHT->Sections || SHT->Excluded || SHT->Offset)) | |||
1662 | return "NoHeaders can't be used together with Offset/Sections/Excluded"; | |||
1663 | return ""; | |||
1664 | } | |||
1665 | ||||
1666 | const ELFYAML::Section &Sec = *cast<ELFYAML::Section>(C.get()); | |||
1667 | if (Sec.Size && Sec.Content && | |||
1668 | (uint64_t)(*Sec.Size) < Sec.Content->binary_size()) | |||
1669 | return "Section size must be greater than or equal to the content size"; | |||
1670 | ||||
1671 | auto BuildErrPrefix = [](ArrayRef<std::pair<StringRef, bool>> EntV) { | |||
1672 | std::string Msg; | |||
1673 | for (size_t I = 0, E = EntV.size(); I != E; ++I) { | |||
1674 | StringRef Name = EntV[I].first; | |||
1675 | if (I == 0) { | |||
1676 | Msg = "\"" + Name.str() + "\""; | |||
1677 | continue; | |||
1678 | } | |||
1679 | if (I != EntV.size() - 1) | |||
1680 | Msg += ", \"" + Name.str() + "\""; | |||
1681 | else | |||
1682 | Msg += " and \"" + Name.str() + "\""; | |||
1683 | } | |||
1684 | return Msg; | |||
1685 | }; | |||
1686 | ||||
1687 | std::vector<std::pair<StringRef, bool>> Entries = Sec.getEntries(); | |||
1688 | const size_t NumUsedEntries = llvm::count_if( | |||
1689 | Entries, [](const std::pair<StringRef, bool> &P) { return P.second; }); | |||
1690 | ||||
1691 | if ((Sec.Size || Sec.Content) && NumUsedEntries > 0) | |||
1692 | return BuildErrPrefix(Entries) + | |||
1693 | " cannot be used with \"Content\" or \"Size\""; | |||
1694 | ||||
1695 | if (NumUsedEntries > 0 && Entries.size() != NumUsedEntries) | |||
1696 | return BuildErrPrefix(Entries) + " must be used together"; | |||
1697 | ||||
1698 | if (const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(C.get())) { | |||
1699 | if (RawSection->Flags && RawSection->ShFlags) | |||
1700 | return "ShFlags and Flags cannot be used together"; | |||
1701 | return ""; | |||
1702 | } | |||
1703 | ||||
1704 | if (const auto *NB = dyn_cast<ELFYAML::NoBitsSection>(C.get())) { | |||
1705 | if (NB->Content) | |||
1706 | return "SHT_NOBITS section cannot have \"Content\""; | |||
1707 | return ""; | |||
1708 | } | |||
1709 | ||||
1710 | if (const auto *MF = dyn_cast<ELFYAML::MipsABIFlags>(C.get())) { | |||
1711 | if (MF->Content) | |||
1712 | return "\"Content\" key is not implemented for SHT_MIPS_ABIFLAGS " | |||
1713 | "sections"; | |||
1714 | if (MF->Size) | |||
1715 | return "\"Size\" key is not implemented for SHT_MIPS_ABIFLAGS sections"; | |||
1716 | return ""; | |||
1717 | } | |||
1718 | ||||
1719 | return ""; | |||
1720 | } | |||
1721 | ||||
1722 | namespace { | |||
1723 | ||||
1724 | struct NormalizedMips64RelType { | |||
1725 | NormalizedMips64RelType(IO &) | |||
1726 | : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), | |||
1727 | Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), | |||
1728 | Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), | |||
1729 | SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {} | |||
1730 | NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original) | |||
1731 | : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF), | |||
1732 | Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {} | |||
1733 | ||||
1734 | ELFYAML::ELF_REL denormalize(IO &) { | |||
1735 | ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24; | |||
1736 | return Res; | |||
1737 | } | |||
1738 | ||||
1739 | ELFYAML::ELF_REL Type; | |||
1740 | ELFYAML::ELF_REL Type2; | |||
1741 | ELFYAML::ELF_REL Type3; | |||
1742 | ELFYAML::ELF_RSS SpecSym; | |||
1743 | }; | |||
1744 | ||||
1745 | } // end anonymous namespace | |||
1746 | ||||
1747 | void MappingTraits<ELFYAML::StackSizeEntry>::mapping( | |||
1748 | IO &IO, ELFYAML::StackSizeEntry &E) { | |||
1749 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1749, __extension__ __PRETTY_FUNCTION__ )); | |||
1750 | IO.mapOptional("Address", E.Address, Hex64(0)); | |||
1751 | IO.mapRequired("Size", E.Size); | |||
1752 | } | |||
1753 | ||||
1754 | void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping( | |||
1755 | IO &IO, ELFYAML::BBAddrMapEntry &E) { | |||
1756 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1756, __extension__ __PRETTY_FUNCTION__ )); | |||
1757 | IO.mapOptional("Address", E.Address, Hex64(0)); | |||
1758 | IO.mapOptional("NumBlocks", E.NumBlocks); | |||
1759 | IO.mapOptional("BBEntries", E.BBEntries); | |||
1760 | } | |||
1761 | ||||
1762 | void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping( | |||
1763 | IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &E) { | |||
1764 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1764, __extension__ __PRETTY_FUNCTION__ )); | |||
1765 | IO.mapRequired("AddressOffset", E.AddressOffset); | |||
1766 | IO.mapRequired("Size", E.Size); | |||
1767 | IO.mapRequired("Metadata", E.Metadata); | |||
1768 | } | |||
1769 | ||||
1770 | void MappingTraits<ELFYAML::GnuHashHeader>::mapping(IO &IO, | |||
1771 | ELFYAML::GnuHashHeader &E) { | |||
1772 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1772, __extension__ __PRETTY_FUNCTION__ )); | |||
1773 | IO.mapOptional("NBuckets", E.NBuckets); | |||
1774 | IO.mapRequired("SymNdx", E.SymNdx); | |||
1775 | IO.mapOptional("MaskWords", E.MaskWords); | |||
1776 | IO.mapRequired("Shift2", E.Shift2); | |||
1777 | } | |||
1778 | ||||
1779 | void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO, | |||
1780 | ELFYAML::DynamicEntry &Rel) { | |||
1781 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1781, __extension__ __PRETTY_FUNCTION__ )); | |||
1782 | ||||
1783 | IO.mapRequired("Tag", Rel.Tag); | |||
1784 | IO.mapRequired("Value", Rel.Val); | |||
1785 | } | |||
1786 | ||||
1787 | void MappingTraits<ELFYAML::NoteEntry>::mapping(IO &IO, ELFYAML::NoteEntry &N) { | |||
1788 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1788, __extension__ __PRETTY_FUNCTION__ )); | |||
1789 | ||||
1790 | IO.mapOptional("Name", N.Name); | |||
1791 | IO.mapOptional("Desc", N.Desc); | |||
1792 | IO.mapRequired("Type", N.Type); | |||
1793 | } | |||
1794 | ||||
1795 | void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO, | |||
1796 | ELFYAML::VerdefEntry &E) { | |||
1797 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1797, __extension__ __PRETTY_FUNCTION__ )); | |||
1798 | ||||
1799 | IO.mapOptional("Version", E.Version); | |||
1800 | IO.mapOptional("Flags", E.Flags); | |||
1801 | IO.mapOptional("VersionNdx", E.VersionNdx); | |||
1802 | IO.mapOptional("Hash", E.Hash); | |||
1803 | IO.mapRequired("Names", E.VerNames); | |||
1804 | } | |||
1805 | ||||
1806 | void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO, | |||
1807 | ELFYAML::VerneedEntry &E) { | |||
1808 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1808, __extension__ __PRETTY_FUNCTION__ )); | |||
1809 | ||||
1810 | IO.mapRequired("Version", E.Version); | |||
1811 | IO.mapRequired("File", E.File); | |||
1812 | IO.mapRequired("Entries", E.AuxV); | |||
1813 | } | |||
1814 | ||||
1815 | void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO, | |||
1816 | ELFYAML::VernauxEntry &E) { | |||
1817 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1817, __extension__ __PRETTY_FUNCTION__ )); | |||
1818 | ||||
1819 | IO.mapRequired("Name", E.Name); | |||
1820 | IO.mapRequired("Hash", E.Hash); | |||
1821 | IO.mapRequired("Flags", E.Flags); | |||
1822 | IO.mapRequired("Other", E.Other); | |||
1823 | } | |||
1824 | ||||
1825 | void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO, | |||
1826 | ELFYAML::Relocation &Rel) { | |||
1827 | const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); | |||
1828 | assert(Object && "The IO context is not initialized")(static_cast <bool> (Object && "The IO context is not initialized" ) ? void (0) : __assert_fail ("Object && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1828, __extension__ __PRETTY_FUNCTION__ )); | |||
1829 | ||||
1830 | IO.mapOptional("Offset", Rel.Offset, (Hex64)0); | |||
1831 | IO.mapOptional("Symbol", Rel.Symbol); | |||
1832 | ||||
1833 | if (Object->getMachine() == ELFYAML::ELF_EM(ELF::EM_MIPS) && | |||
1834 | Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { | |||
1835 | MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key( | |||
1836 | IO, Rel.Type); | |||
1837 | IO.mapRequired("Type", Key->Type); | |||
1838 | IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); | |||
1839 | IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); | |||
1840 | IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF)); | |||
1841 | } else | |||
1842 | IO.mapRequired("Type", Rel.Type); | |||
1843 | ||||
1844 | IO.mapOptional("Addend", Rel.Addend, (ELFYAML::YAMLIntUInt)0); | |||
1845 | } | |||
1846 | ||||
1847 | void MappingTraits<ELFYAML::ARMIndexTableEntry>::mapping( | |||
1848 | IO &IO, ELFYAML::ARMIndexTableEntry &E) { | |||
1849 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1849, __extension__ __PRETTY_FUNCTION__ )); | |||
1850 | IO.mapRequired("Offset", E.Offset); | |||
1851 | ||||
1852 | StringRef CantUnwind = "EXIDX_CANTUNWIND"; | |||
1853 | if (IO.outputting() && (uint32_t)E.Value == ARM::EHABI::EXIDX_CANTUNWIND) | |||
1854 | IO.mapRequired("Value", CantUnwind); | |||
1855 | else if (!IO.outputting() && getStringValue(IO, "Value") == CantUnwind) | |||
1856 | E.Value = ARM::EHABI::EXIDX_CANTUNWIND; | |||
1857 | else | |||
1858 | IO.mapRequired("Value", E.Value); | |||
1859 | } | |||
1860 | ||||
1861 | void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) { | |||
1862 | assert(!IO.getContext() && "The IO context is initialized already")(static_cast <bool> (!IO.getContext() && "The IO context is initialized already" ) ? void (0) : __assert_fail ("!IO.getContext() && \"The IO context is initialized already\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1862, __extension__ __PRETTY_FUNCTION__ )); | |||
1863 | IO.setContext(&Object); | |||
1864 | IO.mapTag("!ELF", true); | |||
1865 | IO.mapRequired("FileHeader", Object.Header); | |||
1866 | IO.mapOptional("ProgramHeaders", Object.ProgramHeaders); | |||
1867 | IO.mapOptional("Sections", Object.Chunks); | |||
1868 | IO.mapOptional("Symbols", Object.Symbols); | |||
1869 | IO.mapOptional("DynamicSymbols", Object.DynamicSymbols); | |||
1870 | IO.mapOptional("DWARF", Object.DWARF); | |||
1871 | if (Object.DWARF) { | |||
1872 | Object.DWARF->IsLittleEndian = | |||
1873 | Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); | |||
1874 | Object.DWARF->Is64BitAddrSize = | |||
1875 | Object.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); | |||
1876 | } | |||
1877 | IO.setContext(nullptr); | |||
1878 | } | |||
1879 | ||||
1880 | void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO, | |||
1881 | ELFYAML::LinkerOption &Opt) { | |||
1882 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1882, __extension__ __PRETTY_FUNCTION__ )); | |||
1883 | IO.mapRequired("Name", Opt.Key); | |||
1884 | IO.mapRequired("Value", Opt.Value); | |||
1885 | } | |||
1886 | ||||
1887 | void MappingTraits<ELFYAML::CallGraphEntryWeight>::mapping( | |||
1888 | IO &IO, ELFYAML::CallGraphEntryWeight &E) { | |||
1889 | assert(IO.getContext() && "The IO context is not initialized")(static_cast <bool> (IO.getContext() && "The IO context is not initialized" ) ? void (0) : __assert_fail ("IO.getContext() && \"The IO context is not initialized\"" , "llvm/lib/ObjectYAML/ELFYAML.cpp", 1889, __extension__ __PRETTY_FUNCTION__ )); | |||
1890 | IO.mapRequired("Weight", E.Weight); | |||
1891 | } | |||
1892 | ||||
1893 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)struct MIPS_AFL_REG { MIPS_AFL_REG() = default; MIPS_AFL_REG( const uint8_t v) : value(v) {} MIPS_AFL_REG(const MIPS_AFL_REG &v) = default; MIPS_AFL_REG &operator=(const MIPS_AFL_REG &rhs) = default; MIPS_AFL_REG &operator=(const uint8_t &rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const MIPS_AFL_REG &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_REG &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
1894 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)struct MIPS_ABI_FP { MIPS_ABI_FP() = default; MIPS_ABI_FP(const uint8_t v) : value(v) {} MIPS_ABI_FP(const MIPS_ABI_FP & v) = default; MIPS_ABI_FP &operator=(const MIPS_ABI_FP & rhs) = default; MIPS_ABI_FP &operator=(const uint8_t & rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const MIPS_ABI_FP &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const MIPS_ABI_FP &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
1895 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)struct MIPS_AFL_EXT { MIPS_AFL_EXT() = default; MIPS_AFL_EXT( const uint32_t v) : value(v) {} MIPS_AFL_EXT(const MIPS_AFL_EXT &v) = default; MIPS_AFL_EXT &operator=(const MIPS_AFL_EXT &rhs) = default; MIPS_AFL_EXT &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_AFL_EXT &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_EXT &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
1896 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)struct MIPS_AFL_ASE { MIPS_AFL_ASE() = default; MIPS_AFL_ASE( const uint32_t v) : value(v) {} MIPS_AFL_ASE(const MIPS_AFL_ASE &v) = default; MIPS_AFL_ASE &operator=(const MIPS_AFL_ASE &rhs) = default; MIPS_AFL_ASE &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_AFL_ASE &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_ASE &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
1897 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)struct MIPS_AFL_FLAGS1 { MIPS_AFL_FLAGS1() = default; MIPS_AFL_FLAGS1 (const uint32_t v) : value(v) {} MIPS_AFL_FLAGS1(const MIPS_AFL_FLAGS1 &v) = default; MIPS_AFL_FLAGS1 &operator=(const MIPS_AFL_FLAGS1 &rhs) = default; MIPS_AFL_FLAGS1 &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_AFL_FLAGS1 &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_FLAGS1 &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
1898 | ||||
1899 | } // end namespace yaml | |||
1900 | ||||
1901 | } // end namespace llvm |
1 | //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | /// | |||
9 | /// \file | |||
10 | /// This file declares classes for handling the YAML representation | |||
11 | /// of ELF. | |||
12 | /// | |||
13 | //===----------------------------------------------------------------------===// | |||
14 | ||||
15 | #ifndef LLVM_OBJECTYAML_ELFYAML_H | |||
16 | #define LLVM_OBJECTYAML_ELFYAML_H | |||
17 | ||||
18 | #include "llvm/ADT/StringRef.h" | |||
19 | #include "llvm/BinaryFormat/ELF.h" | |||
20 | #include "llvm/Object/ELFTypes.h" | |||
21 | #include "llvm/ObjectYAML/DWARFYAML.h" | |||
22 | #include "llvm/ObjectYAML/YAML.h" | |||
23 | #include "llvm/Support/YAMLTraits.h" | |||
24 | #include <cstdint> | |||
25 | #include <memory> | |||
26 | #include <vector> | |||
27 | ||||
28 | namespace llvm { | |||
29 | namespace ELFYAML { | |||
30 | ||||
31 | StringRef dropUniqueSuffix(StringRef S); | |||
32 | std::string appendUniqueSuffix(StringRef Name, const Twine& Msg); | |||
33 | ||||
34 | // These types are invariant across 32/64-bit ELF, so for simplicity just | |||
35 | // directly give them their exact sizes. We don't need to worry about | |||
36 | // endianness because these are just the types in the YAMLIO structures, | |||
37 | // and are appropriately converted to the necessary endianness when | |||
38 | // reading/generating binary object files. | |||
39 | // The naming of these types is intended to be ELF_PREFIX, where PREFIX is | |||
40 | // the common prefix of the respective constants. E.g. ELF_EM corresponds | |||
41 | // to the `e_machine` constants, like `EM_X86_64`. | |||
42 | // In the future, these would probably be better suited by C++11 enum | |||
43 | // class's with appropriate fixed underlying type. | |||
44 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)struct ELF_ET { ELF_ET() = default; ELF_ET(const uint16_t v) : value(v) {} ELF_ET(const ELF_ET &v) = default; ELF_ET & operator=(const ELF_ET &rhs) = default; ELF_ET &operator =(const uint16_t &rhs) { value = rhs; return *this; } operator const uint16_t & () const { return value; } bool operator ==(const ELF_ET &rhs) const { return value == rhs.value; } bool operator==(const uint16_t &rhs) const { return value == rhs; } bool operator<(const ELF_ET &rhs) const { return value < rhs.value; } uint16_t value; using BaseType = uint16_t ; }; | |||
45 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT)struct ELF_PT { ELF_PT() = default; ELF_PT(const uint32_t v) : value(v) {} ELF_PT(const ELF_PT &v) = default; ELF_PT & operator=(const ELF_PT &rhs) = default; ELF_PT &operator =(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator ==(const ELF_PT &rhs) const { return value == rhs.value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const ELF_PT &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t ; }; | |||
46 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)struct ELF_EM { ELF_EM() = default; ELF_EM(const uint32_t v) : value(v) {} ELF_EM(const ELF_EM &v) = default; ELF_EM & operator=(const ELF_EM &rhs) = default; ELF_EM &operator =(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator ==(const ELF_EM &rhs) const { return value == rhs.value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const ELF_EM &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t ; }; | |||
47 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)struct ELF_ELFCLASS { ELF_ELFCLASS() = default; ELF_ELFCLASS( const uint8_t v) : value(v) {} ELF_ELFCLASS(const ELF_ELFCLASS &v) = default; ELF_ELFCLASS &operator=(const ELF_ELFCLASS &rhs) = default; ELF_ELFCLASS &operator=(const uint8_t &rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const ELF_ELFCLASS &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_ELFCLASS &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
48 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)struct ELF_ELFDATA { ELF_ELFDATA() = default; ELF_ELFDATA(const uint8_t v) : value(v) {} ELF_ELFDATA(const ELF_ELFDATA & v) = default; ELF_ELFDATA &operator=(const ELF_ELFDATA & rhs) = default; ELF_ELFDATA &operator=(const uint8_t & rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const ELF_ELFDATA &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_ELFDATA &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
49 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)struct ELF_ELFOSABI { ELF_ELFOSABI() = default; ELF_ELFOSABI( const uint8_t v) : value(v) {} ELF_ELFOSABI(const ELF_ELFOSABI &v) = default; ELF_ELFOSABI &operator=(const ELF_ELFOSABI &rhs) = default; ELF_ELFOSABI &operator=(const uint8_t &rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const ELF_ELFOSABI &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_ELFOSABI &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
50 | // Just use 64, since it can hold 32-bit values too. | |||
51 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)struct ELF_EF { ELF_EF() = default; ELF_EF(const uint64_t v) : value(v) {} ELF_EF(const ELF_EF &v) = default; ELF_EF & operator=(const ELF_EF &rhs) = default; ELF_EF &operator =(const uint64_t &rhs) { value = rhs; return *this; } operator const uint64_t & () const { return value; } bool operator ==(const ELF_EF &rhs) const { return value == rhs.value; } bool operator==(const uint64_t &rhs) const { return value == rhs; } bool operator<(const ELF_EF &rhs) const { return value < rhs.value; } uint64_t value; using BaseType = uint64_t ; }; | |||
52 | // Just use 64, since it can hold 32-bit values too. | |||
53 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG)struct ELF_DYNTAG { ELF_DYNTAG() = default; ELF_DYNTAG(const uint64_t v) : value(v) {} ELF_DYNTAG(const ELF_DYNTAG &v) = default ; ELF_DYNTAG &operator=(const ELF_DYNTAG &rhs) = default ; ELF_DYNTAG &operator=(const uint64_t &rhs) { value = rhs; return *this; } operator const uint64_t & () const { return value; } bool operator==(const ELF_DYNTAG &rhs) const { return value == rhs.value; } bool operator==(const uint64_t &rhs) const { return value == rhs; } bool operator<(const ELF_DYNTAG &rhs) const { return value < rhs.value; } uint64_t value; using BaseType = uint64_t; }; | |||
54 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)struct ELF_PF { ELF_PF() = default; ELF_PF(const uint32_t v) : value(v) {} ELF_PF(const ELF_PF &v) = default; ELF_PF & operator=(const ELF_PF &rhs) = default; ELF_PF &operator =(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator ==(const ELF_PF &rhs) const { return value == rhs.value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const ELF_PF &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t ; }; | |||
55 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)struct ELF_SHT { ELF_SHT() = default; ELF_SHT(const uint32_t v ) : value(v) {} ELF_SHT(const ELF_SHT &v) = default; ELF_SHT &operator=(const ELF_SHT &rhs) = default; ELF_SHT & operator=(const uint32_t &rhs) { value = rhs; return *this ; } operator const uint32_t & () const { return value; } bool operator==(const ELF_SHT &rhs) const { return value == rhs .value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const ELF_SHT &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
| ||||
56 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)struct ELF_REL { ELF_REL() = default; ELF_REL(const uint32_t v ) : value(v) {} ELF_REL(const ELF_REL &v) = default; ELF_REL &operator=(const ELF_REL &rhs) = default; ELF_REL & operator=(const uint32_t &rhs) { value = rhs; return *this ; } operator const uint32_t & () const { return value; } bool operator==(const ELF_REL &rhs) const { return value == rhs .value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const ELF_REL &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
57 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)struct ELF_RSS { ELF_RSS() = default; ELF_RSS(const uint8_t v ) : value(v) {} ELF_RSS(const ELF_RSS &v) = default; ELF_RSS &operator=(const ELF_RSS &rhs) = default; ELF_RSS & operator=(const uint8_t &rhs) { value = rhs; return *this ; } operator const uint8_t & () const { return value; } bool operator==(const ELF_RSS &rhs) const { return value == rhs .value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_RSS &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; }; | |||
58 | // Just use 64, since it can hold 32-bit values too. | |||
59 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)struct ELF_SHF { ELF_SHF() = default; ELF_SHF(const uint64_t v ) : value(v) {} ELF_SHF(const ELF_SHF &v) = default; ELF_SHF &operator=(const ELF_SHF &rhs) = default; ELF_SHF & operator=(const uint64_t &rhs) { value = rhs; return *this ; } operator const uint64_t & () const { return value; } bool operator==(const ELF_SHF &rhs) const { return value == rhs .value; } bool operator==(const uint64_t &rhs) const { return value == rhs; } bool operator<(const ELF_SHF &rhs) const { return value < rhs.value; } uint64_t value; using BaseType = uint64_t; }; | |||
60 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)struct ELF_SHN { ELF_SHN() = default; ELF_SHN(const uint16_t v ) : value(v) {} ELF_SHN(const ELF_SHN &v) = default; ELF_SHN &operator=(const ELF_SHN &rhs) = default; ELF_SHN & operator=(const uint16_t &rhs) { value = rhs; return *this ; } operator const uint16_t & () const { return value; } bool operator==(const ELF_SHN &rhs) const { return value == rhs .value; } bool operator==(const uint16_t &rhs) const { return value == rhs; } bool operator<(const ELF_SHN &rhs) const { return value < rhs.value; } uint16_t value; using BaseType = uint16_t; }; | |||
61 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STB)struct ELF_STB { ELF_STB() = default; ELF_STB(const uint8_t v ) : value(v) {} ELF_STB(const ELF_STB &v) = default; ELF_STB &operator=(const ELF_STB &rhs) = default; ELF_STB & operator=(const uint8_t &rhs) { value = rhs; return *this ; } operator const uint8_t & () const { return value; } bool operator==(const ELF_STB &rhs) const { return value == rhs .value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_STB &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; }; | |||
62 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)struct ELF_STT { ELF_STT() = default; ELF_STT(const uint8_t v ) : value(v) {} ELF_STT(const ELF_STT &v) = default; ELF_STT &operator=(const ELF_STT &rhs) = default; ELF_STT & operator=(const uint8_t &rhs) { value = rhs; return *this ; } operator const uint8_t & () const { return value; } bool operator==(const ELF_STT &rhs) const { return value == rhs .value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const ELF_STT &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; }; | |||
63 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_NT)struct ELF_NT { ELF_NT() = default; ELF_NT(const uint32_t v) : value(v) {} ELF_NT(const ELF_NT &v) = default; ELF_NT & operator=(const ELF_NT &rhs) = default; ELF_NT &operator =(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator ==(const ELF_NT &rhs) const { return value == rhs.value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const ELF_NT &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t ; }; | |||
64 | ||||
65 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)struct MIPS_AFL_REG { MIPS_AFL_REG() = default; MIPS_AFL_REG( const uint8_t v) : value(v) {} MIPS_AFL_REG(const MIPS_AFL_REG &v) = default; MIPS_AFL_REG &operator=(const MIPS_AFL_REG &rhs) = default; MIPS_AFL_REG &operator=(const uint8_t &rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const MIPS_AFL_REG &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_REG &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
66 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)struct MIPS_ABI_FP { MIPS_ABI_FP() = default; MIPS_ABI_FP(const uint8_t v) : value(v) {} MIPS_ABI_FP(const MIPS_ABI_FP & v) = default; MIPS_ABI_FP &operator=(const MIPS_ABI_FP & rhs) = default; MIPS_ABI_FP &operator=(const uint8_t & rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const MIPS_ABI_FP &rhs) const { return value == rhs.value; } bool operator ==(const uint8_t &rhs) const { return value == rhs; } bool operator<(const MIPS_ABI_FP &rhs) const { return value < rhs.value; } uint8_t value; using BaseType = uint8_t; } ; | |||
67 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)struct MIPS_AFL_EXT { MIPS_AFL_EXT() = default; MIPS_AFL_EXT( const uint32_t v) : value(v) {} MIPS_AFL_EXT(const MIPS_AFL_EXT &v) = default; MIPS_AFL_EXT &operator=(const MIPS_AFL_EXT &rhs) = default; MIPS_AFL_EXT &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_AFL_EXT &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_EXT &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
68 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)struct MIPS_AFL_ASE { MIPS_AFL_ASE() = default; MIPS_AFL_ASE( const uint32_t v) : value(v) {} MIPS_AFL_ASE(const MIPS_AFL_ASE &v) = default; MIPS_AFL_ASE &operator=(const MIPS_AFL_ASE &rhs) = default; MIPS_AFL_ASE &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_AFL_ASE &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_ASE &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
69 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)struct MIPS_AFL_FLAGS1 { MIPS_AFL_FLAGS1() = default; MIPS_AFL_FLAGS1 (const uint32_t v) : value(v) {} MIPS_AFL_FLAGS1(const MIPS_AFL_FLAGS1 &v) = default; MIPS_AFL_FLAGS1 &operator=(const MIPS_AFL_FLAGS1 &rhs) = default; MIPS_AFL_FLAGS1 &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_AFL_FLAGS1 &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_AFL_FLAGS1 &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
70 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)struct MIPS_ISA { MIPS_ISA() = default; MIPS_ISA(const uint32_t v) : value(v) {} MIPS_ISA(const MIPS_ISA &v) = default; MIPS_ISA &operator=(const MIPS_ISA &rhs) = default; MIPS_ISA & operator=(const uint32_t &rhs) { value = rhs; return *this ; } operator const uint32_t & () const { return value; } bool operator==(const MIPS_ISA &rhs) const { return value == rhs .value; } bool operator==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const MIPS_ISA &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; | |||
71 | ||||
72 | LLVM_YAML_STRONG_TYPEDEF(StringRef, YAMLFlowString)struct YAMLFlowString { YAMLFlowString() = default; YAMLFlowString (const StringRef v) : value(v) {} YAMLFlowString(const YAMLFlowString &v) = default; YAMLFlowString &operator=(const YAMLFlowString &rhs) = default; YAMLFlowString &operator=(const StringRef &rhs) { value = rhs; return *this; } operator const StringRef & () const { return value; } bool operator==(const YAMLFlowString &rhs) const { return value == rhs.value; } bool operator ==(const StringRef &rhs) const { return value == rhs; } bool operator<(const YAMLFlowString &rhs) const { return value < rhs.value; } StringRef value; using BaseType = StringRef ; }; | |||
73 | LLVM_YAML_STRONG_TYPEDEF(int64_t, YAMLIntUInt)struct YAMLIntUInt { YAMLIntUInt() = default; YAMLIntUInt(const int64_t v) : value(v) {} YAMLIntUInt(const YAMLIntUInt & v) = default; YAMLIntUInt &operator=(const YAMLIntUInt & rhs) = default; YAMLIntUInt &operator=(const int64_t & rhs) { value = rhs; return *this; } operator const int64_t & () const { return value; } bool operator==(const YAMLIntUInt &rhs) const { return value == rhs.value; } bool operator ==(const int64_t &rhs) const { return value == rhs; } bool operator<(const YAMLIntUInt &rhs) const { return value < rhs.value; } int64_t value; using BaseType = int64_t; } ; | |||
74 | ||||
75 | template <class ELFT> | |||
76 | unsigned getDefaultShEntSize(unsigned EMachine, ELF_SHT SecType, | |||
77 | StringRef SecName) { | |||
78 | if (EMachine == ELF::EM_MIPS && SecType == ELF::SHT_MIPS_ABIFLAGS) | |||
79 | return sizeof(object::Elf_Mips_ABIFlags<ELFT>); | |||
80 | ||||
81 | switch (SecType) { | |||
82 | case ELF::SHT_SYMTAB: | |||
83 | case ELF::SHT_DYNSYM: | |||
84 | return sizeof(typename ELFT::Sym); | |||
85 | case ELF::SHT_GROUP: | |||
86 | return sizeof(typename ELFT::Word); | |||
87 | case ELF::SHT_REL: | |||
88 | return sizeof(typename ELFT::Rel); | |||
89 | case ELF::SHT_RELA: | |||
90 | return sizeof(typename ELFT::Rela); | |||
91 | case ELF::SHT_RELR: | |||
92 | return sizeof(typename ELFT::Relr); | |||
93 | case ELF::SHT_DYNAMIC: | |||
94 | return sizeof(typename ELFT::Dyn); | |||
95 | case ELF::SHT_HASH: | |||
96 | return sizeof(typename ELFT::Word); | |||
97 | case ELF::SHT_SYMTAB_SHNDX: | |||
98 | return sizeof(typename ELFT::Word); | |||
99 | case ELF::SHT_GNU_versym: | |||
100 | return sizeof(typename ELFT::Half); | |||
101 | case ELF::SHT_LLVM_CALL_GRAPH_PROFILE: | |||
102 | return sizeof(object::Elf_CGProfile_Impl<ELFT>); | |||
103 | default: | |||
104 | if (SecName == ".debug_str") | |||
105 | return 1; | |||
106 | return 0; | |||
107 | } | |||
108 | } | |||
109 | ||||
110 | // For now, hardcode 64 bits everywhere that 32 or 64 would be needed | |||
111 | // since 64-bit can hold 32-bit values too. | |||
112 | struct FileHeader { | |||
113 | ELF_ELFCLASS Class; | |||
114 | ELF_ELFDATA Data; | |||
115 | ELF_ELFOSABI OSABI; | |||
116 | llvm::yaml::Hex8 ABIVersion; | |||
117 | ELF_ET Type; | |||
118 | Optional<ELF_EM> Machine; | |||
119 | ELF_EF Flags; | |||
120 | llvm::yaml::Hex64 Entry; | |||
121 | Optional<StringRef> SectionHeaderStringTable; | |||
122 | ||||
123 | Optional<llvm::yaml::Hex64> EPhOff; | |||
124 | Optional<llvm::yaml::Hex16> EPhEntSize; | |||
125 | Optional<llvm::yaml::Hex16> EPhNum; | |||
126 | Optional<llvm::yaml::Hex16> EShEntSize; | |||
127 | Optional<llvm::yaml::Hex64> EShOff; | |||
128 | Optional<llvm::yaml::Hex16> EShNum; | |||
129 | Optional<llvm::yaml::Hex16> EShStrNdx; | |||
130 | }; | |||
131 | ||||
132 | struct SectionHeader { | |||
133 | StringRef Name; | |||
134 | }; | |||
135 | ||||
136 | struct Symbol { | |||
137 | StringRef Name; | |||
138 | ELF_STT Type; | |||
139 | Optional<StringRef> Section; | |||
140 | Optional<ELF_SHN> Index; | |||
141 | ELF_STB Binding; | |||
142 | Optional<llvm::yaml::Hex64> Value; | |||
143 | Optional<llvm::yaml::Hex64> Size; | |||
144 | Optional<uint8_t> Other; | |||
145 | ||||
146 | Optional<uint32_t> StName; | |||
147 | }; | |||
148 | ||||
149 | struct SectionOrType { | |||
150 | StringRef sectionNameOrType; | |||
151 | }; | |||
152 | ||||
153 | struct DynamicEntry { | |||
154 | ELF_DYNTAG Tag; | |||
155 | llvm::yaml::Hex64 Val; | |||
156 | }; | |||
157 | ||||
158 | struct BBAddrMapEntry { | |||
159 | struct BBEntry { | |||
160 | llvm::yaml::Hex64 AddressOffset; | |||
161 | llvm::yaml::Hex64 Size; | |||
162 | llvm::yaml::Hex64 Metadata; | |||
163 | }; | |||
164 | llvm::yaml::Hex64 Address; | |||
165 | Optional<uint64_t> NumBlocks; | |||
166 | Optional<std::vector<BBEntry>> BBEntries; | |||
167 | }; | |||
168 | ||||
169 | struct StackSizeEntry { | |||
170 | llvm::yaml::Hex64 Address; | |||
171 | llvm::yaml::Hex64 Size; | |||
172 | }; | |||
173 | ||||
174 | struct NoteEntry { | |||
175 | StringRef Name; | |||
176 | yaml::BinaryRef Desc; | |||
177 | ELF_NT Type; | |||
178 | }; | |||
179 | ||||
180 | struct Chunk { | |||
181 | enum class ChunkKind { | |||
182 | Dynamic, | |||
183 | Group, | |||
184 | RawContent, | |||
185 | Relocation, | |||
186 | Relr, | |||
187 | NoBits, | |||
188 | Note, | |||
189 | Hash, | |||
190 | GnuHash, | |||
191 | Verdef, | |||
192 | Verneed, | |||
193 | StackSizes, | |||
194 | SymtabShndxSection, | |||
195 | Symver, | |||
196 | ARMIndexTable, | |||
197 | MipsABIFlags, | |||
198 | Addrsig, | |||
199 | LinkerOptions, | |||
200 | DependentLibraries, | |||
201 | CallGraphProfile, | |||
202 | BBAddrMap, | |||
203 | ||||
204 | // Special chunks. | |||
205 | SpecialChunksStart, | |||
206 | Fill = SpecialChunksStart, | |||
207 | SectionHeaderTable, | |||
208 | }; | |||
209 | ||||
210 | ChunkKind Kind; | |||
211 | StringRef Name; | |||
212 | Optional<llvm::yaml::Hex64> Offset; | |||
213 | ||||
214 | // Usually chunks are not created implicitly, but rather loaded from YAML. | |||
215 | // This flag is used to signal whether this is the case or not. | |||
216 | bool IsImplicit; | |||
217 | ||||
218 | Chunk(ChunkKind K, bool Implicit) : Kind(K), IsImplicit(Implicit) {} | |||
219 | virtual ~Chunk(); | |||
220 | }; | |||
221 | ||||
222 | struct Section : public Chunk { | |||
223 | ELF_SHT Type; | |||
224 | Optional<ELF_SHF> Flags; | |||
225 | Optional<llvm::yaml::Hex64> Address; | |||
226 | Optional<StringRef> Link; | |||
227 | llvm::yaml::Hex64 AddressAlign; | |||
228 | Optional<llvm::yaml::Hex64> EntSize; | |||
229 | ||||
230 | Optional<yaml::BinaryRef> Content; | |||
231 | Optional<llvm::yaml::Hex64> Size; | |||
232 | ||||
233 | // Holds the original section index. | |||
234 | unsigned OriginalSecNdx; | |||
235 | ||||
236 | Section(ChunkKind Kind, bool IsImplicit = false) : Chunk(Kind, IsImplicit) {} | |||
237 | ||||
238 | static bool classof(const Chunk *S) { | |||
239 | return S->Kind < ChunkKind::SpecialChunksStart; | |||
240 | } | |||
241 | ||||
242 | // Some derived sections might have their own special entries. This method | |||
243 | // returns a vector of <entry name, is used> pairs. It is used for section | |||
244 | // validation. | |||
245 | virtual std::vector<std::pair<StringRef, bool>> getEntries() const { | |||
246 | return {}; | |||
247 | }; | |||
248 | ||||
249 | // The following members are used to override section fields which is | |||
250 | // useful for creating invalid objects. | |||
251 | ||||
252 | // This can be used to override the sh_addralign field. | |||
253 | Optional<llvm::yaml::Hex64> ShAddrAlign; | |||
254 | ||||
255 | // This can be used to override the offset stored in the sh_name field. | |||
256 | // It does not affect the name stored in the string table. | |||
257 | Optional<llvm::yaml::Hex64> ShName; | |||
258 | ||||
259 | // This can be used to override the sh_offset field. It does not place the | |||
260 | // section data at the offset specified. | |||
261 | Optional<llvm::yaml::Hex64> ShOffset; | |||
262 | ||||
263 | // This can be used to override the sh_size field. It does not affect the | |||
264 | // content written. | |||
265 | Optional<llvm::yaml::Hex64> ShSize; | |||
266 | ||||
267 | // This can be used to override the sh_flags field. | |||
268 | Optional<llvm::yaml::Hex64> ShFlags; | |||
269 | ||||
270 | // This can be used to override the sh_type field. It is useful when we | |||
271 | // want to use specific YAML keys for a section of a particular type to | |||
272 | // describe the content, but still want to have a different final type | |||
273 | // for the section. | |||
274 | Optional<ELF_SHT> ShType; | |||
275 | }; | |||
276 | ||||
277 | // Fill is a block of data which is placed outside of sections. It is | |||
278 | // not present in the sections header table, but it might affect the output file | |||
279 | // size and program headers produced. | |||
280 | struct Fill : Chunk { | |||
281 | Optional<yaml::BinaryRef> Pattern; | |||
282 | llvm::yaml::Hex64 Size; | |||
283 | ||||
284 | Fill() : Chunk(ChunkKind::Fill, /*Implicit=*/false) {} | |||
285 | ||||
286 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Fill; } | |||
287 | }; | |||
288 | ||||
289 | struct SectionHeaderTable : Chunk { | |||
290 | SectionHeaderTable(bool IsImplicit) | |||
291 | : Chunk(ChunkKind::SectionHeaderTable, IsImplicit) {} | |||
292 | ||||
293 | static bool classof(const Chunk *S) { | |||
294 | return S->Kind == ChunkKind::SectionHeaderTable; | |||
295 | } | |||
296 | ||||
297 | Optional<std::vector<SectionHeader>> Sections; | |||
298 | Optional<std::vector<SectionHeader>> Excluded; | |||
299 | Optional<bool> NoHeaders; | |||
300 | ||||
301 | size_t getNumHeaders(size_t SectionsNum) const { | |||
302 | if (IsImplicit || isDefault()) | |||
303 | return SectionsNum; | |||
304 | if (NoHeaders) | |||
305 | return (*NoHeaders) ? 0 : SectionsNum; | |||
306 | return (Sections ? Sections->size() : 0) + /*Null section*/ 1; | |||
307 | } | |||
308 | ||||
309 | bool isDefault() const { return !Sections && !Excluded && !NoHeaders; } | |||
310 | ||||
311 | static constexpr StringRef TypeStr = "SectionHeaderTable"; | |||
312 | }; | |||
313 | ||||
314 | struct BBAddrMapSection : Section { | |||
315 | Optional<std::vector<BBAddrMapEntry>> Entries; | |||
316 | ||||
317 | BBAddrMapSection() : Section(ChunkKind::BBAddrMap) {} | |||
318 | ||||
319 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
320 | return {{"Entries", Entries.hasValue()}}; | |||
321 | }; | |||
322 | ||||
323 | static bool classof(const Chunk *S) { | |||
324 | return S->Kind == ChunkKind::BBAddrMap; | |||
325 | } | |||
326 | }; | |||
327 | ||||
328 | struct StackSizesSection : Section { | |||
329 | Optional<std::vector<StackSizeEntry>> Entries; | |||
330 | ||||
331 | StackSizesSection() : Section(ChunkKind::StackSizes) {} | |||
332 | ||||
333 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
334 | return {{"Entries", Entries.hasValue()}}; | |||
335 | }; | |||
336 | ||||
337 | static bool classof(const Chunk *S) { | |||
338 | return S->Kind == ChunkKind::StackSizes; | |||
339 | } | |||
340 | ||||
341 | static bool nameMatches(StringRef Name) { | |||
342 | return Name == ".stack_sizes"; | |||
343 | } | |||
344 | }; | |||
345 | ||||
346 | struct DynamicSection : Section { | |||
347 | Optional<std::vector<DynamicEntry>> Entries; | |||
348 | ||||
349 | DynamicSection() : Section(ChunkKind::Dynamic) {} | |||
350 | ||||
351 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
352 | return {{"Entries", Entries.hasValue()}}; | |||
353 | }; | |||
354 | ||||
355 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Dynamic; } | |||
356 | }; | |||
357 | ||||
358 | struct RawContentSection : Section { | |||
359 | Optional<llvm::yaml::Hex64> Info; | |||
360 | ||||
361 | RawContentSection() : Section(ChunkKind::RawContent) {} | |||
362 | ||||
363 | static bool classof(const Chunk *S) { | |||
364 | return S->Kind == ChunkKind::RawContent; | |||
365 | } | |||
366 | ||||
367 | // Is used when a content is read as an array of bytes. | |||
368 | Optional<std::vector<uint8_t>> ContentBuf; | |||
369 | }; | |||
370 | ||||
371 | struct NoBitsSection : Section { | |||
372 | NoBitsSection() : Section(ChunkKind::NoBits) {} | |||
373 | ||||
374 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::NoBits; } | |||
375 | }; | |||
376 | ||||
377 | struct NoteSection : Section { | |||
378 | Optional<std::vector<ELFYAML::NoteEntry>> Notes; | |||
379 | ||||
380 | NoteSection() : Section(ChunkKind::Note) {} | |||
381 | ||||
382 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
383 | return {{"Notes", Notes.hasValue()}}; | |||
384 | }; | |||
385 | ||||
386 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Note; } | |||
387 | }; | |||
388 | ||||
389 | struct HashSection : Section { | |||
390 | Optional<std::vector<uint32_t>> Bucket; | |||
391 | Optional<std::vector<uint32_t>> Chain; | |||
392 | ||||
393 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
394 | return {{"Bucket", Bucket.hasValue()}, {"Chain", Chain.hasValue()}}; | |||
395 | }; | |||
396 | ||||
397 | // The following members are used to override section fields. | |||
398 | // This is useful for creating invalid objects. | |||
399 | Optional<llvm::yaml::Hex64> NBucket; | |||
400 | Optional<llvm::yaml::Hex64> NChain; | |||
401 | ||||
402 | HashSection() : Section(ChunkKind::Hash) {} | |||
403 | ||||
404 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Hash; } | |||
405 | }; | |||
406 | ||||
407 | struct GnuHashHeader { | |||
408 | // The number of hash buckets. | |||
409 | // Not used when dumping the object, but can be used to override | |||
410 | // the real number of buckets when emiting an object from a YAML document. | |||
411 | Optional<llvm::yaml::Hex32> NBuckets; | |||
412 | ||||
413 | // Index of the first symbol in the dynamic symbol table | |||
414 | // included in the hash table. | |||
415 | llvm::yaml::Hex32 SymNdx; | |||
416 | ||||
417 | // The number of words in the Bloom filter. | |||
418 | // Not used when dumping the object, but can be used to override the real | |||
419 | // number of words in the Bloom filter when emiting an object from a YAML | |||
420 | // document. | |||
421 | Optional<llvm::yaml::Hex32> MaskWords; | |||
422 | ||||
423 | // A shift constant used by the Bloom filter. | |||
424 | llvm::yaml::Hex32 Shift2; | |||
425 | }; | |||
426 | ||||
427 | struct GnuHashSection : Section { | |||
428 | Optional<GnuHashHeader> Header; | |||
429 | Optional<std::vector<llvm::yaml::Hex64>> BloomFilter; | |||
430 | Optional<std::vector<llvm::yaml::Hex32>> HashBuckets; | |||
431 | Optional<std::vector<llvm::yaml::Hex32>> HashValues; | |||
432 | ||||
433 | GnuHashSection() : Section(ChunkKind::GnuHash) {} | |||
434 | ||||
435 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
436 | return {{"Header", Header.hasValue()}, | |||
437 | {"BloomFilter", BloomFilter.hasValue()}, | |||
438 | {"HashBuckets", HashBuckets.hasValue()}, | |||
439 | {"HashValues", HashValues.hasValue()}}; | |||
440 | }; | |||
441 | ||||
442 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::GnuHash; } | |||
443 | }; | |||
444 | ||||
445 | struct VernauxEntry { | |||
446 | uint32_t Hash; | |||
447 | uint16_t Flags; | |||
448 | uint16_t Other; | |||
449 | StringRef Name; | |||
450 | }; | |||
451 | ||||
452 | struct VerneedEntry { | |||
453 | uint16_t Version; | |||
454 | StringRef File; | |||
455 | std::vector<VernauxEntry> AuxV; | |||
456 | }; | |||
457 | ||||
458 | struct VerneedSection : Section { | |||
459 | Optional<std::vector<VerneedEntry>> VerneedV; | |||
460 | Optional<llvm::yaml::Hex64> Info; | |||
461 | ||||
462 | VerneedSection() : Section(ChunkKind::Verneed) {} | |||
463 | ||||
464 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
465 | return {{"Dependencies", VerneedV.hasValue()}}; | |||
466 | }; | |||
467 | ||||
468 | static bool classof(const Chunk *S) { | |||
469 | return S->Kind == ChunkKind::Verneed; | |||
470 | } | |||
471 | }; | |||
472 | ||||
473 | struct AddrsigSection : Section { | |||
474 | Optional<std::vector<YAMLFlowString>> Symbols; | |||
475 | ||||
476 | AddrsigSection() : Section(ChunkKind::Addrsig) {} | |||
477 | ||||
478 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
479 | return {{"Symbols", Symbols.hasValue()}}; | |||
480 | }; | |||
481 | ||||
482 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Addrsig; } | |||
483 | }; | |||
484 | ||||
485 | struct LinkerOption { | |||
486 | StringRef Key; | |||
487 | StringRef Value; | |||
488 | }; | |||
489 | ||||
490 | struct LinkerOptionsSection : Section { | |||
491 | Optional<std::vector<LinkerOption>> Options; | |||
492 | ||||
493 | LinkerOptionsSection() : Section(ChunkKind::LinkerOptions) {} | |||
494 | ||||
495 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
496 | return {{"Options", Options.hasValue()}}; | |||
497 | }; | |||
498 | ||||
499 | static bool classof(const Chunk *S) { | |||
500 | return S->Kind == ChunkKind::LinkerOptions; | |||
501 | } | |||
502 | }; | |||
503 | ||||
504 | struct DependentLibrariesSection : Section { | |||
505 | Optional<std::vector<YAMLFlowString>> Libs; | |||
506 | ||||
507 | DependentLibrariesSection() : Section(ChunkKind::DependentLibraries) {} | |||
508 | ||||
509 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
510 | return {{"Libraries", Libs.hasValue()}}; | |||
511 | }; | |||
512 | ||||
513 | static bool classof(const Chunk *S) { | |||
514 | return S->Kind == ChunkKind::DependentLibraries; | |||
515 | } | |||
516 | }; | |||
517 | ||||
518 | // Represents the call graph profile section entry. | |||
519 | struct CallGraphEntryWeight { | |||
520 | // The weight of the edge. | |||
521 | uint64_t Weight; | |||
522 | }; | |||
523 | ||||
524 | struct CallGraphProfileSection : Section { | |||
525 | Optional<std::vector<CallGraphEntryWeight>> Entries; | |||
526 | ||||
527 | CallGraphProfileSection() : Section(ChunkKind::CallGraphProfile) {} | |||
528 | ||||
529 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
530 | return {{"Entries", Entries.hasValue()}}; | |||
531 | }; | |||
532 | ||||
533 | static bool classof(const Chunk *S) { | |||
534 | return S->Kind == ChunkKind::CallGraphProfile; | |||
535 | } | |||
536 | }; | |||
537 | ||||
538 | struct SymverSection : Section { | |||
539 | Optional<std::vector<uint16_t>> Entries; | |||
540 | ||||
541 | SymverSection() : Section(ChunkKind::Symver) {} | |||
542 | ||||
543 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
544 | return {{"Entries", Entries.hasValue()}}; | |||
545 | }; | |||
546 | ||||
547 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Symver; } | |||
548 | }; | |||
549 | ||||
550 | struct VerdefEntry { | |||
551 | Optional<uint16_t> Version; | |||
552 | Optional<uint16_t> Flags; | |||
553 | Optional<uint16_t> VersionNdx; | |||
554 | Optional<uint32_t> Hash; | |||
555 | std::vector<StringRef> VerNames; | |||
556 | }; | |||
557 | ||||
558 | struct VerdefSection : Section { | |||
559 | Optional<std::vector<VerdefEntry>> Entries; | |||
560 | Optional<llvm::yaml::Hex64> Info; | |||
561 | ||||
562 | VerdefSection() : Section(ChunkKind::Verdef) {} | |||
563 | ||||
564 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
565 | return {{"Entries", Entries.hasValue()}}; | |||
566 | }; | |||
567 | ||||
568 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Verdef; } | |||
569 | }; | |||
570 | ||||
571 | struct GroupSection : Section { | |||
572 | // Members of a group contain a flag and a list of section indices | |||
573 | // that are part of the group. | |||
574 | Optional<std::vector<SectionOrType>> Members; | |||
575 | Optional<StringRef> Signature; /* Info */ | |||
576 | ||||
577 | GroupSection() : Section(ChunkKind::Group) {} | |||
578 | ||||
579 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
580 | return {{"Members", Members.hasValue()}}; | |||
581 | }; | |||
582 | ||||
583 | static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Group; } | |||
584 | }; | |||
585 | ||||
586 | struct Relocation { | |||
587 | llvm::yaml::Hex64 Offset; | |||
588 | YAMLIntUInt Addend; | |||
589 | ELF_REL Type; | |||
590 | Optional<StringRef> Symbol; | |||
591 | }; | |||
592 | ||||
593 | struct RelocationSection : Section { | |||
594 | Optional<std::vector<Relocation>> Relocations; | |||
595 | StringRef RelocatableSec; /* Info */ | |||
596 | ||||
597 | RelocationSection() : Section(ChunkKind::Relocation) {} | |||
598 | ||||
599 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
600 | return {{"Relocations", Relocations.hasValue()}}; | |||
601 | }; | |||
602 | ||||
603 | static bool classof(const Chunk *S) { | |||
604 | return S->Kind == ChunkKind::Relocation; | |||
605 | } | |||
606 | }; | |||
607 | ||||
608 | struct RelrSection : Section { | |||
609 | Optional<std::vector<llvm::yaml::Hex64>> Entries; | |||
610 | ||||
611 | RelrSection() : Section(ChunkKind::Relr) {} | |||
612 | ||||
613 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
614 | return {{"Entries", Entries.hasValue()}}; | |||
615 | }; | |||
616 | ||||
617 | static bool classof(const Chunk *S) { | |||
618 | return S->Kind == ChunkKind::Relr; | |||
619 | } | |||
620 | }; | |||
621 | ||||
622 | struct SymtabShndxSection : Section { | |||
623 | Optional<std::vector<uint32_t>> Entries; | |||
624 | ||||
625 | SymtabShndxSection() : Section(ChunkKind::SymtabShndxSection) {} | |||
626 | ||||
627 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
628 | return {{"Entries", Entries.hasValue()}}; | |||
629 | }; | |||
630 | ||||
631 | static bool classof(const Chunk *S) { | |||
632 | return S->Kind == ChunkKind::SymtabShndxSection; | |||
633 | } | |||
634 | }; | |||
635 | ||||
636 | struct ARMIndexTableEntry { | |||
637 | llvm::yaml::Hex32 Offset; | |||
638 | llvm::yaml::Hex32 Value; | |||
639 | }; | |||
640 | ||||
641 | struct ARMIndexTableSection : Section { | |||
642 | Optional<std::vector<ARMIndexTableEntry>> Entries; | |||
643 | ||||
644 | ARMIndexTableSection() : Section(ChunkKind::ARMIndexTable) {} | |||
645 | ||||
646 | std::vector<std::pair<StringRef, bool>> getEntries() const override { | |||
647 | return {{"Entries", Entries.hasValue()}}; | |||
648 | }; | |||
649 | ||||
650 | static bool classof(const Chunk *S) { | |||
651 | return S->Kind == ChunkKind::ARMIndexTable; | |||
652 | } | |||
653 | }; | |||
654 | ||||
655 | // Represents .MIPS.abiflags section | |||
656 | struct MipsABIFlags : Section { | |||
657 | llvm::yaml::Hex16 Version; | |||
658 | MIPS_ISA ISALevel; | |||
659 | llvm::yaml::Hex8 ISARevision; | |||
660 | MIPS_AFL_REG GPRSize; | |||
661 | MIPS_AFL_REG CPR1Size; | |||
662 | MIPS_AFL_REG CPR2Size; | |||
663 | MIPS_ABI_FP FpABI; | |||
664 | MIPS_AFL_EXT ISAExtension; | |||
665 | MIPS_AFL_ASE ASEs; | |||
666 | MIPS_AFL_FLAGS1 Flags1; | |||
667 | llvm::yaml::Hex32 Flags2; | |||
668 | ||||
669 | MipsABIFlags() : Section(ChunkKind::MipsABIFlags) {} | |||
670 | ||||
671 | static bool classof(const Chunk *S) { | |||
672 | return S->Kind == ChunkKind::MipsABIFlags; | |||
673 | } | |||
674 | }; | |||
675 | ||||
676 | struct ProgramHeader { | |||
677 | ELF_PT Type; | |||
678 | ELF_PF Flags; | |||
679 | llvm::yaml::Hex64 VAddr; | |||
680 | llvm::yaml::Hex64 PAddr; | |||
681 | Optional<llvm::yaml::Hex64> Align; | |||
682 | Optional<llvm::yaml::Hex64> FileSize; | |||
683 | Optional<llvm::yaml::Hex64> MemSize; | |||
684 | Optional<llvm::yaml::Hex64> Offset; | |||
685 | Optional<StringRef> FirstSec; | |||
686 | Optional<StringRef> LastSec; | |||
687 | ||||
688 | // This vector contains all chunks from [FirstSec, LastSec]. | |||
689 | std::vector<Chunk *> Chunks; | |||
690 | }; | |||
691 | ||||
692 | struct Object { | |||
693 | FileHeader Header; | |||
694 | std::vector<ProgramHeader> ProgramHeaders; | |||
695 | ||||
696 | // An object might contain output section descriptions as well as | |||
697 | // custom data that does not belong to any section. | |||
698 | std::vector<std::unique_ptr<Chunk>> Chunks; | |||
699 | ||||
700 | // Although in reality the symbols reside in a section, it is a lot | |||
701 | // cleaner and nicer if we read them from the YAML as a separate | |||
702 | // top-level key, which automatically ensures that invariants like there | |||
703 | // being a single SHT_SYMTAB section are upheld. | |||
704 | Optional<std::vector<Symbol>> Symbols; | |||
705 | Optional<std::vector<Symbol>> DynamicSymbols; | |||
706 | Optional<DWARFYAML::Data> DWARF; | |||
707 | ||||
708 | std::vector<Section *> getSections() { | |||
709 | std::vector<Section *> Ret; | |||
710 | for (const std::unique_ptr<Chunk> &Sec : Chunks) | |||
711 | if (auto S = dyn_cast<ELFYAML::Section>(Sec.get())) | |||
712 | Ret.push_back(S); | |||
713 | return Ret; | |||
714 | } | |||
715 | ||||
716 | const SectionHeaderTable &getSectionHeaderTable() const { | |||
717 | for (const std::unique_ptr<Chunk> &C : Chunks) | |||
718 | if (auto *S = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) | |||
719 | return *S; | |||
720 | llvm_unreachable("the section header table chunk must always be present")::llvm::llvm_unreachable_internal("the section header table chunk must always be present" , "llvm/include/llvm/ObjectYAML/ELFYAML.h", 720); | |||
721 | } | |||
722 | ||||
723 | ELF_ELFOSABI getOSAbi() const; | |||
724 | unsigned getMachine() const; | |||
725 | }; | |||
726 | ||||
727 | bool shouldAllocateFileSpace(ArrayRef<ProgramHeader> Phdrs, | |||
728 | const NoBitsSection &S); | |||
729 | ||||
730 | } // end namespace ELFYAML | |||
731 | } // end namespace llvm | |||
732 | ||||
733 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::StackSizeEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::StackSizeEntry>::value && !std:: is_same<llvm::ELFYAML::StackSizeEntry, std::string>::value && !std::is_same<llvm::ELFYAML::StackSizeEntry, llvm ::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::StackSizeEntry> { static const bool flow = false; }; } } | |||
734 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::BBAddrMapEntry>::value && !std:: is_same<llvm::ELFYAML::BBAddrMapEntry, std::string>::value && !std::is_same<llvm::ELFYAML::BBAddrMapEntry, llvm ::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::BBAddrMapEntry> { static const bool flow = false; }; } } | |||
735 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry::BBEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::BBAddrMapEntry::BBEntry>::value && !std::is_same<llvm::ELFYAML::BBAddrMapEntry::BBEntry, std ::string>::value && !std::is_same<llvm::ELFYAML ::BBAddrMapEntry::BBEntry, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::BBAddrMapEntry::BBEntry> { static const bool flow = false ; }; } } | |||
736 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::DynamicEntry>::value && !std::is_same <llvm::ELFYAML::DynamicEntry, std::string>::value && !std::is_same<llvm::ELFYAML::DynamicEntry, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::DynamicEntry> { static const bool flow = false; }; } } | |||
737 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::LinkerOption)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::LinkerOption>::value && !std::is_same <llvm::ELFYAML::LinkerOption, std::string>::value && !std::is_same<llvm::ELFYAML::LinkerOption, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::LinkerOption> { static const bool flow = false; }; } } | |||
738 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::CallGraphEntryWeight)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::CallGraphEntryWeight>::value && !std::is_same<llvm::ELFYAML::CallGraphEntryWeight, std::string >::value && !std::is_same<llvm::ELFYAML::CallGraphEntryWeight , llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::CallGraphEntryWeight> { static const bool flow = false; } ; } } | |||
739 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::NoteEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::NoteEntry>::value && !std::is_same <llvm::ELFYAML::NoteEntry, std::string>::value && !std::is_same<llvm::ELFYAML::NoteEntry, llvm::StringRef> ::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::NoteEntry> { static const bool flow = false; }; } } | |||
740 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::ProgramHeader>::value && !std:: is_same<llvm::ELFYAML::ProgramHeader, std::string>::value && !std::is_same<llvm::ELFYAML::ProgramHeader, llvm ::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::ProgramHeader> { static const bool flow = false; }; } } | |||
741 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionHeader)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::SectionHeader>::value && !std:: is_same<llvm::ELFYAML::SectionHeader, std::string>::value && !std::is_same<llvm::ELFYAML::SectionHeader, llvm ::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::SectionHeader> { static const bool flow = false; }; } } | |||
742 | LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Chunk>)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <std::unique_ptr<llvm::ELFYAML::Chunk> >::value && !std::is_same<std::unique_ptr<llvm::ELFYAML::Chunk> , std::string>::value && !std::is_same<std::unique_ptr <llvm::ELFYAML::Chunk>, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<std::unique_ptr <llvm::ELFYAML::Chunk> > { static const bool flow = false ; }; } } | |||
743 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::Symbol>::value && !std::is_same <llvm::ELFYAML::Symbol, std::string>::value && ! std::is_same<llvm::ELFYAML::Symbol, llvm::StringRef>::value , "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::Symbol> { static const bool flow = false; }; } } | |||
744 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerdefEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::VerdefEntry>::value && !std::is_same <llvm::ELFYAML::VerdefEntry, std::string>::value && !std::is_same<llvm::ELFYAML::VerdefEntry, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::VerdefEntry> { static const bool flow = false; }; } } | |||
745 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VernauxEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::VernauxEntry>::value && !std::is_same <llvm::ELFYAML::VernauxEntry, std::string>::value && !std::is_same<llvm::ELFYAML::VernauxEntry, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::VernauxEntry> { static const bool flow = false; }; } } | |||
746 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerneedEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::VerneedEntry>::value && !std::is_same <llvm::ELFYAML::VerneedEntry, std::string>::value && !std::is_same<llvm::ELFYAML::VerneedEntry, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::VerneedEntry> { static const bool flow = false; }; } } | |||
747 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::Relocation>::value && !std::is_same <llvm::ELFYAML::Relocation, std::string>::value && !std::is_same<llvm::ELFYAML::Relocation, llvm::StringRef> ::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::Relocation> { static const bool flow = false; }; } } | |||
748 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::SectionOrType>::value && !std:: is_same<llvm::ELFYAML::SectionOrType, std::string>::value && !std::is_same<llvm::ELFYAML::SectionOrType, llvm ::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::SectionOrType> { static const bool flow = false; }; } } | |||
749 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ARMIndexTableEntry)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::ELFYAML::ARMIndexTableEntry>::value && ! std::is_same<llvm::ELFYAML::ARMIndexTableEntry, std::string >::value && !std::is_same<llvm::ELFYAML::ARMIndexTableEntry , llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::ELFYAML ::ARMIndexTableEntry> { static const bool flow = false; }; } } | |||
750 | ||||
751 | namespace llvm { | |||
752 | namespace yaml { | |||
753 | ||||
754 | template <> struct ScalarTraits<ELFYAML::YAMLIntUInt> { | |||
755 | static void output(const ELFYAML::YAMLIntUInt &Val, void *Ctx, | |||
756 | raw_ostream &Out); | |||
757 | static StringRef input(StringRef Scalar, void *Ctx, | |||
758 | ELFYAML::YAMLIntUInt &Val); | |||
759 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } | |||
760 | }; | |||
761 | ||||
762 | template <> | |||
763 | struct ScalarEnumerationTraits<ELFYAML::ELF_ET> { | |||
764 | static void enumeration(IO &IO, ELFYAML::ELF_ET &Value); | |||
765 | }; | |||
766 | ||||
767 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> { | |||
768 | static void enumeration(IO &IO, ELFYAML::ELF_PT &Value); | |||
769 | }; | |||
770 | ||||
771 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_NT> { | |||
772 | static void enumeration(IO &IO, ELFYAML::ELF_NT &Value); | |||
773 | }; | |||
774 | ||||
775 | template <> | |||
776 | struct ScalarEnumerationTraits<ELFYAML::ELF_EM> { | |||
777 | static void enumeration(IO &IO, ELFYAML::ELF_EM &Value); | |||
778 | }; | |||
779 | ||||
780 | template <> | |||
781 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> { | |||
782 | static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value); | |||
783 | }; | |||
784 | ||||
785 | template <> | |||
786 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> { | |||
787 | static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value); | |||
788 | }; | |||
789 | ||||
790 | template <> | |||
791 | struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> { | |||
792 | static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value); | |||
793 | }; | |||
794 | ||||
795 | template <> | |||
796 | struct ScalarBitSetTraits<ELFYAML::ELF_EF> { | |||
797 | static void bitset(IO &IO, ELFYAML::ELF_EF &Value); | |||
798 | }; | |||
799 | ||||
800 | template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> { | |||
801 | static void bitset(IO &IO, ELFYAML::ELF_PF &Value); | |||
802 | }; | |||
803 | ||||
804 | template <> | |||
805 | struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> { | |||
806 | static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value); | |||
807 | }; | |||
808 | ||||
809 | template <> | |||
810 | struct ScalarBitSetTraits<ELFYAML::ELF_SHF> { | |||
811 | static void bitset(IO &IO, ELFYAML::ELF_SHF &Value); | |||
812 | }; | |||
813 | ||||
814 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> { | |||
815 | static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value); | |||
816 | }; | |||
817 | ||||
818 | template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> { | |||
819 | static void enumeration(IO &IO, ELFYAML::ELF_STB &Value); | |||
820 | }; | |||
821 | ||||
822 | template <> | |||
823 | struct ScalarEnumerationTraits<ELFYAML::ELF_STT> { | |||
824 | static void enumeration(IO &IO, ELFYAML::ELF_STT &Value); | |||
825 | }; | |||
826 | ||||
827 | template <> | |||
828 | struct ScalarEnumerationTraits<ELFYAML::ELF_REL> { | |||
829 | static void enumeration(IO &IO, ELFYAML::ELF_REL &Value); | |||
830 | }; | |||
831 | ||||
832 | template <> | |||
833 | struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> { | |||
834 | static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value); | |||
835 | }; | |||
836 | ||||
837 | template <> | |||
838 | struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> { | |||
839 | static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value); | |||
840 | }; | |||
841 | ||||
842 | template <> | |||
843 | struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> { | |||
844 | static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value); | |||
845 | }; | |||
846 | ||||
847 | template <> | |||
848 | struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> { | |||
849 | static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value); | |||
850 | }; | |||
851 | ||||
852 | template <> | |||
853 | struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> { | |||
854 | static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value); | |||
855 | }; | |||
856 | ||||
857 | template <> | |||
858 | struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> { | |||
859 | static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value); | |||
860 | }; | |||
861 | ||||
862 | template <> | |||
863 | struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> { | |||
864 | static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value); | |||
865 | }; | |||
866 | ||||
867 | template <> | |||
868 | struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> { | |||
869 | static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value); | |||
870 | }; | |||
871 | ||||
872 | template <> | |||
873 | struct MappingTraits<ELFYAML::FileHeader> { | |||
874 | static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr); | |||
875 | }; | |||
876 | ||||
877 | template <> struct MappingTraits<ELFYAML::SectionHeader> { | |||
878 | static void mapping(IO &IO, ELFYAML::SectionHeader &SHdr); | |||
879 | }; | |||
880 | ||||
881 | template <> struct MappingTraits<ELFYAML::ProgramHeader> { | |||
882 | static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr); | |||
883 | static std::string validate(IO &IO, ELFYAML::ProgramHeader &FileHdr); | |||
884 | }; | |||
885 | ||||
886 | template <> | |||
887 | struct MappingTraits<ELFYAML::Symbol> { | |||
888 | static void mapping(IO &IO, ELFYAML::Symbol &Symbol); | |||
889 | static std::string validate(IO &IO, ELFYAML::Symbol &Symbol); | |||
890 | }; | |||
891 | ||||
892 | template <> struct MappingTraits<ELFYAML::StackSizeEntry> { | |||
893 | static void mapping(IO &IO, ELFYAML::StackSizeEntry &Rel); | |||
894 | }; | |||
895 | ||||
896 | template <> struct MappingTraits<ELFYAML::BBAddrMapEntry> { | |||
897 | static void mapping(IO &IO, ELFYAML::BBAddrMapEntry &Rel); | |||
898 | }; | |||
899 | ||||
900 | template <> struct MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry> { | |||
901 | static void mapping(IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &Rel); | |||
902 | }; | |||
903 | ||||
904 | template <> struct MappingTraits<ELFYAML::GnuHashHeader> { | |||
905 | static void mapping(IO &IO, ELFYAML::GnuHashHeader &Rel); | |||
906 | }; | |||
907 | ||||
908 | template <> struct MappingTraits<ELFYAML::DynamicEntry> { | |||
909 | static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel); | |||
910 | }; | |||
911 | ||||
912 | template <> struct MappingTraits<ELFYAML::NoteEntry> { | |||
913 | static void mapping(IO &IO, ELFYAML::NoteEntry &N); | |||
914 | }; | |||
915 | ||||
916 | template <> struct MappingTraits<ELFYAML::VerdefEntry> { | |||
917 | static void mapping(IO &IO, ELFYAML::VerdefEntry &E); | |||
918 | }; | |||
919 | ||||
920 | template <> struct MappingTraits<ELFYAML::VerneedEntry> { | |||
921 | static void mapping(IO &IO, ELFYAML::VerneedEntry &E); | |||
922 | }; | |||
923 | ||||
924 | template <> struct MappingTraits<ELFYAML::VernauxEntry> { | |||
925 | static void mapping(IO &IO, ELFYAML::VernauxEntry &E); | |||
926 | }; | |||
927 | ||||
928 | template <> struct MappingTraits<ELFYAML::LinkerOption> { | |||
929 | static void mapping(IO &IO, ELFYAML::LinkerOption &Sym); | |||
930 | }; | |||
931 | ||||
932 | template <> struct MappingTraits<ELFYAML::CallGraphEntryWeight> { | |||
933 | static void mapping(IO &IO, ELFYAML::CallGraphEntryWeight &E); | |||
934 | }; | |||
935 | ||||
936 | template <> struct MappingTraits<ELFYAML::Relocation> { | |||
937 | static void mapping(IO &IO, ELFYAML::Relocation &Rel); | |||
938 | }; | |||
939 | ||||
940 | template <> struct MappingTraits<ELFYAML::ARMIndexTableEntry> { | |||
941 | static void mapping(IO &IO, ELFYAML::ARMIndexTableEntry &E); | |||
942 | }; | |||
943 | ||||
944 | template <> struct MappingTraits<std::unique_ptr<ELFYAML::Chunk>> { | |||
945 | static void mapping(IO &IO, std::unique_ptr<ELFYAML::Chunk> &C); | |||
946 | static std::string validate(IO &io, std::unique_ptr<ELFYAML::Chunk> &C); | |||
947 | }; | |||
948 | ||||
949 | template <> | |||
950 | struct MappingTraits<ELFYAML::Object> { | |||
951 | static void mapping(IO &IO, ELFYAML::Object &Object); | |||
952 | }; | |||
953 | ||||
954 | template <> struct MappingTraits<ELFYAML::SectionOrType> { | |||
955 | static void mapping(IO &IO, ELFYAML::SectionOrType §ionOrType); | |||
956 | }; | |||
957 | ||||
958 | } // end namespace yaml | |||
959 | } // end namespace llvm | |||
960 | ||||
961 | #endif // LLVM_OBJECTYAML_ELFYAML_H |
1 | //===- llvm/Support/YAMLTraits.h --------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_SUPPORT_YAMLTRAITS_H |
10 | #define LLVM_SUPPORT_YAMLTRAITS_H |
11 | |
12 | #include "llvm/ADT/BitVector.h" |
13 | #include "llvm/ADT/Optional.h" |
14 | #include "llvm/ADT/SmallVector.h" |
15 | #include "llvm/ADT/StringExtras.h" |
16 | #include "llvm/ADT/StringMap.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "llvm/ADT/Twine.h" |
19 | #include "llvm/Support/AlignOf.h" |
20 | #include "llvm/Support/Allocator.h" |
21 | #include "llvm/Support/Endian.h" |
22 | #include "llvm/Support/SMLoc.h" |
23 | #include "llvm/Support/SourceMgr.h" |
24 | #include "llvm/Support/YAMLParser.h" |
25 | #include "llvm/Support/raw_ostream.h" |
26 | #include <cassert> |
27 | #include <map> |
28 | #include <memory> |
29 | #include <new> |
30 | #include <string> |
31 | #include <system_error> |
32 | #include <type_traits> |
33 | #include <vector> |
34 | |
35 | namespace llvm { |
36 | |
37 | class VersionTuple; |
38 | |
39 | namespace yaml { |
40 | |
41 | enum class NodeKind : uint8_t { |
42 | Scalar, |
43 | Map, |
44 | Sequence, |
45 | }; |
46 | |
47 | struct EmptyContext {}; |
48 | |
49 | /// This class should be specialized by any type that needs to be converted |
50 | /// to/from a YAML mapping. For example: |
51 | /// |
52 | /// struct MappingTraits<MyStruct> { |
53 | /// static void mapping(IO &io, MyStruct &s) { |
54 | /// io.mapRequired("name", s.name); |
55 | /// io.mapRequired("size", s.size); |
56 | /// io.mapOptional("age", s.age); |
57 | /// } |
58 | /// }; |
59 | template<class T> |
60 | struct MappingTraits { |
61 | // Must provide: |
62 | // static void mapping(IO &io, T &fields); |
63 | // Optionally may provide: |
64 | // static std::string validate(IO &io, T &fields); |
65 | // static void enumInput(IO &io, T &value); |
66 | // |
67 | // The optional flow flag will cause generated YAML to use a flow mapping |
68 | // (e.g. { a: 0, b: 1 }): |
69 | // static const bool flow = true; |
70 | }; |
71 | |
72 | /// This class is similar to MappingTraits<T> but allows you to pass in |
73 | /// additional context for each map operation. For example: |
74 | /// |
75 | /// struct MappingContextTraits<MyStruct, MyContext> { |
76 | /// static void mapping(IO &io, MyStruct &s, MyContext &c) { |
77 | /// io.mapRequired("name", s.name); |
78 | /// io.mapRequired("size", s.size); |
79 | /// io.mapOptional("age", s.age); |
80 | /// ++c.TimesMapped; |
81 | /// } |
82 | /// }; |
83 | template <class T, class Context> struct MappingContextTraits { |
84 | // Must provide: |
85 | // static void mapping(IO &io, T &fields, Context &Ctx); |
86 | // Optionally may provide: |
87 | // static std::string validate(IO &io, T &fields, Context &Ctx); |
88 | // |
89 | // The optional flow flag will cause generated YAML to use a flow mapping |
90 | // (e.g. { a: 0, b: 1 }): |
91 | // static const bool flow = true; |
92 | }; |
93 | |
94 | /// This class should be specialized by any integral type that converts |
95 | /// to/from a YAML scalar where there is a one-to-one mapping between |
96 | /// in-memory values and a string in YAML. For example: |
97 | /// |
98 | /// struct ScalarEnumerationTraits<Colors> { |
99 | /// static void enumeration(IO &io, Colors &value) { |
100 | /// io.enumCase(value, "red", cRed); |
101 | /// io.enumCase(value, "blue", cBlue); |
102 | /// io.enumCase(value, "green", cGreen); |
103 | /// } |
104 | /// }; |
105 | template <typename T, typename Enable = void> struct ScalarEnumerationTraits { |
106 | // Must provide: |
107 | // static void enumeration(IO &io, T &value); |
108 | }; |
109 | |
110 | /// This class should be specialized by any integer type that is a union |
111 | /// of bit values and the YAML representation is a flow sequence of |
112 | /// strings. For example: |
113 | /// |
114 | /// struct ScalarBitSetTraits<MyFlags> { |
115 | /// static void bitset(IO &io, MyFlags &value) { |
116 | /// io.bitSetCase(value, "big", flagBig); |
117 | /// io.bitSetCase(value, "flat", flagFlat); |
118 | /// io.bitSetCase(value, "round", flagRound); |
119 | /// } |
120 | /// }; |
121 | template <typename T, typename Enable = void> struct ScalarBitSetTraits { |
122 | // Must provide: |
123 | // static void bitset(IO &io, T &value); |
124 | }; |
125 | |
126 | /// Describe which type of quotes should be used when quoting is necessary. |
127 | /// Some non-printable characters need to be double-quoted, while some others |
128 | /// are fine with simple-quoting, and some don't need any quoting. |
129 | enum class QuotingType { None, Single, Double }; |
130 | |
131 | /// This class should be specialized by type that requires custom conversion |
132 | /// to/from a yaml scalar. For example: |
133 | /// |
134 | /// template<> |
135 | /// struct ScalarTraits<MyType> { |
136 | /// static void output(const MyType &val, void*, llvm::raw_ostream &out) { |
137 | /// // stream out custom formatting |
138 | /// out << llvm::format("%x", val); |
139 | /// } |
140 | /// static StringRef input(StringRef scalar, void*, MyType &value) { |
141 | /// // parse scalar and set `value` |
142 | /// // return empty string on success, or error string |
143 | /// return StringRef(); |
144 | /// } |
145 | /// static QuotingType mustQuote(StringRef) { return QuotingType::Single; } |
146 | /// }; |
147 | template <typename T, typename Enable = void> struct ScalarTraits { |
148 | // Must provide: |
149 | // |
150 | // Function to write the value as a string: |
151 | // static void output(const T &value, void *ctxt, llvm::raw_ostream &out); |
152 | // |
153 | // Function to convert a string to a value. Returns the empty |
154 | // StringRef on success or an error string if string is malformed: |
155 | // static StringRef input(StringRef scalar, void *ctxt, T &value); |
156 | // |
157 | // Function to determine if the value should be quoted. |
158 | // static QuotingType mustQuote(StringRef); |
159 | }; |
160 | |
161 | /// This class should be specialized by type that requires custom conversion |
162 | /// to/from a YAML literal block scalar. For example: |
163 | /// |
164 | /// template <> |
165 | /// struct BlockScalarTraits<MyType> { |
166 | /// static void output(const MyType &Value, void*, llvm::raw_ostream &Out) |
167 | /// { |
168 | /// // stream out custom formatting |
169 | /// Out << Value; |
170 | /// } |
171 | /// static StringRef input(StringRef Scalar, void*, MyType &Value) { |
172 | /// // parse scalar and set `value` |
173 | /// // return empty string on success, or error string |
174 | /// return StringRef(); |
175 | /// } |
176 | /// }; |
177 | template <typename T> |
178 | struct BlockScalarTraits { |
179 | // Must provide: |
180 | // |
181 | // Function to write the value as a string: |
182 | // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out); |
183 | // |
184 | // Function to convert a string to a value. Returns the empty |
185 | // StringRef on success or an error string if string is malformed: |
186 | // static StringRef input(StringRef Scalar, void *ctxt, T &Value); |
187 | // |
188 | // Optional: |
189 | // static StringRef inputTag(T &Val, std::string Tag) |
190 | // static void outputTag(const T &Val, raw_ostream &Out) |
191 | }; |
192 | |
193 | /// This class should be specialized by type that requires custom conversion |
194 | /// to/from a YAML scalar with optional tags. For example: |
195 | /// |
196 | /// template <> |
197 | /// struct TaggedScalarTraits<MyType> { |
198 | /// static void output(const MyType &Value, void*, llvm::raw_ostream |
199 | /// &ScalarOut, llvm::raw_ostream &TagOut) |
200 | /// { |
201 | /// // stream out custom formatting including optional Tag |
202 | /// Out << Value; |
203 | /// } |
204 | /// static StringRef input(StringRef Scalar, StringRef Tag, void*, MyType |
205 | /// &Value) { |
206 | /// // parse scalar and set `value` |
207 | /// // return empty string on success, or error string |
208 | /// return StringRef(); |
209 | /// } |
210 | /// static QuotingType mustQuote(const MyType &Value, StringRef) { |
211 | /// return QuotingType::Single; |
212 | /// } |
213 | /// }; |
214 | template <typename T> struct TaggedScalarTraits { |
215 | // Must provide: |
216 | // |
217 | // Function to write the value and tag as strings: |
218 | // static void output(const T &Value, void *ctx, llvm::raw_ostream &ScalarOut, |
219 | // llvm::raw_ostream &TagOut); |
220 | // |
221 | // Function to convert a string to a value. Returns the empty |
222 | // StringRef on success or an error string if string is malformed: |
223 | // static StringRef input(StringRef Scalar, StringRef Tag, void *ctxt, T |
224 | // &Value); |
225 | // |
226 | // Function to determine if the value should be quoted. |
227 | // static QuotingType mustQuote(const T &Value, StringRef Scalar); |
228 | }; |
229 | |
230 | /// This class should be specialized by any type that needs to be converted |
231 | /// to/from a YAML sequence. For example: |
232 | /// |
233 | /// template<> |
234 | /// struct SequenceTraits<MyContainer> { |
235 | /// static size_t size(IO &io, MyContainer &seq) { |
236 | /// return seq.size(); |
237 | /// } |
238 | /// static MyType& element(IO &, MyContainer &seq, size_t index) { |
239 | /// if ( index >= seq.size() ) |
240 | /// seq.resize(index+1); |
241 | /// return seq[index]; |
242 | /// } |
243 | /// }; |
244 | template<typename T, typename EnableIf = void> |
245 | struct SequenceTraits { |
246 | // Must provide: |
247 | // static size_t size(IO &io, T &seq); |
248 | // static T::value_type& element(IO &io, T &seq, size_t index); |
249 | // |
250 | // The following is option and will cause generated YAML to use |
251 | // a flow sequence (e.g. [a,b,c]). |
252 | // static const bool flow = true; |
253 | }; |
254 | |
255 | /// This class should be specialized by any type for which vectors of that |
256 | /// type need to be converted to/from a YAML sequence. |
257 | template<typename T, typename EnableIf = void> |
258 | struct SequenceElementTraits { |
259 | // Must provide: |
260 | // static const bool flow; |
261 | }; |
262 | |
263 | /// This class should be specialized by any type that needs to be converted |
264 | /// to/from a list of YAML documents. |
265 | template<typename T> |
266 | struct DocumentListTraits { |
267 | // Must provide: |
268 | // static size_t size(IO &io, T &seq); |
269 | // static T::value_type& element(IO &io, T &seq, size_t index); |
270 | }; |
271 | |
272 | /// This class should be specialized by any type that needs to be converted |
273 | /// to/from a YAML mapping in the case where the names of the keys are not known |
274 | /// in advance, e.g. a string map. |
275 | template <typename T> |
276 | struct CustomMappingTraits { |
277 | // static void inputOne(IO &io, StringRef key, T &elem); |
278 | // static void output(IO &io, T &elem); |
279 | }; |
280 | |
281 | /// This class should be specialized by any type that can be represented as |
282 | /// a scalar, map, or sequence, decided dynamically. For example: |
283 | /// |
284 | /// typedef std::unique_ptr<MyBase> MyPoly; |
285 | /// |
286 | /// template<> |
287 | /// struct PolymorphicTraits<MyPoly> { |
288 | /// static NodeKind getKind(const MyPoly &poly) { |
289 | /// return poly->getKind(); |
290 | /// } |
291 | /// static MyScalar& getAsScalar(MyPoly &poly) { |
292 | /// if (!poly || !isa<MyScalar>(poly)) |
293 | /// poly.reset(new MyScalar()); |
294 | /// return *cast<MyScalar>(poly.get()); |
295 | /// } |
296 | /// // ... |
297 | /// }; |
298 | template <typename T> struct PolymorphicTraits { |
299 | // Must provide: |
300 | // static NodeKind getKind(const T &poly); |
301 | // static scalar_type &getAsScalar(T &poly); |
302 | // static map_type &getAsMap(T &poly); |
303 | // static sequence_type &getAsSequence(T &poly); |
304 | }; |
305 | |
306 | // Only used for better diagnostics of missing traits |
307 | template <typename T> |
308 | struct MissingTrait; |
309 | |
310 | // Test if ScalarEnumerationTraits<T> is defined on type T. |
311 | template <class T> |
312 | struct has_ScalarEnumerationTraits |
313 | { |
314 | using Signature_enumeration = void (*)(class IO&, T&); |
315 | |
316 | template <typename U> |
317 | static char test(SameType<Signature_enumeration, &U::enumeration>*); |
318 | |
319 | template <typename U> |
320 | static double test(...); |
321 | |
322 | static bool const value = |
323 | (sizeof(test<ScalarEnumerationTraits<T>>(nullptr)) == 1); |
324 | }; |
325 | |
326 | // Test if ScalarBitSetTraits<T> is defined on type T. |
327 | template <class T> |
328 | struct has_ScalarBitSetTraits |
329 | { |
330 | using Signature_bitset = void (*)(class IO&, T&); |
331 | |
332 | template <typename U> |
333 | static char test(SameType<Signature_bitset, &U::bitset>*); |
334 | |
335 | template <typename U> |
336 | static double test(...); |
337 | |
338 | static bool const value = (sizeof(test<ScalarBitSetTraits<T>>(nullptr)) == 1); |
339 | }; |
340 | |
341 | // Test if ScalarTraits<T> is defined on type T. |
342 | template <class T> |
343 | struct has_ScalarTraits |
344 | { |
345 | using Signature_input = StringRef (*)(StringRef, void*, T&); |
346 | using Signature_output = void (*)(const T&, void*, raw_ostream&); |
347 | using Signature_mustQuote = QuotingType (*)(StringRef); |
348 | |
349 | template <typename U> |
350 | static char test(SameType<Signature_input, &U::input> *, |
351 | SameType<Signature_output, &U::output> *, |
352 | SameType<Signature_mustQuote, &U::mustQuote> *); |
353 | |
354 | template <typename U> |
355 | static double test(...); |
356 | |
357 | static bool const value = |
358 | (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1); |
359 | }; |
360 | |
361 | // Test if BlockScalarTraits<T> is defined on type T. |
362 | template <class T> |
363 | struct has_BlockScalarTraits |
364 | { |
365 | using Signature_input = StringRef (*)(StringRef, void *, T &); |
366 | using Signature_output = void (*)(const T &, void *, raw_ostream &); |
367 | |
368 | template <typename U> |
369 | static char test(SameType<Signature_input, &U::input> *, |
370 | SameType<Signature_output, &U::output> *); |
371 | |
372 | template <typename U> |
373 | static double test(...); |
374 | |
375 | static bool const value = |
376 | (sizeof(test<BlockScalarTraits<T>>(nullptr, nullptr)) == 1); |
377 | }; |
378 | |
379 | // Test if TaggedScalarTraits<T> is defined on type T. |
380 | template <class T> struct has_TaggedScalarTraits { |
381 | using Signature_input = StringRef (*)(StringRef, StringRef, void *, T &); |
382 | using Signature_output = void (*)(const T &, void *, raw_ostream &, |
383 | raw_ostream &); |
384 | using Signature_mustQuote = QuotingType (*)(const T &, StringRef); |
385 | |
386 | template <typename U> |
387 | static char test(SameType<Signature_input, &U::input> *, |
388 | SameType<Signature_output, &U::output> *, |
389 | SameType<Signature_mustQuote, &U::mustQuote> *); |
390 | |
391 | template <typename U> static double test(...); |
392 | |
393 | static bool const value = |
394 | (sizeof(test<TaggedScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1); |
395 | }; |
396 | |
397 | // Test if MappingContextTraits<T> is defined on type T. |
398 | template <class T, class Context> struct has_MappingTraits { |
399 | using Signature_mapping = void (*)(class IO &, T &, Context &); |
400 | |
401 | template <typename U> |
402 | static char test(SameType<Signature_mapping, &U::mapping>*); |
403 | |
404 | template <typename U> |
405 | static double test(...); |
406 | |
407 | static bool const value = |
408 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); |
409 | }; |
410 | |
411 | // Test if MappingTraits<T> is defined on type T. |
412 | template <class T> struct has_MappingTraits<T, EmptyContext> { |
413 | using Signature_mapping = void (*)(class IO &, T &); |
414 | |
415 | template <typename U> |
416 | static char test(SameType<Signature_mapping, &U::mapping> *); |
417 | |
418 | template <typename U> static double test(...); |
419 | |
420 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); |
421 | }; |
422 | |
423 | // Test if MappingContextTraits<T>::validate() is defined on type T. |
424 | template <class T, class Context> struct has_MappingValidateTraits { |
425 | using Signature_validate = std::string (*)(class IO &, T &, Context &); |
426 | |
427 | template <typename U> |
428 | static char test(SameType<Signature_validate, &U::validate>*); |
429 | |
430 | template <typename U> |
431 | static double test(...); |
432 | |
433 | static bool const value = |
434 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); |
435 | }; |
436 | |
437 | // Test if MappingTraits<T>::validate() is defined on type T. |
438 | template <class T> struct has_MappingValidateTraits<T, EmptyContext> { |
439 | using Signature_validate = std::string (*)(class IO &, T &); |
440 | |
441 | template <typename U> |
442 | static char test(SameType<Signature_validate, &U::validate> *); |
443 | |
444 | template <typename U> static double test(...); |
445 | |
446 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); |
447 | }; |
448 | |
449 | // Test if MappingContextTraits<T>::enumInput() is defined on type T. |
450 | template <class T, class Context> struct has_MappingEnumInputTraits { |
451 | using Signature_validate = void (*)(class IO &, T &); |
452 | |
453 | template <typename U> |
454 | static char test(SameType<Signature_validate, &U::enumInput> *); |
455 | |
456 | template <typename U> static double test(...); |
457 | |
458 | static bool const value = |
459 | (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1); |
460 | }; |
461 | |
462 | // Test if MappingTraits<T>::enumInput() is defined on type T. |
463 | template <class T> struct has_MappingEnumInputTraits<T, EmptyContext> { |
464 | using Signature_validate = void (*)(class IO &, T &); |
465 | |
466 | template <typename U> |
467 | static char test(SameType<Signature_validate, &U::enumInput> *); |
468 | |
469 | template <typename U> static double test(...); |
470 | |
471 | static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1); |
472 | }; |
473 | |
474 | // Test if SequenceTraits<T> is defined on type T. |
475 | template <class T> |
476 | struct has_SequenceMethodTraits |
477 | { |
478 | using Signature_size = size_t (*)(class IO&, T&); |
479 | |
480 | template <typename U> |
481 | static char test(SameType<Signature_size, &U::size>*); |
482 | |
483 | template <typename U> |
484 | static double test(...); |
485 | |
486 | static bool const value = (sizeof(test<SequenceTraits<T>>(nullptr)) == 1); |
487 | }; |
488 | |
489 | // Test if CustomMappingTraits<T> is defined on type T. |
490 | template <class T> |
491 | struct has_CustomMappingTraits |
492 | { |
493 | using Signature_input = void (*)(IO &io, StringRef key, T &v); |
494 | |
495 | template <typename U> |
496 | static char test(SameType<Signature_input, &U::inputOne>*); |
497 | |
498 | template <typename U> |
499 | static double test(...); |
500 | |
501 | static bool const value = |
502 | (sizeof(test<CustomMappingTraits<T>>(nullptr)) == 1); |
503 | }; |
504 | |
505 | // has_FlowTraits<int> will cause an error with some compilers because |
506 | // it subclasses int. Using this wrapper only instantiates the |
507 | // real has_FlowTraits only if the template type is a class. |
508 | template <typename T, bool Enabled = std::is_class<T>::value> |
509 | class has_FlowTraits |
510 | { |
511 | public: |
512 | static const bool value = false; |
513 | }; |
514 | |
515 | // Some older gcc compilers don't support straight forward tests |
516 | // for members, so test for ambiguity cause by the base and derived |
517 | // classes both defining the member. |
518 | template <class T> |
519 | struct has_FlowTraits<T, true> |
520 | { |
521 | struct Fallback { bool flow; }; |
522 | struct Derived : T, Fallback { }; |
523 | |
524 | template<typename C> |
525 | static char (&f(SameType<bool Fallback::*, &C::flow>*))[1]; |
526 | |
527 | template<typename C> |
528 | static char (&f(...))[2]; |
529 | |
530 | static bool const value = sizeof(f<Derived>(nullptr)) == 2; |
531 | }; |
532 | |
533 | // Test if SequenceTraits<T> is defined on type T |
534 | template<typename T> |
535 | struct has_SequenceTraits : public std::integral_constant<bool, |
536 | has_SequenceMethodTraits<T>::value > { }; |
537 | |
538 | // Test if DocumentListTraits<T> is defined on type T |
539 | template <class T> |
540 | struct has_DocumentListTraits |
541 | { |
542 | using Signature_size = size_t (*)(class IO &, T &); |
543 | |
544 | template <typename U> |
545 | static char test(SameType<Signature_size, &U::size>*); |
546 | |
547 | template <typename U> |
548 | static double test(...); |
549 | |
550 | static bool const value = (sizeof(test<DocumentListTraits<T>>(nullptr))==1); |
551 | }; |
552 | |
553 | template <class T> struct has_PolymorphicTraits { |
554 | using Signature_getKind = NodeKind (*)(const T &); |
555 | |
556 | template <typename U> |
557 | static char test(SameType<Signature_getKind, &U::getKind> *); |
558 | |
559 | template <typename U> static double test(...); |
560 | |
561 | static bool const value = (sizeof(test<PolymorphicTraits<T>>(nullptr)) == 1); |
562 | }; |
563 | |
564 | inline bool isNumeric(StringRef S) { |
565 | const auto skipDigits = [](StringRef Input) { |
566 | return Input.ltrim("0123456789"); |
567 | }; |
568 | |
569 | // Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls |
570 | // safe. |
571 | if (S.empty() || S.equals("+") || S.equals("-")) |
572 | return false; |
573 | |
574 | if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN")) |
575 | return true; |
576 | |
577 | // Infinity and decimal numbers can be prefixed with sign. |
578 | StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S; |
579 | |
580 | // Check for infinity first, because checking for hex and oct numbers is more |
581 | // expensive. |
582 | if (Tail.equals(".inf") || Tail.equals(".Inf") || Tail.equals(".INF")) |
583 | return true; |
584 | |
585 | // Section 10.3.2 Tag Resolution |
586 | // YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with |
587 | // [-+], so S should be used instead of Tail. |
588 | if (S.startswith("0o")) |
589 | return S.size() > 2 && |
590 | S.drop_front(2).find_first_not_of("01234567") == StringRef::npos; |
591 | |
592 | if (S.startswith("0x")) |
593 | return S.size() > 2 && S.drop_front(2).find_first_not_of( |
594 | "0123456789abcdefABCDEF") == StringRef::npos; |
595 | |
596 | // Parse float: [-+]? (\. [0-9]+ | [0-9]+ (\. [0-9]* )?) ([eE] [-+]? [0-9]+)? |
597 | S = Tail; |
598 | |
599 | // Handle cases when the number starts with '.' and hence needs at least one |
600 | // digit after dot (as opposed by number which has digits before the dot), but |
601 | // doesn't have one. |
602 | if (S.startswith(".") && |
603 | (S.equals(".") || |
604 | (S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr))) |
605 | return false; |
606 | |
607 | if (S.startswith("E") || S.startswith("e")) |
608 | return false; |
609 | |
610 | enum ParseState { |
611 | Default, |
612 | FoundDot, |
613 | FoundExponent, |
614 | }; |
615 | ParseState State = Default; |
616 | |
617 | S = skipDigits(S); |
618 | |
619 | // Accept decimal integer. |
620 | if (S.empty()) |
621 | return true; |
622 | |
623 | if (S.front() == '.') { |
624 | State = FoundDot; |
625 | S = S.drop_front(); |
626 | } else if (S.front() == 'e' || S.front() == 'E') { |
627 | State = FoundExponent; |
628 | S = S.drop_front(); |
629 | } else { |
630 | return false; |
631 | } |
632 | |
633 | if (State == FoundDot) { |
634 | S = skipDigits(S); |
635 | if (S.empty()) |
636 | return true; |
637 | |
638 | if (S.front() == 'e' || S.front() == 'E') { |
639 | State = FoundExponent; |
640 | S = S.drop_front(); |
641 | } else { |
642 | return false; |
643 | } |
644 | } |
645 | |
646 | assert(State == FoundExponent && "Should have found exponent at this point.")(static_cast <bool> (State == FoundExponent && "Should have found exponent at this point." ) ? void (0) : __assert_fail ("State == FoundExponent && \"Should have found exponent at this point.\"" , "llvm/include/llvm/Support/YAMLTraits.h", 646, __extension__ __PRETTY_FUNCTION__)); |
647 | if (S.empty()) |
648 | return false; |
649 | |
650 | if (S.front() == '+' || S.front() == '-') { |
651 | S = S.drop_front(); |
652 | if (S.empty()) |
653 | return false; |
654 | } |
655 | |
656 | return skipDigits(S).empty(); |
657 | } |
658 | |
659 | inline bool isNull(StringRef S) { |
660 | return S.equals("null") || S.equals("Null") || S.equals("NULL") || |
661 | S.equals("~"); |
662 | } |
663 | |
664 | inline bool isBool(StringRef S) { |
665 | // FIXME: using parseBool is causing multiple tests to fail. |
666 | return S.equals("true") || S.equals("True") || S.equals("TRUE") || |
667 | S.equals("false") || S.equals("False") || S.equals("FALSE"); |
668 | } |
669 | |
670 | // 5.1. Character Set |
671 | // The allowed character range explicitly excludes the C0 control block #x0-#x1F |
672 | // (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1 |
673 | // control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate |
674 | // block #xD800-#xDFFF, #xFFFE, and #xFFFF. |
675 | inline QuotingType needsQuotes(StringRef S) { |
676 | if (S.empty()) |
677 | return QuotingType::Single; |
678 | |
679 | QuotingType MaxQuotingNeeded = QuotingType::None; |
680 | if (isSpace(static_cast<unsigned char>(S.front())) || |
681 | isSpace(static_cast<unsigned char>(S.back()))) |
682 | MaxQuotingNeeded = QuotingType::Single; |
683 | if (isNull(S)) |
684 | MaxQuotingNeeded = QuotingType::Single; |
685 | if (isBool(S)) |
686 | MaxQuotingNeeded = QuotingType::Single; |
687 | if (isNumeric(S)) |
688 | MaxQuotingNeeded = QuotingType::Single; |
689 | |
690 | // 7.3.3 Plain Style |
691 | // Plain scalars must not begin with most indicators, as this would cause |
692 | // ambiguity with other YAML constructs. |
693 | if (std::strchr(R"(-?:\,[]{}#&*!|>'"%@`)", S[0]) != nullptr) |
694 | MaxQuotingNeeded = QuotingType::Single; |
695 | |
696 | for (unsigned char C : S) { |
697 | // Alphanum is safe. |
698 | if (isAlnum(C)) |
699 | continue; |
700 | |
701 | switch (C) { |
702 | // Safe scalar characters. |
703 | case '_': |
704 | case '-': |
705 | case '^': |
706 | case '.': |
707 | case ',': |
708 | case ' ': |
709 | // TAB (0x9) is allowed in unquoted strings. |
710 | case 0x9: |
711 | continue; |
712 | // LF(0xA) and CR(0xD) may delimit values and so require at least single |
713 | // quotes. LLVM YAML parser cannot handle single quoted multiline so use |
714 | // double quoting to produce valid YAML. |
715 | case 0xA: |
716 | case 0xD: |
717 | return QuotingType::Double; |
718 | // DEL (0x7F) are excluded from the allowed character range. |
719 | case 0x7F: |
720 | return QuotingType::Double; |
721 | // Forward slash is allowed to be unquoted, but we quote it anyway. We have |
722 | // many tests that use FileCheck against YAML output, and this output often |
723 | // contains paths. If we quote backslashes but not forward slashes then |
724 | // paths will come out either quoted or unquoted depending on which platform |
725 | // the test is run on, making FileCheck comparisons difficult. |
726 | case '/': |
727 | default: { |
728 | // C0 control block (0x0 - 0x1F) is excluded from the allowed character |
729 | // range. |
730 | if (C <= 0x1F) |
731 | return QuotingType::Double; |
732 | |
733 | // Always double quote UTF-8. |
734 | if ((C & 0x80) != 0) |
735 | return QuotingType::Double; |
736 | |
737 | // The character is not safe, at least simple quoting needed. |
738 | MaxQuotingNeeded = QuotingType::Single; |
739 | } |
740 | } |
741 | } |
742 | |
743 | return MaxQuotingNeeded; |
744 | } |
745 | |
746 | template <typename T, typename Context> |
747 | struct missingTraits |
748 | : public std::integral_constant<bool, |
749 | !has_ScalarEnumerationTraits<T>::value && |
750 | !has_ScalarBitSetTraits<T>::value && |
751 | !has_ScalarTraits<T>::value && |
752 | !has_BlockScalarTraits<T>::value && |
753 | !has_TaggedScalarTraits<T>::value && |
754 | !has_MappingTraits<T, Context>::value && |
755 | !has_SequenceTraits<T>::value && |
756 | !has_CustomMappingTraits<T>::value && |
757 | !has_DocumentListTraits<T>::value && |
758 | !has_PolymorphicTraits<T>::value> {}; |
759 | |
760 | template <typename T, typename Context> |
761 | struct validatedMappingTraits |
762 | : public std::integral_constant< |
763 | bool, has_MappingTraits<T, Context>::value && |
764 | has_MappingValidateTraits<T, Context>::value> {}; |
765 | |
766 | template <typename T, typename Context> |
767 | struct unvalidatedMappingTraits |
768 | : public std::integral_constant< |
769 | bool, has_MappingTraits<T, Context>::value && |
770 | !has_MappingValidateTraits<T, Context>::value> {}; |
771 | |
772 | // Base class for Input and Output. |
773 | class IO { |
774 | public: |
775 | IO(void *Ctxt = nullptr); |
776 | virtual ~IO(); |
777 | |
778 | virtual bool outputting() const = 0; |
779 | |
780 | virtual unsigned beginSequence() = 0; |
781 | virtual bool preflightElement(unsigned, void *&) = 0; |
782 | virtual void postflightElement(void*) = 0; |
783 | virtual void endSequence() = 0; |
784 | virtual bool canElideEmptySequence() = 0; |
785 | |
786 | virtual unsigned beginFlowSequence() = 0; |
787 | virtual bool preflightFlowElement(unsigned, void *&) = 0; |
788 | virtual void postflightFlowElement(void*) = 0; |
789 | virtual void endFlowSequence() = 0; |
790 | |
791 | virtual bool mapTag(StringRef Tag, bool Default=false) = 0; |
792 | virtual void beginMapping() = 0; |
793 | virtual void endMapping() = 0; |
794 | virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0; |
795 | virtual void postflightKey(void*) = 0; |
796 | virtual std::vector<StringRef> keys() = 0; |
797 | |
798 | virtual void beginFlowMapping() = 0; |
799 | virtual void endFlowMapping() = 0; |
800 | |
801 | virtual void beginEnumScalar() = 0; |
802 | virtual bool matchEnumScalar(const char*, bool) = 0; |
803 | virtual bool matchEnumFallback() = 0; |
804 | virtual void endEnumScalar() = 0; |
805 | |
806 | virtual bool beginBitSetScalar(bool &) = 0; |
807 | virtual bool bitSetMatch(const char*, bool) = 0; |
808 | virtual void endBitSetScalar() = 0; |
809 | |
810 | virtual void scalarString(StringRef &, QuotingType) = 0; |
811 | virtual void blockScalarString(StringRef &) = 0; |
812 | virtual void scalarTag(std::string &) = 0; |
813 | |
814 | virtual NodeKind getNodeKind() = 0; |
815 | |
816 | virtual void setError(const Twine &) = 0; |
817 | virtual void setAllowUnknownKeys(bool Allow); |
818 | |
819 | template <typename T> |
820 | void enumCase(T &Val, const char* Str, const T ConstVal) { |
821 | if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) { |
822 | Val = ConstVal; |
823 | } |
824 | } |
825 | |
826 | // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF |
827 | template <typename T> |
828 | void enumCase(T &Val, const char* Str, const uint32_t ConstVal) { |
829 | if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) { |
830 | Val = ConstVal; |
831 | } |
832 | } |
833 | |
834 | template <typename FBT, typename T> |
835 | void enumFallback(T &Val) { |
836 | if (matchEnumFallback()) { |
837 | EmptyContext Context; |
838 | // FIXME: Force integral conversion to allow strong typedefs to convert. |
839 | FBT Res = static_cast<typename FBT::BaseType>(Val); |
840 | yamlize(*this, Res, true, Context); |
841 | Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res)); |
842 | } |
843 | } |
844 | |
845 | template <typename T> |
846 | void bitSetCase(T &Val, const char* Str, const T ConstVal) { |
847 | if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) { |
848 | Val = static_cast<T>(Val | ConstVal); |
849 | } |
850 | } |
851 | |
852 | // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF |
853 | template <typename T> |
854 | void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) { |
855 | if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) { |
856 | Val = static_cast<T>(Val | ConstVal); |
857 | } |
858 | } |
859 | |
860 | template <typename T> |
861 | void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) { |
862 | if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal)) |
863 | Val = Val | ConstVal; |
864 | } |
865 | |
866 | template <typename T> |
867 | void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal, |
868 | uint32_t Mask) { |
869 | if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal)) |
870 | Val = Val | ConstVal; |
871 | } |
872 | |
873 | void *getContext() const; |
874 | void setContext(void *); |
875 | |
876 | template <typename T> void mapRequired(const char *Key, T &Val) { |
877 | EmptyContext Ctx; |
878 | this->processKey(Key, Val, true, Ctx); |
879 | } |
880 | |
881 | template <typename T, typename Context> |
882 | void mapRequired(const char *Key, T &Val, Context &Ctx) { |
883 | this->processKey(Key, Val, true, Ctx); |
884 | } |
885 | |
886 | template <typename T> void mapOptional(const char *Key, T &Val) { |
887 | EmptyContext Ctx; |
888 | mapOptionalWithContext(Key, Val, Ctx); |
889 | } |
890 | |
891 | template <typename T, typename DefaultT> |
892 | void mapOptional(const char *Key, T &Val, const DefaultT &Default) { |
893 | EmptyContext Ctx; |
894 | mapOptionalWithContext(Key, Val, Default, Ctx); |
895 | } |
896 | |
897 | template <typename T, typename Context> |
898 | std::enable_if_t<has_SequenceTraits<T>::value, void> |
899 | mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) { |
900 | // omit key/value instead of outputting empty sequence |
901 | if (this->canElideEmptySequence() && !(Val.begin() != Val.end())) |
902 | return; |
903 | this->processKey(Key, Val, false, Ctx); |
904 | } |
905 | |
906 | template <typename T, typename Context> |
907 | void mapOptionalWithContext(const char *Key, Optional<T> &Val, Context &Ctx) { |
908 | this->processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false, |
909 | Ctx); |
910 | } |
911 | |
912 | template <typename T, typename Context> |
913 | std::enable_if_t<!has_SequenceTraits<T>::value, void> |
914 | mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) { |
915 | this->processKey(Key, Val, false, Ctx); |
916 | } |
917 | |
918 | template <typename T, typename Context, typename DefaultT> |
919 | void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default, |
920 | Context &Ctx) { |
921 | static_assert(std::is_convertible<DefaultT, T>::value, |
922 | "Default type must be implicitly convertible to value type!"); |
923 | this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default), |
924 | false, Ctx); |
925 | } |
926 | |
927 | private: |
928 | template <typename T, typename Context> |
929 | void processKeyWithDefault(const char *Key, Optional<T> &Val, |
930 | const Optional<T> &DefaultValue, bool Required, |
931 | Context &Ctx); |
932 | |
933 | template <typename T, typename Context> |
934 | void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue, |
935 | bool Required, Context &Ctx) { |
936 | void *SaveInfo; |
937 | bool UseDefault; |
938 | const bool sameAsDefault = outputting() && Val == DefaultValue; |
939 | if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault, |
940 | SaveInfo) ) { |
941 | yamlize(*this, Val, Required, Ctx); |
942 | this->postflightKey(SaveInfo); |
943 | } |
944 | else { |
945 | if ( UseDefault ) |
946 | Val = DefaultValue; |
947 | } |
948 | } |
949 | |
950 | template <typename T, typename Context> |
951 | void processKey(const char *Key, T &Val, bool Required, Context &Ctx) { |
952 | void *SaveInfo; |
953 | bool UseDefault; |
954 | if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) { |
955 | yamlize(*this, Val, Required, Ctx); |
956 | this->postflightKey(SaveInfo); |
957 | } |
958 | } |
959 | |
960 | private: |
961 | void *Ctxt; |
962 | }; |
963 | |
964 | namespace detail { |
965 | |
966 | template <typename T, typename Context> |
967 | void doMapping(IO &io, T &Val, Context &Ctx) { |
968 | MappingContextTraits<T, Context>::mapping(io, Val, Ctx); |
969 | } |
970 | |
971 | template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) { |
972 | MappingTraits<T>::mapping(io, Val); |
973 | } |
974 | |
975 | } // end namespace detail |
976 | |
977 | template <typename T> |
978 | std::enable_if_t<has_ScalarEnumerationTraits<T>::value, void> |
979 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
980 | io.beginEnumScalar(); |
981 | ScalarEnumerationTraits<T>::enumeration(io, Val); |
982 | io.endEnumScalar(); |
983 | } |
984 | |
985 | template <typename T> |
986 | std::enable_if_t<has_ScalarBitSetTraits<T>::value, void> |
987 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
988 | bool DoClear; |
989 | if ( io.beginBitSetScalar(DoClear) ) { |
990 | if ( DoClear ) |
991 | Val = T(); |
992 | ScalarBitSetTraits<T>::bitset(io, Val); |
993 | io.endBitSetScalar(); |
994 | } |
995 | } |
996 | |
997 | template <typename T> |
998 | std::enable_if_t<has_ScalarTraits<T>::value, void> yamlize(IO &io, T &Val, bool, |
999 | EmptyContext &Ctx) { |
1000 | if ( io.outputting() ) { |
1001 | std::string Storage; |
1002 | raw_string_ostream Buffer(Storage); |
1003 | ScalarTraits<T>::output(Val, io.getContext(), Buffer); |
1004 | StringRef Str = Buffer.str(); |
1005 | io.scalarString(Str, ScalarTraits<T>::mustQuote(Str)); |
1006 | } |
1007 | else { |
1008 | StringRef Str; |
1009 | io.scalarString(Str, ScalarTraits<T>::mustQuote(Str)); |
1010 | StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val); |
1011 | if ( !Result.empty() ) { |
1012 | io.setError(Twine(Result)); |
1013 | } |
1014 | } |
1015 | } |
1016 | |
1017 | template <typename T> |
1018 | std::enable_if_t<has_BlockScalarTraits<T>::value, void> |
1019 | yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) { |
1020 | if (YamlIO.outputting()) { |
1021 | std::string Storage; |
1022 | raw_string_ostream Buffer(Storage); |
1023 | BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer); |
1024 | StringRef Str = Buffer.str(); |
1025 | YamlIO.blockScalarString(Str); |
1026 | } else { |
1027 | StringRef Str; |
1028 | YamlIO.blockScalarString(Str); |
1029 | StringRef Result = |
1030 | BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val); |
1031 | if (!Result.empty()) |
1032 | YamlIO.setError(Twine(Result)); |
1033 | } |
1034 | } |
1035 | |
1036 | template <typename T> |
1037 | std::enable_if_t<has_TaggedScalarTraits<T>::value, void> |
1038 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1039 | if (io.outputting()) { |
1040 | std::string ScalarStorage, TagStorage; |
1041 | raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage); |
1042 | TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer, |
1043 | TagBuffer); |
1044 | io.scalarTag(TagBuffer.str()); |
1045 | StringRef ScalarStr = ScalarBuffer.str(); |
1046 | io.scalarString(ScalarStr, |
1047 | TaggedScalarTraits<T>::mustQuote(Val, ScalarStr)); |
1048 | } else { |
1049 | std::string Tag; |
1050 | io.scalarTag(Tag); |
1051 | StringRef Str; |
1052 | io.scalarString(Str, QuotingType::None); |
1053 | StringRef Result = |
1054 | TaggedScalarTraits<T>::input(Str, Tag, io.getContext(), Val); |
1055 | if (!Result.empty()) { |
1056 | io.setError(Twine(Result)); |
1057 | } |
1058 | } |
1059 | } |
1060 | |
1061 | template <typename T, typename Context> |
1062 | std::enable_if_t<validatedMappingTraits<T, Context>::value, void> |
1063 | yamlize(IO &io, T &Val, bool, Context &Ctx) { |
1064 | if (has_FlowTraits<MappingTraits<T>>::value) |
1065 | io.beginFlowMapping(); |
1066 | else |
1067 | io.beginMapping(); |
1068 | if (io.outputting()) { |
1069 | std::string Err = MappingTraits<T>::validate(io, Val); |
1070 | if (!Err.empty()) { |
1071 | errs() << Err << "\n"; |
1072 | assert(Err.empty() && "invalid struct trying to be written as yaml")(static_cast <bool> (Err.empty() && "invalid struct trying to be written as yaml" ) ? void (0) : __assert_fail ("Err.empty() && \"invalid struct trying to be written as yaml\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1072, __extension__ __PRETTY_FUNCTION__)); |
1073 | } |
1074 | } |
1075 | detail::doMapping(io, Val, Ctx); |
1076 | if (!io.outputting()) { |
1077 | std::string Err = MappingTraits<T>::validate(io, Val); |
1078 | if (!Err.empty()) |
1079 | io.setError(Err); |
1080 | } |
1081 | if (has_FlowTraits<MappingTraits<T>>::value) |
1082 | io.endFlowMapping(); |
1083 | else |
1084 | io.endMapping(); |
1085 | } |
1086 | |
1087 | template <typename T, typename Context> |
1088 | std::enable_if_t<!has_MappingEnumInputTraits<T, Context>::value, bool> |
1089 | yamlizeMappingEnumInput(IO &io, T &Val) { |
1090 | return false; |
1091 | } |
1092 | |
1093 | template <typename T, typename Context> |
1094 | std::enable_if_t<has_MappingEnumInputTraits<T, Context>::value, bool> |
1095 | yamlizeMappingEnumInput(IO &io, T &Val) { |
1096 | if (io.outputting()) |
1097 | return false; |
1098 | |
1099 | io.beginEnumScalar(); |
1100 | MappingTraits<T>::enumInput(io, Val); |
1101 | bool Matched = !io.matchEnumFallback(); |
1102 | io.endEnumScalar(); |
1103 | return Matched; |
1104 | } |
1105 | |
1106 | template <typename T, typename Context> |
1107 | std::enable_if_t<unvalidatedMappingTraits<T, Context>::value, void> |
1108 | yamlize(IO &io, T &Val, bool, Context &Ctx) { |
1109 | if (yamlizeMappingEnumInput<T, Context>(io, Val)) |
1110 | return; |
1111 | if (has_FlowTraits<MappingTraits<T>>::value) { |
1112 | io.beginFlowMapping(); |
1113 | detail::doMapping(io, Val, Ctx); |
1114 | io.endFlowMapping(); |
1115 | } else { |
1116 | io.beginMapping(); |
1117 | detail::doMapping(io, Val, Ctx); |
1118 | io.endMapping(); |
1119 | } |
1120 | } |
1121 | |
1122 | template <typename T> |
1123 | std::enable_if_t<has_CustomMappingTraits<T>::value, void> |
1124 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1125 | if ( io.outputting() ) { |
1126 | io.beginMapping(); |
1127 | CustomMappingTraits<T>::output(io, Val); |
1128 | io.endMapping(); |
1129 | } else { |
1130 | io.beginMapping(); |
1131 | for (StringRef key : io.keys()) |
1132 | CustomMappingTraits<T>::inputOne(io, key, Val); |
1133 | io.endMapping(); |
1134 | } |
1135 | } |
1136 | |
1137 | template <typename T> |
1138 | std::enable_if_t<has_PolymorphicTraits<T>::value, void> |
1139 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1140 | switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val) |
1141 | : io.getNodeKind()) { |
1142 | case NodeKind::Scalar: |
1143 | return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val), true, Ctx); |
1144 | case NodeKind::Map: |
1145 | return yamlize(io, PolymorphicTraits<T>::getAsMap(Val), true, Ctx); |
1146 | case NodeKind::Sequence: |
1147 | return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val), true, Ctx); |
1148 | } |
1149 | } |
1150 | |
1151 | template <typename T> |
1152 | std::enable_if_t<missingTraits<T, EmptyContext>::value, void> |
1153 | yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) { |
1154 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; |
1155 | } |
1156 | |
1157 | template <typename T, typename Context> |
1158 | std::enable_if_t<has_SequenceTraits<T>::value, void> |
1159 | yamlize(IO &io, T &Seq, bool, Context &Ctx) { |
1160 | if ( has_FlowTraits< SequenceTraits<T>>::value ) { |
1161 | unsigned incnt = io.beginFlowSequence(); |
1162 | unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt; |
1163 | for(unsigned i=0; i < count; ++i) { |
1164 | void *SaveInfo; |
1165 | if ( io.preflightFlowElement(i, SaveInfo) ) { |
1166 | yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx); |
1167 | io.postflightFlowElement(SaveInfo); |
1168 | } |
1169 | } |
1170 | io.endFlowSequence(); |
1171 | } |
1172 | else { |
1173 | unsigned incnt = io.beginSequence(); |
1174 | unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt; |
1175 | for(unsigned i=0; i < count; ++i) { |
1176 | void *SaveInfo; |
1177 | if ( io.preflightElement(i, SaveInfo) ) { |
1178 | yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx); |
1179 | io.postflightElement(SaveInfo); |
1180 | } |
1181 | } |
1182 | io.endSequence(); |
1183 | } |
1184 | } |
1185 | |
1186 | template<> |
1187 | struct ScalarTraits<bool> { |
1188 | static void output(const bool &, void* , raw_ostream &); |
1189 | static StringRef input(StringRef, void *, bool &); |
1190 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1191 | }; |
1192 | |
1193 | template<> |
1194 | struct ScalarTraits<StringRef> { |
1195 | static void output(const StringRef &, void *, raw_ostream &); |
1196 | static StringRef input(StringRef, void *, StringRef &); |
1197 | static QuotingType mustQuote(StringRef S) { return needsQuotes(S); } |
1198 | }; |
1199 | |
1200 | template<> |
1201 | struct ScalarTraits<std::string> { |
1202 | static void output(const std::string &, void *, raw_ostream &); |
1203 | static StringRef input(StringRef, void *, std::string &); |
1204 | static QuotingType mustQuote(StringRef S) { return needsQuotes(S); } |
1205 | }; |
1206 | |
1207 | template<> |
1208 | struct ScalarTraits<uint8_t> { |
1209 | static void output(const uint8_t &, void *, raw_ostream &); |
1210 | static StringRef input(StringRef, void *, uint8_t &); |
1211 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1212 | }; |
1213 | |
1214 | template<> |
1215 | struct ScalarTraits<uint16_t> { |
1216 | static void output(const uint16_t &, void *, raw_ostream &); |
1217 | static StringRef input(StringRef, void *, uint16_t &); |
1218 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1219 | }; |
1220 | |
1221 | template<> |
1222 | struct ScalarTraits<uint32_t> { |
1223 | static void output(const uint32_t &, void *, raw_ostream &); |
1224 | static StringRef input(StringRef, void *, uint32_t &); |
1225 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1226 | }; |
1227 | |
1228 | template<> |
1229 | struct ScalarTraits<uint64_t> { |
1230 | static void output(const uint64_t &, void *, raw_ostream &); |
1231 | static StringRef input(StringRef, void *, uint64_t &); |
1232 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1233 | }; |
1234 | |
1235 | template<> |
1236 | struct ScalarTraits<int8_t> { |
1237 | static void output(const int8_t &, void *, raw_ostream &); |
1238 | static StringRef input(StringRef, void *, int8_t &); |
1239 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1240 | }; |
1241 | |
1242 | template<> |
1243 | struct ScalarTraits<int16_t> { |
1244 | static void output(const int16_t &, void *, raw_ostream &); |
1245 | static StringRef input(StringRef, void *, int16_t &); |
1246 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1247 | }; |
1248 | |
1249 | template<> |
1250 | struct ScalarTraits<int32_t> { |
1251 | static void output(const int32_t &, void *, raw_ostream &); |
1252 | static StringRef input(StringRef, void *, int32_t &); |
1253 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1254 | }; |
1255 | |
1256 | template<> |
1257 | struct ScalarTraits<int64_t> { |
1258 | static void output(const int64_t &, void *, raw_ostream &); |
1259 | static StringRef input(StringRef, void *, int64_t &); |
1260 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1261 | }; |
1262 | |
1263 | template<> |
1264 | struct ScalarTraits<float> { |
1265 | static void output(const float &, void *, raw_ostream &); |
1266 | static StringRef input(StringRef, void *, float &); |
1267 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1268 | }; |
1269 | |
1270 | template<> |
1271 | struct ScalarTraits<double> { |
1272 | static void output(const double &, void *, raw_ostream &); |
1273 | static StringRef input(StringRef, void *, double &); |
1274 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1275 | }; |
1276 | |
1277 | // For endian types, we use existing scalar Traits class for the underlying |
1278 | // type. This way endian aware types are supported whenever the traits are |
1279 | // defined for the underlying type. |
1280 | template <typename value_type, support::endianness endian, size_t alignment> |
1281 | struct ScalarTraits<support::detail::packed_endian_specific_integral< |
1282 | value_type, endian, alignment>, |
1283 | std::enable_if_t<has_ScalarTraits<value_type>::value>> { |
1284 | using endian_type = |
1285 | support::detail::packed_endian_specific_integral<value_type, endian, |
1286 | alignment>; |
1287 | |
1288 | static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) { |
1289 | ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream); |
1290 | } |
1291 | |
1292 | static StringRef input(StringRef Str, void *Ctx, endian_type &E) { |
1293 | value_type V; |
1294 | auto R = ScalarTraits<value_type>::input(Str, Ctx, V); |
1295 | E = static_cast<endian_type>(V); |
1296 | return R; |
1297 | } |
1298 | |
1299 | static QuotingType mustQuote(StringRef Str) { |
1300 | return ScalarTraits<value_type>::mustQuote(Str); |
1301 | } |
1302 | }; |
1303 | |
1304 | template <typename value_type, support::endianness endian, size_t alignment> |
1305 | struct ScalarEnumerationTraits< |
1306 | support::detail::packed_endian_specific_integral<value_type, endian, |
1307 | alignment>, |
1308 | std::enable_if_t<has_ScalarEnumerationTraits<value_type>::value>> { |
1309 | using endian_type = |
1310 | support::detail::packed_endian_specific_integral<value_type, endian, |
1311 | alignment>; |
1312 | |
1313 | static void enumeration(IO &io, endian_type &E) { |
1314 | value_type V = E; |
1315 | ScalarEnumerationTraits<value_type>::enumeration(io, V); |
1316 | E = V; |
1317 | } |
1318 | }; |
1319 | |
1320 | template <typename value_type, support::endianness endian, size_t alignment> |
1321 | struct ScalarBitSetTraits< |
1322 | support::detail::packed_endian_specific_integral<value_type, endian, |
1323 | alignment>, |
1324 | std::enable_if_t<has_ScalarBitSetTraits<value_type>::value>> { |
1325 | using endian_type = |
1326 | support::detail::packed_endian_specific_integral<value_type, endian, |
1327 | alignment>; |
1328 | static void bitset(IO &io, endian_type &E) { |
1329 | value_type V = E; |
1330 | ScalarBitSetTraits<value_type>::bitset(io, V); |
1331 | E = V; |
1332 | } |
1333 | }; |
1334 | |
1335 | // Utility for use within MappingTraits<>::mapping() method |
1336 | // to [de]normalize an object for use with YAML conversion. |
1337 | template <typename TNorm, typename TFinal> |
1338 | struct MappingNormalization { |
1339 | MappingNormalization(IO &i_o, TFinal &Obj) |
1340 | : io(i_o), BufPtr(nullptr), Result(Obj) { |
1341 | if ( io.outputting() ) { |
1342 | BufPtr = new (&Buffer) TNorm(io, Obj); |
1343 | } |
1344 | else { |
1345 | BufPtr = new (&Buffer) TNorm(io); |
1346 | } |
1347 | } |
1348 | |
1349 | ~MappingNormalization() { |
1350 | if ( ! io.outputting() ) { |
1351 | Result = BufPtr->denormalize(io); |
1352 | } |
1353 | BufPtr->~TNorm(); |
1354 | } |
1355 | |
1356 | TNorm* operator->() { return BufPtr; } |
1357 | |
1358 | private: |
1359 | using Storage = AlignedCharArrayUnion<TNorm>; |
1360 | |
1361 | Storage Buffer; |
1362 | IO &io; |
1363 | TNorm *BufPtr; |
1364 | TFinal &Result; |
1365 | }; |
1366 | |
1367 | // Utility for use within MappingTraits<>::mapping() method |
1368 | // to [de]normalize an object for use with YAML conversion. |
1369 | template <typename TNorm, typename TFinal> |
1370 | struct MappingNormalizationHeap { |
1371 | MappingNormalizationHeap(IO &i_o, TFinal &Obj, BumpPtrAllocator *allocator) |
1372 | : io(i_o), Result(Obj) { |
1373 | if ( io.outputting() ) { |
1374 | BufPtr = new (&Buffer) TNorm(io, Obj); |
1375 | } |
1376 | else if (allocator) { |
1377 | BufPtr = allocator->Allocate<TNorm>(); |
1378 | new (BufPtr) TNorm(io); |
1379 | } else { |
1380 | BufPtr = new TNorm(io); |
1381 | } |
1382 | } |
1383 | |
1384 | ~MappingNormalizationHeap() { |
1385 | if ( io.outputting() ) { |
1386 | BufPtr->~TNorm(); |
1387 | } |
1388 | else { |
1389 | Result = BufPtr->denormalize(io); |
1390 | } |
1391 | } |
1392 | |
1393 | TNorm* operator->() { return BufPtr; } |
1394 | |
1395 | private: |
1396 | using Storage = AlignedCharArrayUnion<TNorm>; |
1397 | |
1398 | Storage Buffer; |
1399 | IO &io; |
1400 | TNorm *BufPtr = nullptr; |
1401 | TFinal &Result; |
1402 | }; |
1403 | |
1404 | /// |
1405 | /// The Input class is used to parse a yaml document into in-memory structs |
1406 | /// and vectors. |
1407 | /// |
1408 | /// It works by using YAMLParser to do a syntax parse of the entire yaml |
1409 | /// document, then the Input class builds a graph of HNodes which wraps |
1410 | /// each yaml Node. The extra layer is buffering. The low level yaml |
1411 | /// parser only lets you look at each node once. The buffering layer lets |
1412 | /// you search and interate multiple times. This is necessary because |
1413 | /// the mapRequired() method calls may not be in the same order |
1414 | /// as the keys in the document. |
1415 | /// |
1416 | class Input : public IO { |
1417 | public: |
1418 | // Construct a yaml Input object from a StringRef and optional |
1419 | // user-data. The DiagHandler can be specified to provide |
1420 | // alternative error reporting. |
1421 | Input(StringRef InputContent, |
1422 | void *Ctxt = nullptr, |
1423 | SourceMgr::DiagHandlerTy DiagHandler = nullptr, |
1424 | void *DiagHandlerCtxt = nullptr); |
1425 | Input(MemoryBufferRef Input, |
1426 | void *Ctxt = nullptr, |
1427 | SourceMgr::DiagHandlerTy DiagHandler = nullptr, |
1428 | void *DiagHandlerCtxt = nullptr); |
1429 | ~Input() override; |
1430 | |
1431 | // Check if there was an syntax or semantic error during parsing. |
1432 | std::error_code error(); |
1433 | |
1434 | private: |
1435 | bool outputting() const override; |
1436 | bool mapTag(StringRef, bool) override; |
1437 | void beginMapping() override; |
1438 | void endMapping() override; |
1439 | bool preflightKey(const char *, bool, bool, bool &, void *&) override; |
1440 | void postflightKey(void *) override; |
1441 | std::vector<StringRef> keys() override; |
1442 | void beginFlowMapping() override; |
1443 | void endFlowMapping() override; |
1444 | unsigned beginSequence() override; |
1445 | void endSequence() override; |
1446 | bool preflightElement(unsigned index, void *&) override; |
1447 | void postflightElement(void *) override; |
1448 | unsigned beginFlowSequence() override; |
1449 | bool preflightFlowElement(unsigned , void *&) override; |
1450 | void postflightFlowElement(void *) override; |
1451 | void endFlowSequence() override; |
1452 | void beginEnumScalar() override; |
1453 | bool matchEnumScalar(const char*, bool) override; |
1454 | bool matchEnumFallback() override; |
1455 | void endEnumScalar() override; |
1456 | bool beginBitSetScalar(bool &) override; |
1457 | bool bitSetMatch(const char *, bool ) override; |
1458 | void endBitSetScalar() override; |
1459 | void scalarString(StringRef &, QuotingType) override; |
1460 | void blockScalarString(StringRef &) override; |
1461 | void scalarTag(std::string &) override; |
1462 | NodeKind getNodeKind() override; |
1463 | void setError(const Twine &message) override; |
1464 | bool canElideEmptySequence() override; |
1465 | |
1466 | class HNode { |
1467 | virtual void anchor(); |
1468 | |
1469 | public: |
1470 | HNode(Node *n) : _node(n) { } |
1471 | virtual ~HNode() = default; |
1472 | |
1473 | static bool classof(const HNode *) { return true; } |
1474 | |
1475 | Node *_node; |
1476 | }; |
1477 | |
1478 | class EmptyHNode : public HNode { |
1479 | void anchor() override; |
1480 | |
1481 | public: |
1482 | EmptyHNode(Node *n) : HNode(n) { } |
1483 | |
1484 | static bool classof(const HNode *n) { return NullNode::classof(n->_node); } |
1485 | |
1486 | static bool classof(const EmptyHNode *) { return true; } |
1487 | }; |
1488 | |
1489 | class ScalarHNode : public HNode { |
1490 | void anchor() override; |
1491 | |
1492 | public: |
1493 | ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { } |
1494 | |
1495 | StringRef value() const { return _value; } |
1496 | |
1497 | static bool classof(const HNode *n) { |
1498 | return ScalarNode::classof(n->_node) || |
1499 | BlockScalarNode::classof(n->_node); |
1500 | } |
1501 | |
1502 | static bool classof(const ScalarHNode *) { return true; } |
1503 | |
1504 | protected: |
1505 | StringRef _value; |
1506 | }; |
1507 | |
1508 | class MapHNode : public HNode { |
1509 | void anchor() override; |
1510 | |
1511 | public: |
1512 | MapHNode(Node *n) : HNode(n) { } |
1513 | |
1514 | static bool classof(const HNode *n) { |
1515 | return MappingNode::classof(n->_node); |
1516 | } |
1517 | |
1518 | static bool classof(const MapHNode *) { return true; } |
1519 | |
1520 | using NameToNodeAndLoc = |
1521 | StringMap<std::pair<std::unique_ptr<HNode>, SMRange>>; |
1522 | |
1523 | NameToNodeAndLoc Mapping; |
1524 | SmallVector<std::string, 6> ValidKeys; |
1525 | }; |
1526 | |
1527 | class SequenceHNode : public HNode { |
1528 | void anchor() override; |
1529 | |
1530 | public: |
1531 | SequenceHNode(Node *n) : HNode(n) { } |
1532 | |
1533 | static bool classof(const HNode *n) { |
1534 | return SequenceNode::classof(n->_node); |
1535 | } |
1536 | |
1537 | static bool classof(const SequenceHNode *) { return true; } |
1538 | |
1539 | std::vector<std::unique_ptr<HNode>> Entries; |
1540 | }; |
1541 | |
1542 | std::unique_ptr<Input::HNode> createHNodes(Node *node); |
1543 | void setError(HNode *hnode, const Twine &message); |
1544 | void setError(Node *node, const Twine &message); |
1545 | void setError(const SMRange &Range, const Twine &message); |
1546 | |
1547 | void reportWarning(HNode *hnode, const Twine &message); |
1548 | void reportWarning(Node *hnode, const Twine &message); |
1549 | void reportWarning(const SMRange &Range, const Twine &message); |
1550 | |
1551 | public: |
1552 | // These are only used by operator>>. They could be private |
1553 | // if those templated things could be made friends. |
1554 | bool setCurrentDocument(); |
1555 | bool nextDocument(); |
1556 | |
1557 | /// Returns the current node that's being parsed by the YAML Parser. |
1558 | const Node *getCurrentNode() const; |
1559 | |
1560 | void setAllowUnknownKeys(bool Allow) override; |
1561 | |
1562 | private: |
1563 | SourceMgr SrcMgr; // must be before Strm |
1564 | std::unique_ptr<llvm::yaml::Stream> Strm; |
1565 | std::unique_ptr<HNode> TopNode; |
1566 | std::error_code EC; |
1567 | BumpPtrAllocator StringAllocator; |
1568 | document_iterator DocIterator; |
1569 | llvm::BitVector BitValuesUsed; |
1570 | HNode *CurrentNode = nullptr; |
1571 | bool ScalarMatchFound = false; |
1572 | bool AllowUnknownKeys = false; |
1573 | }; |
1574 | |
1575 | /// |
1576 | /// The Output class is used to generate a yaml document from in-memory structs |
1577 | /// and vectors. |
1578 | /// |
1579 | class Output : public IO { |
1580 | public: |
1581 | Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70); |
1582 | ~Output() override; |
1583 | |
1584 | /// Set whether or not to output optional values which are equal |
1585 | /// to the default value. By default, when outputting if you attempt |
1586 | /// to write a value that is equal to the default, the value gets ignored. |
1587 | /// Sometimes, it is useful to be able to see these in the resulting YAML |
1588 | /// anyway. |
1589 | void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; } |
1590 | |
1591 | bool outputting() const override; |
1592 | bool mapTag(StringRef, bool) override; |
1593 | void beginMapping() override; |
1594 | void endMapping() override; |
1595 | bool preflightKey(const char *key, bool, bool, bool &, void *&) override; |
1596 | void postflightKey(void *) override; |
1597 | std::vector<StringRef> keys() override; |
1598 | void beginFlowMapping() override; |
1599 | void endFlowMapping() override; |
1600 | unsigned beginSequence() override; |
1601 | void endSequence() override; |
1602 | bool preflightElement(unsigned, void *&) override; |
1603 | void postflightElement(void *) override; |
1604 | unsigned beginFlowSequence() override; |
1605 | bool preflightFlowElement(unsigned, void *&) override; |
1606 | void postflightFlowElement(void *) override; |
1607 | void endFlowSequence() override; |
1608 | void beginEnumScalar() override; |
1609 | bool matchEnumScalar(const char*, bool) override; |
1610 | bool matchEnumFallback() override; |
1611 | void endEnumScalar() override; |
1612 | bool beginBitSetScalar(bool &) override; |
1613 | bool bitSetMatch(const char *, bool ) override; |
1614 | void endBitSetScalar() override; |
1615 | void scalarString(StringRef &, QuotingType) override; |
1616 | void blockScalarString(StringRef &) override; |
1617 | void scalarTag(std::string &) override; |
1618 | NodeKind getNodeKind() override; |
1619 | void setError(const Twine &message) override; |
1620 | bool canElideEmptySequence() override; |
1621 | |
1622 | // These are only used by operator<<. They could be private |
1623 | // if that templated operator could be made a friend. |
1624 | void beginDocuments(); |
1625 | bool preflightDocument(unsigned); |
1626 | void postflightDocument(); |
1627 | void endDocuments(); |
1628 | |
1629 | private: |
1630 | void output(StringRef s); |
1631 | void outputUpToEndOfLine(StringRef s); |
1632 | void newLineCheck(bool EmptySequence = false); |
1633 | void outputNewLine(); |
1634 | void paddedKey(StringRef key); |
1635 | void flowKey(StringRef Key); |
1636 | |
1637 | enum InState { |
1638 | inSeqFirstElement, |
1639 | inSeqOtherElement, |
1640 | inFlowSeqFirstElement, |
1641 | inFlowSeqOtherElement, |
1642 | inMapFirstKey, |
1643 | inMapOtherKey, |
1644 | inFlowMapFirstKey, |
1645 | inFlowMapOtherKey |
1646 | }; |
1647 | |
1648 | static bool inSeqAnyElement(InState State); |
1649 | static bool inFlowSeqAnyElement(InState State); |
1650 | static bool inMapAnyKey(InState State); |
1651 | static bool inFlowMapAnyKey(InState State); |
1652 | |
1653 | raw_ostream &Out; |
1654 | int WrapColumn; |
1655 | SmallVector<InState, 8> StateStack; |
1656 | int Column = 0; |
1657 | int ColumnAtFlowStart = 0; |
1658 | int ColumnAtMapFlowStart = 0; |
1659 | bool NeedBitValueComma = false; |
1660 | bool NeedFlowSequenceComma = false; |
1661 | bool EnumerationMatchFound = false; |
1662 | bool WriteDefaultValues = false; |
1663 | StringRef Padding; |
1664 | StringRef PaddingBeforeContainer; |
1665 | }; |
1666 | |
1667 | template <typename T, typename Context> |
1668 | void IO::processKeyWithDefault(const char *Key, Optional<T> &Val, |
1669 | const Optional<T> &DefaultValue, bool Required, |
1670 | Context &Ctx) { |
1671 | assert(DefaultValue.hasValue() == false &&(static_cast <bool> (DefaultValue.hasValue() == false && "Optional<T> shouldn't have a value!") ? void (0) : __assert_fail ("DefaultValue.hasValue() == false && \"Optional<T> shouldn't have a value!\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1672, __extension__ __PRETTY_FUNCTION__)) |
1672 | "Optional<T> shouldn't have a value!")(static_cast <bool> (DefaultValue.hasValue() == false && "Optional<T> shouldn't have a value!") ? void (0) : __assert_fail ("DefaultValue.hasValue() == false && \"Optional<T> shouldn't have a value!\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1672, __extension__ __PRETTY_FUNCTION__)); |
1673 | void *SaveInfo; |
1674 | bool UseDefault = true; |
1675 | const bool sameAsDefault = outputting() && !Val.hasValue(); |
1676 | if (!outputting() && !Val.hasValue()) |
1677 | Val = T(); |
1678 | if (Val.hasValue() && |
1679 | this->preflightKey(Key, Required, sameAsDefault, UseDefault, SaveInfo)) { |
1680 | |
1681 | // When reading an Optional<X> key from a YAML description, we allow the |
1682 | // special "<none>" value, which can be used to specify that no value was |
1683 | // requested, i.e. the DefaultValue will be assigned. The DefaultValue is |
1684 | // usually None. |
1685 | bool IsNone = false; |
1686 | if (!outputting()) |
1687 | if (const auto *Node = dyn_cast<ScalarNode>(((Input *)this)->getCurrentNode())) |
1688 | // We use rtrim to ignore possible white spaces that might exist when a |
1689 | // comment is present on the same line. |
1690 | IsNone = Node->getRawValue().rtrim(' ') == "<none>"; |
1691 | |
1692 | if (IsNone) |
1693 | Val = DefaultValue; |
1694 | else |
1695 | yamlize(*this, Val.getValue(), Required, Ctx); |
1696 | this->postflightKey(SaveInfo); |
1697 | } else { |
1698 | if (UseDefault) |
1699 | Val = DefaultValue; |
1700 | } |
1701 | } |
1702 | |
1703 | /// YAML I/O does conversion based on types. But often native data types |
1704 | /// are just a typedef of built in intergral types (e.g. int). But the C++ |
1705 | /// type matching system sees through the typedef and all the typedefed types |
1706 | /// look like a built in type. This will cause the generic YAML I/O conversion |
1707 | /// to be used. To provide better control over the YAML conversion, you can |
1708 | /// use this macro instead of typedef. It will create a class with one field |
1709 | /// and automatic conversion operators to and from the base type. |
1710 | /// Based on BOOST_STRONG_TYPEDEF |
1711 | #define LLVM_YAML_STRONG_TYPEDEF(_base, _type)struct _type { _type() = default; _type(const _base v) : value (v) {} _type(const _type &v) = default; _type &operator =(const _type &rhs) = default; _type &operator=(const _base &rhs) { value = rhs; return *this; } operator const _base & () const { return value; } bool operator==(const _type &rhs) const { return value == rhs.value; } bool operator ==(const _base &rhs) const { return value == rhs; } bool operator <(const _type &rhs) const { return value < rhs.value ; } _base value; using BaseType = _base; }; \ |
1712 | struct _type { \ |
1713 | _type() = default; \ |
1714 | _type(const _base v) : value(v) {} \ |
1715 | _type(const _type &v) = default; \ |
1716 | _type &operator=(const _type &rhs) = default; \ |
1717 | _type &operator=(const _base &rhs) { value = rhs; return *this; } \ |
1718 | operator const _base & () const { return value; } \ |
1719 | bool operator==(const _type &rhs) const { return value == rhs.value; } \ |
1720 | bool operator==(const _base &rhs) const { return value == rhs; } \ |
1721 | bool operator<(const _type &rhs) const { return value < rhs.value; } \ |
1722 | _base value; \ |
1723 | using BaseType = _base; \ |
1724 | }; |
1725 | |
1726 | /// |
1727 | /// Use these types instead of uintXX_t in any mapping to have |
1728 | /// its yaml output formatted as hexadecimal. |
1729 | /// |
1730 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)struct Hex8 { Hex8() = default; Hex8(const uint8_t v) : value (v) {} Hex8(const Hex8 &v) = default; Hex8 &operator= (const Hex8 &rhs) = default; Hex8 &operator=(const uint8_t &rhs) { value = rhs; return *this; } operator const uint8_t & () const { return value; } bool operator==(const Hex8 & rhs) const { return value == rhs.value; } bool operator==(const uint8_t &rhs) const { return value == rhs; } bool operator <(const Hex8 &rhs) const { return value < rhs.value ; } uint8_t value; using BaseType = uint8_t; }; |
1731 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)struct Hex16 { Hex16() = default; Hex16(const uint16_t v) : value (v) {} Hex16(const Hex16 &v) = default; Hex16 &operator =(const Hex16 &rhs) = default; Hex16 &operator=(const uint16_t &rhs) { value = rhs; return *this; } operator const uint16_t & () const { return value; } bool operator==(const Hex16 &rhs) const { return value == rhs.value; } bool operator ==(const uint16_t &rhs) const { return value == rhs; } bool operator<(const Hex16 &rhs) const { return value < rhs.value; } uint16_t value; using BaseType = uint16_t; }; |
1732 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)struct Hex32 { Hex32() = default; Hex32(const uint32_t v) : value (v) {} Hex32(const Hex32 &v) = default; Hex32 &operator =(const Hex32 &rhs) = default; Hex32 &operator=(const uint32_t &rhs) { value = rhs; return *this; } operator const uint32_t & () const { return value; } bool operator==(const Hex32 &rhs) const { return value == rhs.value; } bool operator ==(const uint32_t &rhs) const { return value == rhs; } bool operator<(const Hex32 &rhs) const { return value < rhs.value; } uint32_t value; using BaseType = uint32_t; }; |
1733 | LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)struct Hex64 { Hex64() = default; Hex64(const uint64_t v) : value (v) {} Hex64(const Hex64 &v) = default; Hex64 &operator =(const Hex64 &rhs) = default; Hex64 &operator=(const uint64_t &rhs) { value = rhs; return *this; } operator const uint64_t & () const { return value; } bool operator==(const Hex64 &rhs) const { return value == rhs.value; } bool operator ==(const uint64_t &rhs) const { return value == rhs; } bool operator<(const Hex64 &rhs) const { return value < rhs.value; } uint64_t value; using BaseType = uint64_t; }; |
1734 | |
1735 | template<> |
1736 | struct ScalarTraits<Hex8> { |
1737 | static void output(const Hex8 &, void *, raw_ostream &); |
1738 | static StringRef input(StringRef, void *, Hex8 &); |
1739 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1740 | }; |
1741 | |
1742 | template<> |
1743 | struct ScalarTraits<Hex16> { |
1744 | static void output(const Hex16 &, void *, raw_ostream &); |
1745 | static StringRef input(StringRef, void *, Hex16 &); |
1746 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1747 | }; |
1748 | |
1749 | template<> |
1750 | struct ScalarTraits<Hex32> { |
1751 | static void output(const Hex32 &, void *, raw_ostream &); |
1752 | static StringRef input(StringRef, void *, Hex32 &); |
1753 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1754 | }; |
1755 | |
1756 | template<> |
1757 | struct ScalarTraits<Hex64> { |
1758 | static void output(const Hex64 &, void *, raw_ostream &); |
1759 | static StringRef input(StringRef, void *, Hex64 &); |
1760 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1761 | }; |
1762 | |
1763 | template <> struct ScalarTraits<VersionTuple> { |
1764 | static void output(const VersionTuple &Value, void *, llvm::raw_ostream &Out); |
1765 | static StringRef input(StringRef, void *, VersionTuple &); |
1766 | static QuotingType mustQuote(StringRef) { return QuotingType::None; } |
1767 | }; |
1768 | |
1769 | // Define non-member operator>> so that Input can stream in a document list. |
1770 | template <typename T> |
1771 | inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &> |
1772 | operator>>(Input &yin, T &docList) { |
1773 | int i = 0; |
1774 | EmptyContext Ctx; |
1775 | while ( yin.setCurrentDocument() ) { |
1776 | yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx); |
1777 | if ( yin.error() ) |
1778 | return yin; |
1779 | yin.nextDocument(); |
1780 | ++i; |
1781 | } |
1782 | return yin; |
1783 | } |
1784 | |
1785 | // Define non-member operator>> so that Input can stream in a map as a document. |
1786 | template <typename T> |
1787 | inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Input &> |
1788 | operator>>(Input &yin, T &docMap) { |
1789 | EmptyContext Ctx; |
1790 | yin.setCurrentDocument(); |
1791 | yamlize(yin, docMap, true, Ctx); |
1792 | return yin; |
1793 | } |
1794 | |
1795 | // Define non-member operator>> so that Input can stream in a sequence as |
1796 | // a document. |
1797 | template <typename T> |
1798 | inline std::enable_if_t<has_SequenceTraits<T>::value, Input &> |
1799 | operator>>(Input &yin, T &docSeq) { |
1800 | EmptyContext Ctx; |
1801 | if (yin.setCurrentDocument()) |
1802 | yamlize(yin, docSeq, true, Ctx); |
1803 | return yin; |
1804 | } |
1805 | |
1806 | // Define non-member operator>> so that Input can stream in a block scalar. |
1807 | template <typename T> |
1808 | inline std::enable_if_t<has_BlockScalarTraits<T>::value, Input &> |
1809 | operator>>(Input &In, T &Val) { |
1810 | EmptyContext Ctx; |
1811 | if (In.setCurrentDocument()) |
1812 | yamlize(In, Val, true, Ctx); |
1813 | return In; |
1814 | } |
1815 | |
1816 | // Define non-member operator>> so that Input can stream in a string map. |
1817 | template <typename T> |
1818 | inline std::enable_if_t<has_CustomMappingTraits<T>::value, Input &> |
1819 | operator>>(Input &In, T &Val) { |
1820 | EmptyContext Ctx; |
1821 | if (In.setCurrentDocument()) |
1822 | yamlize(In, Val, true, Ctx); |
1823 | return In; |
1824 | } |
1825 | |
1826 | // Define non-member operator>> so that Input can stream in a polymorphic type. |
1827 | template <typename T> |
1828 | inline std::enable_if_t<has_PolymorphicTraits<T>::value, Input &> |
1829 | operator>>(Input &In, T &Val) { |
1830 | EmptyContext Ctx; |
1831 | if (In.setCurrentDocument()) |
1832 | yamlize(In, Val, true, Ctx); |
1833 | return In; |
1834 | } |
1835 | |
1836 | // Provide better error message about types missing a trait specialization |
1837 | template <typename T> |
1838 | inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Input &> |
1839 | operator>>(Input &yin, T &docSeq) { |
1840 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; |
1841 | return yin; |
1842 | } |
1843 | |
1844 | // Define non-member operator<< so that Output can stream out document list. |
1845 | template <typename T> |
1846 | inline std::enable_if_t<has_DocumentListTraits<T>::value, Output &> |
1847 | operator<<(Output &yout, T &docList) { |
1848 | EmptyContext Ctx; |
1849 | yout.beginDocuments(); |
1850 | const size_t count = DocumentListTraits<T>::size(yout, docList); |
1851 | for(size_t i=0; i < count; ++i) { |
1852 | if ( yout.preflightDocument(i) ) { |
1853 | yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true, |
1854 | Ctx); |
1855 | yout.postflightDocument(); |
1856 | } |
1857 | } |
1858 | yout.endDocuments(); |
1859 | return yout; |
1860 | } |
1861 | |
1862 | // Define non-member operator<< so that Output can stream out a map. |
1863 | template <typename T> |
1864 | inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Output &> |
1865 | operator<<(Output &yout, T &map) { |
1866 | EmptyContext Ctx; |
1867 | yout.beginDocuments(); |
1868 | if ( yout.preflightDocument(0) ) { |
1869 | yamlize(yout, map, true, Ctx); |
1870 | yout.postflightDocument(); |
1871 | } |
1872 | yout.endDocuments(); |
1873 | return yout; |
1874 | } |
1875 | |
1876 | // Define non-member operator<< so that Output can stream out a sequence. |
1877 | template <typename T> |
1878 | inline std::enable_if_t<has_SequenceTraits<T>::value, Output &> |
1879 | operator<<(Output &yout, T &seq) { |
1880 | EmptyContext Ctx; |
1881 | yout.beginDocuments(); |
1882 | if ( yout.preflightDocument(0) ) { |
1883 | yamlize(yout, seq, true, Ctx); |
1884 | yout.postflightDocument(); |
1885 | } |
1886 | yout.endDocuments(); |
1887 | return yout; |
1888 | } |
1889 | |
1890 | // Define non-member operator<< so that Output can stream out a block scalar. |
1891 | template <typename T> |
1892 | inline std::enable_if_t<has_BlockScalarTraits<T>::value, Output &> |
1893 | operator<<(Output &Out, T &Val) { |
1894 | EmptyContext Ctx; |
1895 | Out.beginDocuments(); |
1896 | if (Out.preflightDocument(0)) { |
1897 | yamlize(Out, Val, true, Ctx); |
1898 | Out.postflightDocument(); |
1899 | } |
1900 | Out.endDocuments(); |
1901 | return Out; |
1902 | } |
1903 | |
1904 | // Define non-member operator<< so that Output can stream out a string map. |
1905 | template <typename T> |
1906 | inline std::enable_if_t<has_CustomMappingTraits<T>::value, Output &> |
1907 | operator<<(Output &Out, T &Val) { |
1908 | EmptyContext Ctx; |
1909 | Out.beginDocuments(); |
1910 | if (Out.preflightDocument(0)) { |
1911 | yamlize(Out, Val, true, Ctx); |
1912 | Out.postflightDocument(); |
1913 | } |
1914 | Out.endDocuments(); |
1915 | return Out; |
1916 | } |
1917 | |
1918 | // Define non-member operator<< so that Output can stream out a polymorphic |
1919 | // type. |
1920 | template <typename T> |
1921 | inline std::enable_if_t<has_PolymorphicTraits<T>::value, Output &> |
1922 | operator<<(Output &Out, T &Val) { |
1923 | EmptyContext Ctx; |
1924 | Out.beginDocuments(); |
1925 | if (Out.preflightDocument(0)) { |
1926 | // FIXME: The parser does not support explicit documents terminated with a |
1927 | // plain scalar; the end-marker is included as part of the scalar token. |
1928 | assert(PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && "plain scalar documents are not supported")(static_cast <bool> (PolymorphicTraits<T>::getKind (Val) != NodeKind::Scalar && "plain scalar documents are not supported" ) ? void (0) : __assert_fail ("PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && \"plain scalar documents are not supported\"" , "llvm/include/llvm/Support/YAMLTraits.h", 1928, __extension__ __PRETTY_FUNCTION__)); |
1929 | yamlize(Out, Val, true, Ctx); |
1930 | Out.postflightDocument(); |
1931 | } |
1932 | Out.endDocuments(); |
1933 | return Out; |
1934 | } |
1935 | |
1936 | // Provide better error message about types missing a trait specialization |
1937 | template <typename T> |
1938 | inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Output &> |
1939 | operator<<(Output &yout, T &seq) { |
1940 | char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)]; |
1941 | return yout; |
1942 | } |
1943 | |
1944 | template <bool B> struct IsFlowSequenceBase {}; |
1945 | template <> struct IsFlowSequenceBase<true> { static const bool flow = true; }; |
1946 | |
1947 | template <typename T, bool Flow> |
1948 | struct SequenceTraitsImpl : IsFlowSequenceBase<Flow> { |
1949 | private: |
1950 | using type = typename T::value_type; |
1951 | |
1952 | public: |
1953 | static size_t size(IO &io, T &seq) { return seq.size(); } |
1954 | |
1955 | static type &element(IO &io, T &seq, size_t index) { |
1956 | if (index >= seq.size()) |
1957 | seq.resize(index + 1); |
1958 | return seq[index]; |
1959 | } |
1960 | }; |
1961 | |
1962 | // Simple helper to check an expression can be used as a bool-valued template |
1963 | // argument. |
1964 | template <bool> struct CheckIsBool { static const bool value = true; }; |
1965 | |
1966 | // If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have |
1967 | // SequenceTraits that do the obvious thing. |
1968 | template <typename T> |
1969 | struct SequenceTraits< |
1970 | std::vector<T>, |
1971 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> |
1972 | : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {}; |
1973 | template <typename T, unsigned N> |
1974 | struct SequenceTraits< |
1975 | SmallVector<T, N>, |
1976 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> |
1977 | : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {}; |
1978 | template <typename T> |
1979 | struct SequenceTraits< |
1980 | SmallVectorImpl<T>, |
1981 | std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>> |
1982 | : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {}; |
1983 | |
1984 | // Sequences of fundamental types use flow formatting. |
1985 | template <typename T> |
1986 | struct SequenceElementTraits<T, |
1987 | std::enable_if_t<std::is_fundamental<T>::value>> { |
1988 | static const bool flow = true; |
1989 | }; |
1990 | |
1991 | // Sequences of strings use block formatting. |
1992 | template<> struct SequenceElementTraits<std::string> { |
1993 | static const bool flow = false; |
1994 | }; |
1995 | template<> struct SequenceElementTraits<StringRef> { |
1996 | static const bool flow = false; |
1997 | }; |
1998 | template<> struct SequenceElementTraits<std::pair<std::string, std::string>> { |
1999 | static const bool flow = false; |
2000 | }; |
2001 | |
2002 | /// Implementation of CustomMappingTraits for std::map<std::string, T>. |
2003 | template <typename T> struct StdMapStringCustomMappingTraitsImpl { |
2004 | using map_type = std::map<std::string, T>; |
2005 | |
2006 | static void inputOne(IO &io, StringRef key, map_type &v) { |
2007 | io.mapRequired(key.str().c_str(), v[std::string(key)]); |
2008 | } |
2009 | |
2010 | static void output(IO &io, map_type &v) { |
2011 | for (auto &p : v) |
2012 | io.mapRequired(p.first.c_str(), p.second); |
2013 | } |
2014 | }; |
2015 | |
2016 | } // end namespace yaml |
2017 | } // end namespace llvm |
2018 | |
2019 | #define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <TYPE>::value && !std::is_same<TYPE, std::string >::value && !std::is_same<TYPE, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<TYPE> { static const bool flow = FLOW; }; } } \ |
2020 | namespace llvm { \ |
2021 | namespace yaml { \ |
2022 | static_assert( \ |
2023 | !std::is_fundamental<TYPE>::value && \ |
2024 | !std::is_same<TYPE, std::string>::value && \ |
2025 | !std::is_same<TYPE, llvm::StringRef>::value, \ |
2026 | "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \ |
2027 | template <> struct SequenceElementTraits<TYPE> { \ |
2028 | static const bool flow = FLOW; \ |
2029 | }; \ |
2030 | } \ |
2031 | } |
2032 | |
2033 | /// Utility for declaring that a std::vector of a particular type |
2034 | /// should be considered a YAML sequence. |
2035 | #define LLVM_YAML_IS_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <type>::value && !std::is_same<type, std::string >::value && !std::is_same<type, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = false; }; } } \ |
2036 | LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <type>::value && !std::is_same<type, std::string >::value && !std::is_same<type, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = false; }; } } |
2037 | |
2038 | /// Utility for declaring that a std::vector of a particular type |
2039 | /// should be considered a YAML flow sequence. |
2040 | #define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <type>::value && !std::is_same<type, std::string >::value && !std::is_same<type, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = true; }; } } \ |
2041 | LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <type>::value && !std::is_same<type, std::string >::value && !std::is_same<type, llvm::StringRef >::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<type> { static const bool flow = true; }; } } |
2042 | |
2043 | #define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type)namespace llvm { namespace yaml { template <> struct MappingTraits <Type> { static void mapping(IO &IO, Type &Obj) ; }; } } \ |
2044 | namespace llvm { \ |
2045 | namespace yaml { \ |
2046 | template <> struct MappingTraits<Type> { \ |
2047 | static void mapping(IO &IO, Type &Obj); \ |
2048 | }; \ |
2049 | } \ |
2050 | } |
2051 | |
2052 | #define LLVM_YAML_DECLARE_ENUM_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarEnumerationTraits <Type> { static void enumeration(IO &io, Type & Value); }; } } \ |
2053 | namespace llvm { \ |
2054 | namespace yaml { \ |
2055 | template <> struct ScalarEnumerationTraits<Type> { \ |
2056 | static void enumeration(IO &io, Type &Value); \ |
2057 | }; \ |
2058 | } \ |
2059 | } |
2060 | |
2061 | #define LLVM_YAML_DECLARE_BITSET_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarBitSetTraits <Type> { static void bitset(IO &IO, Type &Options ); }; } } \ |
2062 | namespace llvm { \ |
2063 | namespace yaml { \ |
2064 | template <> struct ScalarBitSetTraits<Type> { \ |
2065 | static void bitset(IO &IO, Type &Options); \ |
2066 | }; \ |
2067 | } \ |
2068 | } |
2069 | |
2070 | #define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote)namespace llvm { namespace yaml { template <> struct ScalarTraits <Type> { static void output(const Type &Value, void *ctx, raw_ostream &Out); static StringRef input(StringRef Scalar, void *ctxt, Type &Value); static QuotingType mustQuote (StringRef) { return MustQuote; } }; } } \ |
2071 | namespace llvm { \ |
2072 | namespace yaml { \ |
2073 | template <> struct ScalarTraits<Type> { \ |
2074 | static void output(const Type &Value, void *ctx, raw_ostream &Out); \ |
2075 | static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \ |
2076 | static QuotingType mustQuote(StringRef) { return MustQuote; } \ |
2077 | }; \ |
2078 | } \ |
2079 | } |
2080 | |
2081 | /// Utility for declaring that a std::vector of a particular type |
2082 | /// should be considered a YAML document list. |
2083 | #define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type)namespace llvm { namespace yaml { template <unsigned N> struct DocumentListTraits<SmallVector<_type, N>> : public SequenceTraitsImpl<SmallVector<_type, N>, false > {}; template <> struct DocumentListTraits<std:: vector<_type>> : public SequenceTraitsImpl<std::vector <_type>, false> {}; } } \ |
2084 | namespace llvm { \ |
2085 | namespace yaml { \ |
2086 | template <unsigned N> \ |
2087 | struct DocumentListTraits<SmallVector<_type, N>> \ |
2088 | : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \ |
2089 | template <> \ |
2090 | struct DocumentListTraits<std::vector<_type>> \ |
2091 | : public SequenceTraitsImpl<std::vector<_type>, false> {}; \ |
2092 | } \ |
2093 | } |
2094 | |
2095 | /// Utility for declaring that std::map<std::string, _type> should be considered |
2096 | /// a YAML map. |
2097 | #define LLVM_YAML_IS_STRING_MAP(_type)namespace llvm { namespace yaml { template <> struct CustomMappingTraits <std::map<std::string, _type>> : public StdMapStringCustomMappingTraitsImpl <_type> {}; } } \ |
2098 | namespace llvm { \ |
2099 | namespace yaml { \ |
2100 | template <> \ |
2101 | struct CustomMappingTraits<std::map<std::string, _type>> \ |
2102 | : public StdMapStringCustomMappingTraitsImpl<_type> {}; \ |
2103 | } \ |
2104 | } |
2105 | |
2106 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::yaml::Hex64>::value && !std::is_same< llvm::yaml::Hex64, std::string>::value && !std::is_same <llvm::yaml::Hex64, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex64> { static const bool flow = true; }; } } |
2107 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::yaml::Hex32>::value && !std::is_same< llvm::yaml::Hex32, std::string>::value && !std::is_same <llvm::yaml::Hex32, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex32> { static const bool flow = true; }; } } |
2108 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::yaml::Hex16>::value && !std::is_same< llvm::yaml::Hex16, std::string>::value && !std::is_same <llvm::yaml::Hex16, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex16> { static const bool flow = true; }; } } |
2109 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)namespace llvm { namespace yaml { static_assert( !std::is_fundamental <llvm::yaml::Hex8>::value && !std::is_same<llvm ::yaml::Hex8, std::string>::value && !std::is_same <llvm::yaml::Hex8, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control" ); template <> struct SequenceElementTraits<llvm::yaml ::Hex8> { static const bool flow = true; }; } } |
2110 | |
2111 | #endif // LLVM_SUPPORT_YAMLTRAITS_H |