• Uncategorized

About linux : If-you-do-not-use-the-SIZEOFHEADER-built-in-function-in-the-LD-link-script-the-size-of-the-executable-file-changes-why

Question Detail

github:https://github.com/Amyassient/link_test/tree/main

eg:Use LD 2.28 whenever possible
source code

#include <stdio.h>
 int main(){     
 printf("hello world");
 return 0;
}

not use SIZEOF_HEADERS:

PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000));
**. = SEGMENT_START("text-segment", 0x400000);**
.interp         : { *(.interp) }
.note.gnu.build-id : { *(.note.gnu.build-id) }
.hash           : { *(.hash) }
.gnu.hash       : { *(.gnu.hash) }
.dynsym         : { *(.dynsym) }
.dynstr         : { *(.dynstr) }
.gnu.version    : { *(.gnu.version) }
.gnu.version_d  : { *(.gnu.version_d) }
.gnu.version_r  : { *(.gnu.version_r) }

The file size:

[email protected]:~/Desktop/study/linker_test$ ls -al a.out 
-rwxrwxr-x 1 chang chang **2098312** Apr 11 13:54 a.out

There are 30 section headers, starting at offset 0x201970:
Section Headers:

[Nr] Name              Type             Address           Offset
   Size              EntSize          Flags  Link  Info  Align
[ 0]                   NULL             0000000000000000  00000000
   0000000000000000  0000000000000000           0     0     0
[ 1] .interp           PROGBITS         0000000000400000  00200000
   000000000000001c  0000000000000000   A       0     0     1
[ 2] .note.gnu.propert NOTE             0000000000400020  00200020
   00000000000000e0  0000000000000000   A       0     0     8
[ 3] .note.ABI-tag     NOTE             0000000000400100  00200100

use SIZEOF_HEADERS:

PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000));
**. = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;**
.interp         : { *(.interp) }
.note.gnu.build-id : { *(.note.gnu.build-id) }
.hash           : { *(.hash) }
.gnu.hash       : { *(.gnu.hash) }
.dynsym         : { *(.dynsym) }
.dynstr         : { *(.dynstr) }
.gnu.version    : { *(.gnu.version) }
.gnu.version_d  : { *(.gnu.version_d) }
.gnu.version_r  : { *(.gnu.version_r) }

The file size:

[email protected]:~/Desktop/study/linker_test$ ls -al a.out 
-rwxrwxr-x 1 chang chang **1392** Apr 11 14:34 a.out

There are 29 section headers, starting at offset 0x1938:

Section Headers:

[Nr] Name              Type             Address           Offset
   Size              EntSize          Flags  Link  Info  Align
[ 0]                   NULL             0000000000000000  00000000
   0000000000000000  0000000000000000           0     0     0
[ 1] .interp           PROGBITS         0000000000000270  00000270
   000000000000001c  0000000000000000   A       0     0     1
[ 2] .note.gnu.propert NOTE             0000000000000290  00000290

The file size has obviously changed. Why?

Question Answer

No answer for now.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.