kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: vikram186@gmail.com (Vikram Narayanan)
To: kernelnewbies@lists.kernelnewbies.org
Subject: How vmlinux is recognized?
Date: Mon, 16 May 2011 19:42:16 +0530	[thread overview]
Message-ID: <BANLkTi=jw_NRUOJJe-CMaZ8==CZCxiEpAQ@mail.gmail.com> (raw)
In-Reply-To: <BANLkTi=4eGv9dPQ3SnGxcosSaiKqpY1q5w@mail.gmail.com>

On Mon, May 16, 2011 at 9:14 AM, Peter Teoh <htmldeveloper@gmail.com> wrote:
> I loved this reply.......can I annotate it with references to the linux
> kernel sources?
>
> On Fri, May 13, 2011 at 9:42 AM, Dave Hylands <dhylands@gmail.com> wrote:
>>
>> Hi Vikram,
>>
>> ...snip...
>> > So when compiling the kernel, what is the purpose of the other
>> > files(mentioned below)
>> > linux-2.6/vmlinux - ELF executable, not stripped
>> > linux-2.6/arch/x86/boot/vmlinux.bin - Raw binary (Guess this is the
>> > one which is inside the bzImage)
>> > linux-2.6/arch/x86/boot/compressed/vmlinux.bin - ELF executable,
>> > stripped
>> > linux-2.6/arch/x86/boot/compressed/vmlinux - ELF executable, not
>> > stripped
>>
>> Take luca's email and start at the bottom working towards the top.
>>
>> linux-2.6/vmlinux is the output of the linker. As such, it is an ELF file.
>> A binary is then extracted from this to create
>> arch/x86/boot/compressed/vmlinux.bin
>
> yes:
> See ./arch/x86/boot/Makefile
>
>>
>> This binary is then compressed to produce
>> arch/x86/boot/compressed/vmlinux.bin.gz
>
> See ./arch/x86/boot/compressed/Makefile
>
>>
>> This gzipped binary is then converted into an object file (which just
>> contains the gzipped data) but now we're back to having an ELF file
>
> ./arch/x86/boot/compressed/mkpiggy.c is compiled into a commandline binary -
> mkpiggy which will generate the piggy.o.
>
>>
>> called arch/x86/boot/compressed/piggy.o
>> The linker then compiles a decompressor (misc.o) and piggy.o together
>
> Yes, the routine is called "decompress_kernel", residing inside
> ./arch/x86/boot/compressed/misc.c. ? And this routine is called
> from?./arch/x86/boot/compressed/head_32.S (or head_64.S) and at runtime, the
> gzipped data is decompressed and immediately jumped into (perhaps after some
> relocation if needed):
> /*
> ?* Do the decompression, and jump to the new kernel..
> ?*/
> ?? ? ? ?leal ? ?z_extract_offset_negative(%ebx), %ebp
> ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/* push arguments for decompress_kernel: */
> ?? ? ? ?pushl ? %ebp ? ? ? ? ? ?/* output address */
> ?? ? ? ?pushl ? $z_input_len ? ?/* input_len */
> ?? ? ? ?leal ? ?input_data(%ebx), %eax
> ?? ? ? ?pushl ? %eax ? ? ? ? ? ?/* input_data */
> ?? ? ? ?leal ? ?boot_heap(%ebx), %eax
> ?? ? ? ?pushl ? %eax ? ? ? ? ? ?/* heap area */
> ?? ? ? ?pushl ? %esi ? ? ? ? ? ?/* real mode pointer */
> ?? ? ? ?call ? ?decompress_kernel
> ?? ? ? ?addl ? ?$20, %esp
>
>> to produce arch/x86/boot/compressed/vmlinux (an ELF file).
>> objcopy is used again to convert this ELF into a binary:
>> arch/x86/boot/compressed/vmlinux arch/x86/boot/vmlinux.bin
>> Finally, the binary is compressed to produce bzImage.
>
> Inside arch/x86/boot/Makefile:
> Creating the vmlinux.bin from vmlinux via objcopy (note that this operation
> will throw all relocation information):
> $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE
> ?? ? ? ?$(call if_changed,objcopy)
> And then packing together linearly to form the "bzImage" (output from make):
> make -f scripts/Makefile.build obj=arch/x86/boot arch/x86/boot/bzImage
> make -f scripts/Makefile.build obj=arch/x86/boot/compressed
> arch/x86/boot/compressed/vmlinux
> arch/x86/boot/tools/build arch/x86/boot/setup.bin arch/x86/boot/vmlinux.bin
> CURRENT > arch/x86/boot/bzImage
>>
>> So what you get is a compressed binary which contains a decompressor
>> and another compressed binary, this inner compressed binary being the
>> kernel.
>>
>> GRUB loads bzImage into memory and decompresses it and then executes
>> the resulting binary.
>
> To be more precise, grub will load bzImage and jump into the startup_32
> function located in arch/x86/boot/compressed/head_32.S at the following
> fixed address (from source code):
> /*
> ?* ?head.S contains the 32-bit startup code.
> ?*
> ?* NOTE!!! Startup happens at absolute address 0x00001000, which is also
> where
> ?* the page directory will exist. The startup code will be overwritten by
> ?* the page directory. [According to comments etc elsewhere on a compressed
> ?* kernel it will end up at 0x1000 + 1Mb I hope so as I assume this. - AC]
> ?*
> ?* Page 0 is deliberately kept safe, since System Management Mode code in
> ?* laptops may need to access the BIOS data stored there. ?This is also
> ?* useful for future device drivers that either access the BIOS via VM86
> ?* mode.
> ?*/
> More info:
> http://books.google.com/books?id=e8BbHxVhzFAC&pg=PA1224&lpg=PA1224&dq=grub+head_32.S&source=bl&ots=0MSdKwBoM6&sig=2RyEpprl25zueiqi332TQHLIj0E&hl=en&ei=y5vQTY7eBNDNrQeI3bTCCg&sa=X&oi=book_result&ct=result&resnum=3&ved=0CCkQ6AEwAg#v=onepage&q=grub%20head_32.S&f=false
>
>>
>> This binary starts with a decompressor which then decompresses the
>> kernel, and executes the resulting binary.
>> This binary may relocate itself (probably depends on the architecture)
>> to a different spot in memory, and then runs.
>> The kernel is now running.
>>
>> --
>> Dave Hylands
>> Shuswap, BC, Canada
>> http://www.davehylands.com
>>
>> _______________________________________________
>> K
>
> --
> Regards,
> Peter Teoh
>
That was a great explanation. Thanks a lot. I think this will be very
much useful for people who want to know how things work in the
background.

-
Thanks,
Vikram

  reply	other threads:[~2011-05-16 14:12 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-11 18:06 How vmlinux is recognized? Vikram Narayanan
2011-05-11 19:17 ` Dave Hylands
2011-05-11 19:31   ` Vikram Narayanan
2011-05-11 19:45     ` Mulyadi Santosa
2011-05-11 20:04       ` Vikram Narayanan
2011-05-11 20:09         ` Mulyadi Santosa
2011-05-11 20:11           ` Vikram Narayanan
2011-05-11 20:21             ` Mulyadi Santosa
2011-05-12  3:32               ` Vikram Narayanan
2011-05-12  4:32                 ` Sudheer Divakaran
2011-05-12  6:21                   ` Sudheer Divakaran
2011-05-12  7:26                     ` luca ellero
2011-05-12  8:17                 ` Mulyadi Santosa
2011-05-13  0:19                   ` Vikram Narayanan
2011-05-13  1:42                     ` Dave Hylands
2011-05-16  3:14                       ` Vikram Narayanan
2011-05-16  3:44                       ` Peter Teoh
2011-05-16 14:12                         ` Vikram Narayanan [this message]
2011-05-12  9:31                 ` अनुज
2011-05-11 20:33 ` Manohar Vanga
2011-05-12  3:34   ` Vikram Narayanan
2011-05-12 16:46   ` mindentropy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='BANLkTi=jw_NRUOJJe-CMaZ8==CZCxiEpAQ@mail.gmail.com' \
    --to=vikram186@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).