Implement crt1.o, crti.o, and crtn.o.

This helps running cross compiled programs as well as compiling programs
under Sortix with gcc. There is also support for global constructors.

Currently, cross-compiled executables uses these startup files. The current
build system continues to use start.o, which does not offer global
constructors and other useful features.

Note that these using the crtX.o files requires the crtbegin.o and crtend.o
files that ship with the cross compiler, but that should be no problem.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-08 14:37:16 +02:00
parent 16019e5897
commit bde41a37ec
9 changed files with 285 additions and 7 deletions

View File

@ -2,8 +2,6 @@ ifndef CPU
CPU=x86
endif
BINS=libc.a libg.a libmaxsi.a libmaxsi-sortix.a
ifeq ($(CPU),x86)
CPUDEFINES=-DPLATFORM_X86
CPUFLAGS=-m32
@ -52,7 +50,6 @@ init.o \
signal.o \
$(CPU)/signal.o \
$(CPU)/fork.o \
start.o \
time.o \
random.o \
abs.o \
@ -165,6 +162,15 @@ putc.o \
rewind.o \
ungetc.o \
CRTOBJ=\
start.o \
crt1.o \
crti.o \
crtn.o \
MISCOBJ=\
$(CRTOBJ) \
UNPROCHEADERDIRS:=$(shell find include -type d)
UNPROCHEADERS:=$(shell find include -type f)
HEADERDIRS:=$(patsubst include%,preproc%,$(UNPROCHEADERDIRS))
@ -182,6 +188,8 @@ crc32.o \
SORTIXOBJS:=$(addprefix sortix/,$(SORTIXOBJS))
SORTIXCPPFLAGS:=-DSORTIX_KERNEL
BINS=libc.a libg.a libmaxsi.a libmaxsi-sortix.a $(CRTOBJ)
all: $(BINS)
libmaxsi.a: $(OBJS)
@ -206,7 +214,16 @@ libg.so: libc.so
ln -sf $< $@
start.o: $(CPU)/start.o
ln -sf $< $@
ln -f $< $@
crt1.o: $(CPU)/crt1.o
ln -f $< $@
crti.o: $(CPU)/crti.o
ln -f $< $@
crtn.o: $(CPU)/crtn.o
ln -f $< $@
# header preprocessing
$(HEADERDIRS):
@ -246,10 +263,8 @@ install:
for D in $(UNPROCHEADERDIRS); do mkdir -p $(SYSROOT)/usr/$$D || exit $?; done
for SRC in $(HEADERS); do DEST=`echo $$SRC | sed 's/preproc/include/'`; cp $$SRC $(SYSROOT)/usr/$$DEST || exit $?; done
mkdir -p $(SYSROOT)/usr/include
cp start.o $(SYSROOT)/usr/lib/crtbegin.o
touch deleteme.cpp
g++ $(CPUFLAGS) -c deleteme.cpp -o deleteme.o
for F in crt0.o crtn.o crt1.o crtend.o crtbeginT.o crti.o; do cp deleteme.o $(SYSROOT)/usr/lib/$$F; done
for F in libgcc.so libm.so libstdc++.so; do ld $(CPULDFLAGS) -shared deleteme.o -o $(SYSROOT)/usr/lib/$$F; done
rm -f deleteme.o deleteme.cpp

54
libmaxsi/x64/crt1.s Normal file
View File

@ -0,0 +1,54 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
crt1.s
Implement the _start symbol at which execution begins which performs the
task of initializing the standard library and executing the main function.
*******************************************************************************/
/* NOTE: This is almost identical to what is in start.s. crt1.s is used when
compiling with the real cross compiler, while start.s is used when
using the older hacky way of building Sortix. Please try to keep these
files alike by doing changes both places until we only build Sortix
with real cross compilers. */
.section .text
.global _start
.type _start, @function
_start:
movq %rcx, environ # envp
# Prepare signals, memory allocation, stdio and such.
pushq %rsi
pushq %rdi
call initialize_standard_library
# Run the global constructors.
call _init
# Run main
popq %rdi
popq %rsi
call main
# Terminate the process with main's exit code.
movl %eax, %edi
call exit

39
libmaxsi/x64/crti.s Normal file
View File

@ -0,0 +1,39 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
crti.s
Provides the header of the _init and _fini functions.
*******************************************************************************/
.section .init
.global _init
.type _init, @function
_init:
push %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
.section .fini
.global _fini
.type _fini, @function
_fini:
push %rbp
movq %rsp, %rbp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

33
libmaxsi/x64/crtn.s Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
crtn.s
Provides the tail of the _init and _fini functions.
*******************************************************************************/
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
popq %rbp
ret
.section .fini
/* gcc will nicely put the contents of crtend.o's .fini section here. */
popq %rbp
ret

View File

@ -22,6 +22,12 @@
******************************************************************************/
/* NOTE: This is almost identical to what is in crt1.s. crt1.s is used when
compiling with the real cross compiler, while start.s is used when
using the older hacky way of building Sortix. Please try to keep these
files alike by doing changes both places until we only build Sortix
with real cross compilers. */
.globl _start
.section .text

54
libmaxsi/x86/crt1.s Normal file
View File

@ -0,0 +1,54 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
crt1.s
Implement the _start symbol at which execution begins which performs the
task of initializing the standard library and executing the main function.
*******************************************************************************/
/* NOTE: This is almost identical to what is in start.s. crt1.s is used when
compiling with the real cross compiler, while start.s is used when
using the older hacky way of building Sortix. Please try to keep these
files alike by doing changes both places until we only build Sortix
with real cross compilers. */
.section .text
.global _start
.type _start, @function
_start:
movl %ecx, environ # envp
# Arguments for main
push %ebx # argv
push %eax # argc
# Prepare signals, memory allocation, stdio and such.
call initialize_standard_library
# Run the global constructors.
call _init
# Run main
call main
# Terminate the process with main's exit code.
push %eax
call exit

39
libmaxsi/x86/crti.s Normal file
View File

@ -0,0 +1,39 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
crti.s
Provides the header of the _init and _fini functions.
*******************************************************************************/
.section .init
.global _init
.type _init, @function
_init:
push %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
.section .fini
.global _fini
.type _fini, @function
_fini:
push %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

33
libmaxsi/x86/crtn.s Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
crtn.s
Provides the tail of the _init and _fini functions.
*******************************************************************************/
.section .init
/* gcc will nicely put the contents of crtend.o's .init section here. */
popl %ebp
ret
.section .fini
/* gcc will nicely put the contents of crtend.o's .fini section here. */
popl %ebp
ret

View File

@ -22,11 +22,16 @@
******************************************************************************/
/* NOTE: This is almost identical to what is in crt1.s. crt1.s is used when
compiling with the real cross compiler, while start.s is used when
using the older hacky way of building Sortix. Please try to keep these
files alike by doing changes both places until we only build Sortix
with real cross compilers. */
.globl _start
.section .text
.text 0x100000
.type _start, @function
_start: