[PATCH] D137089: [readobj] Fix JSON output for Relocations

Paul Kirth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 17:30:13 PST 2022


paulkirth added a comment.

Today we emit the exact, same text for relocations in both JSON and the LLVM format. The issue is that it isn't valid JSON. In the example below the Section (9) is the first item in the array. But it isnt a key, and isn't an object. To make matters worse, the newlines are hard coded, so even if you don't want pretty printing, you end up with JSON filled with newlines. If you compare the `printRelRelaReloc()` implementation for the JSONELFDumper and the LLVMELFDumper, you'll see that the JSON one is basically identical to the LLVM style when the expand relocs option is set. While that makes the output more verbose, it's also much more correct, though there are still other issues(like the newlines from the Section printer ...).

Example output:

- Before

  [{"FileSummary":{"File":"a.out","Format":"elf64-x86-64","Arch":"x86_64","AddressSize":"64bit","LoadName":"<Not found>"},"Relocations":[Section (9) .rela.dyn {
    0xA3B0 R_X86_64_RELATIVE - 0x43D0
    0xA3B8 R_X86_64_RELATIVE - 0x4A30
    0xA3C0 R_X86_64_RELATIVE - 0x4420
    0xA3C8 R_X86_64_RELATIVE - 0x1622
    0xA3D0 R_X86_64_RELATIVE - 0x16B0
    0xA3D8 R_X86_64_RELATIVE - 0x18A1
    0xA3E0 R_X86_64_RELATIVE - 0x1955
    0xB5D0 R_X86_64_RELATIVE - 0xB5D0
    0xB5D8 R_X86_64_RELATIVE - 0xB8A0
    0xB5E0 R_X86_64_RELATIVE - 0x118A0
    0xB5F0 R_X86_64_RELATIVE - 0x86C0
    0xB5F8 R_X86_64_RELATIVE - 0x7A60
    0xB600 R_X86_64_RELATIVE - 0x7CC0
    0xB608 R_X86_64_RELATIVE - 0x87F0
    0xB610 R_X86_64_RELATIVE - 0x8810
    0xB618 R_X86_64_RELATIVE - 0x8890
    0xB6D8 R_X86_64_RELATIVE - 0x4490
    0xB708 R_X86_64_RELATIVE - 0x4730
    0xB738 R_X86_64_RELATIVE - 0x4760
    0xB768 R_X86_64_RELATIVE - 0x4790
    0xB798 R_X86_64_RELATIVE - 0x47C0
    0xB7C8 R_X86_64_RELATIVE - 0x4860
    0xB7F8 R_X86_64_RELATIVE - 0x4900
    0xB828 R_X86_64_RELATIVE - 0x49C0
    0xB858 R_X86_64_RELATIVE - 0x49E0
    0xB888 R_X86_64_RELATIVE - 0x4A00
    0xA588 R_X86_64_GLOB_DAT __libc_start_main at GLIBC_2.34 0x0
    0xA590 R_X86_64_GLOB_DAT __gmon_start__ 0x0
    0xA598 R_X86_64_GLOB_DAT __register_frame_info 0x0
    0xA5A0 R_X86_64_GLOB_DAT __cxa_finalize at GLIBC_2.2.5 0x0
    0xA5A8 R_X86_64_GLOB_DAT __deregister_frame_info 0x0
    0xA5B0 R_X86_64_GLOB_DAT free at GLIBC_2.2.5 0x0
    0xA5B8 R_X86_64_GLOB_DAT stderr at GLIBC_2.2.5 0x0
  }
  Section (10) .rela.plt {
    0x118B8 R_X86_64_JUMP_SLOT __register_frame_info 0x0
    0x118C0 R_X86_64_JUMP_SLOT __cxa_finalize at GLIBC_2.2.5 0x0
    0x118C8 R_X86_64_JUMP_SLOT __deregister_frame_info 0x0
    0x118D0 R_X86_64_JUMP_SLOT strlen at GLIBC_2.2.5 0x0
    0x118D8 R_X86_64_JUMP_SLOT rand at GLIBC_2.2.5 0x0
    0x118E0 R_X86_64_JUMP_SLOT printf at GLIBC_2.2.5 0x0
    0x118E8 R_X86_64_JUMP_SLOT free at GLIBC_2.2.5 0x0
    0x118F0 R_X86_64_JUMP_SLOT calloc at GLIBC_2.2.5 0x0
    0x118F8 R_X86_64_JUMP_SLOT malloc at GLIBC_2.2.5 0x0
    0x11900 R_X86_64_JUMP_SLOT memcpy at GLIBC_2.14 0x0
    0x11908 R_X86_64_JUMP_SLOT fprintf at GLIBC_2.2.5 0x0
    0x11910 R_X86_64_JUMP_SLOT getpid at GLIBC_2.2.5 0x0
    0x11918 R_X86_64_JUMP_SLOT snprintf at GLIBC_2.2.5 0x0
    0x11920 R_X86_64_JUMP_SLOT getenv at GLIBC_2.2.5 0x0
    0x11928 R_X86_64_JUMP_SLOT strcmp at GLIBC_2.2.5 0x0
    0x11930 R_X86_64_JUMP_SLOT __strdup at GLIBC_2.2.5 0x0
    0x11938 R_X86_64_JUMP_SLOT getpagesize at GLIBC_2.2.5 0x0
    0x11940 R_X86_64_JUMP_SLOT __errno_location at GLIBC_2.2.5 0x0
    0x11948 R_X86_64_JUMP_SLOT strerror at GLIBC_2.2.5 0x0
    0x11950 R_X86_64_JUMP_SLOT fopen at GLIBC_2.2.5 0x0
    0x11958 R_X86_64_JUMP_SLOT fileno at GLIBC_2.2.5 0x0
    0x11960 R_X86_64_JUMP_SLOT ftruncate at GLIBC_2.2.5 0x0
    0x11968 R_X86_64_JUMP_SLOT munmap at GLIBC_2.2.5 0x0
    0x11970 R_X86_64_JUMP_SLOT fseek at GLIBC_2.2.5 0x0
    0x11978 R_X86_64_JUMP_SLOT strtol at GLIBC_2.2.5 0x0
    0x11980 R_X86_64_JUMP_SLOT fclose at GLIBC_2.2.5 0x0
    0x11988 R_X86_64_JUMP_SLOT fflush at GLIBC_2.2.5 0x0
    0x11990 R_X86_64_JUMP_SLOT fwrite at GLIBC_2.2.5 0x0
    0x11998 R_X86_64_JUMP_SLOT ftell at GLIBC_2.2.5 0x0
    0x119A0 R_X86_64_JUMP_SLOT mmap at GLIBC_2.2.5 0x0
    0x119A8 R_X86_64_JUMP_SLOT setenv at GLIBC_2.2.5 0x0
    0x119B0 R_X86_64_JUMP_SLOT strncpy at GLIBC_2.2.5 0x0
    0x119B8 R_X86_64_JUMP_SLOT memset at GLIBC_2.2.5 0x0
    0x119C0 R_X86_64_JUMP_SLOT mkdir at GLIBC_2.2.5 0x0
    0x119C8 R_X86_64_JUMP_SLOT uname at GLIBC_2.2.5 0x0
    0x119D0 R_X86_64_JUMP_SLOT fcntl at GLIBC_2.2.5 0x0
    0x119D8 R_X86_64_JUMP_SLOT open at GLIBC_2.2.5 0x0
    0x119E0 R_X86_64_JUMP_SLOT fdopen at GLIBC_2.2.5 0x0
    0x119E8 R_X86_64_JUMP_SLOT strchr at GLIBC_2.2.5 0x0
    0x119F0 R_X86_64_JUMP_SLOT strrchr at GLIBC_2.2.5 0x0
    0x119F8 R_X86_64_JUMP_SLOT prctl at GLIBC_2.2.5 0x0
    0x11A00 R_X86_64_JUMP_SLOT madvise at GLIBC_2.2.5 0x0
    0x11A08 R_X86_64_JUMP_SLOT __cxa_atexit at GLIBC_2.2.5 0x0
  }
  ]}]



- After

  [{"FileSummary":{"File":"a.out","Format":"elf64-x86-64","Arch":"x86_64","AddressSize":"64bit","LoadName":"<Not found>"},"Relocations":[Section (9) .rela.dyn {
  {"Relocation":{"Offset":41904,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":17360}},{"Relocation":{"Offset":41912,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18992}},{"Relocation":{"Offset":41920,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":17440}},{"Relocation":{"Offset":41928,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":5666}},{"Relocation":{"Offset":41936,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":5808}},{"Relocation":{"Offset":41944,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":6305}},{"Relocation":{"Offset":41952,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":6485}},{"Relocation":{"Offset":46544,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":46544}},{"Relocation":{"Offset":46552,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":47264}},{"Relocation":{"Offset":46560,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":71840}},{"Relocation":{"Offset":46576,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":34496}},{"Relocation":{"Offset":46584,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":31328}},{"Relocation":{"Offset":46592,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":31936}},{"Relocation":{"Offset":46600,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":34800}},{"Relocation":{"Offset":46608,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":34832}},{"Relocation":{"Offset":46616,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":34960}},{"Relocation":{"Offset":46808,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":17552}},{"Relocation":{"Offset":46856,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18224}},{"Relocation":{"Offset":46904,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18272}},{"Relocation":{"Offset":46952,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18320}},{"Relocation":{"Offset":47000,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18368}},{"Relocation":{"Offset":47048,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18528}},{"Relocation":{"Offset":47096,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18688}},{"Relocation":{"Offset":47144,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18880}},{"Relocation":{"Offset":47192,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18912}},{"Relocation":{"Offset":47240,"Type":{"Value":"R_X86_64_RELATIVE","RawValue":8},"Symbol":{"Value":"","RawValue":0},"Addend":18944}},{"Relocation":{"Offset":42376,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"__libc_start_main at GLIBC_2.34","RawValue":1},"Addend":0}},{"Relocation":{"Offset":42384,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"__gmon_start__","RawValue":2},"Addend":0}},{"Relocation":{"Offset":42392,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"__register_frame_info","RawValue":3},"Addend":0}},{"Relocation":{"Offset":42400,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"__cxa_finalize at GLIBC_2.2.5","RawValue":4},"Addend":0}},{"Relocation":{"Offset":42408,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"__deregister_frame_info","RawValue":5},"Addend":0}},{"Relocation":{"Offset":42416,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"free at GLIBC_2.2.5","RawValue":9},"Addend":0}},{"Relocation":{"Offset":42424,"Type":{"Value":"R_X86_64_GLOB_DAT","RawValue":6},"Symbol":{"Value":"stderr at GLIBC_2.2.5","RawValue":13},"Addend":0}}}
  Section (10) .rela.plt {
  ,{"Relocation":{"Offset":71864,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"__register_frame_info","RawValue":3},"Addend":0}},{"Relocation":{"Offset":71872,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"__cxa_finalize at GLIBC_2.2.5","RawValue":4},"Addend":0}},{"Relocation":{"Offset":71880,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"__deregister_frame_info","RawValue":5},"Addend":0}},{"Relocation":{"Offset":71888,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strlen at GLIBC_2.2.5","RawValue":6},"Addend":0}},{"Relocation":{"Offset":71896,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"rand at GLIBC_2.2.5","RawValue":7},"Addend":0}},{"Relocation":{"Offset":71904,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"printf at GLIBC_2.2.5","RawValue":8},"Addend":0}},{"Relocation":{"Offset":71912,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"free at GLIBC_2.2.5","RawValue":9},"Addend":0}},{"Relocation":{"Offset":71920,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"calloc at GLIBC_2.2.5","RawValue":10},"Addend":0}},{"Relocation":{"Offset":71928,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"malloc at GLIBC_2.2.5","RawValue":11},"Addend":0}},{"Relocation":{"Offset":71936,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"memcpy at GLIBC_2.14","RawValue":12},"Addend":0}},{"Relocation":{"Offset":71944,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fprintf at GLIBC_2.2.5","RawValue":14},"Addend":0}},{"Relocation":{"Offset":71952,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"getpid at GLIBC_2.2.5","RawValue":15},"Addend":0}},{"Relocation":{"Offset":71960,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"snprintf at GLIBC_2.2.5","RawValue":16},"Addend":0}},{"Relocation":{"Offset":71968,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"getenv at GLIBC_2.2.5","RawValue":17},"Addend":0}},{"Relocation":{"Offset":71976,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strcmp at GLIBC_2.2.5","RawValue":18},"Addend":0}},{"Relocation":{"Offset":71984,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"__strdup at GLIBC_2.2.5","RawValue":19},"Addend":0}},{"Relocation":{"Offset":71992,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"getpagesize at GLIBC_2.2.5","RawValue":20},"Addend":0}},{"Relocation":{"Offset":72000,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"__errno_location at GLIBC_2.2.5","RawValue":21},"Addend":0}},{"Relocation":{"Offset":72008,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strerror at GLIBC_2.2.5","RawValue":22},"Addend":0}},{"Relocation":{"Offset":72016,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fopen at GLIBC_2.2.5","RawValue":23},"Addend":0}},{"Relocation":{"Offset":72024,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fileno at GLIBC_2.2.5","RawValue":24},"Addend":0}},{"Relocation":{"Offset":72032,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"ftruncate at GLIBC_2.2.5","RawValue":25},"Addend":0}},{"Relocation":{"Offset":72040,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"munmap at GLIBC_2.2.5","RawValue":26},"Addend":0}},{"Relocation":{"Offset":72048,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fseek at GLIBC_2.2.5","RawValue":27},"Addend":0}},{"Relocation":{"Offset":72056,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strtol at GLIBC_2.2.5","RawValue":28},"Addend":0}},{"Relocation":{"Offset":72064,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fclose at GLIBC_2.2.5","RawValue":29},"Addend":0}},{"Relocation":{"Offset":72072,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fflush at GLIBC_2.2.5","RawValue":30},"Addend":0}},{"Relocation":{"Offset":72080,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fwrite at GLIBC_2.2.5","RawValue":31},"Addend":0}},{"Relocation":{"Offset":72088,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"ftell at GLIBC_2.2.5","RawValue":32},"Addend":0}},{"Relocation":{"Offset":72096,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"mmap at GLIBC_2.2.5","RawValue":33},"Addend":0}},{"Relocation":{"Offset":72104,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"setenv at GLIBC_2.2.5","RawValue":34},"Addend":0}},{"Relocation":{"Offset":72112,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strncpy at GLIBC_2.2.5","RawValue":35},"Addend":0}},{"Relocation":{"Offset":72120,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"memset at GLIBC_2.2.5","RawValue":36},"Addend":0}},{"Relocation":{"Offset":72128,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"mkdir at GLIBC_2.2.5","RawValue":37},"Addend":0}},{"Relocation":{"Offset":72136,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"uname at GLIBC_2.2.5","RawValue":38},"Addend":0}},{"Relocation":{"Offset":72144,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fcntl at GLIBC_2.2.5","RawValue":39},"Addend":0}},{"Relocation":{"Offset":72152,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"open at GLIBC_2.2.5","RawValue":40},"Addend":0}},{"Relocation":{"Offset":72160,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"fdopen at GLIBC_2.2.5","RawValue":41},"Addend":0}},{"Relocation":{"Offset":72168,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strchr at GLIBC_2.2.5","RawValue":42},"Addend":0}},{"Relocation":{"Offset":72176,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"strrchr at GLIBC_2.2.5","RawValue":43},"Addend":0}},{"Relocation":{"Offset":72184,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"prctl at GLIBC_2.2.5","RawValue":44},"Addend":0}},{"Relocation":{"Offset":72192,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"madvise at GLIBC_2.2.5","RawValue":45},"Addend":0}},{"Relocation":{"Offset":72200,"Type":{"Value":"R_X86_64_JUMP_SLOT","RawValue":7},"Symbol":{"Value":"__cxa_atexit at GLIBC_2.2.5","RawValue":46},"Addend":0}}}
  ]}]%


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137089/new/

https://reviews.llvm.org/D137089



More information about the llvm-commits mailing list