OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 14, 2021 12:06 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Linker-script writers beware: COMMON Symbols
PostPosted: Wed Jan 13, 2010 1:25 pm 
Offline
User avatar

Joined: Sat Oct 17, 2009 4:32 am
Posts: 21
So I was writing some memory management test code to find that my kernel panic()s at different places once any of the text, data, or bss sections size exceed something close to 20 kilobytes.

After a day of debugging, I found that the compiler does not put my uninitialized globally-exported symbols under BSS; it marks them as "common symbols" instead. This has lead to disastrous effects cause I use the following innocent-looking segment in the linker script:
Code:
SECTIONS {
          ...
          .bss : {
                  __bss_start = .;
                  *(EXCLUDE_FILE (*head.o *e820.o) .bss)
                  __bss_end = .;
           }
           __kernel_end = .;
}
Which meant that some of the kernel's uninitialized variables was not cleared by my clear_bss() method, leading to unpredictable behavior.

The best explanation of "common symbols" I found was from Ulrich Drepper's paper (archive) on shared libraries, mainly the following part:
Quote:
Common variables are widely used in fortran, but they got used in C and C++ as well to work around mistakes of programmers. Since in the early days people used to drop the 'extern' keyword from variable definitions, in the same way it is possible to drop it from function declaration, the compiler often had multiple definitions of the same variable in different files.

To help the poor and clueless programmer, the C/C++ compiler normally generates common variables for uninitialized definitions such as `int foo;' For common variables, there can be more than one definition, and they all get unified in one location in the output file .. their values does not need to be stored in the ELF file.

There are also further information about those symbols from Ian Lance Taylor's articles series on linkers here (archive), on nm(1) manpage, and on ld(1) about LD's very useful `--warn-common' flag. You can also check some nice historical details from this binutils bugreport (archive). To let your BSS variable boundaries really reflect your entire BSS space, you either need to use GCC's '--no-common' parameter or modify the above script segment to:
Code:
SECTIONS {
          ...
          .bss : {
                  __bss_start = .;
                  *(EXCLUDE_FILE (*head.o *e820.o) .bss)
                  *(EXCLUDE_FILE (*head.o *e820.o) COMMON)
                  __bss_end = .;
           }
           __kernel_end = .;
}

I've noticed that this wasn't mentiond on the WIKI, I'll wait for any corrections or comments, then add it there.
Thanks all :)

_________________
For latest news, please check my homepage and my blog.
—Darwish


Last edited by Darwish on Sun Feb 07, 2010 4:49 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Linker-script writers beware: COMMON Symbols
PostPosted: Wed Jan 13, 2010 3:04 pm 
Offline
Member
Member
User avatar

Joined: Fri Jun 13, 2008 3:21 pm
Posts: 1700
Location: Cambridge, United Kingdom
Please don't use dark red for emphasis; that's what italic or bold are for, and some of us use white on dark themes.


Top
 Profile  
 
 Post subject: Re: Linker-script writers beware: COMMON Symbols
PostPosted: Fri Jan 15, 2010 10:56 pm 
Offline
User avatar

Joined: Sat Oct 17, 2009 4:32 am
Posts: 21
Owen wrote:
Please don't use dark red for emphasis; that's what italic or bold are for, and some of us use white on dark themes.

No problem; fixed.

_________________
For latest news, please check my homepage and my blog.
—Darwish


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group