glibc是什么_runtime是什么

Runtime是指应用程序的执行环境,负责准备运行条件,如异常处理、内存管理和启动流程。C Runtime(crt0)是最小的运行时环境,包含启动过程的多个步骤。运行时库与标准库不同,标准库提供程序员可用的功能,而运行时库是程序运行的必要部分,即使不使用标准库,程序也需要运行时库来执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是runtime

在windows上安装或执行程序,都有机会遇到词汇——runtime。

  • CRT(C runtime library)

  • Microsoft Access 2016 Runtime

  • microsoft visual c++ runtime library

  • C Runtime

  • Visual C++ 2008 Runtime

  • .NET Common Language Runtime

runtime究竟是什么,首先runtime在英文里是合成单词,无论是英文还是中文都容易在文字层面被误解,中文直译“运行时”,中文的断句容易引起歧义,究竟是“运行、时”还是“运行时”傻傻分不清。

为了准确描述runtime的实际意思,我认为runtime换成execution environment理解起来更容易:即位应用程序的执行准备运行环境。

运行时库是在编译时使用的特殊库,用于在计算机程序的执行中实现内置于编程语言中的功能,包括:输入、输出、内存管理。

crt0

比如C语言需要的最小runtime叫做crt0(C runtime)。“crt”代表 “c runtime”,“0”代表“最基本、最开始”。

crt0应该包含如下7个步骤。

  • 异常向量配置

  • _start函数和stack初始化

  • cache 初始化

  • 清除BSS

  • 构造函数和析构函数处理

  • C初始化功能

  • 调用main入口

这个crt0的结构看起来是不是很熟悉?没错,在u-boot源码启动代码看到类似结构。

.text.globl _start_start: # _start is the entry point known to the linker    xor %ebp, %ebp            # effectively RBP := 0, mark the end of stack frames    mov (%rsp), %edi          # get argc from the stack (implicitly zero-extended to 64-bit)    lea 8(%rsp), %rsi         # take the address of argv from the stack    lea 16(%rsp,%rdi,8), %rdx # take the address of envp from the stack    xor %eax, %eax            # per ABI and compatibility with icc    call main                 # %edi, %rsi, %rdx are the three args (of which first two are C standard) to main    mov %eax, %edi    # transfer the return of main to the first argument of _exit    xor %eax, %eax    # per ABI and compatibility with icc    call _exit        # terminate the program

crt0.S编译生成crt0.o,今后gcc编译的所有应用程序前都加上这段内容,既然有crt0,那么再发挥想象力,是不是还会有crt1什么的呢,全盘搜索看到若干crt前缀的*.o文件,这些crt*.o文件合并起来被称做 "runtime library"

运行时库和标准库区别

运行时库(runtime library)与标准库(standard library)不是一个东西。

标准库和运行库之间有一个非常重要的区别。尽管标准库定义了程序员可以使用的功能,但不是编程语言的规范的一部分,至少在C语言中不是,运行时库却时程序运行所必需的部分。

举个例子,printf()是C标准库的一部分,程序的启动是在运行时库实现的,启动过程对程序员不可见,因此,你编写的程序可以不使用标准库,但始终需要运行时库,否则无法运行。老实说,在操作系统上编写不使用标准库的应用程序几乎无实际意义,那样的程序没有访问外设的方法、屏幕上不会输出令人印象深刻的结果。在裸机上情况就不一样了,访问外设不需要系统调用,没有系统的权限隔离,外设的寄存器也有读写权限。

简单编写一个只有main

int main(int argc, char **argv){  return 0;}

编译后看看符号表:

  • gcc a.c

  • readelf -s a.out

很多不知那来的函数符号都来源与crt*.o,如register_tm_clones源于crtbegin.o;__data_start、__libc_start_main源于crt1.o

31:  0 FILE    LOCAL  DEFAULT  ABS crtstuff.c32:  0 FUNC    LOCAL  DEFAULT   13 deregister_tm_clones33:  0 FUNC    LOCAL  DEFAULT   13 register_tm_clones34:  0 FUNC    LOCAL  DEFAULT   13 __do_global_dtors_aux35:  1 OBJECT  LOCAL  DEFAULT   24 completed.732536:  0 OBJECT  LOCAL  DEFAULT   19 __do_global_dtors_aux_fin37:  0 FUNC    LOCAL  DEFAULT   13 frame_dummy38:  0 OBJECT  LOCAL  DEFAULT   18 __frame_dummy_init_array_39:  0 FILE    LOCAL  DEFAULT  ABS a.c40:  0 FILE    LOCAL  DEFAULT  ABS crtstuff.c41:  0 OBJECT  LOCAL  DEFAULT   17 __FRAME_END__42:  0 FILE    LOCAL  DEFAULT  ABS 43:  0 NOTYPE  LOCAL  DEFAULT   18 __init_array_end44:  0 OBJECT  LOCAL  DEFAULT   20 _DYNAMIC45:  0 NOTYPE  LOCAL  DEFAULT   18 __init_array_start46:  0 NOTYPE  LOCAL  DEFAULT   16 __GNU_EH_FRAME_HDR47:  0 OBJECT  LOCAL  DEFAULT   22 _GLOBAL_OFFSET_TABLE_48:  0 FUNC    LOCAL  DEFAULT   10 _init49:  1 FUNC    GLOBAL DEFAULT   13 __libc_csu_fini50:  0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab51:  0 NOTYPE  WEAK   DEFAULT   23 data_start52:  0 NOTYPE  GLOBAL DEFAULT   23 _edata53:  0 FUNC    GLOBAL HIDDEN    14 _fini54:  0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_55:  0 NOTYPE  GLOBAL DEFAULT   23 __data_start56:  0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__57:  0 OBJECT  GLOBAL HIDDEN    23 __dso_handle58:  4 OBJECT  GLOBAL DEFAULT   15 _IO_stdin_used59: 93 FUNC    GLOBAL DEFAULT   13 __libc_csu_init60:  0 NOTYPE  GLOBAL DEFAULT   24 _end61: 43 FUNC    GLOBAL DEFAULT   13 _start62:  0 NOTYPE  GLOBAL DEFAULT   24 __bss_start63: 18 FUNC    GLOBAL DEFAULT   13 main64:  0 OBJECT  GLOBAL HIDDEN    23 __TMC_END__65:  0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable66:  0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@@GLIBC_2.2
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值