flat assembler
Message board for the users of flat assembler.

Index > Main > Is it possible to make an ELF section not `alloc`?

Author
Thread Post new topic Reply to topic
itsfarseen



Joined: 23 Jul 2025
Posts: 1
Location: India
itsfarseen 23 Jul 2025, 09:58
I was trying to manually create a `.stabstr` section to learn about the format.
FASM sets the `A` (`alloc`) flag on the section.
The default linker script links `.stabstr` at address `0x0`.
This shouldn't matter, because normally compilers create the `.stabstr` section without the `alloc` flag, so the loader never loads it into memory.
Since the `alloc` flag is set in this case, the loader maps the `.stabstr` section at address `0x0` and causes SEGFAULT.

Does FASM always add the `alloc` flag to all sections?
Is there a way to change this?

Here's the code that I'm using
Code:
format ELF

section '.text'
public _start
_start:
mov eax, 1
mov ebx, 0
int 0x80

section '.stabstr'
align 1
string_table_start:
    db 0
    


build commands:
Code:
        fasm stab.fasm ./build/stab.o
        ld -m elf_i386 ./build/stab.o -o ./build/stab
    


readelf output before linking:
Code:
$ readelf -a ./build/stab.o
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          172 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         5
  Section header string table index: 4

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000034 00000c 00   A  0   0  4
  [ 2] .stabstr          PROGBITS        00000000 000040 000001 00   A  0   0  4
  [ 3] .symtab           SYMTAB          00000000 000044 000040 10      4   3  4
  [ 4] .strtab           STRTAB          00000000 000084 000027 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), p (processor specific)

There are no section groups in this file.

There are no program headers in this file.

There is no dynamic section in this file.

There are no relocations in this file.
No processor specific unwind information to decode

Symbol table '.symtab' contains 4 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 SECTION LOCAL  DEFAULT    1 .text
     2: 00000000     0 SECTION LOCAL  DEFAULT    2 .stabstr
     3: 00000000     0 FUNC    GLOBAL DEFAULT    1 _start

No version information found in this file.
    


readelf output after linking:
Code:
$ readelf -a ./build/stab
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x8049000
  Start of program headers:          52 (bytes into file)
  Start of section headers:          8352 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         2
  Size of section headers:           40 (bytes)
  Number of section headers:         6
  Section header string table index: 5

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08049000 002000 00000c 00   A  0   0  4
  [ 2] .stabstr          STRTAB          00000000 001000 000001 00   A  0   0  4
  [ 3] .symtab           SYMTAB          00000000 00200c 000050 10      4   1  4
  [ 4] .strtab           STRTAB          00000000 00205c 000019 00      0   0  1
  [ 5] .shstrtab         STRTAB          00000000 002075 00002a 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x001000 0x00000000 0x00000000 0x00001 0x00001 R   0x1000
  LOAD           0x002000 0x08049000 0x08049000 0x0000c 0x0000c R   0x1000

 Section to Segment mapping:
  Segment Sections...
   00     .stabstr
   01     .text

There is no dynamic section in this file.

There are no relocations in this file.
No processor specific unwind information to decode

Symbol table '.symtab' contains 5 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 08049000     0 FUNC    GLOBAL DEFAULT    1 _start
     2: 0804a000     0 NOTYPE  GLOBAL DEFAULT    1 __bss_start
     3: 0804a000     0 NOTYPE  GLOBAL DEFAULT    1 _edata
     4: 0804a000     0 NOTYPE  GLOBAL DEFAULT    1 _end

No version information found in this file.
    

_________________
- Farseen
Post 23 Jul 2025, 09:58
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20708
Location: In your JS exploiting you and your system
revolution 23 Jul 2025, 16:56
The fasm.pdf document states this:
Quote:
section directive defines a new section, it should be followed by quoted string defining the name of section, then can follow one or both of the executable and writeable flags, optionally also align operator followed by the number specifying the alignment of section (it has to be the power of two), if no alignment is specified, the default value is used, which is 4 or 8, depending on which format variant has been chosen.
So currently there is no option to change the allocation setting from inside the assembly source code.
Post 23 Jul 2025, 16:56
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8437
Location: Kraków, Poland
Tomasz Grysztar 24 Jul 2025, 11:30
If you take a look at implementation in fasm2, which is mostly fasm-compatible, you can see that it always includes the SHF_ALLOC flag. But at least in this case the macro that prepares the section data can be easily customized.

Therefore with fasm2 there is an easier route to add custom options that you might need for specialized purposes (it was the rationale behind making fasmg engine for fasm2 the way it is done).
Post 24 Jul 2025, 11:30
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.