This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Add support for special section indexes in symbol table greater than SHN_LORESERVE
ClosedPublic

Authored by jakehehrlich on Sep 1 2017, 12:49 PM.

Details

Summary

As is indexes above SHN_LORESERVE will not be handled correctly because they'll be treated as indexes of sections rather than special values that should just be copied. This change adds support to copy them though.

Diff Detail

Repository
rL LLVM

Event Timeline

jakehehrlich created this revision.Sep 1 2017, 12:49 PM
mcgrathr requested changes to this revision.Sep 1 2017, 12:57 PM
mcgrathr added inline comments.
tools/llvm-objcopy/Object.cpp
222 ↗(On Diff #113580)

This fails to handle SHN_XINDEX correctly. See its handling elsewhere in the code base, or read the ELF spec.

It's probably OK to just pass all other >= SHN_LORESERVE values through blindly, but I'm not sure it's the best idea.
Their meaning and appropriate handling depends on each specific value defined by a particular machine or OS.
For all the ones I'm familiar with, just passing it through is all you need to do. But I'm not entirely sanguine about assuming that uniformly.
I think it's probably better to handle each known case individually, and error out for values we don't expect.

This revision now requires changes to proceed.Sep 1 2017, 12:57 PM
jakehehrlich edited edge metadata.
jakehehrlich retitled this revision from [llvm-objcopy] Add support for section indexes greater than SHN_LORESERVE to [llvm-objcopy] Add support for special section indexes in symbol table greater than SHN_LORESERVE.
  1. Followed Roland's recomendation and throw an error on SHN_XINDEX and I only support specific values. If an unsupported value is used llvm-objcopy should now throw an error.
mcgrathr accepted this revision.Sep 2 2017, 1:04 AM

I'm not closely familiar with this code base, so it should probably still get some review from someone who has reviewed the objcopy implementation so far.
But just looking at the apparent logic in this change, it looks OK to me with the caveats noted below.

tools/llvm-objcopy/Object.cpp
200 ↗(On Diff #113615)

This seems an oddly general-sounding name for this.
Perhaps isValidReservedSectionIndex?

239 ↗(On Diff #113615)

It's >=, not >.

tools/llvm-objcopy/Object.h
119 ↗(On Diff #113615)

Since this is currently not actually the section index per se, I'd prefer not to call it that.
It's the st_shndx value, but it's not an index if it's a reserved value.
In fact, arguably it should be ElfNN_Section type, i.e. uint16_t.
And have a comment about the meaning.
When you actually support 32-bit section indices, you can have a field called SectionIndex that's 32 bits.
(You'll still need to separately represent when st_shndx is a reserved value, so as to distinguish it from when st_shndx is SHN_XINDEX and the 32-bit SHT_SYMTAB_SHNDX entry happens to be in the [SHN_LORESERVE,SHN_HIRESERVE] range.)

This revision is now accepted and ready to land.Sep 2 2017, 1:04 AM
jhenderson edited edge metadata.Sep 4 2017, 2:22 AM

In addition to the already-added SHN_ABS test, could we please have a test for at least SHN_COMMON, and possibly the hexagon values as well.

tools/llvm-objcopy/Object.h
119 ↗(On Diff #113615)

As the Symbol has a reference to the corresponding section via "DefinedIn", I don't think we need to have separate fields for a section index and the st_shndx value. Indeed, with exception of the special indexes, we don't need the section index here at all. Therefore, I think it might make more sense to have a little enum that can be converted from and to, which only supports the special values we support (plus a "normal symbol" value). It would then be the responsibility of finalize and the caller of addSymbol to map between the two values.

jakehehrlich updated this revision to Diff 113920.EditedSep 5 2017, 2:58 PM
  1. Fixed function name to be isValidReservedSectionIndex
  2. Changed message to correctly indicate that it should be >= and not >
  3. Switched to using an enum style solution. I added a method, getShndx that figures out what the correct value of st_shndx should be. I also switched how addSymbol works to account for this.
  4. Added requested tests
jhenderson added inline comments.Sep 6 2017, 2:51 AM
test/tools/llvm-objcopy/section-index-unsupported.test
2 ↗(On Diff #113920)

I think we should also check the error message here.

tools/llvm-objcopy/Object.cpp
93 ↗(On Diff #113920)

uint32_t -> uint16_t (to match st_shndx size).

123 ↗(On Diff #113920)

I think this should have an llvm_unreachable clause at the end.

tools/llvm-objcopy/Object.h
152 ↗(On Diff #113920)

SectionIndex -> Shndx. It should also be a uint16_t, since that is the st_shndx field size.

Made changes requested by James

jhenderson accepted this revision.Sep 7 2017, 1:47 AM

LGTM, with the two minor changes I've suggested inline.

test/tools/llvm-objcopy/section-index-unsupported.test
15 ↗(On Diff #114053)

I don't think you need the "[[_:.*]]". The check doesn't need to match the whole line.

tools/llvm-objcopy/Object.cpp
110 ↗(On Diff #114053)

As the if returns, I think the LLVM style is to not have the "else" here - it just becomes the rest of the function.

Fixed those two things!

This revision was automatically updated to reflect the committed changes.