2012-08-09 22 views
8

के भीतर सेगमेंट मैं अनुभागों और सेगमेंट के बारे में पढ़ रहा था। ऐसा लगता है कि आप नीचे दिए गए अनुभागों और सेगमेंट के बीच मैपिंग सूचीबद्ध कर सकते हैं।एक निष्पादन योग्य सी प्रोग्राम

$ 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) की पहचान की।
+0

यह वास्तव में एक प्रोग्रामिंग सवाल नहीं है। क्या यह कहीं और "यूनिक्स और लिनक्स" जैसा है .. – John

उत्तर

10

readelf के आउटपुट को समझने के लिए यह ELF फ़ाइल के प्रारूप को समझने में आपकी सहायता करेगा। कृपया this document.

readelf के आउटपुट की व्याख्या करने के तरीके को समझने के लिए यह link सहायता हो सकती है।

आपके प्रश्न 2, this link सेगमेंट का वर्णन करता है। उस दस्तावेज़ में "विभिन्न अनुभागों को प्रोग्राम और नियंत्रण जानकारी रखने के लिए खोजें:" उस क्षेत्र को ढूंढने के लिए जहां सेगमेंट नामों का वर्णन किया गया है। इस प्रकार

उस दस्तावेज़ क्षेत्रों में बताता है:

विभिन्न वर्गों कार्यक्रम पकड़ और नियंत्रण की जानकारी:

.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. 
+0

मैंने लिंक के माध्यम से नहीं जाना है, लेकिन एक नौसिखिया के लिए आपकी व्याख्या से यह खंडों और खंडों को एक और –

+0

सेगमेंट और सेक्शन एक ही चीज़ नहीं हैं। अनुभाग सामान्य रूप से स्थिर लिंकर (लिंक संपादक) द्वारा उपयोग किए जाते हैं और सेगमेंट मुख्य रूप से गतिशील लोडर द्वारा उपयोग किए जाते हैं। यह देखने के लिए कि केवल एक सेगमेंट में कई अनुभागों को समूहीकृत किया जा सकता है, केवल 'readelf -l बाइनरी' का उपयोग करें। – JohnTortugo

2

ईएलएफ बाइनरी में प्रोग्राम हेडर बताते हैं कि बाइनरी कैसे चलनी चाहिए। दिलचस्प भाग लोड हेडर हैं जो स्मृति में विभिन्न स्थानों में बाइनरी का हिस्सा लोड करते हैं। बाइनरी में लोड हेडर की लगभग मनमानी संख्या हो सकती है, लेकिन आम तौर पर लिंकर सबकुछ केवल पढ़ने और निष्पादन योग्य रखता है और सब कुछ एक दूसरे में पढ़/लिखता है। ऐसे ऑपरेटिंग सिस्टम हैं जिनमें थोड़ी-थोड़ी बढ़ी हुई सुरक्षा के लिए केवल पढ़ने वाले डेटा लोड हेडर, रीड-राइट डेटा और केवल-पढ़ने योग्य निष्पादन योग्य कोड होंगे।

यहां सेगमेंट मेमोरी में विभिन्न स्थानों में लोड बाइनरी के कुछ हिस्सों का मतलब है। तो मूल रूप से विभिन्न लोड हेडर।

अनुभाग यह है कि लिंक के दौरान डेटा कैसे व्यवस्थित किया गया था। विभिन्न कारणों से आप डेटा/कोड की तुलना में चीजों को व्यवस्थित करने के लिए बेहतर ग्रैन्युलरिटी चाहते हैं। कुछ डेटा केवल पढ़ने के लिए है, इसे आपके उदाहरण में ".rodata" में रखा गया है। कोड ".text" में है, प्रारंभिक डेटा ".डेटा" में है जबकि प्रोग्राम प्रारंभ पर शून्य किए गए चर में डेटा ".bss" में हैं।

"सेगमेंट मैपिंग सेक्शन" आपको बताता है कि कौन से सेक्शन खंड (विभिन्न लोड हेडर) हैं। तो ".text" और ".rodata" पहले लोड हेडर (तीसरा प्रोग्राम हेडर) में हैं और ".डेटा" दूसरे लोड हेडर (चौथा प्रोग्राम हेडर) में है।

ढेर कुछ ऐसा है जो ऑपरेटिंग सिस्टम आपको निष्पादन पर देता है और इसे ईएलएफ बाइनरी द्वारा वर्णित नहीं किया जाता है।

संबंधित मुद्दे