gimli-rs / object

A unified interface for reading and writing object file formats
https://docs.rs/object/
Apache License 2.0
673 stars 157 forks source link

[BUG] added zeros to my symbols #721

Closed Cr0a3 closed 2 months ago

Cr0a3 commented 3 months ago

Hi, I am writting a code generation libary where i use your libary to write my data into object files. As i debugged another error and looked at my disassemblys i found a bunch of unexppected zeros in my object file. Which are sometimes there but sometimes aren't. I don't know if it's an error in my libary but i logged the values and they seem alright when i log them.

Bye Cr0a3

philipc commented 3 months ago

I'll need a way to reproduce the problem. Note that it is normal to have zeroes due to alignment padding.

Cr0a3 commented 3 months ago

Is the alignment in bytes? So when i give it 0x123456 it appends an empty byte if i specfie as the alignemnt 4?

philipc commented 3 months ago

Padding is inserted before the data that needs to be aligned, not after it.

Cr0a3 commented 3 months ago

Ok, thank you.

I'll need a way to reproduce the problem. Note that it is normal to have zeroes due to alignment padding.

Here's the rough code where i use your libary: https://github.com/Cr0a3/ygen/blob/main/src/Obj/wrapper.rs#L168-L287 To test the problem you need to run ./test.bat or these 3 commands (from the root of the repo:

cargo run -p simplelang -- -in="tools/simplelang/example.sl" -o="out.o"
llvm-objdump -d out.o --x86-asm-syntax=intel > dump.asm
gcc out.o

The issue is there only sometimes. Mostly when the add function is on top. The expected data is printed and the data from the object file is in dump.asm. Sorry for the incovienence

Cr0a3 commented 2 months ago

I fixed the bug. It was caused by having invalid relocation offsets in my code. Then llvm-objdump added padding zeros so that most bytes would be a valid instruction