This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] - Do not crash on object that has relocations but no symbol table.
ClosedPublic

Authored by grimar on Oct 22 2019, 5:38 AM.

Details

Summary

It was revealed by D69260.

Tool crashed when scanned relocations in a object without a symbol table.
This patch teaches it either to handle such objects (when relocations
does not use symbols we do not need a symbol table to proceed)
or to show an appropriate error otherwise.

Diff Detail

Event Timeline

grimar created this revision.Oct 22 2019, 5:38 AM

An alternative approach is to generalize else if (EnsureSymtab) for non --add-symbol= operations as well. I need to investigate more to check whether that is feasible. If it does, it can save us some SymbolTable nullness checks later. ()I should also address another problem raised in D69093 (SectionNames is initialized after if (Obj.SectionNames != &Sec))

test/tools/llvm-objcopy/ELF/relocations-no-symtab.test
2 ↗(On Diff #226034)

that holds symbol index 0

4 ↗(On Diff #226034)

-o

An alternative approach is to generalize else if (EnsureSymtab) for non --add-symbol= operations as well.

I am not sure I understood the idea honestly. Here we have an object that has a relocation which references a symbol when symbol table is not exist.
In my understanding it is not a situation where we might want to create a symbol table, it is probably about we should report an error?

MaskRay added inline comments.Oct 24 2019, 10:37 PM
test/tools/llvm-objcopy/ELF/relocations-no-symtab.test
31 ↗(On Diff #226034)

a relocation refers to a non-zero symbol index but there is no symbol table.

grimar updated this revision to Diff 226626.Oct 28 2019, 2:59 AM
grimar marked 3 inline comments as done.
grimar edited the summary of this revision. (Show Details)
  • Addressed review comments.
Herald added a project: Restricted Project. · View Herald TranscriptOct 28 2019, 2:59 AM
jhenderson added inline comments.Oct 28 2019, 7:35 AM
llvm/test/tools/llvm-objcopy/ELF/relocations-no-symtab.test
3

I think "with a symbol index of 0" would be more natural than "that holds symbol index 0"

35

-o for consistency.

44

It probably makes slightly more sense for this to be ET_REL.

llvm/tools/llvm-objcopy/ELF/Object.cpp
908–909

This looks dodgy if no symbol exists.

1427–1428

I'm not sure this error message is clear enough. I think it is not obvious what the number in it represents. Perhaps it could be rephrased "relocation references symbol with index N, but there is no symbol table". What do you think?

MaskRay added inline comments.Oct 28 2019, 7:35 PM
llvm/test/tools/llvm-objcopy/ELF/relocations-no-symtab.test
36

%t4 -> /dev/null

llvm/tools/llvm-objcopy/ELF/Object.cpp
1427–1428

"relocation references symbol with index N, but there is no symbol table"

+1

grimar updated this revision to Diff 226864.Oct 29 2019, 3:09 AM
grimar marked 9 inline comments as done.
grimar edited the summary of this revision. (Show Details)
  • Addressed review comments.
llvm/tools/llvm-objcopy/ELF/Object.cpp
908–909

"relocation references symbol with index..." error is triggered earlier than execution flow might reach here.

It is reported from initRelocations which is called inside Reader.create() here:
https://github.com/llvm-mirror/llvm/blob/master/tools/llvm-objcopy/ELF/ELFObjcopy.cpp#L773

markSymbols() is called from updateAndRemoveSymbols which is itself caled in handleArgs:
https://github.com/llvm-mirror/llvm/blob/master/tools/llvm-objcopy/ELF/ELFObjcopy.cpp#L799
https://github.com/llvm-mirror/llvm/blob/master/tools/llvm-objcopy/ELF/ELFObjcopy.cpp#L613
https://github.com/llvm-mirror/llvm/blob/master/tools/llvm-objcopy/ELF/ELFObjcopy.cpp#L402

handleArgs is called later than Reader.create().

Also, it checks for an empty symbol table at its begining:
https://github.com/llvm-mirror/llvm/blob/master/tools/llvm-objcopy/ELF/ELFObjcopy.cpp#L350

1427–1428

Yep, that is better.

grimar planned changes to this revision.Oct 29 2019, 3:19 AM

Going to check one more case for RelocationSection::markSymbols() mentioned.

jhenderson accepted this revision.Oct 29 2019, 3:30 AM

Okay, everything else LGTM. If you don't find any more issues, feel free to commit from my point of view.

Okay, everything else LGTM. If you don't find any more issues, feel free to commit from my point of view.

I've found you actually was right. I.e. I found a different scenario to trigger a crash in markSymbols. Working on a test case, going to update the patch soon.

grimar updated this revision to Diff 226870.Oct 29 2019, 4:00 AM
grimar marked an inline comment as done.
  • Added a test case and did a code change for a one more case.
This revision is now accepted and ready to land.Oct 29 2019, 4:00 AM
grimar requested review of this revision.Oct 29 2019, 4:00 AM
grimar added inline comments.
llvm/tools/llvm-objcopy/ELF/Object.cpp
908–909

I've found this concern was valid. To trigger an issue we need an object with a symbol table and a relocation with a symbol index of 0. Then it is possible to reach markSymbol for example when --strip-unneeded is given.

I've added a test to no-symbol-relocation.test.

jhenderson accepted this revision.Oct 29 2019, 4:14 AM

LGTM. Thanks!

llvm/tools/llvm-objcopy/ELF/Object.cpp
908–909

It's probably safer this way, even if it was unreachable. But I'm glad my caution was not misplaced this time!

This revision is now accepted and ready to land.Oct 29 2019, 4:14 AM
This revision was automatically updated to reflect the committed changes.