Porting NewLib: some questions.

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
Thonaud
Posts: 5
Joined: Mon Jul 26, 2010 3:17 am

Porting NewLib: some questions.

Post by Thonaud »

Hello,
I have to port NewLib to a new OS, and it's something completly new for me. I am following this tutorial: http://wiki.osdev.org/Porting_Newlib
And I have few (noob) questions:

1) First I have to complete the system calls in libgloss. For example I have done this:

Code: Select all

#include "glue.h"
#include "include.c" // I have the cpu_syscall function in it, write in assembler

int _DEFUN (close, (fd), int fd) 
{
  register int retval;
  retval = (int)cpu_syscall((void*)fd,NULL,NULL,NULL,__CLOSE);
  return retval;
}
Am I doing it right?

2) If my OS doesn't have fork(), I don't have to modify the forck.c in libgloss, right?

3) Once I have finished all the libgloss system calls, I have to compil NewLib. So I do:
mkdir build-newlib
cd build-newlib
newlib/configure --prefix=/home/Thonaud/CrossCompiler/ --target=mipsel-uknown-elf
make all install
It work, but I can't find the .a that should have been created... :(

4) Then I will have to do the crt0.

5) And finally I will have to test NewLib. So I will create a simple main.c file. How I am supposed to compile it with newlib?


Thanks for your help! :-)

PS: sorry for my english...
Last edited by Thonaud on Fri Jul 30, 2010 6:33 am, edited 3 times in total.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Porting NewLib: some questions.

Post by gerryg400 »

1) Your syscall looks okay, although having a file called include.c is unusual.

2) You should return ENOSYS

3) install should put the libs in /home/Thonaud/CrossCompiler/usr/mipsel-uknown-elf/lib (or similar)

4) Yes, crt0 requires some work. I built mine up basically by trial and error. Don't forget crtbegin, crtend etc. from the gcc side built with make all-target-gcc

5) Depends on your configuration. If your binutils, cross-compiler and newlib are all configured to work together perfectly, at the most you may need to add -l c to the ld command line. Otherwise you may need to explicitly include the library.

With regards to testing, firstly make sure that some OS independant library functions work, then work your way to the complex ones.

e.g. I am working through in this order.

i) memcpy, strcpy etc.
ii) atoi, strtol etc.
iii) open, read, write etc.
iv) fopen, fgets etc (IIRC these don't use malloc for the first 3 files)
v) malloc etc.
vi) fprintf etc.
vii) exec, fork, exit etc.

and so on.
If a trainstation is where trains stop, what is a workstation ?
Thonaud
Posts: 5
Joined: Mon Jul 26, 2010 3:17 am

Re: Porting NewLib: some questions.

Post by Thonaud »

Thanks for your answer!

1) Ok.

2) I just noticed that there is no fork.c (neither execve.c, link.c, times.c, wait.c. Witch are all mentionned in the tutorial I flollow) in libgloss... is it normal?

3) You're right, thanks !

4) We are on mipsel. So based on the tutorial, our crto will be something like this?

Code: Select all

      .global _start
      .extern main
_start:

       # call the user's function
       call main

       # call the 'kill me' syscall
       ori    $4, $0, $?
       syscall

	# loop in case we haven't yet rescheduled
lp:
	nop
	j lp
But once it's create, do we have something special to do? Is there a way to test it efficiently?

5) So the idea is to:
- Create simple main.c. Example:

Code: Select all

int main(){
    printf("Hello, World!");
    return 0;
}
- Compil it with something like this: gcc main.c -L$NEWLIB
- Put the main.o in our OS, and run it
Right?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Porting NewLib: some questions.

Post by gerryg400 »

Code: Select all

int main(){
    printf("Hello, World!");
    return 0;
}
A warning about printf. In newlib it uses malloc. Therefore it might be better to test malloc first. As I said, I tested low-level stuff (like mem* and str*) first and then tested more complex things.
If a trainstation is where trains stop, what is a workstation ?
Thonaud
Posts: 5
Joined: Mon Jul 26, 2010 3:17 am

Re: Porting NewLib: some questions.

Post by Thonaud »

Thanks again for your answer!
I undestood that I have to test memcpy, strcpy at first. It was just an example.

I have two questions:
- I am making my crt0. To test it I have to "link it as the first object", how can I do that?
- Once my crt0.S is done, I'll have to compil my code with newlib. Is it with something like this: mipsel-gcc main.c -L$NEWLIB ?

Code: Select all

> ls $NEWLIB
cfe.ld      crt0.o        ddb.ld  idtecoff.ld  ldscripts  libcfe.a  libidt.a  libm.a      libnullmon.a  lsi.ld      pcrt0.o  soft-float
crt0_cfe.o  ddb-kseg0.ld  eb      idt.ld       libc.a     libg.a    liblsi.a  libnosys.a  libpmon.a     nullmon.ld  pmon.ld
Thanks for your help :-)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Porting NewLib: some questions.

Post by gerryg400 »

- I am making my crt0. To test it I have to "link it as the first object", how can I do that?
It depends on how you configure your tools. I followed this http://wiki.osdev.org/OS_Specific_Toolchain to configure my toolchain. When I do a build, crt0 is automatically put first. I don't specify it at all. (Actually it's not first, there's an .init section before it).
Once my crt0.S is done, I'll have to compil my code with newlib. Is it with something like this: mipsel-gcc main.c -L$NEWLIB ?
I just type

Code: Select all

x86_64-artix-gcc main.c
If a trainstation is where trains stop, what is a workstation ?
Thonaud
Posts: 5
Joined: Mon Jul 26, 2010 3:17 am

Re: Porting NewLib: some questions.

Post by Thonaud »

We can't test NewLib and our crt0 without following this tutorial: http://wiki.osdev.org/OS_Specific_Toolchain? There is no others ways?
Thonaud
Posts: 5
Joined: Mon Jul 26, 2010 3:17 am

Re: Porting NewLib: some questions.

Post by Thonaud »

I have tree more questions:

- I've writen the 17 syscalls that act as glue between newlib and my OS, and they say in the tutorial that I have to put them in the LibGloss directory.
But in LibGloss there isn't all the 17 files. For example, I can't find the fork.c or link.c. So where do I have to put my 17 syscalls?


- Once LibGloss is done and compiled, when exactly I'll have to use the .a that have been created? Is it when I will compile my main.c?

Code: Select all

> mipsel-uknown-elf-gcc main.c -L$NEWLIB

> ls $NEWLIB
cfe.ld      crt0.o        ddb.ld        idtecoff.ld   ldscripts  libcfe.a  
libidt.a    libm.a        libnullmon.a  lsi.ld        pcrt0.o    soft-float
crt0_cfe.o  ddb-kseg0.ld  eb            idt.ld        libc.a     libg.a    
liblsi.a    libnosys.a    libpmon.a     nullmon.ld    pmon.ld

- My crt0.c is done. And I have to "link it as the first object". How can I do that? Is it something like this?

Code: Select all

> mipsel-uknown-elf-ld crt0.o main.o

Thanks for your answers!
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Porting NewLib: some questions.

Post by gerryg400 »

From memory, I think that if you put your files in the 'libgloss/libnosys' directory they will end up in the library 'libnosys.a'
If a trainstation is where trains stop, what is a workstation ?
Post Reply