基于aarch64分析kernel源码 二:确定第一行代码

一、参考

【linux kernel】基于ARM64分析linux内核的链接脚本vmlinux.lds.S_linux内核连接脚本_iriczhao的博客-CSDN博客

二、概述

第一行代码通过链接脚本 vmlinux.lds 进行确定。

注:vmlinux.lds 是通过 vmlinux.lds.S 编译后生成的。

三、连接器相关概念

四、vmlinux.lds.S

/* SPDX-License-Identifier: GPL-2.0 */
/*
 * ld script to make ARM Linux kernel
 * taken from the i386 version by Russell King
 * Written by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
 */

#include <asm/hyp_image.h>
/* 开启 KVM 相关配置 */
#ifdef CONFIG_KVM
#define HYPERVISOR_EXTABLE					\
	. = ALIGN(SZ_8);					\
	__start___kvm_ex_table = .;				\
	*(__kvm_ex_table)					\
	__stop___kvm_ex_table = .;

#define HYPERVISOR_DATA_SECTIONS				\
	HYP_SECTION_NAME(.rodata) : {				\
		. = ALIGN(PAGE_SIZE);				\
		__hyp_rodata_start = .;				\
		*(HYP_SECTION_NAME(.data..ro_after_init))	\
		*(HYP_SECTION_NAME(.rodata))			\
		. = ALIGN(PAGE_SIZE);				\
		__hyp_rodata_end = .;				\
	}

#define HYPERVISOR_PERCPU_SECTION				\
	. = ALIGN(PAGE_SIZE);					\
	HYP_SECTION_NAME(.data..percpu) : {			\
		*(HYP_SECTION_NAME(.data..percpu))		\
	}

#define HYPERVISOR_RELOC_SECTION				\
	.hyp.reloc : ALIGN(4) {					\
		__hyp_reloc_begin = .;				\
		*(.hyp.reloc)					\
		__hyp_reloc_end = .;				\
	}

#define BSS_FIRST_SECTIONS					\
	__hyp_bss_start = .;					\
	*(HYP_SECTION_NAME(.bss))				\
	. = ALIGN(PAGE_SIZE);					\
	__hyp_bss_end = .;

/*
 * We require that __hyp_bss_start and __bss_start are aligned, and enforce it
 * with an assertion. But the BSS_SECTION macro places an empty .sbss section
 * between them, which can in some cases cause the linker to misalign them. To
 * work around the issue, force a page alignment for __bss_start.
 * 我们要求__hyp_bss_start和__bss_start保持一致,并通过断言强制执行它。
 * 但是BSS_SECTION宏在它们之间放置一个空的 .sbss 部分,这在某些情况下可能会导致链接器将它们错位。
 * 若要解决此问题,请强制页面对齐以进行__bss_start。
 */
#define SBSS_ALIGN			PAGE_SIZE
/* 不开启 KVM 相关配置 */
#else /* CONFIG_KVM */
#define HYPERVISOR_EXTABLE
#define HYPERVISOR_DATA_SECTIONS
#define HYPERVISOR_PERCPU_SECTION
#define HYPERVISOR_RELOC_SECTION
#define SBSS_ALIGN			0
#endif

#define RO_EXCEPTION_TABLE_ALIGN	4
#define RUNTIME_DISCARD_EXIT

#include <asm-generic/vmlinux.lds.h>
#include <asm/cache.h>
#include <asm/kernel-pgtable.h>
#include <asm/kexec.h>
#include <asm/memory.h>
#include <asm/page.h>

#include "image.h"

/* 指定生成的内核映像的目标架构为 AArch64 */
OUTPUT_ARCH(aarch64)
/* 指定内核映像的入口点 */
ENTRY(_text)

jiffies = jiffies_64;

/* 定义一个名为HYPERVISOR_TEXT的段(section) */
#define HYPERVISOR_TEXT					\
	/* 将当前位置对齐到页大小 */
	. = ALIGN(PAGE_SIZE);				\
	/* 定义一个符号__hyp_idmap_text_start,并将其值设置为当前位置。这用于标识.hyp.idmap.text段的起始位置 */
	__hyp_idmap_text_start = .;			\
	/* 将.hyp.idmap.text段的内容放置在该位置。该段可能包含与虚拟地址映射相关的代码或数据 */
	*(.hyp.idmap.text)				\
	/* 定义一个符号__hyp_idmap_text_end,并将其值设置为当前位置。这用于标识.hyp.idmap.text段的结束位置 */
	__hyp_idmap_text_end = .;			\
	/* 定义一个符号__hyp_text_start,并将其值设置为当前位置。这用于标识.hyp.text段的起始位置 */
	__hyp_text_start = .;				\
	/* 定义一个符号__hyp_text_start,并将其值设置为当前位置。这用于标识.hyp.text段的起始位置 */
	*(.hyp.text)					\
	/* 插入有关异常处理的代码 */
	HYPERVISOR_EXTABLE				\
	/* 将当前位置对齐到页大小 */
	. = ALIGN(PAGE_SIZE);				\
	/* 定义一个符号__hyp_text_end,并将其值设置为当前位置。这用于标识.hyp.text段的结束位置 */
	__hyp_text_end = .;
/* 以上代码片段定义了内核映像中的Hypervisor文本段(HYPERVISOR_TEXT),并确定了其中各个子段的起始位置和结束位置。这些段可能包含与虚拟地址映射和hypervisor相关的代码或数据 */	


#define IDMAP_TEXT					\
	. = ALIGN(SZ_4K);				\
	__idmap_text_start = .;				\
	*(.idmap.text)					\
	__idmap_text_end = .;

#ifdef CONFIG_HIBERNATION
#define HIBERNATE_TEXT					\
	/* 将当前位置对齐到函数对齐边界。这通常用于确保函数在内存上对齐,以提高执行效率 */
	ALIGN_FUNCTION();				\
	__hibernate_exit_text_start = .;		\
	*(.hibernate_exit.text)				\
	__hibernate_exit_text_end = .;
#else
#define HIBERNATE_TEXT
#endif

#ifdef CONFIG_KEXEC_CORE
#define KEXEC_TEXT					\
	ALIGN_FUNCTION();				\
	__relocate_new_kernel_start = .;		\
	*(.kexec_relocate.text)				\
	__relocate_new_kernel_end = .;
#else
#define KEXEC_TEXT
#endif

#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
#define TRAMP_TEXT					\
	. = ALIGN(PAGE_SIZE);				\
	__entry_tramp_text_start = .;			\
	*(.entry.tramp.text)				\
	. = ALIGN(PAGE_SIZE);				\
	__entry_tramp_text_end = .;			\
	*(.entry.tramp.rodata)
#else
#define TRAMP_TEXT
#endif

#ifdef CONFIG_UNWIND_TABLES
#define UNWIND_DATA_SECTIONS				\
	.eh_frame : {					\
		__eh_frame_start = .;			\
		*(.eh_frame)				\
		__eh_frame_end = .;			\
	}
#else
#define UNWIND_DATA_SECTIONS
#endif

/*
 * The size of the PE/COFF section that covers the kernel image, which
 * runs from _stext to _edata, must be a round multiple of the PE/COFF
 * FileAlignment, which we set to its minimum value of 0x200. '_stext'
 * itself is 4 KB aligned, so padding out _edata to a 0x200 aligned
 * boundary should be sufficient.
 * 覆盖内核映像的 PE/COFF 部分(从 _stext 到 _edata)的大小必须是 PE/COFF 文档对齐的整数倍,
 * 我们将其设置为最小值 0x200。“_stext”本身对齐 4 KB,因此填充_edata到0x200对齐的边界就足够了。
 */
PECOFF_FILE_ALIGNMENT = 0x200;

#ifdef CONFIG_EFI
#define PECOFF_EDATA_PADDING	\
	/* 定义一个名为.pecoff_edata_padding的段,该段用于填充PE/COFF格式文件中的数据节(.edata) */
	/* BYTE(0):  在.pecoff_edata_padding段中插入一个字节为0的数据。这样做是为了填充数据节(.edata)中的空白处*/
	/* . = ALIGN(PECOFF_FILE_ALIGNMENT): 将当前位置对齐到PE/COFF文件对齐边界(PECOFF_FILE_ALIGNMENT) */
	.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
#else
#define PECOFF_EDATA_PADDING
#endif

/* SECTIONS用于定义各个段(sections)的存放位置和属性 */
SECTIONS
{
	/*
	 * XXX: The linker does not define how output sections are
	 * assigned to input sections when there are multiple statements
	 * matching the same input section name.  There is no documented
	 * order of matching.
	 * XXX:当有多个语句与同一输入节名称匹配时,链接器不定义如何将输出节分配给输入节。 没有记录的匹配顺序。
	 */
	/* DISCARDS部分: 用于指定需要丢弃(discard)的输入节(input section) */
	DISCARDS
	/DISCARD/ : {
		/* 匹配并丢弃.interp和.dynamic输入节。.interp通常用于指定动态器(dynamic linker),.dynamic包含了动态链接器所需的动态信息 */
		*(.interp .dynamic)
		/* 配并丢弃.dynsym、.dynstr、.hash和.gnu.hash输入节。这些节与动态链接库(shared libraries)的符号表、字符串表和哈希表相关 */
		*(.dynsym .dynstr .hash .gnu.hash)
	}

	/* 将当前位置(.)设置为指定的虚拟地址(KIMAGE_VADDR) */
	. = KIMAGE_VADDR;

	/* 定义一个名为.head.text的段(section) */
	.head.text : {
		/* 将当前位置(.)赋值给一个符号 _text,用于标记.head.text段的起始位置 */
		_text = .;
		/* 插入了一个未定义的语句 HEAD_TEXT。这通常用于在链接脚本中的其他位置进行引用或扩展 */
		HEAD_TEXT
	}
	/* 定义一个名为.head.text的段,并将它的起始位置标记为 _text。同时,这段代码留出了一个未定义的位置,使得在链接脚本的其他位置能够引用或扩展该段 */
	
	/* 定义了一个名为.text的段,使用ALIGN函数将当前位置对齐到SEGMENT_ALIGN(预先定义的对齐值) */
	.text : ALIGN(SEGMENT_ALIGN) {	/* Real text segment		*/
		/* 将当前位置(.)赋值给符号_stext,用于标记.text段的起始位置 */
		_stext = .;		/* Text and read-only data	*/
			/* 列出了一系列输入节(input section)的名称,这些输入节将被放置在.text段中 */
			IRQENTRY_TEXT
			SOFTIRQENTRY_TEXT
			ENTRY_TEXT
			TEXT_TEXT
			SCHED_TEXT
			LOCK_TEXT
			KPROBES_TEXT
			HYPERVISOR_TEXT
			*(.gnu.warning)
	}

	/* 将当前位置(.)与指定的 SEGMENT_ALIGN 进行对齐。这样做是为了确保后续的数据和段能够按照设定的对齐方式进行布局 */
	. = ALIGN(SEGMENT_ALIGN);
	/* 将当前位置(.)赋值给一个符号 _etext,用于标记文本段(.text)的结束位置 */
	_etext = .;			/* End of text section */

	/* everything from this point to __init_begin will be marked RO NX */
	/* 调用了名为RO_DATA的宏,并传递了一个参数PAGE_SIZE */
	RO_DATA(PAGE_SIZE)

	HYPERVISOR_DATA_SECTIONS

	/* 定义了一个名为.got的数据段,并将其中的所有内容(通过通配符*)放置在该段内。.got通常用于存储全局偏移表(Global Offset Table)相关的数据 */
	.got : { *(.got) }
	/*
	 * Make sure that the .got.plt is either completely empty or it
	 * contains only the lazy dispatch entries.
	 */
	/* 定义了一个名为.got.plt的数据段,并将其中的所有内容放置在该段内。.got.plt通常用于存储过程链接表(Procedure Linkage Table)相关的数据 */
	.got.plt : { *(.got.plt) }
	/* 如果.got.plt的大小为0或0x18(24字节),则断言通过;否则,将抛出一个错误信息 */
	ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18,
	       "Unexpected GOT/PLT entries detected!")

	/* code sections that are never executed via the kernel mapping */
	/* 定义了一个名为.rodata.text的段(section),以及在该段中需要放置的一些代码节(input section) */
	.rodata.text : {
		TRAMP_TEXT
		HIBERNATE_TEXT
		KEXEC_TEXT
		IDMAP_TEXT
		. = ALIGN(PAGE_SIZE);
	}

	/* 将符号idmap_pg_dir设置为当前位置(.)的地址 */
	idmap_pg_dir = .;
	/* 将当前位置(.)增加一页的大小(PAGE_SIZE)。这样做是为了将当前位置向前移动一页的字节,以便为下一个段或数据保留适当的对齐 */
	. += PAGE_SIZE;

#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
	tramp_pg_dir = .;
	. += PAGE_SIZE;
#endif

	reserved_pg_dir = .;
	. += PAGE_SIZE;

	swapper_pg_dir = .;
	. += PAGE_SIZE;

	/* 将当前位置(.)与指定的 SEGMENT_ALIGN 进行对齐 */
	. = ALIGN(SEGMENT_ALIGN);
	/* 将当前位置(.)赋值给符号 __init_begin 和 __inittext_begin,用于标记初始化代码段的起始位置 */
	__init_begin = .;
	__inittext_begin = .;

	/* 调用名为 INIT_TEXT_SECTION 的宏,并传递参数 8 */
	INIT_TEXT_SECTION(8)

	/* 将当前位置(.)赋值给符号 __exittext_begin,然后定义一个名为 .exit.text 的段,并将其中的 EXIT_TEXT 放置在段内。最后,将当前位置(.)赋值给符号 __exittext_end,用于标记退出代码段的结束位置 */
	__exittext_begin = .;
	.exit.text : {
		EXIT_TEXT
	}
	__exittext_end = .;

	. = ALIGN(4);
	.altinstructions : {
		__alt_instructions = .;
		*(.altinstructions)
		__alt_instructions_end = .;
	}

	UNWIND_DATA_SECTIONS

	. = ALIGN(SEGMENT_ALIGN);
	__inittext_end = .;
	__initdata_begin = .;

	init_idmap_pg_dir = .;
	/* 这行代码将当前位置(.)增加一个偏移量 INIT_IDMAP_DIR_SIZE。通过这个操作,当前位置前进了指定大小的字节 */
	. += INIT_IDMAP_DIR_SIZE;
	init_idmap_pg_end = .;

	.init.data : {
		INIT_DATA
		INIT_SETUP(16)
		INIT_CALLS
		CON_INITCALL
		INIT_RAM_FS
		*(.init.altinstructions .init.bss)	/* from the EFI stub */
	}
	.exit.data : {
		EXIT_DATA
	}

	PERCPU_SECTION(L1_CACHE_BYTES)
	HYPERVISOR_PERCPU_SECTION

	HYPERVISOR_RELOC_SECTION

	.rela.dyn : ALIGN(8) {
		__rela_start = .;
		*(.rela .rela*)
		__rela_end = .;
	}

	.relr.dyn : ALIGN(8) {
		__relr_start = .;
		*(.relr.dyn)
		__relr_end = .;
	}

	. = ALIGN(SEGMENT_ALIGN);
	__initdata_end = .;
	__init_end = .;

	_data = .;
	_sdata = .;
	RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN)

	/*
	 * Data written with the MMU off but read with the MMU on requires
	 * cache lines to be invalidated, discarding up to a Cache Writeback
	 * Granule (CWG) of data from the cache. Keep the section that
	 * requires this type of maintenance to be in its own Cache Writeback
	 * Granule (CWG) area so the cache maintenance operations don't
	 * interfere with adjacent data.
	 */
	.mmuoff.data.write : ALIGN(SZ_2K) {
		__mmuoff_data_start = .;
		*(.mmuoff.data.write)
	}
	. = ALIGN(SZ_2K);
	.mmuoff.data.read : {
		*(.mmuoff.data.read)
		__mmuoff_data_end = .;
	}

	PECOFF_EDATA_PADDING
	__pecoff_data_rawsize = ABSOLUTE(. - __initdata_begin);
	_edata = .;

	BSS_SECTION(SBSS_ALIGN, 0, 0)

	. = ALIGN(PAGE_SIZE);
	init_pg_dir = .;
	. += INIT_DIR_SIZE;
	init_pg_end = .;

	. = ALIGN(SEGMENT_ALIGN);
	__pecoff_data_size = ABSOLUTE(. - __initdata_begin);
	_end = .;

	STABS_DEBUG
	DWARF_DEBUG
	ELF_DETAILS

	HEAD_SYMBOLS

	/*
	 * Sections that should stay zero sized, which is safer to
	 * explicitly check instead of blindly discarding.
	 */
	.plt : {
		*(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
	}
	ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")

	.data.rel.ro : { *(.data.rel.ro) }
	ASSERT(SIZEOF(.data.rel.ro) == 0, "Unexpected RELRO detected!")
}

#include "image-vars.h"

/*
 * The HYP init code and ID map text can't be longer than a page each. The
 * former is page-aligned, but the latter may not be with 16K or 64K pages, so
 * it should also not cross a page boundary.
 */
ASSERT(__hyp_idmap_text_end - __hyp_idmap_text_start <= PAGE_SIZE,
	"HYP init code too big")
ASSERT(__idmap_text_end - (__idmap_text_start & ~(SZ_4K - 1)) <= SZ_4K,
	"ID map text too big or misaligned")
#ifdef CONFIG_HIBERNATION
ASSERT(__hibernate_exit_text_end - __hibernate_exit_text_start <= SZ_4K,
       "Hibernate exit text is bigger than 4 KiB")
ASSERT(__hibernate_exit_text_start == swsusp_arch_suspend_exit,
       "Hibernate exit text does not start with swsusp_arch_suspend_exit")
#endif
#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
ASSERT((__entry_tramp_text_end - __entry_tramp_text_start) <= 3*PAGE_SIZE,
	"Entry trampoline text too big")
#endif
#ifdef CONFIG_KVM
ASSERT(__hyp_bss_start == __bss_start, "HYP and Host BSS are misaligned")
#endif
/*
 * If padding is applied before .head.text, virt<->phys conversions will fail.
 */
ASSERT(_text == KIMAGE_VADDR, "HEAD is misaligned")

ASSERT(swapper_pg_dir - reserved_pg_dir == RESERVED_SWAPPER_OFFSET,
       "RESERVED_SWAPPER_OFFSET is wrong!")

#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
ASSERT(swapper_pg_dir - tramp_pg_dir == TRAMP_SWAPPER_OFFSET,
       "TRAMP_SWAPPER_OFFSET is wrong!")
#endif

#ifdef CONFIG_KEXEC_CORE
/* kexec relocation code should fit into one KEXEC_CONTROL_PAGE_SIZE */
ASSERT(__relocate_new_kernel_end - __relocate_new_kernel_start <= SZ_4K,
       "kexec relocation code is bigger than 4 KiB")
ASSERT(KEXEC_CONTROL_PAGE_SIZE >= SZ_4K, "KEXEC_CONTROL_PAGE_SIZE is broken")
ASSERT(__relocate_new_kernel_start == arm64_relocate_new_kernel,
       "kexec control page does not start with arm64_relocate_new_kernel")
#endif

通过链接脚本可以确定,程序起始地址为 ENTRY(_text)_text.head.text段的开始。

确定 .head.text 段定义:

1、在 include/linux/init.h 文件中找到如下定义:

#define __HEAD		.section	".head.text","ax"

2、在 arch/arm64/kernel/head.S 文件中找到如下定义:

/*
 * Kernel startup entry point.
 * ---------------------------
 *
 * The requirements are:
 *   MMU = off, D-cache = off, I-cache = on or off,
 *   x0 = physical address to the FDT blob.
 *
 * Note that the callee-saved registers are used for storing variables
 * that are useful before the MMU is enabled. The allocations are described
 * in the entry routines.
 */
	__HEAD

3、总结

在链接脚本中定义代码开始为 .head.text 段,在 include/linux/init.h 中通过宏定义将 .head.text 定义为 __HEAD__HEADarch/arm64/kernel/head.S 中使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 'b\'/lib/ld-linux-aarch64.so.1: no such file or directory\'\'的意思是找不到/lib/ld-linux-aarch64.so.1这个文件或目录。 \'\' ### 回答2: “/lib/ld-linux-aarch64.so.1: no such file or directory”是一个Linux系统下常见的错误提示。这个错误通常表示在运行一个程序时,程序需要的一个动态链接库文件“ld-linux-aarch64.so.1”不存在或无法找到。 在Linux系统中,动态链接库是一组已经编译好的代码,可以供不同程序共享使用。这些动态链接库通常存放在/lib或/usr/lib等位置。当一个可执行文件需要使用某个函数或库时,会自动加载相应的动态链接库。 然而,当系统在运行时无法找到所需的动态链接库文件时,就会发生“/lib/ld-linux-aarch64.so.1: no such file or directory”错误。这可能是由于系统缺少某些必需的库文件,或者由于程序安装位置不正确所导致的。 解决这个错误的方法通常是重新安装程序或者安装缺失的库文件。具体方法如下: 1. 使用命令“locate ld-linux-aarch64.so.1”查找系统中是否存在这个库文件。如果不存在,则需要安装该库文件。 2. 如果该库文件存在,则使用命令“ldd 应用程序名”查看该应用程序所依赖的库文件是否都已存在。 3. 如果仍然无法解决这个错误,则可以尝试更新系统、更新程序或者重新编译程序。 总之,在遇到类似错误时,需要仔细检查缺失的库文件及其依赖库文件是否已经完整安装。如果还无法解决问题,可以尝试重新安装程序或者更新系统等操作。 ### 回答3: 这个错误提示表明在执行程序时出现问题,因为所需的文件'/lib/ld-linux-aarch64.so.1'不存在于指定的路径中。这个文件是运行基于Linux的应用程序所需的链接器文件。 造成这种情况的原因可能是缺失这个文件,或者是链接器的路径设置错误。解决这个问题的方法就是检查系统中是否存在'/lib/ld-linux-aarch64.so.1'文件,如果不存在,需要通过安装相应的软件包来解决。如果文件存在,则可能是由于程序运行时链接路径没有设置正确,需要添加链接路径。 另外,这个错误通常发生在基于ARM架构的设备中。因为ARM架构与x86架构的CPU不同,需要使用不同的链接器文件,因此需要相应的链接器文件才能正常运行程序。 总之,这个错误提示需要注意操作系统、架构以及程序链接器文件的设置,以确保程序能够正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值