OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 1:08 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: cross-compiler removes newline in printf() function
PostPosted: Sat Jul 25, 2015 11:49 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
Hello,

I have had to restart my kernel which included rebuilding my development system. I have built a new cross-compiler from binutils 2.25 and gcc 5.1.0. I was able to salvage an earlier version of my printf() function which I knew to have worked properly in a 386 kernel. My cross compiler is built for i686-elf.

I have been looking at the resulting binaries and object files and have noticed that when the printf() function is called, the compiler strips the terminating '\n' character from the string, in particular with the following code:
Code:
printf("Hello, world!\n");


If I change the code to use my own puts() function, the '\n' character remains and is printed properly. If I rename my printf() function to kprintf(), then the '\n' character remains and is printed properly. No other changes are made to have this character remain in the resulting object.

This is the command I am using to compile the source to .o: i686-elf-gcc -c -O2 -g -ffreestanding -fbuiltin -Wall -Wextra -I/home/adam/src/century/isodir/usr/local/include -o obj/kernel.o kernel.c
I can also confirm that the problem exists at the object file before the objects are linked. I did try to remove the -O2 option which changed nothing.

This seems to be a compiler problem, but I am looking to see if anyone can confirm. Or am I doing something ridiculous?


Thanks in advance for your input.

Adam

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: cross-compiler removes newline in printf() function
PostPosted: Sun Jul 26, 2015 12:01 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
I lack a GCC 5.1 at the moment so I can't see what it's doing exactly, but I would expect that it also optimises printf("string without formatters") into a different function call altogether - as long as they're equivalent according to the C standard. This is also why kprintf does not get "optimised"

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: cross-compiler removes newline in printf() function
PostPosted: Sun Jul 26, 2015 12:06 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
If you have written your own printf (which strikes me as a good idea for your kernel) why are you telling the compiler to use its built in version of the function?


Top
 Profile  
 
 Post subject: Re: cross-compiler removes newline in printf() function
PostPosted: Sun Jul 26, 2015 12:14 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
Combuster wrote:
I lack a GCC 5.1 at the moment so I can't see what it's doing exactly, but I would expect that it also optimises printf("string without formatters") into a different function call altogether - as long as they're equivalent according to the C standard. This is also why kprintf does not get "optimised"

Thank you. I executed "readelf -a kernel.o" and reviewed it carefully. I noticed in the relocation section that the function names were puts, and not printf. Then doing some digging, I found that puts() -- which I never really use -- appends a '\n' to the end of the string. This is why it was stripping the '\n' character.

So, to your point, the compiler was optimizing printf("string without formatters ending in \n") to use puts(). So, I was doing something really ridiculous.

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: [SOLVED]: cross-compiler removes newline in printf() functio
PostPosted: Sun Jul 26, 2015 12:21 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
iansjack wrote:
If you have written your own printf (which strikes me as a good idea for your kernel) why are you telling the compiler to use its built in version of the function?

Good question. I did lift several concepts from the build system and organization from here. Removing the -fbuiltin option creates an error with printf not found (even though I provide my own). I will work on that.

Edit: All good now; thanks for your assistance.

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 48 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group