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
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:
fasm stab.fasm ./build/stab.o
ld -m elf_i386 ./build/stab.o -o ./build/stab
readelf output before linking:
$ 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:
$ 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.