objcopy:膨胀的二进制输出文件

如何解决objcopy:膨胀的二进制输出文件

在处理引导程序项目时,我注意到生成的二进制文件大于原始ELF文件中每个部分的大小之和。

通过ld链接引导程序映像后,ELF文件的结构如下:

$ readelf -S elfboot.elf 
There are 10 section headers,starting at offset 0xa708:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00007e00 000e00 004bc7 00  AX  0   0 16
  [ 2] .rodata           PROGBITS        0000c9d0 0059d0 000638 00   A  0   0 32
  [ 3] .initcalls        PROGBITS        0000ec20 007c20 00000c 00  WA  0   0  4
  [ 4] .exitcalls        PROGBITS        0000ec30 007c30 00000c 00  WA  0   0  4
  [ 5] .data             PROGBITS        0000ec40 007c40 0001e0 00  WA  0   0 32
  [ 6] .bss              NOBITS          0000ee20 007e20 00025c 00  WA  0   0 32
  [ 7] .symtab           SYMTAB          00000000 007e20 0016e0 10      8 166  4
  [ 8] .strtab           STRTAB          00000000 009500 0011bb 00      0   0  1
  [ 9] .shstrtab         STRTAB          00000000 00a6bb 00004a 00      0   0  1
Key to Flags:
  W (write),A (alloc),X (execute),M (merge),S (strings),I (info),L (link order),O (extra OS processing required),G (group),T (TLS),C (compressed),x (unknown),o (OS specific),E (exclude),p (processor specific)

我注意到.rodata.initcalls节之间有很大的差距(〜7KB)。我还检查了每个部分的内容:

$ objdump -s elfboot.elf
[...]
Contents of section .rodata:
[...]
 cfc0 23cf0000 80000000 2fcf0000 00010000  #......./.......
 cfd0 3bcf0000 00020000 47cf0000 00040000  ;.......G.......
 cfe0 54cf0000 00080000 61cf0000 00100000  T.......a.......
 cff0 6ecf0000 00200000 7bcf0000 00400000  n.... ..{....@..
 d000 89cf0000 00800000                    ........        
Contents of section .initcalls:
 ec20 02b50000 6db50000 feac0000           ....m.......    
Contents of section .exitcalls:
[...]

我的链接脚本告诉我们将每个节对齐在16字节(0x10)的边界处。之后

objcopy -O binary elfboot.elf elfboot.bin

生成的二进制文件在偏移量0x5200 (0xd000 - 0x7e00)到偏移量0x6e20 (0xec20 - 0x7e00)处都包含一大块零填充字节。现在,我知道零填充字节来自何处,如何删除它们?作为参考,我添加了链接器步骤的详细输出,其中包括所使用的链接器脚本:

  i686-elfboot-gcc -o elfboot.elf -T elfboot.ld    ./src/arch/x86/bootstrap.o ./src/arch/x86/a20.o ./src/arch/x86/bda.o ./src/arch/x86/bios.o ./src/arch/x86/copy.o ./src/arch/x86/e820.o ./src/arch/x86/entry.o ./src/arch/x86/idt.o ./src/arch/x86/realmode_jmp.o ./src/arch/x86/setup.o ./src/arch/x86/opmode.o ./src/arch/x86/pic.o ./src/arch/x86/ptrace.o ./src/arch/x86/video.o ./src/core/bdev.o ./src/core/cdev.o ./src/core/elf.o ./src/core/input.o ./src/core/interrupt.o ./src/core/loader.o ./src/core/main.o ./src/core/module.o ./src/core/pci.o ./src/core/printf.o ./src/core/string.o ./src/core/symbol.o ./src/crypto/crc32.o     ./src/drivers/ide/ide.o  ./src/fs/fs.o ./src/fs/file.o ./src/fs/super.o ./src/fs/ramfs/ramfs.o ./src/fs/isofs/isofs.o  ./src/lib/ata/libata.o ./src/lib/tmg/libtmg.o  ./src/mm/memblock.o ./src/mm/page_alloc.o ./src/mm/slub.o ./src/mm/util.o -O2 -Wl,--verbose -nostdlib -lgcc
GNU ld (GNU Binutils) 2.31.1
  Supported emulations:
   elf_i386
   elf_iamcu
opened script file elfboot.ld
using external linker script:
==================================================
OUTPUT_FORMAT(elf32-i386)
ENTRY(_arch_start)

SECTIONS
{
    /*
     * The boot stack starts at 0x6000 and grows towards lower addresses.
     * The stack is designed to be only one page in size,which should be
     * sufficient for the bootstrap stage.
     */

    . = 0x6000;
    __stack_start = .;

    /*
     * Buffer for reading files from the boot device. Usually boot devices
     * are designed to read data in chunks of 0x200 (512) or 0x800 (2048)
     * bytes. The buffer is large enough to read 4 512 or 2 2048 chunks of
     * data from the disk.
     */

    __buffer_start = .;

    . = 0x7000;
    __buffer_end = .;

    /*
     * Actual bootstrap stage code and data.
     */

    . = 0x7E00;
    __bootstrap_start = .;

    .text ALIGN(0x10) : {
        __text_start = .;
        *(.text*)
        __text_end = .;
    }

    .rodata ALIGN(0x10) : {
        __rodata_start = .;
        *(.rodata*)
        __rodata_end = .;
    }

    /*
     * This section is dedicated to all built-in modules. If a module will
     * be included in the elfboot binary,the module_init function pointer
     * of that module is placed in this section.
     *
     * Make sure to initialize built-in filesystems first as we need it to
     * setup the root file system node.
     */

    .initcalls ALIGN(0x10) : {
        __initcalls_vfs_start = .;

        /* Built-in file systems */
        *(.initcalls_vfs*)

        __initcalls_vfs_end = .;
        __initcalls_dev_start = .;

        /* Built-in devices */
        *(.initcalls_dev*)

        __initcalls_dev_end = .;
        __initcalls_start = .;

        /* Built-in modules */
        *(.initcalls*)

        __initcalls_end = .;
    }

    .exitcalls ALIGN(0x10) : {
        __exitcalls_start = .;
        *(.exitcalls*)
        __exitcalls_end = .;
    }

    .data ALIGN(0x10) : {
        __data_start = .;
        *(.data*)
        __data_end = .;
    }

    .bss ALIGN(0x10) : {
        __bss_start = .;
        *(.bss*)
        __bss_end = .;
    }

    __bootstrap_end = .;
}

==================================================
attempt to open ./src/arch/x86/bootstrap.o succeeded
./src/arch/x86/bootstrap.o
attempt to open ./src/arch/x86/a20.o succeeded
./src/arch/x86/a20.o
attempt to open ./src/arch/x86/bda.o succeeded
./src/arch/x86/bda.o
attempt to open ./src/arch/x86/bios.o succeeded
./src/arch/x86/bios.o
attempt to open ./src/arch/x86/copy.o succeeded
./src/arch/x86/copy.o
attempt to open ./src/arch/x86/e820.o succeeded
./src/arch/x86/e820.o
attempt to open ./src/arch/x86/entry.o succeeded
./src/arch/x86/entry.o
attempt to open ./src/arch/x86/idt.o succeeded
./src/arch/x86/idt.o
attempt to open ./src/arch/x86/realmode_jmp.o succeeded
./src/arch/x86/realmode_jmp.o
attempt to open ./src/arch/x86/setup.o succeeded
./src/arch/x86/setup.o
attempt to open ./src/arch/x86/opmode.o succeeded
./src/arch/x86/opmode.o
attempt to open ./src/arch/x86/pic.o succeeded
./src/arch/x86/pic.o
attempt to open ./src/arch/x86/ptrace.o succeeded
./src/arch/x86/ptrace.o
attempt to open ./src/arch/x86/video.o succeeded
./src/arch/x86/video.o
attempt to open ./src/core/bdev.o succeeded
./src/core/bdev.o
attempt to open ./src/core/cdev.o succeeded
./src/core/cdev.o
attempt to open ./src/core/elf.o succeeded
./src/core/elf.o
attempt to open ./src/core/input.o succeeded
./src/core/input.o
attempt to open ./src/core/interrupt.o succeeded
./src/core/interrupt.o
attempt to open ./src/core/loader.o succeeded
./src/core/loader.o
attempt to open ./src/core/main.o succeeded
./src/core/main.o
attempt to open ./src/core/module.o succeeded
./src/core/module.o
attempt to open ./src/core/pci.o succeeded
./src/core/pci.o
attempt to open ./src/core/printf.o succeeded
./src/core/printf.o
attempt to open ./src/core/string.o succeeded
./src/core/string.o
attempt to open ./src/core/symbol.o succeeded
./src/core/symbol.o
attempt to open ./src/crypto/crc32.o succeeded
./src/crypto/crc32.o
attempt to open ./src/drivers/ide/ide.o succeeded
./src/drivers/ide/ide.o
attempt to open ./src/fs/fs.o succeeded
./src/fs/fs.o
attempt to open ./src/fs/file.o succeeded
./src/fs/file.o
attempt to open ./src/fs/super.o succeeded
./src/fs/super.o
attempt to open ./src/fs/ramfs/ramfs.o succeeded
./src/fs/ramfs/ramfs.o
attempt to open ./src/fs/isofs/isofs.o succeeded
./src/fs/isofs/isofs.o
attempt to open ./src/lib/ata/libata.o succeeded
./src/lib/ata/libata.o
attempt to open ./src/lib/tmg/libtmg.o succeeded
./src/lib/tmg/libtmg.o
attempt to open ./src/mm/memblock.o succeeded
./src/mm/memblock.o
attempt to open ./src/mm/page_alloc.o succeeded
./src/mm/page_alloc.o
attempt to open ./src/mm/slub.o succeeded
./src/mm/slub.o
attempt to open ./src/mm/util.o succeeded
./src/mm/util.o
attempt to open /home/croemheld/Repositories/elfboot/elfboot-toolchain/lib/gcc/i686-elfboot/8.2.0/libgcc.so failed
attempt to open /home/croemheld/Repositories/elfboot/elfboot-toolchain/lib/gcc/i686-elfboot/8.2.0/libgcc.a succeeded
(/home/croemheld/Repositories/elfboot/elfboot-toolchain/lib/gcc/i686-elfboot/8.2.0/libgcc.a)_umoddi3.o
(/home/croemheld/Repositories/elfboot/elfboot-toolchain/lib/gcc/i686-elfboot/8.2.0/libgcc.a)_udivmoddi4.o

请注意,由于第一阶段的引导加载程序(引导扇区)仅以与引导设备指定的偏移量加载整个文件,因此无法将这些段分别加载到内存中。为了减小第二阶段引导加载程序映像的大小,我想尝试在链接时或如果可能的话,在链接后(objcopy,...)消除间隙。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-