[PATCH] D69997: [llvm-objdump] Print relocation addends in hexadecimal
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 12 10:09:46 PST 2019
MaskRay added inline comments.
================
Comment at: llvm/tools/llvm-objdump/ELFDump.cpp:110
+ const char *AddendFmt = "0x%" PRIx64;
+ char const *Sign = Addend < 0 ? "-" : "+";
----------------
jhenderson wrote:
> `const char` rather than `char const` is LLVM style, I believe. (Does clang-format fix this?)
You may even delete the variable and inline it as `(Addend < 0 ? '-' : '+')` (prefer a character to a string)
================
Comment at: llvm/tools/llvm-objdump/ELFDump.cpp:113
+ if (Addend < 0)
+ Addend = -Addend;
+
----------------
davidb wrote:
> jhenderson wrote:
> > davidb wrote:
> > > jhenderson wrote:
> > > > davidb wrote:
> > > > > jhenderson wrote:
> > > > > > davidb wrote:
> > > > > > > jhenderson wrote:
> > > > > > > > Won't this fail for minimum int64_t?
> > > > > > > Yes. Is there a better abs implementation or function in LLVM that works around this case?
> > > > > > I'm not aware of one, unfortunately, but do you need it?
> > > > > >
> > > > > > My reading of the code is that format(AddendFmt, Addend) combined with the PRIx64 (which uses a signed integer type) you used above should be sufficient.
> > > > > Yes, I need the absolute value for the string representation so `foo-0x4` rather than `foo+0xfffffffc`....
> > > > Right, of course, sorry.
> > > >
> > > > The answer to [[ https://stackoverflow.com/questions/17313579/is-there-a-safe-way-to-get-the-unsigned-absolute-value-of-a-signed-integer-with | this stack overflow post ]] suggests the way to do it is to cast the type to the unsigned equivalent and then negating it (rather than the other way around). I haven't tested that this works though. You should add a test case for it.
> > > Might be tricky given that objtoyaml can't seem to parse large signed integers,
> > yaml2obj can handle the following yaml fine for me (the integer is 0x8000000000000000):
> >
> > ```
> > --- !ELF
> > FileHeader:
> > Class: ELFCLASS64
> > Data: ELFDATA2LSB
> > Type: ET_REL
> > Machine: EM_X86_64
> > Sections:
> > - Name: .foo
> > Type: SHT_PROGBITS
> > - Name: .rela.foo
> > Type: SHT_RELA
> > Info: .foo
> > Relocations:
> > - Offset: 0
> > Symbol: foo
> > Type: R_X86_64_NONE
> > Addend: -9223372036854775808
> > Symbols:
> > - Name: foo
> > ```
> Yeah, strange.... maybe I had a typo in my Addend... working now.
I think std::abs is very inconvenient here.
Probably just use `Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend`
`(uint64_t)INT64_MIN` is implementation-defined before C++20 and defined since C++20. "implementation-defined" does not matter, because there is so much code that relies on similar implementation defined behaviors.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D69997/new/
https://reviews.llvm.org/D69997
More information about the llvm-commits
mailing list