Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objcopy remove section doesn’t work with PEI files #63081

Open
abbapatris opened this issue Jun 3, 2023 · 1 comment
Open

Objcopy remove section doesn’t work with PEI files #63081

abbapatris opened this issue Jun 3, 2023 · 1 comment

Comments

@abbapatris
Copy link

abbapatris commented Jun 3, 2023

I was using the objcopy to remove a section from a PEI file and I kept getting the error:
The specified executable is not a valid application for this OS platform.At line:1 char:1

I started breaking down the difference from the original generated executable and the one with the removed section and found that the first section is starting at location offset of 0x600 and the original one has the first start at the offset of 0x1000. Which made me realize the the section alignment is on a 4k boundary (or 0x1000) so the section should start at that location. I dug in to the code and found in file CoffWriter.cpp, method void COFFWriter::writeSections(), the placement of the section is based on the RawDataOffset.

I updated the line

uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
                   S.Header.PointerToRawData;

To be

uint8_t *Ptr = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
                     alignTo(S.Header.PointerToRawData, Obj.PeHeader.SectionAlignment);

I noticed the tests only verify that windows object files remove the sections, but they don’t check the PE or PEI files. I assume the linker fixes the problem automatically for the object files.

My executable I was doing this only had a dummy section that did nothing in order to verify that the section isn’t the problem itself.

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 3, 2023

@llvm/issue-subscribers-tools-llvm-objcopy-strip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants