ELFYAML.cpp 63 KB

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