dnel38
dnel38

Reputation: 31

Why is there more than one .rodata section being mapped to the LOAD segment, yet only one .rodata output section is specified in the linker script?

Here is my linker script :

ENTRY(__startup)
SECTIONS
{
   . = 0x0000;
   .text : { *(.text) }
   . = . + 0x4000;
   .rodata : 
   { 
     *(.rodata) 
     *(.rodata.*) 
   }
   .bss : { *(.bss) }
   .data : { *(.data) }
}

Simple Enough. However, when all of the object files get linked together and I look at the segment mapping for the executable I get the following:

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

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0xfffff034 0xfffff034 0x000c0 0x000c0 R   0x4
  LOAD           0x000000 0xfffff000 0xfffff000 0x000f4 0x000f4 R   0x1000
  LOAD           0x001000 0x00000000 0x00000000 0x01a18 0x01a18 R E 0x1000
  LOAD           0x002a18 0x00005a18 0x00005a18 0x00038 0x00038 R   0x1000
  LOAD           0x002a50 0x00005a50 0x00005a50 0x0010c 0x0010c RW  0x1000
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0

 Section to Segment mapping:
  Segment Sections...
   00     
   01     
   02     .text 
   03     .rodata .rodata 
   04     .bss .bss .data 
   05     

Doing an objdump on the program also shows two .rodata sections. The system I'm running on is ubuntu 16.0.4 and my compiler is gcc (I'm using LD, the GNU linker).

Does anyone know the reason why two output sections of the same name would be generated by the compilation? And is there a way to force it to one? It's entirely possible that this is more than just a generic question about the command language script, but as I can't show the codebase I'm working with, I still thought it wouldn't hurt to ask the good people on Stack Overflow.

Upvotes: 1

Views: 1951

Answers (1)

dnel38
dnel38

Reputation: 31

It seems that the default behavior of the LD linker is to allocate different .rodata sections based on whatever alignment is required for the constants that are being stored in read-only memory. For more information refer to here:

what does `.rodata.str1.8` section mean in elf file

Upvotes: 1

Related Questions