This is an archive of the discontinued LLVM Phabricator instance.

[llvm-readobj/llvm-readelf] - Report a proper warning when dumping a broken dynamic relocation.
ClosedPublic

Authored by grimar on Aug 26 2019, 5:08 AM.

Details

Summary

When we have a dynamic relocation with a broken symbol's st_name,
tools report a useless error: "Invalid data was encountered while parsing the file".

After this change we report a warning + "<corrupt>" as a symbol name.

Diff Detail

Repository
rL LLVM

Event Timeline

grimar created this revision.Aug 26 2019, 5:08 AM
MaskRay added inline comments.Aug 26 2019, 6:06 AM
test/tools/llvm-readobj/elf-broken-dynamic-reloc-name.test
31 ↗(On Diff #217119)

Address and Info can be deleted.

50 ↗(On Diff #217119)

Is the PT_LOAD used?

grimar updated this revision to Diff 217148.Aug 26 2019, 7:20 AM
grimar marked 4 inline comments as done.
  • Addressed review comments.
test/tools/llvm-readobj/elf-broken-dynamic-reloc-name.test
31 ↗(On Diff #217119)

Right, removed.

50 ↗(On Diff #217119)

Yeah. Otherwise here we'd have "Unable to parse DT_RELA: virtual address is not in any segment" error.

rupprecht added inline comments.Aug 26 2019, 7:21 AM
include/llvm/Object/ELFTypes.h
251–255 ↗(On Diff #217119)

nit: use createStringError w/ a format string

tools/llvm-readobj/ELFDumper.cpp
3539 ↗(On Diff #217119)

using Elf_Sym = typename ELFT::Sym at the start of this method should allow you to keep it like before (and is more idiomatic)

grimar marked an inline comment as done.Aug 26 2019, 7:34 AM
grimar added inline comments.
include/llvm/Object/ELFTypes.h
251–255 ↗(On Diff #217119)

I think this what I initially did (used createStringError) , but it did not compile for me under linux (I am using windows for work most of the time), seems it wanted me to include a ELF.h header, but I was not sure it is a good idea to do that.

I'll revisit.

MaskRay added inline comments.Aug 26 2019, 7:49 AM
include/llvm/Object/ELFTypes.h
251–255 ↗(On Diff #217119)
return createStringError(
    object_error::parse_failed,
    "st_name (0x" + utohexstr(Offset) +
        ") is past the end of the string table of size 0x" +
        utohexstr(StrTab.size()));

should work. createStringError is defined in include/llvm/Support/Error.h which is included

tools/llvm-readobj/ELFDumper.cpp
3539 ↗(On Diff #217119)

ELFT::Sym is used once. I think it is fine to keep it as is.

grimar marked 2 inline comments as done.Aug 26 2019, 7:57 AM
grimar added inline comments.
include/llvm/Object/ELFTypes.h
251–255 ↗(On Diff #217119)

Yeah, I found that tried to use createError from 'ELF.h'. But createStringError works fine here.

grimar updated this revision to Diff 217157.Aug 26 2019, 8:03 AM
grimar marked an inline comment as done.
  • Addressed review comments.
tools/llvm-readobj/ELFDumper.cpp
3539 ↗(On Diff #217119)

I also came to this. We often use using or declaring vars when have multiple usings, but here we don't, so it would be one more line which we can avoid probably.

MaskRay accepted this revision.Aug 26 2019, 10:21 PM
This revision is now accepted and ready to land.Aug 26 2019, 10:21 PM
jhenderson added inline comments.Aug 27 2019, 2:44 AM
include/llvm/Object/ELFTypes.h
253 ↗(On Diff #217157)

I think you want to replace utohextr(Offset) with PRIx32 to take advantage of createStringError's printf formatting rules:

return createStringError(object_error::parse_failed,
    "st_name (0x" PRIx32 ") is past the end of the string table"
    " of size 0x%zx", Offset, StrTab.size());
test/tools/llvm-readobj/elf-broken-dynamic-reloc-name.test
1 ↗(On Diff #217157)

if a dynamic

14 ↗(On Diff #217157)

I think the offset here is irrelevant, right? Can it be replaced with {{.+}}?

tools/llvm-readobj/ELFDumper.cpp
3537 ↗(On Diff #217157)

Maybe R -> Reloc

3547 ↗(On Diff #217157)

I'd replace "a dynamic symbol" with "the dynamic symbol"

grimar updated this revision to Diff 217362.Aug 27 2019, 4:25 AM
grimar marked 6 inline comments as done.
  • Addressed review comments.
include/llvm/Object/ELFTypes.h
253 ↗(On Diff #217157)

Ok. I am not in favour of using format strings usually, because remembering non trivial format specifiers
is hard. And I also faced wierd issues before because of wrong specifer used by mistake (or because of a variable type change).

But since both you and Jordan think it is better.. Done.

test/tools/llvm-readobj/elf-broken-dynamic-reloc-name.test
14 ↗(On Diff #217157)

Yes, done. BTW, I searched for "at offset" and noticed our tests are using {{.*}} for testing this line which is not entirely correct.

jhenderson accepted this revision.Aug 27 2019, 5:56 AM

LGTM.

test/tools/llvm-readobj/elf-broken-dynamic-reloc-name.test
14 ↗(On Diff #217157)

I think in most cases it probably doesn't matter either way, as we aren't really testing the printing of this line.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2019, 3:55 AM