Debugging binutils under my OS?
Posted: Sat Oct 10, 2015 5:00 pm
I recently ported binutils to my OS. I've already been using cross-compiling binutils running under Linux and targetting Glidix (x86_64-glidix), and they were working fine. So recently I used my cross-compiler to build a native binutils. Configuration line:
I installed it under a destination directory (DESTDIR=/glidix-mipdir make install) then used that directory as the root of an installable package that my OS recognises. There were some symbolic links in the directory, and the package creator copied the files instead of creating links (I'm not sure how to handle symlinks in an archive creator). The symlinks were just /usr/x86_64-glidix/bin stuff, I don't think that is needed for the simple test I attempted.
Anyway, I installed the binaries under my OS, and I attempted to assemble this simple program which basically does:
In assembly:
I tried assembling under Glidix with:
At first it saw an instruction "text" instead of ".text" for some reason (this could be due to the fact there was no tab at the beginning of the line? I know it shouldn't happen). I then changed the configuration to the one specified above. Now it assembles without errors. But, when I look at the "hello.o" file, it is far too small (about 200 bytes smaller than the one produced by the cross-assembler under Linux) and horribly corrupt.
OK, so I attempt to skip this step to see what else is wrong. I assemble under Linux (using x86_64-glidix-as) and copy the hello.o into Glidix. Then I try linking it:
Again, "hello" is too small and horribly corrupt! By horribly corrupt I mean:
But if I produce "hello" under Linux, it runs perfectly under Glidix and prints "hello world".
The problem appears to be similar in both cases: smaller and corrupt files. So I decided to trace all fopen(), fwrite(), fseek(), fclose() calls, and here's what I got (from running ld):
So indeed with this series of calls, we expect the size to be 528 bytes, which is what it is - but it is this series of calls that produces the corrupt binary! When linking under Linux, the binary is 796 bytes and not corrupt.
I'm not sure exactly what could be the problem and how to debug it. Any clues?
Code: Select all
../glidix-binutils/configure --prefix=/usr --exec-prefix=/usr --host=x86_64-glidix --target=x86_64-glidix --disable-werror --disable-nls
Anyway, I installed the binaries under my OS, and I attempted to assemble this simple program which basically does:
Code: Select all
write(1, "hello world\n", 12);
exit(0);
Code: Select all
.text
.globl _start
_start:
mov $1, %rdi
mov $hello_world, %rsi
mov $12, %rdx
ud2
.hword 1
mov $0, %rdi
ud2
.hword 0
hello_world:
.ascii "hello world\n"
Code: Select all
as -c hello.s -o hello.o
OK, so I attempt to skip this step to see what else is wrong. I assemble under Linux (using x86_64-glidix-as) and copy the hello.o into Glidix. Then I try linking it:
Code: Select all
ld hello.o -o hello
Code: Select all
Elf file type is EXEC (Executable file)
Entry point 0x400078
There are 1 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
<unknown>: 9cc 0x010b0f0000000cc2 0x00000000c7c74800 0x6c6c656800000b0f
0x0a646c726f77206f 0x0000000000000000 0
Section to Segment mapping:
Segment Sections...
00
The problem appears to be similar in both cases: smaller and corrupt files. So I decided to trace all fopen(), fwrite(), fseek(), fclose() calls, and here's what I got (from running ld):
Code: Select all
{fopen 'hello', mode 'w+'}
{fopen '/media/cdrom/memes/hello.o', mode 'r'}
{fseek 168:0 in 6}
{fseek 616:0 in 6}
{fseek 232:0 in 6}
{fseek 112:0 in 6}
{fseek 800:0 in 6}
{fseek 848:0 in 6}
{fseek 704:0 in 6}
{fseek 64:0 in 5}
{fwrite 0+56 -> 5}
{fseek 680:0 in 6}
{fseek 64:0 in 6}
{fwrite 56+48 -> 5}
{fseek 528:0 in 5}
{fwrite 104+216 -> 5}
{fwrite 320+1 -> 5}
{fwrite 321+27 -> 5}
{fwrite 348+12 -> 5}
{fwrite 360+7 -> 5}
{fwrite 367+12 -> 5}
{fwrite 379+7 -> 5}
{fwrite 386+5 -> 5}
{fseek 168:0 in 5}
{fwrite 168+1 -> 5}
{fwrite 169+8 -> 5}
{fwrite 177+8 -> 5}
{fwrite 185+10 -> 5}
{fwrite 195+6 -> 5}
{fseek 0:0 in 5}
{fwrite 0+64 -> 5}
{fseek 208:0 in 5}
{fwrite 208+320 -> 5}
{fclose}
{fclose}
I'm not sure exactly what could be the problem and how to debug it. Any clues?