vmlinux.lds.S linker script

vmlinux.lds.S linker script

I'll provide concrete examples to explain the key aspects of the vmlinux.lds.S linker script:

  1. Preprocessor Conditional Compilation

#ifdef CONFIG_XIP_KERNEL

#include "vmlinux-xip.lds.S"

#else // Rest of the linker script #endif

Example scenario:

  • If kernel is configured for XIP (Execute In Place), it uses a different linker script
  • This allows different memory layouts for embedded systems where kernel runs directly from flash

  1. Discarding Sections

/DISCARD/ : { ARM_DISCARD

#ifndef CONFIG_SMP_ON_UP

*(.alt.smp.init)

#endif

#ifndef CONFIG_ARM_UNWIND

*(.ARM.exidx) *(.ARM.exidx.*) *(.ARM.extab) *(.ARM.extab.*)

#endif }

Concrete examples:

  • If not using SMP (Single-core), discard alternate SMP initialization code
  • If not using ARM unwind tables, remove exception handling metadata
  • Reduces kernel binary size by removing unnecessary sections

  1. Memory Offset and Entry Point

. = KERNEL_OFFSET + TEXT_OFFSET; ENTRY(stext)

Real-world example:

  • KERNEL_OFFSET might be 0x80000000 for a typical ARM system
  • TEXT_OFFSET could be 0x8000
  • This means the kernel code starts at physical address 0x80008000
  • stext is the first instruction executed when the kernel starts

  1. Endianness Handling

#ifndef __ARMEB__

jiffies = jiffies_64;

#else jiffies = jiffies_64 + 4;

#endif

Explanation:

  • For little-endian systems, jiffies points directly to jiffies_64
  • For big-endian systems, it adjusts the pointer by 4 bytes to account for different memory layout

  1. Section Definitions

.head.text : {

_text = .;

HEAD_TEXT . = ALIGN(8);

}

Practical meaning:

  • .head.text contains the kernel's initial startup code
  • _text marks the start of the text (code) section
  • HEAD_TEXT likely includes architecture-specific startup instructions
  • ALIGN(8) ensures 8-byte alignment for performance and hardware requirements

Let me view more of the file to provide additional context:

Analyzed

vmlinux.lds.S:(100-200)

  1. Architecture and Initialization Sections

.init.tagtable : {

__tagtable_begin = .;

*(.taglist.init)

__tagtable_end = .;

}

Example use:

  • Collects initialization tag information
  • Helps in early kernel setup, especially for embedded systems
  • Defines start and end symbols for easy reference

  1. Conditional Sections

#ifdef CONFIG_SMP_ON_UP

.init.smpalt : {

__smpalt_begin = .;

*(.alt.smp.init)

__smpalt_end = .; }

#endif

Practical scenario:

  • Includes alternative SMP initialization code only when specifically configured
  • Allows flexibility for different system configurations

  1. Memory Section Alignment

#ifdef CONFIG_STRICT_KERNEL_RWX

. = ALIGN(1<<SECTION_SHIFT);

#else

. = ALIGN(THREAD_ALIGN);

#endif

Real-world implications:

  • STRICT_KERNEL_RWX ensures strict Read-Write-Execute memory protections
  • Aligns sections according to hardware-specific requirements
  • Improves security and memory management

  1. Final Assertions

ASSERT((__proc_info_end - __proc_info_begin), "missing CPU support")

#ifndef CONFIG_COMPILE_TEST ASSERT

((__arch_info_end - __arch_info_begin), "no machine record defined")

#endif

Purpose:

  • Ensures that CPU support and machine records are defined
  • Prevents compilation of incomplete or misconfigured kernel builds

The linker script is essentially a blueprint that tells the linker how to construct the final kernel binary, taking into account:

  • Architecture-specific requirements
  • Memory layout
  • Initialization process
  • Conditional compilation
  • Alignment and memory protection

要查看或添加评论,请登录

David Zhu的更多文章

社区洞察

其他会员也浏览了