nykek5i 发表于 2024-10-26 18:37:51

可执行程序内部到底什么样,U-Boot链接文件


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>一个程序而言,<span style="color: black;">她们</span>内部的结构、<span style="color: black;">构成</span><span style="color: black;">一般</span>是不可见的,<span style="color: black;">然则</span>不可见并不<span style="color: black;">寓意</span>着其内部是杂乱无章的排布,仅仅是众多的二进制数据拼凑而成。一份源代码生成<span style="color: black;">最后</span>的可执行文件来驱动<span style="color: black;">咱们</span>的<span style="color: black;">设备</span>正常工作,中间必经的两个环节是编译和链接。编译的过程好比是厨师将菜炒好,而链接的过程好比是将菜进行摆盘,而端上桌的饭菜<span style="color: black;">便是</span>我们<span style="color: black;">最后</span>的可执行程序。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/04cb85128a8c41fbb7dcb3d7865686ca~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=%2FASHfqwU30NVyOwIart%2BX9Ike30%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">链接的<span style="color: black;">详细</span>工作由链接器完成,链接器<span style="color: black;">按照</span>链接脚本的内容,<span style="color: black;">根据</span><span style="color: black;">需求</span>将中间件组装成<span style="color: black;">最后</span>的可执行文件。链接脚本完成的工作<span style="color: black;">重点</span><span style="color: black;">包含</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 输出是什么</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 输入是什么</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 需要<span style="color: black;">那些</span>库以及库所在的位置</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. 内存分布<span style="color: black;">位置</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5. <span style="color: black;">起步</span>代码<span style="color: black;">关联</span>内容</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基于ARM的SoC U-Boot链接lds文件,<span style="color: black;">重点</span>包括:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 指定<span style="color: black;">第1</span>条执行的指令</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 描述中间件的链接规则</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">链接器</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">源码经编译之后的状态是<span style="color: black;">各样</span>的.o文件,如下图所示:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/6c773d2f126c49a3898fe4e567d3fcb6~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=t0OCupgB7cvxxdQoMqUuH41%2BZ5A%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">各样</span>.o文件</p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">而<span style="color: black;">每一个</span>中间.o文件都会<span style="color: black;">包括</span>TEXT、DATA、BSS部分内容:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/5fc8181be2994376af86a4fe5f87cded~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=HX%2FfnNMeYA7gRAeCtT%2FxX2BhrA4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">链接的过程大致如下图所示,将<span style="color: black;">每一个</span>.o文件<span style="color: black;">根据</span>下面的规则,生成一个<span style="color: black;">最后</span>的u-boot文件。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/acdae62274d04abb881d5f15b49f83bd~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=neeAtolD7SvpETSz4e%2FHEA%2Fyo6c%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图中的TEXT、DATA、BSS段的含义分别是:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. TEXT,存放程序代码,<span style="color: black;">亦</span><span style="color: black;">便是</span><span style="color: black;">包括</span>编程语言<span style="color: black;">规律</span>部分的内容</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. DATA,存放程序中所<span style="color: black;">运用</span>到的数据</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. BSS,存放程序中所<span style="color: black;">运用</span>到的未初始化的数据</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">U-Boot的链接脚本</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>ARM架构的SoC而言,<span style="color: black;">她们</span>的链接脚本<span style="color: black;">一般</span>在cpu代码文件夹下,这个<span style="color: black;">亦</span>很容易理解,<span style="color: black;">由于</span>链接器毕竟是和架构强<span style="color: black;">关联</span>的,每种架构<span style="color: black;">第1</span>条执行的指令、<span style="color: black;">能够</span>支持哪种<span style="color: black;">起步</span>设备等等,这些都是事先定义好的。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/af72c717e4e445cfa9a85a6feea8f6b0~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=OA94J9GkYdIobgIWUQJAF9LsMgg%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">打开上图所示路径中的链接文件,<span style="color: black;">能够</span><span style="color: black;">发掘</span>链接文件内的代码量并不是<span style="color: black;">非常多</span>,仅仅200多行,<span style="color: black;">因此</span><span style="color: black;">认识</span>U-Boot的可执行程序的内部<span style="color: black;">形成</span>并不是什么难事。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">u-boot.lds文件仅由一个SECTIONS<span style="color: black;">形成</span>,定义了.o文件各个段的<span style="color: black;">最后</span>组织方式、可执行程序的入口点、可执行程序的格式等。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/9a72c6198c954a31aea60fd7e850e1df~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=g7byYfS9ZqQuQdbXqV3q6hSn%2FF0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面13-1行分别定义了输出文件数据为小端、支持ARM、elf格式;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">支持ARM架构;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">入口符号为__start,<span style="color: black;">亦</span><span style="color: black;">便是</span><span style="color: black;">第1</span>条执行的指令是__start,该符号执行完紧接着<span style="color: black;">转</span>到reset入口。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">LDS实战</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>修改<span style="color: black;">初始</span>text段的基址,来查看lds文件<span style="color: black;">怎样</span>决定执行程序内部各段的排布<span style="color: black;">状况</span>。text的基<span style="color: black;">位置</span>在顶层Makefile文件的808行<span style="color: black;">运用</span>,定义在include/configs/xxx.h中。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>直接修改Makefile中的链接<span style="color: black;">位置</span>,如下所示:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/559eda5f7f6b446a9e199d9b7e073dde~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=AIRUf%2BXA%2FsA9fZkE8Itb2EcemBs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">修改完成后,查看u-boot.sym中各个段以及各符号的排布<span style="color: black;">状况</span>之后,<span style="color: black;">咱们</span><span style="color: black;">发掘</span>修改成功了。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/5f272314c4c84e8bb783886e76543e0a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=TNM89y1u4fLzu0Ebk6hLmU0iBF8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">修改include/configs/xxx.h中的CONFIG_SYS_TEXT_ADDR</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/7cdb563b969e4f28836ca56e32f3a250~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=OPiWsrakAZkVzEv9Sl0OJ1fWg%2F0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">修改后再次编译,查看u-boot.sym</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/a225c1de064b4dff9814679cc2e59e56~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1729478132&amp;x-signature=eedMlR1ZX3UpJnwY96RBo%2F80S8g%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">若<span style="color: black;">期盼</span>U-Boot从<span style="color: black;">区别</span>的<span style="color: black;">起步</span>介质加载系统,<span style="color: black;">能够</span>采用第二种方式实现。</p>




wrjc1hod 发表于 1 小时前

祝福你、祝你幸福、早日实现等。
页: [1]
查看完整版本: 可执行程序内部到底什么样,U-Boot链接文件