This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Add --update-section
Needs ReviewPublic

Authored by evgeny777 on Mar 14 2019, 3:11 AM.
Tokens
"Like" token, awarded by AsafFisher.

Details

Summary

This patch adds --update-section option which is present in GNU objcopy. It's being occasionally used for hacking/binary patching tasks.

Diff Detail

Event Timeline

evgeny777 created this revision.Mar 14 2019, 3:11 AM
jhenderson added a comment.EditedMar 18 2019, 6:27 AM

I suspect that this is going to interact nastily with D59483. Does GNU objcopy support updating sections within segments?

tools/llvm-objcopy/ObjcopyOpts.td
260

Add, append to or replace the section contents? Adding doesn't make sense if the switch is called update-section...

evgeny777 marked an inline comment as done.Mar 18 2019, 6:56 AM

I suspect that this is going to interact nastily with D59843.

Sorry, can't find this patch. What's this?

Does GNU objcopy support updating sections within segments?

Yep. Here is RFC https://binutils.sourceware.narkive.com/LI0gnMHb/rfc-objcopy-add-update-section-option.

tools/llvm-objcopy/ObjcopyOpts.td
260

s/Add/Update/g

I suspect that this is going to interact nastily with D59843.

Sorry, can't find this patch. What's this?

Oops typo (now fixed). It should be D59483.

Does GNU objcopy support updating sections within segments?

Yep. Here is RFC https://binutils.sourceware.narkive.com/LI0gnMHb/rfc-objcopy-add-update-section-option.

Okay, thanks. I'll take a look at that.

rupprecht added a subscriber: pcc.
rupprecht added inline comments.
tools/llvm-objcopy/ELF/Object.cpp
781

@pcc has a similar change for this: D58426

I didn't totally grep that change either, but since you've independently come to the same conclusion, something like this is probably correct.

tools/llvm-objcopy/ELF/Object.h
322

*Sections.rbegin() is slightly simpler

Rather than updating a section in place we should extend the "replaceSectionReferences" method to work in every section type, and then swap the object out for a new object rather than updating in place.

jakehehrlich added inline comments.Mar 18 2019, 11:58 AM
tools/llvm-objcopy/ELF/Object.cpp
783

We need to be *very* careful here. Can you explain why you did this?

1489

We should not be changing segment layout. This is a huge bag of worms and this function does not correctly handle this. You basically have to re-link an executable to do this. Changing the size of a section that's within a segment should just simply be an error. I'm highly skeptical of use cases that require this.

evgeny777 marked 2 inline comments as done.Mar 19 2019, 3:07 AM
evgeny777 added inline comments.
tools/llvm-objcopy/ELF/Object.cpp
783

This patch introduces segment fixup procedure which works incorrectly without this change
Besides that this change has some sense, because:

  • Non-allocatable section can't be in segment
  • Using MemSize instead of FileSize seems correct, because some allocatable sections (like .bss) have file size of 0.
1489

One of use cases is adding some initialization to existing binary, e.g you dump text section and your own function, change start address, write section back to the module. There are other use cases as well:

https://github.com/hioa-cs/IncludeOS/blob/master/cmake/post.service.cmake
https://github.com/torvalds/linux/blob/master/arch/mips/Kconfig
https://github.com/abelromeroperez/underc0rerk/blob/master/patch-lkm.py

Also see RFC

pcc added inline comments.Mar 19 2019, 9:46 AM
tools/llvm-objcopy/ELF/Object.cpp
783

For sections such as bss, sh_offset can have an arbitrary value (because there is no file data), so this can end up returning the wrong result. To properly classify bss sections, sh_addr must be used as in D58426.

AsafFisher added a subscriber: AsafFisher.EditedMay 13 2021, 2:36 PM

What's up with this PR? What happens if the section size increases? Where do you handle the fixing of the .bss section?

What's up with this PR?

tools/llvm-objcopy/ELF/Object.cpp
783

Seems like D58426 is done, rebase!

1489

Lets make this work, GNU binutils implements it.

@AsafFisher I don't have time for this ATM. Feel free to take over.
Probably you can simplify this patch, so it doesn't touch segments

GNU’s objcopy touches segments why don’t we?