ELFYAML.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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. #include "llvm/ObjectYAML/ELFYAML.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/MapVector.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/BinaryFormat/ELF.h"
  17. #include "llvm/Support/ARMEHABI.h"
  18. #include "llvm/Support/Casting.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/MipsABIFlags.h"
  21. #include "llvm/Support/YAMLTraits.h"
  22. #include "llvm/Support/WithColor.h"
  23. #include <cassert>
  24. #include <cstdint>
  25. namespace llvm {
  26. ELFYAML::Chunk::~Chunk() = default;
  27. namespace ELFYAML {
  28. unsigned Object::getMachine() const {
  29. if (Header.Machine)
  30. return *Header.Machine;
  31. return llvm::ELF::EM_NONE;
  32. }
  33. constexpr StringRef SectionHeaderTable::TypeStr;
  34. } // namespace ELFYAML
  35. namespace yaml {
  36. void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(
  37. IO &IO, ELFYAML::ELF_ET &Value) {
  38. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  39. ECase(ET_NONE);
  40. ECase(ET_REL);
  41. ECase(ET_EXEC);
  42. ECase(ET_DYN);
  43. ECase(ET_CORE);
  44. #undef ECase
  45. IO.enumFallback<Hex16>(Value);
  46. }
  47. void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration(
  48. IO &IO, ELFYAML::ELF_PT &Value) {
  49. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  50. ECase(PT_NULL);
  51. ECase(PT_LOAD);
  52. ECase(PT_DYNAMIC);
  53. ECase(PT_INTERP);
  54. ECase(PT_NOTE);
  55. ECase(PT_SHLIB);
  56. ECase(PT_PHDR);
  57. ECase(PT_TLS);
  58. ECase(PT_GNU_EH_FRAME);
  59. ECase(PT_GNU_STACK);
  60. ECase(PT_GNU_RELRO);
  61. ECase(PT_GNU_PROPERTY);
  62. #undef ECase
  63. IO.enumFallback<Hex32>(Value);
  64. }
  65. void ScalarEnumerationTraits<ELFYAML::ELF_NT>::enumeration(
  66. IO &IO, ELFYAML::ELF_NT &Value) {
  67. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  68. // Generic note types.
  69. ECase(NT_VERSION);
  70. ECase(NT_ARCH);
  71. ECase(NT_GNU_BUILD_ATTRIBUTE_OPEN);
  72. ECase(NT_GNU_BUILD_ATTRIBUTE_FUNC);
  73. // Core note types.
  74. ECase(NT_PRSTATUS);
  75. ECase(NT_FPREGSET);
  76. ECase(NT_PRPSINFO);
  77. ECase(NT_TASKSTRUCT);
  78. ECase(NT_AUXV);
  79. ECase(NT_PSTATUS);
  80. ECase(NT_FPREGS);
  81. ECase(NT_PSINFO);
  82. ECase(NT_LWPSTATUS);
  83. ECase(NT_LWPSINFO);
  84. ECase(NT_WIN32PSTATUS);
  85. ECase(NT_PPC_VMX);
  86. ECase(NT_PPC_VSX);
  87. ECase(NT_PPC_TAR);
  88. ECase(NT_PPC_PPR);
  89. ECase(NT_PPC_DSCR);
  90. ECase(NT_PPC_EBB);
  91. ECase(NT_PPC_PMU);
  92. ECase(NT_PPC_TM_CGPR);
  93. ECase(NT_PPC_TM_CFPR);
  94. ECase(NT_PPC_TM_CVMX);
  95. ECase(NT_PPC_TM_CVSX);
  96. ECase(NT_PPC_TM_SPR);
  97. ECase(NT_PPC_TM_CTAR);
  98. ECase(NT_PPC_TM_CPPR);
  99. ECase(NT_PPC_TM_CDSCR);
  100. ECase(NT_386_TLS);
  101. ECase(NT_386_IOPERM);
  102. ECase(NT_X86_XSTATE);
  103. ECase(NT_S390_HIGH_GPRS);
  104. ECase(NT_S390_TIMER);
  105. ECase(NT_S390_TODCMP);
  106. ECase(NT_S390_TODPREG);
  107. ECase(NT_S390_CTRS);
  108. ECase(NT_S390_PREFIX);
  109. ECase(NT_S390_LAST_BREAK);
  110. ECase(NT_S390_SYSTEM_CALL);
  111. ECase(NT_S390_TDB);
  112. ECase(NT_S390_VXRS_LOW);
  113. ECase(NT_S390_VXRS_HIGH);
  114. ECase(NT_S390_GS_CB);
  115. ECase(NT_S390_GS_BC);
  116. ECase(NT_ARM_VFP);
  117. ECase(NT_ARM_TLS);
  118. ECase(NT_ARM_HW_BREAK);
  119. ECase(NT_ARM_HW_WATCH);
  120. ECase(NT_ARM_SVE);
  121. ECase(NT_ARM_PAC_MASK);
  122. ECase(NT_FILE);
  123. ECase(NT_PRXFPREG);
  124. ECase(NT_SIGINFO);
  125. // LLVM-specific notes.
  126. ECase(NT_LLVM_HWASAN_GLOBALS);
  127. // GNU note types
  128. ECase(NT_GNU_ABI_TAG);
  129. ECase(NT_GNU_HWCAP);
  130. ECase(NT_GNU_BUILD_ID);
  131. ECase(NT_GNU_GOLD_VERSION);
  132. ECase(NT_GNU_PROPERTY_TYPE_0);
  133. // FreeBSD note types.
  134. ECase(NT_FREEBSD_ABI_TAG);
  135. ECase(NT_FREEBSD_NOINIT_TAG);
  136. ECase(NT_FREEBSD_ARCH_TAG);
  137. ECase(NT_FREEBSD_FEATURE_CTL);
  138. // FreeBSD core note types.
  139. ECase(NT_FREEBSD_THRMISC);
  140. ECase(NT_FREEBSD_PROCSTAT_PROC);
  141. ECase(NT_FREEBSD_PROCSTAT_FILES);
  142. ECase(NT_FREEBSD_PROCSTAT_VMMAP);
  143. ECase(NT_FREEBSD_PROCSTAT_GROUPS);
  144. ECase(NT_FREEBSD_PROCSTAT_UMASK);
  145. ECase(NT_FREEBSD_PROCSTAT_RLIMIT);
  146. ECase(NT_FREEBSD_PROCSTAT_OSREL);
  147. ECase(NT_FREEBSD_PROCSTAT_PSSTRINGS);
  148. ECase(NT_FREEBSD_PROCSTAT_AUXV);
  149. // NetBSD core note types.
  150. ECase(NT_NETBSDCORE_PROCINFO);
  151. ECase(NT_NETBSDCORE_AUXV);
  152. ECase(NT_NETBSDCORE_LWPSTATUS);
  153. // OpenBSD core note types.
  154. ECase(NT_OPENBSD_PROCINFO);
  155. ECase(NT_OPENBSD_AUXV);
  156. ECase(NT_OPENBSD_REGS);
  157. ECase(NT_OPENBSD_FPREGS);
  158. ECase(NT_OPENBSD_XFPREGS);
  159. ECase(NT_OPENBSD_WCOOKIE);
  160. // AMD specific notes. (Code Object V2)
  161. ECase(NT_AMD_HSA_CODE_OBJECT_VERSION);
  162. ECase(NT_AMD_HSA_HSAIL);
  163. ECase(NT_AMD_HSA_ISA_VERSION);
  164. ECase(NT_AMD_HSA_METADATA);
  165. ECase(NT_AMD_HSA_ISA_NAME);
  166. ECase(NT_AMD_PAL_METADATA);
  167. // AMDGPU specific notes. (Code Object V3)
  168. ECase(NT_AMDGPU_METADATA);
  169. #undef ECase
  170. IO.enumFallback<Hex32>(Value);
  171. }
  172. void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(
  173. IO &IO, ELFYAML::ELF_EM &Value) {
  174. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  175. ECase(EM_NONE);
  176. ECase(EM_M32);
  177. ECase(EM_SPARC);
  178. ECase(EM_386);
  179. ECase(EM_68K);
  180. ECase(EM_88K);
  181. ECase(EM_IAMCU);
  182. ECase(EM_860);
  183. ECase(EM_MIPS);
  184. ECase(EM_S370);
  185. ECase(EM_MIPS_RS3_LE);
  186. ECase(EM_PARISC);
  187. ECase(EM_VPP500);
  188. ECase(EM_SPARC32PLUS);
  189. ECase(EM_960);
  190. ECase(EM_PPC);
  191. ECase(EM_PPC64);
  192. ECase(EM_S390);
  193. ECase(EM_SPU);
  194. ECase(EM_V800);
  195. ECase(EM_FR20);
  196. ECase(EM_RH32);
  197. ECase(EM_RCE);
  198. ECase(EM_ARM);
  199. ECase(EM_ALPHA);
  200. ECase(EM_SH);
  201. ECase(EM_SPARCV9);
  202. ECase(EM_TRICORE);
  203. ECase(EM_ARC);
  204. ECase(EM_H8_300);
  205. ECase(EM_H8_300H);
  206. ECase(EM_H8S);
  207. ECase(EM_H8_500);
  208. ECase(EM_IA_64);
  209. ECase(EM_MIPS_X);
  210. ECase(EM_COLDFIRE);
  211. ECase(EM_68HC12);
  212. ECase(EM_MMA);
  213. ECase(EM_PCP);
  214. ECase(EM_NCPU);
  215. ECase(EM_NDR1);
  216. ECase(EM_STARCORE);
  217. ECase(EM_ME16);
  218. ECase(EM_ST100);
  219. ECase(EM_TINYJ);
  220. ECase(EM_X86_64);
  221. ECase(EM_PDSP);
  222. ECase(EM_PDP10);
  223. ECase(EM_PDP11);
  224. ECase(EM_FX66);
  225. ECase(EM_ST9PLUS);
  226. ECase(EM_ST7);
  227. ECase(EM_68HC16);
  228. ECase(EM_68HC11);
  229. ECase(EM_68HC08);
  230. ECase(EM_68HC05);
  231. ECase(EM_SVX);
  232. ECase(EM_ST19);
  233. ECase(EM_VAX);
  234. ECase(EM_CRIS);
  235. ECase(EM_JAVELIN);
  236. ECase(EM_FIREPATH);
  237. ECase(EM_ZSP);
  238. ECase(EM_MMIX);
  239. ECase(EM_HUANY);
  240. ECase(EM_PRISM);
  241. ECase(EM_AVR);
  242. ECase(EM_FR30);
  243. ECase(EM_D10V);
  244. ECase(EM_D30V);
  245. ECase(EM_V850);
  246. ECase(EM_M32R);
  247. ECase(EM_MN10300);
  248. ECase(EM_MN10200);
  249. ECase(EM_PJ);
  250. ECase(EM_OPENRISC);
  251. ECase(EM_ARC_COMPACT);
  252. ECase(EM_XTENSA);
  253. ECase(EM_VIDEOCORE);
  254. ECase(EM_TMM_GPP);
  255. ECase(EM_NS32K);
  256. ECase(EM_TPC);
  257. ECase(EM_SNP1K);
  258. ECase(EM_ST200);
  259. ECase(EM_IP2K);
  260. ECase(EM_MAX);
  261. ECase(EM_CR);
  262. ECase(EM_F2MC16);
  263. ECase(EM_MSP430);
  264. ECase(EM_BLACKFIN);
  265. ECase(EM_SE_C33);
  266. ECase(EM_SEP);
  267. ECase(EM_ARCA);
  268. ECase(EM_UNICORE);
  269. ECase(EM_EXCESS);
  270. ECase(EM_DXP);
  271. ECase(EM_ALTERA_NIOS2);
  272. ECase(EM_CRX);
  273. ECase(EM_XGATE);
  274. ECase(EM_C166);
  275. ECase(EM_M16C);
  276. ECase(EM_DSPIC30F);
  277. ECase(EM_CE);
  278. ECase(EM_M32C);
  279. ECase(EM_TSK3000);
  280. ECase(EM_RS08);
  281. ECase(EM_SHARC);
  282. ECase(EM_ECOG2);
  283. ECase(EM_SCORE7);
  284. ECase(EM_DSP24);
  285. ECase(EM_VIDEOCORE3);
  286. ECase(EM_LATTICEMICO32);
  287. ECase(EM_SE_C17);
  288. ECase(EM_TI_C6000);
  289. ECase(EM_TI_C2000);
  290. ECase(EM_TI_C5500);
  291. ECase(EM_MMDSP_PLUS);
  292. ECase(EM_CYPRESS_M8C);
  293. ECase(EM_R32C);
  294. ECase(EM_TRIMEDIA);
  295. ECase(EM_HEXAGON);
  296. ECase(EM_8051);
  297. ECase(EM_STXP7X);
  298. ECase(EM_NDS32);
  299. ECase(EM_ECOG1);
  300. ECase(EM_ECOG1X);
  301. ECase(EM_MAXQ30);
  302. ECase(EM_XIMO16);
  303. ECase(EM_MANIK);
  304. ECase(EM_CRAYNV2);
  305. ECase(EM_RX);
  306. ECase(EM_METAG);
  307. ECase(EM_MCST_ELBRUS);
  308. ECase(EM_ECOG16);
  309. ECase(EM_CR16);
  310. ECase(EM_ETPU);
  311. ECase(EM_SLE9X);
  312. ECase(EM_L10M);
  313. ECase(EM_K10M);
  314. ECase(EM_AARCH64);
  315. ECase(EM_AVR32);
  316. ECase(EM_STM8);
  317. ECase(EM_TILE64);
  318. ECase(EM_TILEPRO);
  319. ECase(EM_MICROBLAZE);
  320. ECase(EM_CUDA);
  321. ECase(EM_TILEGX);
  322. ECase(EM_CLOUDSHIELD);
  323. ECase(EM_COREA_1ST);
  324. ECase(EM_COREA_2ND);
  325. ECase(EM_ARC_COMPACT2);
  326. ECase(EM_OPEN8);
  327. ECase(EM_RL78);
  328. ECase(EM_VIDEOCORE5);
  329. ECase(EM_78KOR);
  330. ECase(EM_56800EX);
  331. ECase(EM_AMDGPU);
  332. ECase(EM_RISCV);
  333. ECase(EM_LANAI);
  334. ECase(EM_BPF);
  335. ECase(EM_VE);
  336. ECase(EM_CSKY);
  337. #undef ECase
  338. IO.enumFallback<Hex16>(Value);
  339. }
  340. void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
  341. IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
  342. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  343. // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
  344. // here.
  345. ECase(ELFCLASS32);
  346. ECase(ELFCLASS64);
  347. #undef ECase
  348. }
  349. void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
  350. IO &IO, ELFYAML::ELF_ELFDATA &Value) {
  351. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  352. // ELFDATANONE is an invalid data encoding, but we accept it because
  353. // we want to be able to produce invalid binaries for the tests.
  354. ECase(ELFDATANONE);
  355. ECase(ELFDATA2LSB);
  356. ECase(ELFDATA2MSB);
  357. #undef ECase
  358. }
  359. void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
  360. IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
  361. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  362. ECase(ELFOSABI_NONE);
  363. ECase(ELFOSABI_HPUX);
  364. ECase(ELFOSABI_NETBSD);
  365. ECase(ELFOSABI_GNU);
  366. ECase(ELFOSABI_LINUX);
  367. ECase(ELFOSABI_HURD);
  368. ECase(ELFOSABI_SOLARIS);
  369. ECase(ELFOSABI_AIX);
  370. ECase(ELFOSABI_IRIX);
  371. ECase(ELFOSABI_FREEBSD);
  372. ECase(ELFOSABI_TRU64);
  373. ECase(ELFOSABI_MODESTO);
  374. ECase(ELFOSABI_OPENBSD);
  375. ECase(ELFOSABI_OPENVMS);
  376. ECase(ELFOSABI_NSK);
  377. ECase(ELFOSABI_AROS);
  378. ECase(ELFOSABI_FENIXOS);
  379. ECase(ELFOSABI_CLOUDABI);
  380. ECase(ELFOSABI_AMDGPU_HSA);
  381. ECase(ELFOSABI_AMDGPU_PAL);
  382. ECase(ELFOSABI_AMDGPU_MESA3D);
  383. ECase(ELFOSABI_ARM);
  384. ECase(ELFOSABI_C6000_ELFABI);
  385. ECase(ELFOSABI_C6000_LINUX);
  386. ECase(ELFOSABI_STANDALONE);
  387. #undef ECase
  388. IO.enumFallback<Hex8>(Value);
  389. }
  390. void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
  391. ELFYAML::ELF_EF &Value) {
  392. const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
  393. assert(Object && "The IO context is not initialized");
  394. #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
  395. #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
  396. switch (Object->getMachine()) {
  397. case ELF::EM_ARM:
  398. BCase(EF_ARM_SOFT_FLOAT);
  399. BCase(EF_ARM_VFP_FLOAT);
  400. BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK);
  401. BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK);
  402. BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK);
  403. BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK);
  404. BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK);
  405. BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK);
  406. break;
  407. case ELF::EM_MIPS:
  408. BCase(EF_MIPS_NOREORDER);
  409. BCase(EF_MIPS_PIC);
  410. BCase(EF_MIPS_CPIC);
  411. BCase(EF_MIPS_ABI2);
  412. BCase(EF_MIPS_32BITMODE);
  413. BCase(EF_MIPS_FP64);
  414. BCase(EF_MIPS_NAN2008);
  415. BCase(EF_MIPS_MICROMIPS);
  416. BCase(EF_MIPS_ARCH_ASE_M16);
  417. BCase(EF_MIPS_ARCH_ASE_MDMX);
  418. BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI);
  419. BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI);
  420. BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI);
  421. BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI);
  422. BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH);
  423. BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH);
  424. BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH);
  425. BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH);
  426. BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH);
  427. BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH);
  428. BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH);
  429. BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH);
  430. BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH);
  431. BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH);
  432. BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH);
  433. BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH);
  434. BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH);
  435. BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH);
  436. BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH);
  437. BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH);
  438. BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH);
  439. BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH);
  440. BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH);
  441. BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH);
  442. BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH);
  443. BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH);
  444. BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH);
  445. BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH);
  446. BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH);
  447. BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH);
  448. BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH);
  449. BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH);
  450. BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH);
  451. break;
  452. case ELF::EM_HEXAGON:
  453. BCaseMask(EF_HEXAGON_MACH_V2, EF_HEXAGON_MACH);
  454. BCaseMask(EF_HEXAGON_MACH_V3, EF_HEXAGON_MACH);
  455. BCaseMask(EF_HEXAGON_MACH_V4, EF_HEXAGON_MACH);
  456. BCaseMask(EF_HEXAGON_MACH_V5, EF_HEXAGON_MACH);
  457. BCaseMask(EF_HEXAGON_MACH_V55, EF_HEXAGON_MACH);
  458. BCaseMask(EF_HEXAGON_MACH_V60, EF_HEXAGON_MACH);
  459. BCaseMask(EF_HEXAGON_MACH_V62, EF_HEXAGON_MACH);
  460. BCaseMask(EF_HEXAGON_MACH_V65, EF_HEXAGON_MACH);
  461. BCaseMask(EF_HEXAGON_MACH_V66, EF_HEXAGON_MACH);
  462. BCaseMask(EF_HEXAGON_MACH_V67, EF_HEXAGON_MACH);
  463. BCaseMask(EF_HEXAGON_MACH_V67T, EF_HEXAGON_MACH);
  464. BCaseMask(EF_HEXAGON_MACH_V68, EF_HEXAGON_MACH);
  465. BCaseMask(EF_HEXAGON_MACH_V69, EF_HEXAGON_MACH);
  466. BCaseMask(EF_HEXAGON_ISA_V2, EF_HEXAGON_ISA);
  467. BCaseMask(EF_HEXAGON_ISA_V3, EF_HEXAGON_ISA);
  468. BCaseMask(EF_HEXAGON_ISA_V4, EF_HEXAGON_ISA);
  469. BCaseMask(EF_HEXAGON_ISA_V5, EF_HEXAGON_ISA);
  470. BCaseMask(EF_HEXAGON_ISA_V55, EF_HEXAGON_ISA);
  471. BCaseMask(EF_HEXAGON_ISA_V60, EF_HEXAGON_ISA);
  472. BCaseMask(EF_HEXAGON_ISA_V62, EF_HEXAGON_ISA);
  473. BCaseMask(EF_HEXAGON_ISA_V65, EF_HEXAGON_ISA);
  474. BCaseMask(EF_HEXAGON_ISA_V66, EF_HEXAGON_ISA);
  475. BCaseMask(EF_HEXAGON_ISA_V67, EF_HEXAGON_ISA);
  476. BCaseMask(EF_HEXAGON_ISA_V68, EF_HEXAGON_ISA);
  477. BCaseMask(EF_HEXAGON_ISA_V69, EF_HEXAGON_ISA);
  478. break;
  479. case ELF::EM_AVR:
  480. BCaseMask(EF_AVR_ARCH_AVR1, EF_AVR_ARCH_MASK);
  481. BCaseMask(EF_AVR_ARCH_AVR2, EF_AVR_ARCH_MASK);
  482. BCaseMask(EF_AVR_ARCH_AVR25, EF_AVR_ARCH_MASK);
  483. BCaseMask(EF_AVR_ARCH_AVR3, EF_AVR_ARCH_MASK);
  484. BCaseMask(EF_AVR_ARCH_AVR31, EF_AVR_ARCH_MASK);
  485. BCaseMask(EF_AVR_ARCH_AVR35, EF_AVR_ARCH_MASK);
  486. BCaseMask(EF_AVR_ARCH_AVR4, EF_AVR_ARCH_MASK);
  487. BCaseMask(EF_AVR_ARCH_AVR5, EF_AVR_ARCH_MASK);
  488. BCaseMask(EF_AVR_ARCH_AVR51, EF_AVR_ARCH_MASK);
  489. BCaseMask(EF_AVR_ARCH_AVR6, EF_AVR_ARCH_MASK);
  490. BCaseMask(EF_AVR_ARCH_AVRTINY, EF_AVR_ARCH_MASK);
  491. BCaseMask(EF_AVR_ARCH_XMEGA1, EF_AVR_ARCH_MASK);
  492. BCaseMask(EF_AVR_ARCH_XMEGA2, EF_AVR_ARCH_MASK);
  493. BCaseMask(EF_AVR_ARCH_XMEGA3, EF_AVR_ARCH_MASK);
  494. BCaseMask(EF_AVR_ARCH_XMEGA4, EF_AVR_ARCH_MASK);
  495. BCaseMask(EF_AVR_ARCH_XMEGA5, EF_AVR_ARCH_MASK);
  496. BCaseMask(EF_AVR_ARCH_XMEGA6, EF_AVR_ARCH_MASK);
  497. BCaseMask(EF_AVR_ARCH_XMEGA7, EF_AVR_ARCH_MASK);
  498. BCase(EF_AVR_LINKRELAX_PREPARED);
  499. break;
  500. case ELF::EM_RISCV:
  501. BCase(EF_RISCV_RVC);
  502. BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI);
  503. BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI);
  504. BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI);
  505. BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI);
  506. BCase(EF_RISCV_RVE);
  507. BCase(EF_RISCV_TSO);
  508. break;
  509. case ELF::EM_AMDGPU:
  510. BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH);
  511. BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH);
  512. BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH);
  513. BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH);
  514. BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH);
  515. BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH);
  516. BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH);
  517. BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH);
  518. BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH);
  519. BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH);
  520. BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH);
  521. BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH);
  522. BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH);
  523. BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH);
  524. BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH);
  525. BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH);
  526. BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH);
  527. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH);
  528. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH);
  529. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX602, EF_AMDGPU_MACH);
  530. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH);
  531. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH);
  532. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH);
  533. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH);
  534. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH);
  535. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX705, EF_AMDGPU_MACH);
  536. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH);
  537. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH);
  538. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH);
  539. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX805, EF_AMDGPU_MACH);
  540. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH);
  541. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH);
  542. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH);
  543. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH);
  544. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH);
  545. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH);
  546. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH);
  547. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90A, EF_AMDGPU_MACH);
  548. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90C, EF_AMDGPU_MACH);
  549. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH);
  550. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH);
  551. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH);
  552. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1013, EF_AMDGPU_MACH);
  553. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1030, EF_AMDGPU_MACH);
  554. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1031, EF_AMDGPU_MACH);
  555. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1032, EF_AMDGPU_MACH);
  556. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1033, EF_AMDGPU_MACH);
  557. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1034, EF_AMDGPU_MACH);
  558. BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1035, EF_AMDGPU_MACH);
  559. switch (Object->Header.ABIVersion) {
  560. default:
  561. // ELFOSABI_AMDGPU_PAL, ELFOSABI_AMDGPU_MESA3D support *_V3 flags.
  562. LLVM_FALLTHROUGH;
  563. case ELF::ELFABIVERSION_AMDGPU_HSA_V3:
  564. BCase(EF_AMDGPU_FEATURE_XNACK_V3);
  565. BCase(EF_AMDGPU_FEATURE_SRAMECC_V3);
  566. break;
  567. case ELF::ELFABIVERSION_AMDGPU_HSA_V4:
  568. case ELF::ELFABIVERSION_AMDGPU_HSA_V5:
  569. BCaseMask(EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4,
  570. EF_AMDGPU_FEATURE_XNACK_V4);
  571. BCaseMask(EF_AMDGPU_FEATURE_XNACK_ANY_V4,
  572. EF_AMDGPU_FEATURE_XNACK_V4);
  573. BCaseMask(EF_AMDGPU_FEATURE_XNACK_OFF_V4,
  574. EF_AMDGPU_FEATURE_XNACK_V4);
  575. BCaseMask(EF_AMDGPU_FEATURE_XNACK_ON_V4,
  576. EF_AMDGPU_FEATURE_XNACK_V4);
  577. BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4,
  578. EF_AMDGPU_FEATURE_SRAMECC_V4);
  579. BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ANY_V4,
  580. EF_AMDGPU_FEATURE_SRAMECC_V4);
  581. BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_OFF_V4,
  582. EF_AMDGPU_FEATURE_SRAMECC_V4);
  583. BCaseMask(EF_AMDGPU_FEATURE_SRAMECC_ON_V4,
  584. EF_AMDGPU_FEATURE_SRAMECC_V4);
  585. break;
  586. }
  587. break;
  588. default:
  589. break;
  590. }
  591. #undef BCase
  592. #undef BCaseMask
  593. }
  594. void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
  595. IO &IO, ELFYAML::ELF_SHT &Value) {
  596. const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
  597. assert(Object && "The IO context is not initialized");
  598. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  599. ECase(SHT_NULL);
  600. ECase(SHT_PROGBITS);
  601. ECase(SHT_SYMTAB);
  602. // FIXME: Issue a diagnostic with this information.
  603. ECase(SHT_STRTAB);
  604. ECase(SHT_RELA);
  605. ECase(SHT_HASH);
  606. ECase(SHT_DYNAMIC);
  607. ECase(SHT_NOTE);
  608. ECase(SHT_NOBITS);
  609. ECase(SHT_REL);
  610. ECase(SHT_SHLIB);
  611. ECase(SHT_DYNSYM);
  612. ECase(SHT_INIT_ARRAY);
  613. ECase(SHT_FINI_ARRAY);
  614. ECase(SHT_PREINIT_ARRAY);
  615. ECase(SHT_GROUP);
  616. ECase(SHT_SYMTAB_SHNDX);
  617. ECase(SHT_RELR);
  618. ECase(SHT_ANDROID_REL);
  619. ECase(SHT_ANDROID_RELA);
  620. ECase(SHT_ANDROID_RELR);
  621. ECase(SHT_LLVM_ODRTAB);
  622. ECase(SHT_LLVM_LINKER_OPTIONS);
  623. ECase(SHT_LLVM_CALL_GRAPH_PROFILE);
  624. ECase(SHT_LLVM_ADDRSIG);
  625. ECase(SHT_LLVM_DEPENDENT_LIBRARIES);
  626. ECase(SHT_LLVM_SYMPART);
  627. ECase(SHT_LLVM_PART_EHDR);
  628. ECase(SHT_LLVM_PART_PHDR);
  629. ECase(SHT_LLVM_BB_ADDR_MAP);
  630. ECase(SHT_GNU_ATTRIBUTES);
  631. ECase(SHT_GNU_HASH);
  632. ECase(SHT_GNU_verdef);
  633. ECase(SHT_GNU_verneed);
  634. ECase(SHT_GNU_versym);
  635. switch (Object->getMachine()) {
  636. case ELF::EM_ARM:
  637. ECase(SHT_ARM_EXIDX);
  638. ECase(SHT_ARM_PREEMPTMAP);
  639. ECase(SHT_ARM_ATTRIBUTES);
  640. ECase(SHT_ARM_DEBUGOVERLAY);
  641. ECase(SHT_ARM_OVERLAYSECTION);
  642. break;
  643. case ELF::EM_HEXAGON:
  644. ECase(SHT_HEX_ORDERED);
  645. break;
  646. case ELF::EM_X86_64:
  647. ECase(SHT_X86_64_UNWIND);
  648. break;
  649. case ELF::EM_MIPS:
  650. ECase(SHT_MIPS_REGINFO);
  651. ECase(SHT_MIPS_OPTIONS);
  652. ECase(SHT_MIPS_DWARF);
  653. ECase(SHT_MIPS_ABIFLAGS);
  654. break;
  655. case ELF::EM_RISCV:
  656. ECase(SHT_RISCV_ATTRIBUTES);
  657. break;
  658. case ELF::EM_MSP430:
  659. ECase(SHT_MSP430_ATTRIBUTES);
  660. break;
  661. default:
  662. // Nothing to do.
  663. break;
  664. }
  665. #undef ECase
  666. IO.enumFallback<Hex32>(Value);
  667. }
  668. void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO,
  669. ELFYAML::ELF_PF &Value) {
  670. #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
  671. BCase(PF_X);
  672. BCase(PF_W);
  673. BCase(PF_R);
  674. }
  675. void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
  676. ELFYAML::ELF_SHF &Value) {
  677. const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
  678. #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
  679. BCase(SHF_WRITE);
  680. BCase(SHF_ALLOC);
  681. BCase(SHF_EXCLUDE);
  682. BCase(SHF_EXECINSTR);
  683. BCase(SHF_MERGE);
  684. BCase(SHF_STRINGS);
  685. BCase(SHF_INFO_LINK);
  686. BCase(SHF_LINK_ORDER);
  687. BCase(SHF_OS_NONCONFORMING);
  688. BCase(SHF_GROUP);
  689. BCase(SHF_TLS);
  690. BCase(SHF_COMPRESSED);
  691. BCase(SHF_GNU_RETAIN);
  692. switch (Object->getMachine()) {
  693. case ELF::EM_ARM:
  694. BCase(SHF_ARM_PURECODE);
  695. break;
  696. case ELF::EM_HEXAGON:
  697. BCase(SHF_HEX_GPREL);
  698. break;
  699. case ELF::EM_MIPS:
  700. BCase(SHF_MIPS_NODUPES);
  701. BCase(SHF_MIPS_NAMES);
  702. BCase(SHF_MIPS_LOCAL);
  703. BCase(SHF_MIPS_NOSTRIP);
  704. BCase(SHF_MIPS_GPREL);
  705. BCase(SHF_MIPS_MERGE);
  706. BCase(SHF_MIPS_ADDR);
  707. BCase(SHF_MIPS_STRING);
  708. break;
  709. case ELF::EM_X86_64:
  710. BCase(SHF_X86_64_LARGE);
  711. break;
  712. default:
  713. // Nothing to do.
  714. break;
  715. }
  716. #undef BCase
  717. }
  718. void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration(
  719. IO &IO, ELFYAML::ELF_SHN &Value) {
  720. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  721. ECase(SHN_UNDEF);
  722. ECase(SHN_LORESERVE);
  723. ECase(SHN_LOPROC);
  724. ECase(SHN_HIPROC);
  725. ECase(SHN_LOOS);
  726. ECase(SHN_HIOS);
  727. ECase(SHN_ABS);
  728. ECase(SHN_COMMON);
  729. ECase(SHN_XINDEX);
  730. ECase(SHN_HIRESERVE);
  731. ECase(SHN_AMDGPU_LDS);
  732. ECase(SHN_HEXAGON_SCOMMON);
  733. ECase(SHN_HEXAGON_SCOMMON_1);
  734. ECase(SHN_HEXAGON_SCOMMON_2);
  735. ECase(SHN_HEXAGON_SCOMMON_4);
  736. ECase(SHN_HEXAGON_SCOMMON_8);
  737. #undef ECase
  738. IO.enumFallback<Hex16>(Value);
  739. }
  740. void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration(
  741. IO &IO, ELFYAML::ELF_STB &Value) {
  742. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  743. ECase(STB_LOCAL);
  744. ECase(STB_GLOBAL);
  745. ECase(STB_WEAK);
  746. ECase(STB_GNU_UNIQUE);
  747. #undef ECase
  748. IO.enumFallback<Hex8>(Value);
  749. }
  750. void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
  751. IO &IO, ELFYAML::ELF_STT &Value) {
  752. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  753. ECase(STT_NOTYPE);
  754. ECase(STT_OBJECT);
  755. ECase(STT_FUNC);
  756. ECase(STT_SECTION);
  757. ECase(STT_FILE);
  758. ECase(STT_COMMON);
  759. ECase(STT_TLS);
  760. ECase(STT_GNU_IFUNC);
  761. #undef ECase
  762. IO.enumFallback<Hex8>(Value);
  763. }
  764. void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
  765. IO &IO, ELFYAML::ELF_RSS &Value) {
  766. #define ECase(X) IO.enumCase(Value, #X, ELF::X)
  767. ECase(RSS_UNDEF);
  768. ECase(RSS_GP);
  769. ECase(RSS_GP0);
  770. ECase(RSS_LOC);
  771. #undef ECase
  772. }
  773. void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
  774. IO &IO, ELFYAML::ELF_REL &Value) {
  775. const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
  776. assert(Object && "The IO context is not initialized");
  777. #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
  778. switch (Object->getMachine()) {
  779. case ELF::EM_X86_64:
  780. #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
  781. break;
  782. case ELF::EM_MIPS:
  783. #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
  784. break;
  785. case ELF::EM_HEXAGON:
  786. #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
  787. break;
  788. case ELF::EM_386:
  789. case ELF::EM_IAMCU:
  790. #include "llvm/BinaryFormat/ELFRelocs/i386.def"
  791. break;
  792. case ELF::EM_AARCH64:
  793. #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
  794. break;
  795. case ELF::EM_ARM:
  796. #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
  797. break;
  798. case ELF::EM_ARC:
  799. #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
  800. break;
  801. case ELF::EM_RISCV:
  802. #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
  803. break;
  804. case ELF::EM_LANAI:
  805. #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
  806. break;
  807. case ELF::EM_AMDGPU:
  808. #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
  809. break;
  810. case ELF::EM_BPF:
  811. #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
  812. break;
  813. case ELF::EM_VE:
  814. #include "llvm/BinaryFormat/ELFRelocs/VE.def"
  815. break;
  816. case ELF::EM_CSKY:
  817. #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
  818. break;
  819. case ELF::EM_PPC:
  820. #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
  821. break;
  822. case ELF::EM_PPC64:
  823. #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
  824. break;
  825. case ELF::EM_68K:
  826. #include "llvm/BinaryFormat/ELFRelocs/M68k.def"
  827. break;
  828. default:
  829. // Nothing to do.
  830. break;
  831. }
  832. #undef ELF_RELOC
  833. IO.enumFallback<Hex32>(Value);
  834. }
  835. void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration(
  836. IO &IO, ELFYAML::ELF_DYNTAG &Value) {
  837. const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
  838. assert(Object && "The IO context is not initialized");
  839. // Disable architecture specific tags by default. We might enable them below.
  840. #define AARCH64_DYNAMIC_TAG(name, value)
  841. #define MIPS_DYNAMIC_TAG(name, value)
  842. #define HEXAGON_DYNAMIC_TAG(name, value)
  843. #define PPC_DYNAMIC_TAG(name, value)
  844. #define PPC64_DYNAMIC_TAG(name, value)
  845. // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
  846. #define DYNAMIC_TAG_MARKER(name, value)
  847. #define STRINGIFY(X) (#X)
  848. #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
  849. switch (Object->getMachine()) {
  850. case ELF::EM_AARCH64:
  851. #undef AARCH64_DYNAMIC_TAG
  852. #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
  853. #include "llvm/BinaryFormat/DynamicTags.def"
  854. #undef AARCH64_DYNAMIC_TAG
  855. #define AARCH64_DYNAMIC_TAG(name, value)
  856. break;
  857. case ELF::EM_MIPS:
  858. #undef MIPS_DYNAMIC_TAG
  859. #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
  860. #include "llvm/BinaryFormat/DynamicTags.def"
  861. #undef MIPS_DYNAMIC_TAG
  862. #define MIPS_DYNAMIC_TAG(name, value)
  863. break;
  864. case ELF::EM_HEXAGON:
  865. #undef HEXAGON_DYNAMIC_TAG
  866. #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
  867. #include "llvm/BinaryFormat/DynamicTags.def"
  868. #undef HEXAGON_DYNAMIC_TAG
  869. #define HEXAGON_DYNAMIC_TAG(name, value)
  870. break;
  871. case ELF::EM_PPC:
  872. #undef PPC_DYNAMIC_TAG
  873. #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
  874. #include "llvm/BinaryFormat/DynamicTags.def"
  875. #undef PPC_DYNAMIC_TAG
  876. #define PPC_DYNAMIC_TAG(name, value)
  877. break;
  878. case ELF::EM_PPC64:
  879. #undef PPC64_DYNAMIC_TAG
  880. #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
  881. #include "llvm/BinaryFormat/DynamicTags.def"
  882. #undef PPC64_DYNAMIC_TAG
  883. #define PPC64_DYNAMIC_TAG(name, value)
  884. break;
  885. case ELF::EM_RISCV:
  886. #undef RISCV_DYNAMIC_TAG
  887. #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
  888. #include "llvm/BinaryFormat/DynamicTags.def"
  889. #undef RISCV_DYNAMIC_TAG
  890. #define RISCV_DYNAMIC_TAG(name, value)
  891. break;
  892. default:
  893. #include "llvm/BinaryFormat/DynamicTags.def"
  894. break;
  895. }
  896. #undef AARCH64_DYNAMIC_TAG
  897. #undef MIPS_DYNAMIC_TAG
  898. #undef HEXAGON_DYNAMIC_TAG
  899. #undef PPC_DYNAMIC_TAG
  900. #undef PPC64_DYNAMIC_TAG
  901. #undef DYNAMIC_TAG_MARKER
  902. #undef STRINGIFY
  903. #undef DYNAMIC_TAG
  904. IO.enumFallback<Hex64>(Value);
  905. }
  906. void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
  907. IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
  908. #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
  909. ECase(REG_NONE);
  910. ECase(REG_32);
  911. ECase(REG_64);
  912. ECase(REG_128);
  913. #undef ECase
  914. }
  915. void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
  916. IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
  917. #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
  918. ECase(FP_ANY);
  919. ECase(FP_DOUBLE);
  920. ECase(FP_SINGLE);
  921. ECase(FP_SOFT);
  922. ECase(FP_OLD_64);
  923. ECase(FP_XX);
  924. ECase(FP_64);
  925. ECase(FP_64A);
  926. #undef ECase
  927. }
  928. void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
  929. IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
  930. #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
  931. ECase(EXT_NONE);
  932. ECase(EXT_XLR);
  933. ECase(EXT_OCTEON2);
  934. ECase(EXT_OCTEONP);
  935. ECase(EXT_LOONGSON_3A);
  936. ECase(EXT_OCTEON);
  937. ECase(EXT_5900);
  938. ECase(EXT_4650);
  939. ECase(EXT_4010);
  940. ECase(EXT_4100);
  941. ECase(EXT_3900);
  942. ECase(EXT_10000);
  943. ECase(EXT_SB1);
  944. ECase(EXT_4111);
  945. ECase(EXT_4120);
  946. ECase(EXT_5400);
  947. ECase(EXT_5500);
  948. ECase(EXT_LOONGSON_2E);
  949. ECase(EXT_LOONGSON_2F);
  950. ECase(EXT_OCTEON3);
  951. #undef ECase
  952. }
  953. void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
  954. IO &IO, ELFYAML::MIPS_ISA &Value) {
  955. IO.enumCase(Value, "MIPS1", 1);
  956. IO.enumCase(Value, "MIPS2", 2);
  957. IO.enumCase(Value, "MIPS3", 3);
  958. IO.enumCase(Value, "MIPS4", 4);
  959. IO.enumCase(Value, "MIPS5", 5);
  960. IO.enumCase(Value, "MIPS32", 32);
  961. IO.enumCase(Value, "MIPS64", 64);
  962. IO.enumFallback<Hex32>(Value);
  963. }
  964. void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
  965. IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
  966. #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
  967. BCase(DSP);
  968. BCase(DSPR2);
  969. BCase(EVA);
  970. BCase(MCU);
  971. BCase(MDMX);
  972. BCase(MIPS3D);
  973. BCase(MT);
  974. BCase(SMARTMIPS);
  975. BCase(VIRT);
  976. BCase(MSA);
  977. BCase(MIPS16);
  978. BCase(MICROMIPS);
  979. BCase(XPA);
  980. BCase(CRC);
  981. BCase(GINV);
  982. #undef BCase
  983. }
  984. void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
  985. IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
  986. #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
  987. BCase(ODDSPREG);
  988. #undef BCase
  989. }
  990. void MappingTraits<ELFYAML::SectionHeader>::mapping(
  991. IO &IO, ELFYAML::SectionHeader &SHdr) {
  992. IO.mapRequired("Name", SHdr.Name);
  993. }
  994. void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
  995. ELFYAML::FileHeader &FileHdr) {
  996. IO.mapRequired("Class", FileHdr.Class);
  997. IO.mapRequired("Data", FileHdr.Data);
  998. IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
  999. IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
  1000. IO.mapRequired("Type", FileHdr.Type);
  1001. IO.mapOptional("Machine", FileHdr.Machine);
  1002. IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
  1003. IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
  1004. IO.mapOptional("SectionHeaderStringTable", FileHdr.SectionHeaderStringTable);
  1005. // obj2yaml does not dump these fields.
  1006. assert(!IO.outputting() ||
  1007. (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum));
  1008. IO.mapOptional("EPhOff", FileHdr.EPhOff);
  1009. IO.mapOptional("EPhEntSize", FileHdr.EPhEntSize);
  1010. IO.mapOptional("EPhNum", FileHdr.EPhNum);
  1011. IO.mapOptional("EShEntSize", FileHdr.EShEntSize);
  1012. IO.mapOptional("EShOff", FileHdr.EShOff);
  1013. IO.mapOptional("EShNum", FileHdr.EShNum);
  1014. IO.mapOptional("EShStrNdx", FileHdr.EShStrNdx);
  1015. }
  1016. void MappingTraits<ELFYAML::ProgramHeader>::mapping(
  1017. IO &IO, ELFYAML::ProgramHeader &Phdr) {
  1018. IO.mapRequired("Type", Phdr.Type);
  1019. IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
  1020. IO.mapOptional("FirstSec", Phdr.FirstSec);
  1021. IO.mapOptional("LastSec", Phdr.LastSec);
  1022. IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
  1023. IO.mapOptional("PAddr", Phdr.PAddr, Phdr.VAddr);
  1024. IO.mapOptional("Align", Phdr.Align);
  1025. IO.mapOptional("FileSize", Phdr.FileSize);
  1026. IO.mapOptional("MemSize", Phdr.MemSize);
  1027. IO.mapOptional("Offset", Phdr.Offset);
  1028. }
  1029. std::string MappingTraits<ELFYAML::ProgramHeader>::validate(
  1030. IO &IO, ELFYAML::ProgramHeader &FileHdr) {
  1031. if (!FileHdr.FirstSec && FileHdr.LastSec)
  1032. return "the \"LastSec\" key can't be used without the \"FirstSec\" key";
  1033. if (FileHdr.FirstSec && !FileHdr.LastSec)
  1034. return "the \"FirstSec\" key can't be used without the \"LastSec\" key";
  1035. return "";
  1036. }
  1037. LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece)
  1038. template <> struct ScalarTraits<StOtherPiece> {
  1039. static void output(const StOtherPiece &Val, void *, raw_ostream &Out) {
  1040. Out << Val;
  1041. }
  1042. static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) {
  1043. Val = Scalar;
  1044. return {};
  1045. }
  1046. static QuotingType mustQuote(StringRef) { return QuotingType::None; }
  1047. };
  1048. template <> struct SequenceElementTraits<StOtherPiece> {
  1049. static const bool flow = true;
  1050. };
  1051. template <> struct ScalarTraits<ELFYAML::YAMLFlowString> {
  1052. static void output(const ELFYAML::YAMLFlowString &Val, void *,
  1053. raw_ostream &Out) {
  1054. Out << Val;
  1055. }
  1056. static StringRef input(StringRef Scalar, void *,
  1057. ELFYAML::YAMLFlowString &Val) {
  1058. Val = Scalar;
  1059. return {};
  1060. }
  1061. static QuotingType mustQuote(StringRef S) {
  1062. return ScalarTraits<StringRef>::mustQuote(S);
  1063. }
  1064. };
  1065. template <> struct SequenceElementTraits<ELFYAML::YAMLFlowString> {
  1066. static const bool flow = true;
  1067. };
  1068. namespace {
  1069. struct NormalizedOther {
  1070. NormalizedOther(IO &IO) : YamlIO(IO) {}
  1071. NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) {
  1072. assert(Original && "This constructor is only used for outputting YAML and "
  1073. "assumes a non-empty Original");
  1074. std::vector<StOtherPiece> Ret;
  1075. const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
  1076. for (std::pair<StringRef, uint8_t> &P :
  1077. getFlags(Object->getMachine()).takeVector()) {
  1078. uint8_t FlagValue = P.second;
  1079. if ((*Original & FlagValue) != FlagValue)
  1080. continue;
  1081. *Original &= ~FlagValue;
  1082. Ret.push_back({P.first});
  1083. }
  1084. if (*Original != 0) {
  1085. UnknownFlagsHolder = std::to_string(*Original);
  1086. Ret.push_back({UnknownFlagsHolder});
  1087. }
  1088. if (!Ret.empty())
  1089. Other = std::move(Ret);
  1090. }
  1091. uint8_t toValue(StringRef Name) {
  1092. const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
  1093. MapVector<StringRef, uint8_t> Flags = getFlags(Object->getMachine());
  1094. auto It = Flags.find(Name);
  1095. if (It != Flags.end())
  1096. return It->second;
  1097. uint8_t Val;
  1098. if (to_integer(Name, Val))
  1099. return Val;
  1100. YamlIO.setError("an unknown value is used for symbol's 'Other' field: " +
  1101. Name);
  1102. return 0;
  1103. }
  1104. Optional<uint8_t> denormalize(IO &) {
  1105. if (!Other)
  1106. return None;
  1107. uint8_t Ret = 0;
  1108. for (StOtherPiece &Val : *Other)
  1109. Ret |= toValue(Val);
  1110. return Ret;
  1111. }
  1112. // st_other field is used to encode symbol visibility and platform-dependent
  1113. // flags and values. This method returns a name to value map that is used for
  1114. // parsing and encoding this field.
  1115. MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) {
  1116. MapVector<StringRef, uint8_t> Map;
  1117. // STV_* values are just enumeration values. We add them in a reversed order
  1118. // because when we convert the st_other to named constants when printing
  1119. // YAML we want to use a maximum number of bits on each step:
  1120. // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but
  1121. // not as STV_HIDDEN (2) + STV_INTERNAL (1).
  1122. Map["STV_PROTECTED"] = ELF::STV_PROTECTED;
  1123. Map["STV_HIDDEN"] = ELF::STV_HIDDEN;
  1124. Map["STV_INTERNAL"] = ELF::STV_INTERNAL;
  1125. // STV_DEFAULT is used to represent the default visibility and has a value
  1126. // 0. We want to be able to read it from YAML documents, but there is no
  1127. // reason to print it.
  1128. if (!YamlIO.outputting())
  1129. Map["STV_DEFAULT"] = ELF::STV_DEFAULT;
  1130. // MIPS is not consistent. All of the STO_MIPS_* values are bit flags,
  1131. // except STO_MIPS_MIPS16 which overlaps them. It should be checked and
  1132. // consumed first when we print the output, because we do not want to print
  1133. // any other flags that have the same bits instead.
  1134. if (EMachine == ELF::EM_MIPS) {
  1135. Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16;
  1136. Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS;
  1137. Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC;
  1138. Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT;
  1139. Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL;
  1140. }
  1141. if (EMachine == ELF::EM_AARCH64)
  1142. Map["STO_AARCH64_VARIANT_PCS"] = ELF::STO_AARCH64_VARIANT_PCS;
  1143. if (EMachine == ELF::EM_RISCV)
  1144. Map["STO_RISCV_VARIANT_CC"] = ELF::STO_RISCV_VARIANT_CC;
  1145. return Map;
  1146. }
  1147. IO &YamlIO;
  1148. Optional<std::vector<StOtherPiece>> Other;
  1149. std::string UnknownFlagsHolder;
  1150. };
  1151. } // end anonymous namespace
  1152. void ScalarTraits<ELFYAML::YAMLIntUInt>::output(const ELFYAML::YAMLIntUInt &Val,
  1153. void *Ctx, raw_ostream &Out) {
  1154. Out << Val;
  1155. }
  1156. StringRef ScalarTraits<ELFYAML::YAMLIntUInt>::input(StringRef Scalar, void *Ctx,
  1157. ELFYAML::YAMLIntUInt &Val) {
  1158. const bool Is64 = static_cast<ELFYAML::Object *>(Ctx)->Header.Class ==
  1159. ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
  1160. StringRef ErrMsg = "invalid number";
  1161. // We do not accept negative hex numbers because their meaning is ambiguous.
  1162. // For example, would -0xfffffffff mean 1 or INT32_MIN?
  1163. if (Scalar.empty() || Scalar.startswith("-0x"))
  1164. return ErrMsg;
  1165. if (Scalar.startswith("-")) {
  1166. const int64_t MinVal = Is64 ? INT64_MIN : INT32_MIN;
  1167. long long Int;
  1168. if (getAsSignedInteger(Scalar, /*Radix=*/0, Int) || (Int < MinVal))
  1169. return ErrMsg;
  1170. Val = Int;
  1171. return "";
  1172. }
  1173. const uint64_t MaxVal = Is64 ? UINT64_MAX : UINT32_MAX;
  1174. unsigned long long UInt;
  1175. if (getAsUnsignedInteger(Scalar, /*Radix=*/0, UInt) || (UInt > MaxVal))
  1176. return ErrMsg;
  1177. Val = UInt;
  1178. return "";
  1179. }
  1180. void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
  1181. IO.mapOptional("Name", Symbol.Name, StringRef());
  1182. IO.mapOptional("StName", Symbol.StName);
  1183. IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
  1184. IO.mapOptional("Section", Symbol.Section);
  1185. IO.mapOptional("Index", Symbol.Index);
  1186. IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
  1187. IO.mapOptional("Value", Symbol.Value);
  1188. IO.mapOptional("Size", Symbol.Size);
  1189. // Symbol's Other field is a bit special. It is usually a field that
  1190. // represents st_other and holds the symbol visibility. However, on some
  1191. // platforms, it can contain bit fields and regular values, or even sometimes a
  1192. // crazy mix of them (see comments for NormalizedOther). Because of this, we
  1193. // need special handling.
  1194. MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO,
  1195. Symbol.Other);
  1196. IO.mapOptional("Other", Keys->Other);
  1197. }
  1198. std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
  1199. ELFYAML::Symbol &Symbol) {
  1200. if (Symbol.Index && Symbol.Section)
  1201. return "Index and Section cannot both be specified for Symbol";
  1202. return "";
  1203. }
  1204. static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
  1205. IO.mapOptional("Name", Section.Name, StringRef());
  1206. IO.mapRequired("Type", Section.Type);
  1207. IO.mapOptional("Flags", Section.Flags);
  1208. IO.mapOptional("Address", Section.Address);
  1209. IO.mapOptional("Link", Section.Link);
  1210. IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
  1211. IO.mapOptional("EntSize", Section.EntSize);
  1212. IO.mapOptional("Offset", Section.Offset);
  1213. IO.mapOptional("Content", Section.Content);
  1214. IO.mapOptional("Size", Section.Size);
  1215. // obj2yaml does not dump these fields. They are expected to be empty when we
  1216. // are producing YAML, because yaml2obj sets appropriate values for them
  1217. // automatically when they are not explicitly defined.
  1218. assert(!IO.outputting() ||
  1219. (!Section.ShOffset && !Section.ShSize && !Section.ShName &&
  1220. !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign));
  1221. IO.mapOptional("ShAddrAlign", Section.ShAddrAlign);
  1222. IO.mapOptional("ShName", Section.ShName);
  1223. IO.mapOptional("ShOffset", Section.ShOffset);
  1224. IO.mapOptional("ShSize", Section.ShSize);
  1225. IO.mapOptional("ShFlags", Section.ShFlags);
  1226. IO.mapOptional("ShType", Section.ShType);
  1227. }
  1228. static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
  1229. commonSectionMapping(IO, Section);
  1230. IO.mapOptional("Entries", Section.Entries);
  1231. }
  1232. static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
  1233. commonSectionMapping(IO, Section);
  1234. // We also support reading a content as array of bytes using the ContentArray
  1235. // key. obj2yaml never prints this field.
  1236. assert(!IO.outputting() || !Section.ContentBuf.hasValue());
  1237. IO.mapOptional("ContentArray", Section.ContentBuf);
  1238. if (Section.ContentBuf) {
  1239. if (Section.Content)
  1240. IO.setError("Content and ContentArray can't be used together");
  1241. Section.Content = yaml::BinaryRef(*Section.ContentBuf);
  1242. }
  1243. IO.mapOptional("Info", Section.Info);
  1244. }
  1245. static void sectionMapping(IO &IO, ELFYAML::BBAddrMapSection &Section) {
  1246. commonSectionMapping(IO, Section);
  1247. IO.mapOptional("Content", Section.Content);
  1248. IO.mapOptional("Entries", Section.Entries);
  1249. }
  1250. static void sectionMapping(IO &IO, ELFYAML::StackSizesSection &Section) {
  1251. commonSectionMapping(IO, Section);
  1252. IO.mapOptional("Entries", Section.Entries);
  1253. }
  1254. static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) {
  1255. commonSectionMapping(IO, Section);
  1256. IO.mapOptional("Bucket", Section.Bucket);
  1257. IO.mapOptional("Chain", Section.Chain);
  1258. // obj2yaml does not dump these fields. They can be used to override nchain
  1259. // and nbucket values for creating broken sections.
  1260. assert(!IO.outputting() ||
  1261. (!Section.NBucket.hasValue() && !Section.NChain.hasValue()));
  1262. IO.mapOptional("NChain", Section.NChain);
  1263. IO.mapOptional("NBucket", Section.NBucket);
  1264. }
  1265. static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) {
  1266. commonSectionMapping(IO, Section);
  1267. IO.mapOptional("Notes", Section.Notes);
  1268. }
  1269. static void sectionMapping(IO &IO, ELFYAML::GnuHashSection &Section) {
  1270. commonSectionMapping(IO, Section);
  1271. IO.mapOptional("Header", Section.Header);
  1272. IO.mapOptional("BloomFilter", Section.BloomFilter);
  1273. IO.mapOptional("HashBuckets", Section.HashBuckets);
  1274. IO.mapOptional("HashValues", Section.HashValues);
  1275. }
  1276. static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
  1277. commonSectionMapping(IO, Section);
  1278. }
  1279. static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) {
  1280. commonSectionMapping(IO, Section);
  1281. IO.mapOptional("Info", Section.Info);
  1282. IO.mapOptional("Entries", Section.Entries);
  1283. }
  1284. static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) {
  1285. commonSectionMapping(IO, Section);
  1286. IO.mapOptional("Entries", Section.Entries);
  1287. }
  1288. static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) {
  1289. commonSectionMapping(IO, Section);
  1290. IO.mapOptional("Info", Section.Info);
  1291. IO.mapOptional("Dependencies", Section.VerneedV);
  1292. }
  1293. static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
  1294. commonSectionMapping(IO, Section);
  1295. IO.mapOptional("Info", Section.RelocatableSec, StringRef());
  1296. IO.mapOptional("Relocations", Section.Relocations);
  1297. }
  1298. static void sectionMapping(IO &IO, ELFYAML::RelrSection &Section) {
  1299. commonSectionMapping(IO, Section);
  1300. IO.mapOptional("Entries", Section.Entries);
  1301. }
  1302. static void groupSectionMapping(IO &IO, ELFYAML::GroupSection &Group) {
  1303. commonSectionMapping(IO, Group);
  1304. IO.mapOptional("Info", Group.Signature);
  1305. IO.mapOptional("Members", Group.Members);
  1306. }
  1307. static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) {
  1308. commonSectionMapping(IO, Section);
  1309. IO.mapOptional("Entries", Section.Entries);
  1310. }
  1311. static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) {
  1312. commonSectionMapping(IO, Section);
  1313. IO.mapOptional("Symbols", Section.Symbols);
  1314. }
  1315. static void fillMapping(IO &IO, ELFYAML::Fill &Fill) {
  1316. IO.mapOptional("Name", Fill.Name, StringRef());
  1317. IO.mapOptional("Pattern", Fill.Pattern);
  1318. IO.mapOptional("Offset", Fill.Offset);
  1319. IO.mapRequired("Size", Fill.Size);
  1320. }
  1321. static void sectionHeaderTableMapping(IO &IO,
  1322. ELFYAML::SectionHeaderTable &SHT) {
  1323. IO.mapOptional("Offset", SHT.Offset);
  1324. IO.mapOptional("Sections", SHT.Sections);
  1325. IO.mapOptional("Excluded", SHT.Excluded);
  1326. IO.mapOptional("NoHeaders", SHT.NoHeaders);
  1327. }
  1328. static void sectionMapping(IO &IO, ELFYAML::LinkerOptionsSection &Section) {
  1329. commonSectionMapping(IO, Section);
  1330. IO.mapOptional("Options", Section.Options);
  1331. }
  1332. static void sectionMapping(IO &IO,
  1333. ELFYAML::DependentLibrariesSection &Section) {
  1334. commonSectionMapping(IO, Section);
  1335. IO.mapOptional("Libraries", Section.Libs);
  1336. }
  1337. static void sectionMapping(IO &IO, ELFYAML::CallGraphProfileSection &Section) {
  1338. commonSectionMapping(IO, Section);
  1339. IO.mapOptional("Entries", Section.Entries);
  1340. }
  1341. void MappingTraits<ELFYAML::SectionOrType>::mapping(
  1342. IO &IO, ELFYAML::SectionOrType &sectionOrType) {
  1343. IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
  1344. }
  1345. static void sectionMapping(IO &IO, ELFYAML::ARMIndexTableSection &Section) {
  1346. commonSectionMapping(IO, Section);
  1347. IO.mapOptional("Entries", Section.Entries);
  1348. }
  1349. static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
  1350. commonSectionMapping(IO, Section);
  1351. IO.mapOptional("Version", Section.Version, Hex16(0));
  1352. IO.mapRequired("ISA", Section.ISALevel);
  1353. IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
  1354. IO.mapOptional("ISAExtension", Section.ISAExtension,
  1355. ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
  1356. IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
  1357. IO.mapOptional("FpABI", Section.FpABI,
  1358. ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
  1359. IO.mapOptional("GPRSize", Section.GPRSize,
  1360. ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
  1361. IO.mapOptional("CPR1Size", Section.CPR1Size,
  1362. ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
  1363. IO.mapOptional("CPR2Size", Section.CPR2Size,
  1364. ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
  1365. IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
  1366. IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
  1367. }
  1368. static StringRef getStringValue(IO &IO, const char *Key) {
  1369. StringRef Val;
  1370. IO.mapRequired(Key, Val);
  1371. return Val;
  1372. }
  1373. static void setStringValue(IO &IO, const char *Key, StringRef Val) {
  1374. IO.mapRequired(Key, Val);
  1375. }
  1376. static bool isInteger(StringRef Val) {
  1377. APInt Tmp;
  1378. return !Val.getAsInteger(0, Tmp);
  1379. }
  1380. void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
  1381. IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) {
  1382. ELFYAML::ELF_SHT Type;
  1383. StringRef TypeStr;
  1384. if (IO.outputting()) {
  1385. if (auto *S = dyn_cast<ELFYAML::Section>(Section.get()))
  1386. Type = S->Type;
  1387. else if (auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(Section.get()))
  1388. TypeStr = SHT->TypeStr;
  1389. } else {
  1390. // When the Type string does not have a "SHT_" prefix, we know it is not a
  1391. // description of a regular ELF output section.
  1392. TypeStr = getStringValue(IO, "Type");
  1393. if (TypeStr.startswith("SHT_") || isInteger(TypeStr))
  1394. IO.mapRequired("Type", Type);
  1395. }
  1396. if (TypeStr == "Fill") {
  1397. assert(!IO.outputting()); // We don't dump fills currently.
  1398. Section.reset(new ELFYAML::Fill());
  1399. fillMapping(IO, *cast<ELFYAML::Fill>(Section.get()));
  1400. return;
  1401. }
  1402. if (TypeStr == ELFYAML::SectionHeaderTable::TypeStr) {
  1403. if (IO.outputting())
  1404. setStringValue(IO, "Type", TypeStr);
  1405. else
  1406. Section.reset(new ELFYAML::SectionHeaderTable(/*IsImplicit=*/false));
  1407. sectionHeaderTableMapping(
  1408. IO, *cast<ELFYAML::SectionHeaderTable>(Section.get()));
  1409. return;
  1410. }
  1411. const auto &Obj = *static_cast<ELFYAML::Object *>(IO.getContext());
  1412. if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) {
  1413. if (!IO.outputting())
  1414. Section.reset(new ELFYAML::MipsABIFlags());
  1415. sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
  1416. return;
  1417. }
  1418. if (Obj.getMachine() == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX) {
  1419. if (!IO.outputting())
  1420. Section.reset(new ELFYAML::ARMIndexTableSection());
  1421. sectionMapping(IO, *cast<ELFYAML::ARMIndexTableSection>(Section.get()));
  1422. return;
  1423. }
  1424. switch (Type) {
  1425. case ELF::SHT_DYNAMIC:
  1426. if (!IO.outputting())
  1427. Section.reset(new ELFYAML::DynamicSection());
  1428. sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
  1429. break;
  1430. case ELF::SHT_REL:
  1431. case ELF::SHT_RELA:
  1432. if (!IO.outputting())
  1433. Section.reset(new ELFYAML::RelocationSection());
  1434. sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
  1435. break;
  1436. case ELF::SHT_RELR:
  1437. if (!IO.outputting())
  1438. Section.reset(new ELFYAML::RelrSection());
  1439. sectionMapping(IO, *cast<ELFYAML::RelrSection>(Section.get()));
  1440. break;
  1441. case ELF::SHT_GROUP:
  1442. if (!IO.outputting())
  1443. Section.reset(new ELFYAML::GroupSection());
  1444. groupSectionMapping(IO, *cast<ELFYAML::GroupSection>(Section.get()));
  1445. break;
  1446. case ELF::SHT_NOBITS:
  1447. if (!IO.outputting())
  1448. Section.reset(new ELFYAML::NoBitsSection());
  1449. sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
  1450. break;
  1451. case ELF::SHT_HASH:
  1452. if (!IO.outputting())
  1453. Section.reset(new ELFYAML::HashSection());
  1454. sectionMapping(IO, *cast<ELFYAML::HashSection>(Section.get()));
  1455. break;
  1456. case ELF::SHT_NOTE:
  1457. if (!IO.outputting())
  1458. Section.reset(new ELFYAML::NoteSection());
  1459. sectionMapping(IO, *cast<ELFYAML::NoteSection>(Section.get()));
  1460. break;
  1461. case ELF::SHT_GNU_HASH:
  1462. if (!IO.outputting())
  1463. Section.reset(new ELFYAML::GnuHashSection());
  1464. sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get()));
  1465. break;
  1466. case ELF::SHT_GNU_verdef:
  1467. if (!IO.outputting())
  1468. Section.reset(new ELFYAML::VerdefSection());
  1469. sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
  1470. break;
  1471. case ELF::SHT_GNU_versym:
  1472. if (!IO.outputting())
  1473. Section.reset(new ELFYAML::SymverSection());
  1474. sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
  1475. break;
  1476. case ELF::SHT_GNU_verneed:
  1477. if (!IO.outputting())
  1478. Section.reset(new ELFYAML::VerneedSection());
  1479. sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
  1480. break;
  1481. case ELF::SHT_SYMTAB_SHNDX:
  1482. if (!IO.outputting())
  1483. Section.reset(new ELFYAML::SymtabShndxSection());
  1484. sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get()));
  1485. break;
  1486. case ELF::SHT_LLVM_ADDRSIG:
  1487. if (!IO.outputting())
  1488. Section.reset(new ELFYAML::AddrsigSection());
  1489. sectionMapping(IO, *cast<ELFYAML::AddrsigSection>(Section.get()));
  1490. break;
  1491. case ELF::SHT_LLVM_LINKER_OPTIONS:
  1492. if (!IO.outputting())
  1493. Section.reset(new ELFYAML::LinkerOptionsSection());
  1494. sectionMapping(IO, *cast<ELFYAML::LinkerOptionsSection>(Section.get()));
  1495. break;
  1496. case ELF::SHT_LLVM_DEPENDENT_LIBRARIES:
  1497. if (!IO.outputting())
  1498. Section.reset(new ELFYAML::DependentLibrariesSection());
  1499. sectionMapping(IO,
  1500. *cast<ELFYAML::DependentLibrariesSection>(Section.get()));
  1501. break;
  1502. case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
  1503. if (!IO.outputting())
  1504. Section.reset(new ELFYAML::CallGraphProfileSection());
  1505. sectionMapping(IO, *cast<ELFYAML::CallGraphProfileSection>(Section.get()));
  1506. break;
  1507. case ELF::SHT_LLVM_BB_ADDR_MAP:
  1508. if (!IO.outputting())
  1509. Section.reset(new ELFYAML::BBAddrMapSection());
  1510. sectionMapping(IO, *cast<ELFYAML::BBAddrMapSection>(Section.get()));
  1511. break;
  1512. default:
  1513. if (!IO.outputting()) {
  1514. StringRef Name;
  1515. IO.mapOptional("Name", Name, StringRef());
  1516. Name = ELFYAML::dropUniqueSuffix(Name);
  1517. if (ELFYAML::StackSizesSection::nameMatches(Name))
  1518. Section = std::make_unique<ELFYAML::StackSizesSection>();
  1519. else
  1520. Section = std::make_unique<ELFYAML::RawContentSection>();
  1521. }
  1522. if (auto S = dyn_cast<ELFYAML::RawContentSection>(Section.get()))
  1523. sectionMapping(IO, *S);
  1524. else
  1525. sectionMapping(IO, *cast<ELFYAML::StackSizesSection>(Section.get()));
  1526. }
  1527. }
  1528. std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate(
  1529. IO &io, std::unique_ptr<ELFYAML::Chunk> &C) {
  1530. if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) {
  1531. if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size)
  1532. return "\"Size\" can't be 0 when \"Pattern\" is not empty";
  1533. return "";
  1534. }
  1535. if (const auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) {
  1536. if (SHT->NoHeaders && (SHT->Sections || SHT->Excluded || SHT->Offset))
  1537. return "NoHeaders can't be used together with Offset/Sections/Excluded";
  1538. return "";
  1539. }
  1540. const ELFYAML::Section &Sec = *cast<ELFYAML::Section>(C.get());
  1541. if (Sec.Size && Sec.Content &&
  1542. (uint64_t)(*Sec.Size) < Sec.Content->binary_size())
  1543. return "Section size must be greater than or equal to the content size";
  1544. auto BuildErrPrefix = [](ArrayRef<std::pair<StringRef, bool>> EntV) {
  1545. std::string Msg;
  1546. for (size_t I = 0, E = EntV.size(); I != E; ++I) {
  1547. StringRef Name = EntV[I].first;
  1548. if (I == 0) {
  1549. Msg = "\"" + Name.str() + "\"";
  1550. continue;
  1551. }
  1552. if (I != EntV.size() - 1)
  1553. Msg += ", \"" + Name.str() + "\"";
  1554. else
  1555. Msg += " and \"" + Name.str() + "\"";
  1556. }
  1557. return Msg;
  1558. };
  1559. std::vector<std::pair<StringRef, bool>> Entries = Sec.getEntries();
  1560. const size_t NumUsedEntries = llvm::count_if(
  1561. Entries, [](const std::pair<StringRef, bool> &P) { return P.second; });
  1562. if ((Sec.Size || Sec.Content) && NumUsedEntries > 0)
  1563. return BuildErrPrefix(Entries) +
  1564. " cannot be used with \"Content\" or \"Size\"";
  1565. if (NumUsedEntries > 0 && Entries.size() != NumUsedEntries)
  1566. return BuildErrPrefix(Entries) + " must be used together";
  1567. if (const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(C.get())) {
  1568. if (RawSection->Flags && RawSection->ShFlags)
  1569. return "ShFlags and Flags cannot be used together";
  1570. return "";
  1571. }
  1572. if (const auto *NB = dyn_cast<ELFYAML::NoBitsSection>(C.get())) {
  1573. if (NB->Content)
  1574. return "SHT_NOBITS section cannot have \"Content\"";
  1575. return "";
  1576. }
  1577. if (const auto *MF = dyn_cast<ELFYAML::MipsABIFlags>(C.get())) {
  1578. if (MF->Content)
  1579. return "\"Content\" key is not implemented for SHT_MIPS_ABIFLAGS "
  1580. "sections";
  1581. if (MF->Size)
  1582. return "\"Size\" key is not implemented for SHT_MIPS_ABIFLAGS sections";
  1583. return "";
  1584. }
  1585. return "";
  1586. }
  1587. namespace {
  1588. struct NormalizedMips64RelType {
  1589. NormalizedMips64RelType(IO &)
  1590. : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
  1591. Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
  1592. Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
  1593. SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
  1594. NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
  1595. : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
  1596. Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
  1597. ELFYAML::ELF_REL denormalize(IO &) {
  1598. ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
  1599. return Res;
  1600. }
  1601. ELFYAML::ELF_REL Type;
  1602. ELFYAML::ELF_REL Type2;
  1603. ELFYAML::ELF_REL Type3;
  1604. ELFYAML::ELF_RSS SpecSym;
  1605. };
  1606. } // end anonymous namespace
  1607. void MappingTraits<ELFYAML::StackSizeEntry>::mapping(
  1608. IO &IO, ELFYAML::StackSizeEntry &E) {
  1609. assert(IO.getContext() && "The IO context is not initialized");
  1610. IO.mapOptional("Address", E.Address, Hex64(0));
  1611. IO.mapRequired("Size", E.Size);
  1612. }
  1613. void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping(
  1614. IO &IO, ELFYAML::BBAddrMapEntry &E) {
  1615. assert(IO.getContext() && "The IO context is not initialized");
  1616. IO.mapOptional("Address", E.Address, Hex64(0));
  1617. IO.mapOptional("NumBlocks", E.NumBlocks);
  1618. IO.mapOptional("BBEntries", E.BBEntries);
  1619. }
  1620. void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping(
  1621. IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &E) {
  1622. assert(IO.getContext() && "The IO context is not initialized");
  1623. IO.mapRequired("AddressOffset", E.AddressOffset);
  1624. IO.mapRequired("Size", E.Size);
  1625. IO.mapRequired("Metadata", E.Metadata);
  1626. }
  1627. void MappingTraits<ELFYAML::GnuHashHeader>::mapping(IO &IO,
  1628. ELFYAML::GnuHashHeader &E) {
  1629. assert(IO.getContext() && "The IO context is not initialized");
  1630. IO.mapOptional("NBuckets", E.NBuckets);
  1631. IO.mapRequired("SymNdx", E.SymNdx);
  1632. IO.mapOptional("MaskWords", E.MaskWords);
  1633. IO.mapRequired("Shift2", E.Shift2);
  1634. }
  1635. void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO,
  1636. ELFYAML::DynamicEntry &Rel) {
  1637. assert(IO.getContext() && "The IO context is not initialized");
  1638. IO.mapRequired("Tag", Rel.Tag);
  1639. IO.mapRequired("Value", Rel.Val);
  1640. }
  1641. void MappingTraits<ELFYAML::NoteEntry>::mapping(IO &IO, ELFYAML::NoteEntry &N) {
  1642. assert(IO.getContext() && "The IO context is not initialized");
  1643. IO.mapOptional("Name", N.Name);
  1644. IO.mapOptional("Desc", N.Desc);
  1645. IO.mapRequired("Type", N.Type);
  1646. }
  1647. void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO,
  1648. ELFYAML::VerdefEntry &E) {
  1649. assert(IO.getContext() && "The IO context is not initialized");
  1650. IO.mapOptional("Version", E.Version);
  1651. IO.mapOptional("Flags", E.Flags);
  1652. IO.mapOptional("VersionNdx", E.VersionNdx);
  1653. IO.mapOptional("Hash", E.Hash);
  1654. IO.mapRequired("Names", E.VerNames);
  1655. }
  1656. void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO,
  1657. ELFYAML::VerneedEntry &E) {
  1658. assert(IO.getContext() && "The IO context is not initialized");
  1659. IO.mapRequired("Version", E.Version);
  1660. IO.mapRequired("File", E.File);
  1661. IO.mapRequired("Entries", E.AuxV);
  1662. }
  1663. void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO,
  1664. ELFYAML::VernauxEntry &E) {
  1665. assert(IO.getContext() && "The IO context is not initialized");
  1666. IO.mapRequired("Name", E.Name);
  1667. IO.mapRequired("Hash", E.Hash);
  1668. IO.mapRequired("Flags", E.Flags);
  1669. IO.mapRequired("Other", E.Other);
  1670. }
  1671. void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
  1672. ELFYAML::Relocation &Rel) {
  1673. const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
  1674. assert(Object && "The IO context is not initialized");
  1675. IO.mapOptional("Offset", Rel.Offset, (Hex64)0);
  1676. IO.mapOptional("Symbol", Rel.Symbol);
  1677. if (Object->getMachine() == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
  1678. Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
  1679. MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
  1680. IO, Rel.Type);
  1681. IO.mapRequired("Type", Key->Type);
  1682. IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
  1683. IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
  1684. IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
  1685. } else
  1686. IO.mapRequired("Type", Rel.Type);
  1687. IO.mapOptional("Addend", Rel.Addend, (ELFYAML::YAMLIntUInt)0);
  1688. }
  1689. void MappingTraits<ELFYAML::ARMIndexTableEntry>::mapping(
  1690. IO &IO, ELFYAML::ARMIndexTableEntry &E) {
  1691. assert(IO.getContext() && "The IO context is not initialized");
  1692. IO.mapRequired("Offset", E.Offset);
  1693. StringRef CantUnwind = "EXIDX_CANTUNWIND";
  1694. if (IO.outputting() && (uint32_t)E.Value == ARM::EHABI::EXIDX_CANTUNWIND)
  1695. IO.mapRequired("Value", CantUnwind);
  1696. else if (!IO.outputting() && getStringValue(IO, "Value") == CantUnwind)
  1697. E.Value = ARM::EHABI::EXIDX_CANTUNWIND;
  1698. else
  1699. IO.mapRequired("Value", E.Value);
  1700. }
  1701. void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
  1702. assert(!IO.getContext() && "The IO context is initialized already");
  1703. IO.setContext(&Object);
  1704. IO.mapTag("!ELF", true);
  1705. IO.mapRequired("FileHeader", Object.Header);
  1706. IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
  1707. IO.mapOptional("Sections", Object.Chunks);
  1708. IO.mapOptional("Symbols", Object.Symbols);
  1709. IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
  1710. IO.mapOptional("DWARF", Object.DWARF);
  1711. if (Object.DWARF) {
  1712. Object.DWARF->IsLittleEndian =
  1713. Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB);
  1714. Object.DWARF->Is64BitAddrSize =
  1715. Object.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64);
  1716. }
  1717. IO.setContext(nullptr);
  1718. }
  1719. void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO,
  1720. ELFYAML::LinkerOption &Opt) {
  1721. assert(IO.getContext() && "The IO context is not initialized");
  1722. IO.mapRequired("Name", Opt.Key);
  1723. IO.mapRequired("Value", Opt.Value);
  1724. }
  1725. void MappingTraits<ELFYAML::CallGraphEntryWeight>::mapping(
  1726. IO &IO, ELFYAML::CallGraphEntryWeight &E) {
  1727. assert(IO.getContext() && "The IO context is not initialized");
  1728. IO.mapRequired("Weight", E.Weight);
  1729. }
  1730. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
  1731. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
  1732. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
  1733. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
  1734. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
  1735. } // end namespace yaml
  1736. } // end namespace llvm