2

I'm following the course of Baking Pi – Operating Systems Development. In it they created another section .init.

So can we create as many sections as we want (not just .data, .bss, .text) and can we put code and data (initialized of no) in any of them?.

If so, what's the purpose of sections then?

2 Answers 2

4

Initial research

At first sight it would appear that the answer would be "no" the specification for ELF only allows the following sections.

C32/kernel/bin/.process.o
architecture: i386, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00000333  00000000  00000000  00000040  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 .data         00000050  00000000  00000000  00000380  2**5
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  00000000  00000000  000003d0  2**2
                  ALLOC
  3 .note         00000014  00000000  00000000  000003d0  2**0
                  CONTENTS, READONLY
  4 .stab         000020e8  00000000  00000000  000003e4  2**2
                  CONTENTS, RELOC, READONLY, DEBUGGING
  5 .stabstr      00008f17  00000000  00000000  000024cc  2**0
                  CONTENTS, READONLY, DEBUGGING
  6 .rodata       000001e4  00000000  00000000  0000b400  2**5
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
  7 .comment      00000023  00000000  00000000  0000b5e4  2**0
                  CONTENTS, READONLY

Source: http://wiki.osdev.org/ELF

Other sources such as Wikipedia also show only the most basic section names, leading you to believe that these are all that are allowed. Additional searching showed that there are these 2 sections as well:

.fini

This section holds executable instructions that contribute to the process termination code. That is, when a program exits normally, the system arranges to execute the code in this section.

.init

This section holds executable instructions that contribute to the process initialization code. That is, when a program starts to run the system arranges to execute the code in this section before the main program entry point (called main in C programs).

The .init and .fini sections have a special purpose. If a function is placed in the .init section, the system will execute it before the main function. Also the functions placed in the .fini section will be executed by the system after the main function returns. This feature is utilized by compilers to implement global constructors and destructors in C++.

Source: http://l4u-00.jinr.ru/usoft/WWW/www_debian.org/Documentation/elf/node3.html

But, yes you can have any sections

But thanks to @AProgrammer for pointing me to the actual ELF Specification v1.2, there's a paragraph on page 1-16 which states the following:

Section names with a dot (.) prefix are reserved for the system, although applications may use these sections if their existing meanings are satisfactory. Applications may use names without the prefix to avoid conflicts with system sections. The object file format lets one define sections not in the list above. An object file may have more than one section with the same name.

So it would appear that it's entirely up to the program what sections it wants to utilize.

2
  • @Makouda - added new info thanks to AProgrammer to A.
    – slm
    Jun 4, 2014 at 13:51
  • 1
    @AProgrammer - thanks for the correction, updated A w/ info from spec. I'd UV your A but I'm already out of votes for the day. I'll catch you later tonight when I get more.
    – slm
    Jun 4, 2014 at 13:51
3

What is allowed as section and section names depend on the file format. For ELF, the definition of the format define a bunch of them and their purpose and then says:

Section names with a dot (.) prefix are reserved for the system, although applications may use these sections if their existing meanings are satisfactory. Applications may use names without the prefix to avoid conflicts with system sections. The object file format lets one define sections not in the list above. An object file may have more than one section with the same name.

So yes you can create sections with whatever name you want, the way the system will handle them is determined by their types and attributes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .