[SOLVED] g++: _init and _fini functions half compiled

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
wichtounet
Member
Member
Posts: 90
Joined: Fri Nov 01, 2013 4:05 pm
Location: Fribourg, Switzerland
Contact:

[SOLVED] g++: _init and _fini functions half compiled

Post by wichtounet »

Hi,

I've a big problem with my user space programs. The _init and _fini functions are only half compiled:
0000008000401f70 <_init>:

.section .init
.global _init
.type _init, @function
_init:
push rbp
8000401f70: 55 push %rbp
mov rbp, rsp
8000401f71: 48 89 e5 mov %rsp,%rbp

Disassembly of section .fini:

0000008000401f74 <_fini>:

.section .fini
.global _fini
.type _fini, @function
_fini:
push rbp
8000401f74: 55 push %rbp
mov rbp, rsp
8000401f75: 48 89 e5 mov %rsp,%rbp
They only contain the code from crti.s and not the code from crtn.s (and neither the code from crtbegin/crtend) :S And of course, when I call the function all hell breaks loose. I never had any issue with this for the kernel itself.

Here is my link command for an executable:

Code: Select all

x86_64-elf-g++ -o debug/odin -masm=intel -I../../tstl/include/ -I../printf/include/ -I../tstl/include/ -I../tlib/include/ -Iinclude/ -nostdlib -g -Os -fno-stack-protector -fno-exceptions -funsigned-char -ffreestanding -fomit-frame-pointer -mno-red-zone -mno-3dnow -mno-mmx -fno-asynchronous-unwind-tables -isystem acpica/source/include -std=c++11 -fno-rtti -mpreferred-stack-boundary=4 -msse -msse2 -msse3 -msse4 -msse4.1 -msse4.2 -mno-avx -mno-avx2 -fstack-protector -Wall -Wextra -pedantic -Wold-style-cast -lgcc -static -L../../tlib/debug/ -mcmodel=small -fPIC -z max-page-size=0x1000 -T ../linker.ld -Wl,-gc-sections ../../tlib/debug/src/crti.s.o /home/wichtounet/opt/cross/lib/gcc/x86_64-elf/4.8.2/crtbegin.o debug/src/main.cpp.o -ltlib /home/wichtounet/opt/cross/lib/gcc/x86_64-elf/4.8.2/crtend.o ../../tlib/debug/src/crtn.s.o
The link order sems ok: crtis.s.o crtbegin.o main.o mylibc.o crtend.o crtn.so

If I decompile crtn.s.o, I get the code that should be concatenated:
../../tlib/debug/src/crtn.s.o: file format elf64-x86-64

Disassembly of section .init:

0000000000000000 <.init>:
.intel_syntax noprefix

.section .init
pop rbp
0: 5d pop %rbp
ret
1: c3 retq

Disassembly of section .fini:

0000000000000000 <.fini>:
.intel_syntax noprefix

.section .init
pop rbp
0: 5d pop %rbp
ret
1: c3 retq
Does anybody know what I'm doing wrong ?

I don't seen anything wrong in the link command. The only think I thought of was to remove the gc-sections from the command line, but in that case, I get a huge load of relocation errors that I was unable to find a solution for. Could that be the issue ?

Thanks
Last edited by wichtounet on Sat Aug 20, 2016 2:32 pm, edited 1 time in total.
Thor Operating System: C++ 64 bits OS: https://github.com/wichtounet/thor-os
Good osdeving!
User avatar
wichtounet
Member
Member
Posts: 90
Joined: Fri Nov 01, 2013 4:05 pm
Location: Fribourg, Switzerland
Contact:

Re: g++: _init and _fini functions half compiled

Post by wichtounet »

I found the solution. It was indeed -gc-sections that was removing the wrong sections.

As for the relocation errors, it was coming from crtend/crtbegin who where not compiled with the correct options for my userspace. I recompiled libgcc with:
make all-target-libgcc CFLAGS_FOR_TARGET="-mcmodel=large"
And now it works fine in userspace :)
Thor Operating System: C++ 64 bits OS: https://github.com/wichtounet/thor-os
Good osdeving!
Post Reply