LD : Entry point not at the beginning of the output file ?

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.
Ankeraout
Member
Member
Posts: 25
Joined: Tue Feb 28, 2017 10:22 am

LD : Entry point not at the beginning of the output file ?

Post by Ankeraout »

Hi,

I am currently writing my kernel in C, and compiling it with i686-elf toolchain, and until now I used to create "exhaustive object files list" in my makefile, like this :

Code: Select all

LD := i686-elf-ld
LDFLAGS := -Ttext 0x1000 --oformat binary -e _start
ASM := nasm
ASMFLAGS := -f elf
CC := i686-elf-gcc
CFLAGS := -Wall -Wextra -c -nostdlib -fno-builtin

all: kernel.bin

kernel.bin: bootstrp.o kernel.o gdt.o idt.o pic.o keyboard.o ps2.o disk.o interrupt.o mouse.o string.o screen.o isr.o
	$(LD) $(LDFLAGS) $^ -o $@
	
%.o: %.asm
	$(ASM) $(ASMFLAGS) $< -o $@
	
%.o: %.c
	$(CC) $(CFLAGS) $< -o $@
	
clean:
	rm kernel.bin *.o
It was working very well, despite the fact that I found it really dirty.
So today I decided to replace this list with wildcards, like this :

Code: Select all

LD := i686-elf-ld
LDFLAGS := -Ttext 0x1000 --oformat binary -e _start
ASM := nasm
ASMFLAGS := -f elf
CC := i686-elf-gcc
CFLAGS := -Wall -Wextra -c -nostdlib -fno-builtin
SRC_C := $(wildcard *.c)
SRC_ASM := $(wildcard *.asm)
OBJ := $(SRC_ASM:.asm=.o) $(SRC_C:.c=.o)

all: kernel.bin

kernel.bin: $(OBJ)
	$(LD) $(LDFLAGS) $^ -o $@
	
%.o: %.asm
	$(ASM) $(ASMFLAGS) $< -o $@
	
%.o: %.c
	$(CC) $(CFLAGS) $< -o $@
	
clean:
	rm kernel.bin *.o
The problem I encounter is that when bootstrp.o (which contains _start) is not the first file in the argument list of ld, my kernel does not start because the instructions at 0x1000 are not from bootstrp.o.

Here is the code of bootstrp.asm :

Code: Select all

; Multiboot bootstrap

global _start
extern Kernel_start

_start:
	jmp $ ;for debugging
	push ebx
	call Kernel_start
	
align 4
dd 0x1BADB002
dd 0x00000003
dd -(0x1BADB002 + 0x00000003)
And here is an extract of the disassembly of kernel.bin :

Code: Select all

00001740  EBFE              jmp short 0x1740
00001742  53                push ebx
00001743  E8D1E9FFFF        call dword 0x119
_start is located at 0x1740...

I don't know what I am doing wrong here, can somebody help me ?

Thanks by advance,
Ankeraout.
LtG
Member
Member
Posts: 384
Joined: Thu Aug 13, 2015 4:57 pm

Re: LD : Entry point not at the beginning of the output file

Post by LtG »

Not an expert on the tools, but..

Did you read the barebones tutorial? Specifically http://wiki.osdev.org/Bare_Bones#Linking_the_Kernel?
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: LD : Entry point not at the beginning of the output file

Post by onlyonemac »

I'm guessing the wildcard expansion is sorting the files in alphabetical order, and that that's the order they're being packed into the output file, which could put your entry point further into the file.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: LD : Entry point not at the beginning of the output file

Post by dozniak »

You need a linker script to put your objects in the right order.

onlyonemac is right about default make sorting of the object file list not matching the order you expect.
Learn to read.
User avatar
zaval
Member
Member
Posts: 659
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: LD : Entry point not at the beginning of the output file

Post by zaval »

open for yourself linker scripts.
put there something like this

Code: Select all

MEMORY{
	Sdram :	ORIGIN = SDRAM_START, LENGTH = SDRAM_LENGTH
}

PHDRS {
    image PT_LOAD;
}

ENTRY( _start )

SECTIONS{
    .text{
        bootstrp.o ( .text )
        *(.text)
    } >Sdram :image
}
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
Ankeraout
Member
Member
Posts: 25
Joined: Tue Feb 28, 2017 10:22 am

Re: LD : Entry point not at the beginning of the output file

Post by Ankeraout »

zaval wrote:open for yourself linker scripts.
put there something like this

Code: Select all

MEMORY{
	Sdram :	ORIGIN = SDRAM_START, LENGTH = SDRAM_LENGTH
}

PHDRS {
    image PT_LOAD;
}

ENTRY( _start )

SECTIONS{
    .text{
        bootstrp.o ( .text )
        *(.text)
    } >Sdram :image
}
I had to create my own script, but now it works !
Thank you all !
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: LD : Entry point not at the beginning of the output file

Post by dchapiesky »

Note that if you put a function above _start() in bootstrp.c that _start() will no longer be the first function in the executable...

just mentioning it cause that gave me a couple hours consternation...
Plagiarize. Plagiarize. Let not one line escape thine eyes...
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: LD : Entry point not at the beginning of the output file

Post by dozniak »

dchapiesky wrote:Note that if you put a function above _start() in bootstrp.c that _start() will no longer be the first function in the executable...

just mentioning it cause that gave me a couple hours consternation...
IIRC you should be able to arrange layout down to function level, look in LD scripts section for exact syntax.
Learn to read.
User avatar
dchapiesky
Member
Member
Posts: 204
Joined: Sun Dec 25, 2016 1:54 am
Libera.chat IRC: dchapiesky

Re: LD : Entry point not at the beginning of the output file

Post by dchapiesky »

dozniak wrote: IIRC you should be able to arrange layout down to function level, look in LD scripts section for exact syntax.
Yah.. true... Just mentioned because of his script...

Code: Select all

SECTIONS{
    .text{
        bootstrp.o ( .text )
        *(.text)
    } >Sdram :image
}
Plagiarize. Plagiarize. Let not one line escape thine eyes...
User avatar
zaval
Member
Member
Posts: 659
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: LD : Entry point not at the beginning of the output file

Post by zaval »

^ that was just a quick example. But I can't imagine why one would need to put his/her start function NOT at the top of his/her start file. Why? For the life to not look that easy? xD
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: LD : Entry point not at the beginning of the output file

Post by dozniak »

zaval wrote:^ that was just a quick example. But I can't imagine why one would need to put his/her start function NOT at the top of his/her start file. Why? For the life to not look that easy? xD
There's no direct correspondence between the top of C source and where it ends up in the object file. You may have it at the top of your source. It may end up being the last function in the .o after some other functions.
Learn to read.
User avatar
zaval
Member
Member
Posts: 659
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: LD : Entry point not at the beginning of the output file

Post by zaval »

right. except the file in question was from an assembly file.
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: LD : Entry point not at the beginning of the output file

Post by dozniak »

zaval wrote:right. except the file in question was from an assembly file.
That doesn't matter too much. You could declare multiple functions in asm file as well.
Learn to read.
User avatar
zaval
Member
Member
Posts: 659
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: LD : Entry point not at the beginning of the output file

Post by zaval »

dozniak wrote:
zaval wrote: right. except the file in question was from an assembly file.
That doesn't matter too much. You could declare multiple functions in asm file as well.
If you put functions A, B, C in your assembly in that order, they remain in the same order in the output file. Linker won't reorder functions inside input sections. It operates on sections as units. Merging them into output sections. It doesn't change symbol positions inside a section. I don't remember anything about linker scripts where one could shuffle functions inside a section. Nor linker does so. Unless it is some weird link time optimization which I am not aware of.
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: LD : Entry point not at the beginning of the output file

Post by dozniak »

zaval wrote: If you put functions A, B, C in your assembly in that order, they remain in the same order in the output file. Linker won't reorder functions inside input sections. It operates on sections as units. Merging them into output sections. It doesn't change symbol positions inside a section. I don't remember anything about linker scripts where one could shuffle functions inside a section. Nor linker does so. Unless it is some weird link time optimization which I am not aware of.
Thanks, but are you just trying to maintain discussion or want to get at something? I wrote linkers and have an idea how they work, thank you very much.
Learn to read.
Post Reply