79798868

Date: 2025-10-24 14:37:54
Score: 1
Natty:
Report link

@Stas Simonov's response contains a key finding: that objcopy -O binary doesn't correctly handle multiple sections inside the object file.

However, this is not just on Windows but also on Linux. It happens even for my example but it's a bit hidden.

So, if I do

gcc -c

then the sections are in the order .data, .comment and .note.gnu.property.

If I do

gcc -r

then the sections are in the order .note.gnu.property, .data and .comment.

When .data is first, it's written but then overriden by .note.gnu.property.

When .note.gnu.property is first, it's partially overriden by data because .data is smaller. That's why I see 0102030405 when I use -r.

A possible solution to this issue is to use the -j flag so we can select the .data section i.e.

objcopy -j .data -O binary ...

This way, just the .data section is copied to the binary file.

I'm honestly not sure if this is a bug, a limitation or simply a counterintuitive intended behaviour of objcopy.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Stas
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Sterpu Mihai