Make second level relocatable using -fpic and "GLOBAL_OFFSET_TABLE"

should be improved by using -mrelpc, but this flags is only supported by
gcc > 3.0 (and I use gcc 2.95)
This commit is contained in:
Laurent Vivier 2004-05-24 23:59:13 +00:00
parent b31edbfa11
commit e3780d21ef
3 changed files with 122 additions and 19 deletions

View File

@ -5,7 +5,7 @@
#
CPPFLAGS = -DVERSION="\"$(VERSION)\""
CFLAGS = -Wno-multichar -O -m68030 -nostdlib -nodefaultlibs -Wall -Werror
CFLAGS = -Wno-multichar -O -m68030 -nostdlib -nodefaultlibs -Wall -Werror -fpic
ASFLAGS =
LS = ls
AWK = awk
@ -32,11 +32,11 @@ endif
endif
second: second.o
$(OBJCOPY) -j .text -j .data -j .rodata -j .got -j .got.plt \
$(OBJCOPY) -j .text -j .data -j .rodata -j .got \
-O binary second.o second
second.o: $(OBJS) ld.script
$(LD) -T ld.script -Ttext $(BASE_ADDRESS) \
$(LD) -T ld.script \
--defsym _KERNEL_SIZE=$(KERNEL_SIZE) -o second.o $(OBJS)
ifeq ($(shell ls $(RAMDISK) 2> /dev/null),$(RAMDISK))

View File

@ -18,33 +18,65 @@ _command_line:
.align 4
setup:
/* relocate C code, need to be compile with -fpic */
bsr relocate
/* identify system */
bsr arch_init
bsr arch_init@PLTPC
/* initialize console, so we can debug ;-) */
bsr console_init
bsr console_init@PLTPC
/* initialize memory, so we can work */
bsr memory_init
bsr memory_init@PLTPC
/* retrieve machine info */
bsr bootinfo_init;
bsr bootinfo_init@PLTPC
/* allocate stack, so we can jump */
pea 0x2000
bsr malloc
bsr malloc@PLTPC
add.l #0x2000 - 16, %d0
move.l %d0, %sp
/* begin to work */
bsr main
bsr main@PLTPC
/* We guess to never come here */
loop:
bra loop
relocate:
/* real address of GOT */
lea (%pc, _GLOBAL_OFFSET_TABLE_@GOTPC), %a0
/* linked address of GOT */
move.l #_GLOBAL_OFFSET_TABLE_, %d0
/* offset to add to GOT items */
move.l %a0,%d2
sub.l %d0,%d2
beq exit_relocate
move.l #_GOT_SIZE, %d0
beq exit_relocate
got_loop:
move.l (%a0), %d1
add.l %d2, %d1
move.l %d1, (%a0)+
subq.l #4, %d0
bgt got_loop
exit_relocate:
rts

View File

@ -15,21 +15,59 @@ SECTIONS
UnivROMBits = 0x0DD4;
Time = 0x020C;
/* Read-only sections, merged into text segment: */
.text . :
.text :
{
__bootloader_start = .;
__bootloader_start = .;
*(.text .stub .text.* .gnu.linkonce.t.*)
*(.gnu.warning)
} =0x4e754e75
. = ALIGN(4);
.data . :
.hash : { *(.hash) }
.dynsym : { *(.dynsym) }
.dynstr : { *(.dynstr) }
.rel.dyn :
{
*(.rel.init)
*(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)
*(.rel.fini)
*(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)
*(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)
*(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)
*(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)
*(.rel.ctors)
*(.rel.dtors)
*(.rel.got)
*(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)
}
.rela.dyn :
{
*(.rela.init)
*(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
*(.rela.fini)
*(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
*(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
*(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
*(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
*(.rela.ctors)
*(.rela.dtors)
*(.rela.got)
*(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
}
.rel.plt : { *(.rel.plt) }
.rela.plt : { *(.rela.plt) }
.init :
{
*(.data .data1 .data.* .gnu.linkonce.d.*)
}
. = ALIGN(4);
.rodata . :
KEEP (*(.init))
} =0x4e754e75
.plt : { *(.plt) }
.fini :
{
*(.rodata .rodata1 .rodata.* .gnu.linkonce.r.*)
KEEP (*(.fini))
} =0x4e754e75
PROVIDE (__etext = .);
PROVIDE (_etext = .);
PROVIDE (etext = .);
.rodata : {
*(.rodata .rodata.* .gnu.linkonce.r.*)
. = ALIGN(4);
_kernel_start = . ;
*(.image)
@ -39,5 +77,38 @@ SECTIONS
*(.ramdisk)
_ramdisk_end = . ;
}
.rodata1 : { *(.rodata1) }
.eh_frame_hdr : { *(.eh_frame_hdr) }
. = ALIGN(0x2000) + (. & (0x2000 - 1));
. = ALIGN(32 / 8);
.preinit_array : { *(.preinit_array) }
.init_array : { *(.init_array) }
.fini_array : { *(.fini_array) }
.data :
{
*(.data .data.* .gnu.linkonce.d.*)
SORT(CONSTRUCTORS)
}
.data1 : { *(.data1) }
.tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }
.tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
.eh_frame : { KEEP (*(.eh_frame)) }
.gcc_except_table : { *(.gcc_except_table) }
.dynamic : { *(.dynamic) }
.got : { *(.got.plt) *(.got) }
_GOT_SIZE = . - _GLOBAL_OFFSET_TABLE_;
_edata = .;
PROVIDE (edata = .);
__bss_start = .;
.bss :
{
*(.dynbss)
*(.bss .bss.* .gnu.linkonce.b.*)
*(COMMON)
. = ALIGN(32 / 8);
}
. = ALIGN(32 / 8);
_end = .;
__bootloader_end = .;
PROVIDE (end = .);
}