'How do we get a list of all predefined input memory sections from the toolchain?

I am trying to write my own linker script for a baremetal ARM target. While writing it one of the most important part of the script is the SECTIONS part where we consolidate the input memory sections from relocatable object files (*.o files) to create a output final executable object file (*.ELF file). In order to consolidate the sections, I believe that we first need to understand the available input memory sections and then to decide which goes where. Pretty much every resource in the internet points to only .text, .data and .bss sections and conveniently leaves the rest of the memory sections and unwind sections to the user's imagination.

I use the arm-none-eabi toolchain and I did quite a bit of searching trying to find a definitive list of input memory sections defined somewhere in the toolchain source file.

In elf.h file within the toolchain I was able to find the below definition provided for the structure of an ELF file.

typedef struct {
  Elf32_Word    sh_name;
  Elf32_Word    sh_type;
  Elf32_Word    sh_flags;
  Elf32_Addr    sh_addr;
  Elf32_Off sh_offset;
  Elf32_Word    sh_size;
  Elf32_Word    sh_link;
  Elf32_Word    sh_info;
  Elf32_Word    sh_addralign;
  Elf32_Word    sh_entsize;
} Elf32_Shdr;

For each of the members of this strucuture there is an explanation provided in the linux man page https://man7.org/linux/man-pages/man5/elf.5.html which makes sense. In the same man page they have also provided a exhaustive list of "special sections" listing .text, .data., .bss, etc. but they have not mentioned where they obtained that information or its corresponding explanation.

In some sources, people use readelf to read section headers and program headers. But my problem with that approach is, if I build the target with say debugging options (-g3) I get a new .debug section in the section headers. But in other build options I don't. I am afraid that if I need to have a trial and error method to identify the sections, I may miss out on some section types. I have taken up the task to get a good understanding and not get-things-done, so I am taking the extra effort to study and search online for a thorough understanding.

So my question is, I believe that the input memory sections are compiler dependent and they must be defined somewhere in the toolchain. If my understanding is correct, where is it present? If not, how do we get a definitive list of input memory sections in order to decide on ways to consolidate it in the output memory section.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source