当前位置: 首页 > 面试题库 >

可执行C程序中的段

商同
2023-03-14
问题内容

我正在阅读有关章节的内容。似乎您可以按以下方式列出节和段之间的映射。

$ readelf -l test

Elf file type is EXEC (Executable file)
Entry point 0x8048330
There are 9 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x08048034 0x08048034 0x00120 0x00120 R E 0x4
  INTERP         0x000154 0x08048154 0x08048154 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.2]
  LOAD           0x000000 0x08048000 0x08048000 0x0065c 0x0065c R E 0x1000
  LOAD           0x000f14 0x08049f14 0x08049f14 0x00104 0x00110 RW  0x1000
  DYNAMIC        0x000f28 0x08049f28 0x08049f28 0x000c8 0x000c8 RW  0x4
  NOTE           0x000168 0x08048168 0x08048168 0x00044 0x00044 R   0x4
  GNU_EH_FRAME   0x000564 0x08048564 0x08048564 0x00034 0x00034 R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4
  GNU_RELRO      0x000f14 0x08049f14 0x08049f14 0x000ec 0x000ec R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .ctors .dtors .jcr .dynamic .got

我的问题,

  1. 我不明白程序头是什么意思?它们与细分有何关系?
  2. 段到段的映射很清楚。但是有人可以命名吗?我只看到数字。我确定了代码段(03),数据段(02)和堆栈(07)。

问题答案:

了解readelf它的输出将帮助您了解ELF文件的格式。请参考本文件。

就了解如何解释readelf此链接的输出而言可能会有帮助。

关于您的问题2,此链接描述了细分。在该文档中搜索“各种节包含程序和控制信息:”以找到描述段名的区域。

该文档描述了以下部分:

各个部分包含程序和控制信息:

   .bss      This section holds uninitialized data that contributes to the
             program's memory image.  By definition, the system initializes the
             data with zeros when the program begins to run.  This section is of
             type SHT_NOBITS.  The attribute types are SHF_ALLOC and SHF_WRITE.

   .comment  This section holds version control information.  This section is of
             type SHT_PROGBITS.  No attribute types are used.

   .ctors    This section holds initialized pointers to the C++ constructor
             functions.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .data     This section holds initialized data that contribute to the program's
             memory image.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .data1    This section holds initialized data that contribute to the program's
             memory image.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .debug    This section holds information for symbolic debugging.  The contents
             are unspecified.  This section is of type SHT_PROGBITS.  No
             attribute types are used.

   .dtors    This section holds initialized pointers to the C++ destructor
             functions.  This section is of type SHT_PROGBITS.  The attribute
             types are SHF_ALLOC and SHF_WRITE.

   .dynamic  This section holds dynamic linking information.  The section's
             attributes will include the SHF_ALLOC bit.  Whether the SHF_WRITE
             bit is set is processor-specific.  This section is of type
             SHT_DYNAMIC.  See the attributes above.

   .dynstr   This section holds strings needed for dynamic linking, most commonly
             the strings that represent the names associated with symbol table
             entries.  This section is of type SHT_STRTAB.  The attribute type
             used is SHF_ALLOC.

   .dynsym   This section holds the dynamic linking symbol table.  This section
             is of type SHT_DYNSYM.  The attribute used is SHF_ALLOC.

   .fini     This section holds executable instructions that contribute to the
             process termination code.  When a program exits normally the system
             arranges to execute the code in this section.  This section is of
             type SHT_PROGBITS.  The attributes used are SHF_ALLOC and
             SHF_EXECINSTR.

   .gnu.version
             This section holds the version symbol table, an array of ElfN_Half
             elements.  This section is of type SHT_GNU_versym.  The attribute
             type used is SHF_ALLOC.

   .gnu.version_d
             This section holds the version symbol definitions, a table of
             ElfN_Verdef structures.  This section is of type SHT_GNU_verdef.
             The attribute type used is SHF_ALLOC.

   .gnu.version_r
             This section holds the version symbol needed elements, a table of
             ElfN_Verneed structures.  This section is of type SHT_GNU_versym.
             The attribute type used is SHF_ALLOC.

   .got      This section holds the global offset table.  This section is of type
             SHT_PROGBITS.  The attributes are processor specific.

   .hash     This section holds a symbol hash table.  This section is of type
             SHT_HASH.  The attribute used is SHF_ALLOC.

   .init     This section holds executable instructions that contribute to the
             process initialization code.  When a program starts to run the
             system arranges to execute the code in this section before calling
             the main program entry point.  This section is of type SHT_PROGBITS.
             The attributes used are SHF_ALLOC and SHF_EXECINSTR.

   .interp   This section holds the pathname of a program interpreter.  If the
             file has a loadable segment that includes the section, the section's
             attributes will include the SHF_ALLOC bit.  Otherwise, that bit will
             be off.  This section is of type SHT_PROGBITS.

   .line     This section holds line number information for symbolic debugging,
             which describes the correspondence between the program source and
             the machine code.  The contents are unspecified.  This section is of
             type SHT_PROGBITS.  No attribute types are used.

   .note     This section holds information in the "Note Section" format.  This
             section is of type SHT_NOTE.  No attribute types are used.  OpenBSD
             native executables usually contain a .note.openbsd.ident section to
             identify themselves, for the kernel to bypass any compatibility ELF
             binary emulation tests when loading the file.

   .note.GNU-stack
             This section is used in Linux object files for declaring stack
             attributes.  This section is of type SHT_PROGBITS.  The only
             attribute used is SHF_EXECINSTR.  This indicates to the GNU linker
             that the object file requires an executable stack.

   .plt      This section holds the procedure linkage table.  This section is of
             type SHT_PROGBITS.  The attributes are processor specific.

   .relNAME  This section holds relocation information as described below.  If
             the file has a loadable segment that includes relocation, the
             section's attributes will include the SHF_ALLOC bit.  Otherwise the
             bit will be off.  By convention, "NAME" is supplied by the section
             to which the relocations apply.  Thus a relocation section for .text
             normally would have the name .rel.text.  This section is of type
             SHT_REL.

   .relaNAME This section holds relocation information as described below.  If
             the file has a loadable segment that includes relocation, the
             section's attributes will include the SHF_ALLOC bit.  Otherwise the
             bit will be off.  By convention, "NAME" is supplied by the section
             to which the relocations apply.  Thus a relocation section for .text
             normally would have the name .rela.text.  This section is of type
             SHT_RELA.

   .rodata   This section holds read-only data that typically contributes to a
             nonwritable segment in the process image.  This section is of type
             SHT_PROGBITS.  The attribute used is SHF_ALLOC.

   .rodata1  This section holds read-only data that typically contributes to a
             nonwritable segment in the process image.  This section is of type
             SHT_PROGBITS.  The attribute used is SHF_ALLOC.

   .shstrtab This section holds section names.  This section is of type
             SHT_STRTAB.  No attribute types are used.

   .strtab   This section holds strings, most commonly the strings that represent
             the names associated with symbol table entries.  If the file has a
             loadable segment that includes the symbol string table, the
             section's attributes will include the SHF_ALLOC bit.  Otherwise the
             bit will be off.  This section is of type SHT_STRTAB.

   .symtab   This section holds a symbol table.  If the file has a loadable
             segment that includes the symbol table, the section's attributes
             will include the SHF_ALLOC bit.  Otherwise the bit will be off.
             This section is of type SHT_SYMTAB.

   .text     This section holds the "text", or executable instructions, of a
             program.  This section is of type SHT_PROGBITS.  The attributes used
             are SHF_ALLOC and SHF_EXECINSTR.


 类似资料:
  • 问题内容: 我应该如何从我的程序中运行另一个程序?我需要能够将数据写入启动的程序中(并可能从中读取) 我不确定这是否是标准的C函数。我需要应该在Linux下工作的解决方案。 问题答案: 您要使用。它为您提供了一个单向管道,您可以使用该管道访问程序的stdin和stdout。 popen是现代unix和类似unix的操作系统的标准配置,其中Linux是其中之一:-) 类型 在终端上阅读有关它的更多信

  • 我有以下片段: 其中生成: 如果我从本(§无序执行)手册中正确理解:以上代码转换为<代码>((a b)c)d 。为了计算这个,CPU必须等待第一个括号和第二个括号,以此类推。在这里,我们看到LEA位于中间,这意味着它们不能并行执行(如果我理解正确的话)。因此,作者建议: 在“独立”对上写括号: 但这会生成相同的程序集: 相反,这是GCC(O2)为C生成的代码: 以下是输出: 我是否正确理解了手册?

  • 问题内容: 想知道是否有人知道一种在运行时从C#代码执行Java命令行程序的好方法吗? 它与执行本机.EXE文件相同吗? 它会同步还是异步运行(这意味着我可能必须等待线程完成才能找到结果) 具体来说,我想从服务器端的Web应用程序中调用一个小工具(恰好用Java编写),以对文本文件进行一些处理。我想等待它完成,因为在Java程序完成对文本文件的处理之后,我想获取处理后的文本,并在C#应用程序中使用

  • 我有我的chromedriver和chromedriver。项目文件夹中的exe权限。我正在用这段代码创建驱动程序的实例。 在本地windows上也可以(如果我们将“chromedriver”更改为“chromedriver.exe”),但当我在CentOS上运行它时,会收到错误消息:“驱动程序不可执行”。

  • 该程序的输出: 是: 为什么当 开始时 不是 1?

  • 这部分内容只对Linux/Unix用户适用,不过Windows用户可能也对程序的第一行比较好奇。首先我们需要通过chmod命令,给程序可执行的许可,然后 运行 程序。 $ chmod a+x helloworld.py $ ./helloworld.py Hello World chmod命令用来 改变 文件的 模式 ,给系统中所有用户这个源文件的执行许可。然后我们可以直接通过指定源文件的位置来执