0

I am trying to understand the contents of an ELF relocatable object (*.o file), which appears to be a Fortran file compiled to SPARC object code. I am looking at the sections below, which all look fine. Note the .data and .bss sized 0xB4 and 0x12C respectively.

                   sh_type: SHT_NULL     sh_flags:      sh_offset: 00000000 sh_size: 00000000 sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000000 sh_entsize: 00000000
.shstrtab          sh_type: SHT_STRTAB   sh_flags:      sh_offset: 00000034 sh_size: 00000052 sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000001 sh_entsize: 00000000
.text              sh_type: SHT_PROGBITS sh_flags: xa   sh_offset: 00000088 sh_size: 00002154 sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000008 sh_entsize: 00000000
.data              sh_type: SHT_PROGBITS sh_flags:  aw  sh_offset: 000021E0 sh_size: 000000B4 sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000008 sh_entsize: 00000000
.bss               sh_type: SHT_NOBITS   sh_flags:  aw  sh_offset: 00002298 sh_size: 0000012C sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000008 sh_entsize: 00000000
.stab.index        sh_type: SHT_PROGBITS sh_flags:      sh_offset: 00002298 sh_size: 00000024 sh_link: 00000006 sh_info: 00000000 sh_addralign: 00000004 sh_entsize: 0000000C
.stab.indexstr     sh_type: SHT_STRTAB   sh_flags:      sh_offset: 000022BC sh_size: 00000079 sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000001 sh_entsize: 00000000
.symtab            sh_type: SHT_SYMTAB   sh_flags:  a   sh_offset: 00002338 sh_size: 00000300 sh_link: 00000008 sh_info: 00000023 sh_addralign: 00000004 sh_entsize: 00000010
.strtab            sh_type: SHT_STRTAB   sh_flags:  a   sh_offset: 00002638 sh_size: 00000232 sh_link: 00000000 sh_info: 00000000 sh_addralign: 00000001 sh_entsize: 00000000
.rela.text         sh_type: SHT_RELA     sh_flags:  a   sh_offset: 0000286C sh_size: 00000150 sh_link: 00000007 sh_info: 00000002 sh_addralign: 00000004 sh_entsize: 0000000C

Looking at the symbols in the object file, I found some symbols explicitly allocated in the .data and .bss sections (I've trimmed the list of symbols to what I believe is relevant to the questions) as well as symbols that appear to be referring to large data objects (likely arrays given that this is a Fortran program) in the SHN_COMMON pseudo-section.

name                         type                section         value        size
...snippage...
11 GPB.t5matp.i              STT_OBJECT        4 .bss            000000A8         4
12 GPB.t5matp.x1             STT_OBJECT        4 .bss            000000B0         4
13 GPB.t5matp.xdenom         STT_OBJECT        4 .bss            000000B8         4
14 GPB.t5matp.pbar           STT_OBJECT        4 .bss            000000C0         4
...snippage...
31 GPB.t5matp.rgasx          STT_OBJECT        3 .data           000000AC         4
32 GPB.t5matp.xmolm          STT_OBJECT        3 .data           000000B0         4
33                           STT_SECTION       5 .stab.index     00000000         0
34                           STT_SECTION       6 .stab.indexstr  00000000         0
35 wrncom_                   STT_OBJECT    65522 SHN_COMMON      00000008       800
36 wrnco2_                   STT_OBJECT    65522 SHN_COMMON      00000008      6404
37 psiinc_                   STT_OBJECT    65522 SHN_COMMON      00000008   6000000
38 psirec_                   STT_OBJECT    65522 SHN_COMMON      00000008   4000000
39 psichc_                   STT_OBJECT    65522 SHN_COMMON      00000008   3000000
40 psiloc_                   STT_OBJECT    65522 SHN_COMMON      00000008    400000
41 psicoc_                   STT_OBJECT    65522 SHN_COMMON      00000008     40000
42 psidoc_                   STT_OBJECT    65522 SHN_COMMON      00000008   8000000
43 t5mat_                    STT_FUNC          2 .text           00000028       756
44 apcad_                    STT_NOTYPE        0                 00000000         0
45 t5matp_                   STT_FUNC          2 .text           00000520      7220

Reading the ELF docs (e.g. http://www.sco.com/developers/gabi/2003-12-17/ch4.sheader.html) I read that symbols defined relative to SHN_COMMON sections "...are common symbols, such as FORTRAN COMMON or unallocated C external variables." The linker is supposed to allocate space for the objects, but my question is: in which segment does this allocation occur? Perhaps I'm missing something obvious, but it isn't clear to me which segment will end up containing these objects. The combined size of the SHN_COMMON objects greatly exceeds the specified sizes of the .data and .bss sections. Is there any other metadata that guides the linker when it comes to SHN_COMMON sections?

3
  • Wild guess: as these segments are uninitialized, the linker doesn't put them in any specific segment. When the loader starts the program, it allocates memory wherever convenient, which may offen mean mmap, so it's not any specific segment. Think about it as a poor man's malloc emulation. Apr 3, 2016 at 21:41
  • Would you agree that, given your guess, it would be appropriate to allocate space for these objects in the segment containing the .bss section, seeing as no indication is given in the file as to their final location? Apr 3, 2016 at 21:43
  • try linking the .o and see where they end up.
    – Igor Skochinsky
    Apr 4, 2016 at 7:40

1 Answer 1

1

The uninitialized variables are placed in the Common section before the linking process. If they are initialized in another file, they will go into the .data section, otherwise, if still uninitialized or initialized to 0, they will be put in the .bss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.